足球 pygame,需要计时器帮助 [英] Football pygame, need help on timer

查看:81
本文介绍了足球 pygame,需要计时器帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一场足球比赛,我想让玩家只有 10 秒的时间才能结束,否则比赛就会结束.

I want to make a football game and I want to make it so that the player only gets 10 seconds to get to the end or else the game ends.

我该怎么做?

推荐答案

我建议使用 pygame.time.set_timer() 和 pygames 事件机制,因为您可能已经有一个事件处理循环游戏.请参阅 set_timer 文档此处.

I would suggest using pygame.time.set_timer() and pygames event mechanics since you already likely have an event processing loop in your game. See the set_timer docs here.

你这样做:

pygame.time.set_timer(pygame.USEREVENT, 10000)

启动 10 秒计时器,当它关闭时将触发 pygame.USEREVENT.您可以通过添加如下测试在事件循环中监视该事件:

to start the a 10 second timer that will trigger a pygame.USEREVENT when it goes off. You would watch for that event in the event loop by adding a test like this:

if event.type == pygame.USEREVENT:
    # Countdown expired

我怀疑您是否需要它,但是如果您需要多个计时器并且需要能够区分它们,您可以创建一个具有属性的事件,您可以将其设置为不同的值来跟踪它们.像这样:

I doubt that you require it, but if you need multiple timers and need to be able to tell them apart, you can create an event with an attribute that you can set to different values to track them. Something like this:

my_event = pygame.event.Event(pygame.USEREVENT, {"tracker": "gameover"})
pygame.time.set_timer(my_event , 2000)

这篇关于足球 pygame,需要计时器帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆