Cravan PZ
Cravan PZ

Reputation: 104

Problem when using keyboard commands in pygame

While doing some game programming, I ran into a problem with the keyboard commands. In my code, I have a food bar and a money bank variable named money_bar. The food bar in my game would increase when I press a key, say f, in my game, and also the game deduct say $10 from my money_bar when I press f.
The food bar shows the current amount of food I have, which is supposed to decrease every second. However, it appears that none of my keyboard commands in the event() are working. May I know what is the problem in my code? This is my food_bar and `money_bar initialisation:

def __init__(self):
        pygame.init()
        self.clock = pygame.time.Clock()
        self.living = 1
        self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
        pygame.display.set_caption(TITLE)
        self.time = pygame.time.get_ticks()
        pygame.key.set_repeat(500, 100)
        self.all_sprites = pygame.sprite.Group()
        self.console = Console(self, 0)
        self.player = Player(self, 390, 595)
        self.work = Work(self, 450, 250)
        self.food_station = Food_Station(self, 750, 200)
        self.food = Food(self, 25, 20)
        self.education = Education(self, 300, 10)
        self.school = School(self, 100, 200)
        self.family = Family(self, 600, 10)
        self.money = Money(self, 800, 15)
        initial_food = 100
        self.food_bar = initial_food
        initial_money = 0
        self.money_bar = initial_money
        initial_education = "Student"
        self.education_level = initial_education
        initial_family = 3
        self.family_member = 3

This is where i run the main algorithm:

    def run(self):
        self.playing = True
        self.hunger()
        while self.playing:
            self.dt = self.clock.tick(FPS) / 1000
            self.events()
            self.draw()
            self.update()

and here's how i check for events(including keyboard commands)

    def events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.quit()
            if event.type == self.HUNGEREVENT:
                self.food_bar = self.food_bar - 10
                self.all_sprites.update()
                pygame.display.flip()

            if event.type == pygame.K_f:
                self.money_bar = self.money_bar - 10
                self.food_bar = self.food_bar + 15
                self.all_sprites.update()
                pygame.display.flip()

            if event.type == pygame.K_ESCAPE:
                self.quit()

Thanks in advance

Upvotes: 1

Views: 117

Answers (1)

Rabbid76
Rabbid76

Reputation: 211135

While pygame.K_f is a key enumerator constant (see pygame.key) the content of event.type is event enumerator constant (see pygame.event).
If you want to determine if a certain key is pressed, the you've to verify if the event type is pygame.KEYDOWN (or pygame.KEYUP for button release) and if the .key attribute of the event is equal the key enumerator. e.g.:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        self.quit()

    # [...]

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_f:
            # [...]

Upvotes: 1

Related Questions