import pygame import sys import keyboard # Player class class Player(pygame.sprite.Sprite): def __init__(self, x, y): super().__init__() self.image = pygame.Surface((20, 20)) self.image.fill((255, 255, 255)) self.rect = self.image.get_rect() self.rect.center = (x, y) self.speed = 5 def update(self): keys = pygame.key.get_pressed() if keys[pygame.K_w]: self.rect.y -= self.speed if keys[pygame.K_a]: self.rect.x -= self.speed if keys[pygame.K_s]: self.rect.y += self.speed if keys[pygame.K_d]: self.rect.x += self.speed # General setup pygame.init() clock = pygame.time.Clock() # Screen setup screen_width = 400 screen_height = 400 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Sprite Animation") background = pygame.Surface(screen.get_size()) background.fill((0, 0, 0)) screen.blit(background, (0, 0)) pygame.mouse.set_visible(False) # Creating the sprites and groups moving_sprites = pygame.sprite.Group() player = Player(100, 100) moving_sprites.add(player) # Game Loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() moving_sprites.update() screen.blit(background, (0, 0)) moving_sprites.draw(screen) pygame.display.flip() clock.tick(60)