gpt4 book ai didi

python - 为什么 pygame set_timer() 在我的代码中不起作用?

转载 作者:太空宇宙 更新时间:2023-11-04 01:49:43 25 4
gpt4 key购买 nike

<分区>

在进行一些游戏编程时,我遇到了 set_timer 函数的问题。在我的代码中,我想要一个显示我当前食物量的条形图,它应该每秒减少一次。但是,我的栏中的空白空间量似乎没有增加,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()

提前致谢

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com