Code: We can control the character with the right arrow key but it goes forward only. Can you change the code to move all 4 directions (up, down, left, right)?
import sysimport pygamepygame.init()FPS = 60fpsClock = pygame.time.Clock()WIDTH = 800HEIGHT = 400DISPLAY = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)WHITE = (255, 255, 255)class Player: sprite = pygame.image.load('crag.png') sprite = pygame.transform.scale(sprite, (128, 128)) def __init__(self, pos): self.x = pos[0] self.y = pos[1] def control(self): pressed_keys = pygame.key.get_pressed() if pressed_keys[pygame.K_RIGHT]: self.x = self.x + 5 def show(self): DISPLAY.blit(self.sprite, (self.x, self.y))PC = Player((200, 200))while True: DISPLAY.fill(WHITE) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() PC.control() PC.show() pygame.display.update() fpsClock.tick(FPS)