python-cheatsheet
python-cheatsheet copied to clipboard
Mario pygame code snippet returns None to 'pressed' disabling controls
In the Mario game code snippet the run function currently passes None into 'pressed' dict. disabling controls. Revised run function below fixes. Per PG documentation, get_pressed() isn't the best way to get text entry from user: https://www.pygame.org/docs/ref/key.html#pygame.key.get_pressed
RUNNING = True def run(screen, images, mario, tiles): clock = pg.time.Clock() global RUNNING current_key = None while RUNNING: for event in pg.event.get(): if event.type == pg.QUIT: RUNNING = False keys = {pg.K_UP: D.n, pg.K_RIGHT: D.e, pg.K_DOWN: D.s, pg.K_LEFT: D.w} if event.type == pg.KEYDOWN: current_key = event.key else: current_key = None pressed = {keys.get(current_key)} update_speed(mario, tiles, pressed) update_position(mario, tiles) draw(screen, images, mario, tiles, pressed) clock.tick(28)