Why does anything with key input not update?

Viewed 53

I used a working version of a movement system that I had used before. In the working version, pressing wasd will move the player. However, I switched the player class to a sprite when adding collision and now nothing with key input will update on the screen. This being said, anything not using key input works. I copied a movement demo from another question on here and when I ran it nothing happened again? Is this a bug with pygame?

Here's what I copied:

# GitHub:
# https://github.com/Rabbid76/PyGameExamplesAndAnswers/blob/master/documentation/pygame/pygame_keys_and_keyboard_event.md
#
# Stack Overflow:
# https://stackoverflow.com/questions/63540062/pygame-unresponsive-display/63540113#63540113

import pygame

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

rect = pygame.Rect(0, 0, 20, 20)
rect.center = window.get_rect().center
vel = 5

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    
    rect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel
    rect.y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * vel
        
    rect.centerx = rect.centerx % window.get_width()
    rect.centery = rect.centery % window.get_height()

    window.fill(0)
    pygame.draw.rect(window, (255, 0, 0), rect)
    pygame.display.flip()

pygame.quit()
exit()
0 Answers
Related