3

在进行一些游戏编程时,我遇到了 set_timer 函数的问题。在我的代码中,我想要一个显示我当前拥有的食物量的条形图,它应该每秒减少一次。但是,我的 bar 中的空间量似乎没有增加,pygame 无法检测到我的 HUNGEREVENT。我可以知道我的代码有什么问题吗?

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)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.quit()
            if event.type == HUNGEREVENT:
                self.food_bar = self.food_bar - 10
                print("hi")
                self.all_sprites.update()
                pygame.display.flip()
                if event.key == pygame.K_ESCAPE:
                    self.quit()

提前致谢

4

1 回答 1

3

因为定时器不断重启pygame.time.set_time设置定时器事件时间并重启定时器。

将呼叫移至

pygame.time.set_timer(HUNGEREVENT, 10000)

在主应用程序循环之前初始化您的应用程序。例如:

def run(self):
    self.playing = True

    # start timer event
    self.hunger()

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

def hunger(self):
    self.HUNGEREVENT = pygame.USEREVENT + 1
    pygame.time.set_timer(HUNGEREVENT, 1000)

# [...]

def events(self):

    # HUNGEREVENT = pygame.USEREVENT + 1        <--- delete
    # pygame.time.set_timer(HUNGEREVENT, 10000) <--- delete

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
                self.quit()
        if event.type == self.HUNGEREVENT:
            # [...]

请注意,事件是在事件队列上重复创建的。没有必要连续启动它。


pygame.event.get()此外,我建议从应用程序中分别删除所有调用pygame.display.update()
在主应用程序循环结束时对显示进行一次更新。例如:

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

        # update the disaplay
        self.update()

def update(self):
    self.all_sprites.update()
于 2019-10-09T06:35:43.750 回答