[Tutor] USEREVENT not detected

Cravan savageapple850 at gmail.com
Wed Oct 9 02:09:42 EDT 2019


Hi all,

                While doing some game programming, I ran into a problem with the set_timer function. In my code, I wanted a bar which shows the current amount of food I have, which is supposed to decrease every second. However, it appears that the amount of space empty in my bar does not increase and pygame cannot detect my HUNGEREVENT. May I know what is the problem in my code?

````

def run(self):

        self.playing = True

        while self.playing:

            self.dt = self.clock.tick(FPS) / 1000

            self.hunger()

            self.events()

            self.update()

            self.draw()

 

    def hunger(self):

        HUNGEREVENT = pygame.USEREVENT + 1

        pygame.time.set_timer(HUNGEREVENT, 1000)

        self.all_sprites.update()

        pygame.display.flip()

 

    def food_food(self, x, y, cool):

        if cool < 0:

            cool = 0

        BAR_LENGTH = 100

        BAR_HEIGHT = 10

        fill = (cool / 100) * BAR_LENGTH

        outline_rect = pygame.Rect(x, y, BAR_LENGTH, BAR_HEIGHT)

        fill_rect = pygame.Rect(x, y, fill, BAR_HEIGHT)

        pygame.draw.rect(screen, GREEN, fill_rect)

        pygame.draw.rect(screen, WHITE, outline_rect, 2)

 

    def quit(self):

        pygame.quit()

        sys.exit()

 

    def update(self):

        self.all_sprites.update()

 

 

    def draw(self):

        self.screen.fill(BGCOLOR)

        self.all_sprites.draw(self.screen)

        font = pygame.font.SysFont('Arial', 15, True, False)

        self.food_food(120, 50, self.food_bar)

        text = font.render("Number of days:" , True, BLACK)

        screen.blit(text, [0, 110])

        font = pygame.font.SysFont('Arial', 30, True, False)

        text = font.render("= " + str(self.education_level), True, BLACK)

        screen.blit(text, [400, 40])

        font = pygame.font.SysFont('Arial', 30, True, False)

        text = font.render("= " + str(self.family_member), True, BLACK)

        screen.blit(text, [700, 40])

        font = pygame.font.SysFont('Arial', 30, True, False)

        text = font.render("= $" + str(self.money_bar), True, BLACK)

        screen.blit(text, [900, 40])

        self.all_sprites.update()

        pygame.display.flip()

 

    def events(self):

        # catch all events here

        HUNGEREVENT = pygame.USEREVENT + 1

        pygame.time.set_timer(HUNGEREVENT, 10000)

        if pygame.event.get(HUNGEREVENT):

            self.food_bar = self.food_bar - 10

            print("hi")

            self.all_sprites.update()

            pygame.display.flip()

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                self.quit()

                if event.key == pygame.K_ESCAPE:

                    self.quit()

````

Thanks,

Cravan



More information about the Tutor mailing list