Can someone explain to me why the player is shaken when he walks? When I walk, the player is shaking and I don't know how to solve it, I've been thinking for a while and I can't understand, I think it has something to do with the scroll list I use to move the camera, but I don't know what's causing it.
import pygame as pg
from pygame.locals import *
from sys import exit
from time import perf_counter
def load_map(path):
file = open(f"{path}.txt", "r")
info = file.read()
file.close()
info = info.split("\n")
game_map = []
for row in info:
game_map.append(list(row))
return game_map
def create_tile(path, pos):
image = pg.image.load(f"graphics/{path}.png").convert()
image = pg.transform.scale(image, (TILE_SIZE, TILE_SIZE))
rect = image.get_rect(topleft = pos)
return [image, rect]
def create_map():
for y, row in enumerate(game_map):
for x, tile in enumerate(row):
if tile == "1":
tiles.append(create_tile("grass", (x * TILE_SIZE, y * TILE_SIZE)))
if tile == "2":
tiles.append(create_tile("dirt", (x * TILE_SIZE, y * TILE_SIZE)))
def get_input(flip):
key = pg.key.get_pressed()
acc[0] = 0
if key[K_RIGHT]:
acc[0] += speed
flip = False
elif key[K_LEFT]:
acc[0] -= speed
flip = True
if key[K_SPACE] and on_ground:
vel[1] = jump
return flip
def movement(on_ground):
acc[0] += vel[0] * F
vel[0] += acc[0] * dt
if abs(vel[0]) < .2:
vel[0] = 0
pos[0] += vel[0] * dt - (acc[0] / 2 * (dt ** 2))
rect.x = round(pos[0])
overlap = get_overlap()
for tile in overlap:
if vel[0] > 0:
rect.right = tile.left
pos[0] = rect.x
if vel[0] < 0:
rect.left = tile.right
pos[0] = rect.x
vel[1] += acc[1] * dt
pos[1] += vel[1] * dt - (acc[1] / 2 * (dt ** 2))
rect.y = round(pos[1])
overlap = get_overlap()
for tile in overlap:
if vel[1] > 0:
rect.bottom = tile.top
pos[1] = rect.y
vel[1] = 0
on_ground = True
if vel[1] < 0:
rect.top = tile.bottom
pos[1] = rect.y
vel[1] = 0
if on_ground and vel[1] != 0:
on_ground = False
return on_ground
def get_overlap():
overlap = []
for tile in tiles:
if rect.colliderect(tile[1]):
overlap.append(tile[1])
return overlap
BLUE = (110, 184, 199)
WIDTH, HEIGHT = 640, 512
TILE_SIZE = 64
G, F = 2100, -4
pg.init()
win = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption("2D Platformer")
image = pg.image.load("graphics/player.png").convert()
image = pg.transform.scale(image, (TILE_SIZE // 2, TILE_SIZE))
image.set_colorkey("white")
rect = image.get_rect(bottomleft = (TILE_SIZE * 2, HEIGHT - TILE_SIZE * 2))
pos = [rect.x, rect.y]
vel = [0, 0]
acc = [0, G]
speed = 1500
jump = -800
flip = False
on_ground = False
tiles = []
game_map = load_map("map")
create_map()
scroll = [0, 0]
pf = perf_counter()
while True:
dt = perf_counter() - pf
pf = perf_counter()
for event in pg.event.get():
if event.type == QUIT:
pg.quit()
exit()
scroll[0] = rect.centerx - WIDTH // 2
scroll[1] = rect.centery - HEIGHT // 2
flip = get_input(flip)
on_ground = movement(on_ground)
win.fill(BLUE)
for tile in tiles:
win.blit(tile[0], (tile[1].x - scroll[0], tile[1].y - scroll[1]))
win.blit(pg.transform.flip(image, flip, False), (rect.x - scroll[0], rect.y - scroll[1]))
pg.display.update()