Home > OS >  Why Pygame is not listening Keyboard input?
Why Pygame is not listening Keyboard input?

Time:10-10

I´m a beginner in coding, and I was trying to code a basic game using pytmx, tiled, and pyscroll. so when I run my game it does successfully, the game screen pop up and everything seems to be fine, but when I try to move my player, there´s no answer. anyway, here´s the player´s code in the movi, please help, thanks in advance.

import pygame

class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.sprite_sheet = pygame.image.load('player.png')
        self.image = self.get_image(0,0)
        self.image.set_colorkey([0,0,0])
        self.rect = self.image.get_rect()
        self.position = [x,y]
        self.speed = 3

def move_right(self): self.position[0]  = self.speed

def move_left(self): self.position[0] -= self.speed

def move_up(self): self.position[1] -= self.speed

def move_down(self): self.position[1]  = self.speed

def update(self):
    self.rect.topleft = self.position

def get_image(self, x, y):
    image = pygame.Surface([32,32])
    image.blit(self.sprite_sheet,(0,0),(x,y,32,32))
    return image

here´s the game´s code

import pygame
import pytmx
import pyscroll
from player import Player

class Game:
    def __init__(self):
        #creer la fenetre du Jeu 
        self.screen = pygame.display.set_mode((800, 600))
        pygame.display.set_caption('Yelda')
    
    #charger la carte tmx
    tmx_data = pytmx.util_pygame.load_pygame('carte.tmx')
    map_data = pyscroll.data.TiledMapData(tmx_data)
    map_layer = pyscroll.orthographic.BufferedRenderer(map_data, self.screen.get_size())
    map_layer.zoom = 2

    #generer un joeur
    player_position = tmx_data.get_object_by_name('player')
    self.player = Player(player_position.x, player_position.y)
    
    #dessiner le groupe de calques 
    self.group = pyscroll.PyscrollGroup(map_layer=map_layer, default_layer=5)
    self.group.add(self.player)

def handle_input(self):
    pressed = pygame.key.get_pressed()
    for event in pressed:
        if pressed[pygame.K_UP]:
            print('up')
            self.player.move_up()
        elif pressed[pygame.K_DOWN]:
            self.player.move_down()
        elif pressed[pygame.K_RIGHT]:
            self.player.move_right()
        elif pressed[pygame.K_LEFT]:
            self.player.move_left()

     


def run(self):
    #boucle du jeu 
    running = True
    
    while running:

        self.group.update()
        self.group.center(self.player.rect)
        self.group.draw(self.screen)
        pygame.display.flip()
       


        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

pygame.quit()

and here the main code

import pygame
from game import Game

if __name__ == '__main__':
    pygame.init()
    game = Game()
    game.run()

CodePudding user response:

You need to run handle_input() in the while loop

while running:
    ...
    handle_input()
    ...

CodePudding user response:

I think it works if you get rid of the event for loop. In other words, you don't have to loop pressed, just check for pygame.K_UP

def handle_input(self):
    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP]:
        print('up')
        self.player.move_up()
    elif pressed[pygame.K_DOWN]:
        self.player.move_down()
    elif pressed[pygame.K_RIGHT]:
        self.player.move_right()
    elif pressed[pygame.K_LEFT]:
        self.player.move_left()
  • Related