Python 游戏的计时器 [英] Timer for Python game

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

问题描述

我正在尝试创建一个简单的游戏,重点是在一定时间内(例如 10 秒)收集尽可能多的块.如何让计时器在程序开始时开始计时,并在达到 10 秒时执行某些操作(在这种情况下,退出循环)?

I'm trying to create a simple game where the point is to collect as many blocks as you can in a certain amount of time, say 10 seconds. How can I get a timer to begin ticking at the start of the program and when it reaches 10 seconds, do something (in this case, exit a loop)?

推荐答案

import time

now = time.time()
future = now + 10
while time.time() < future:
    # do stuff
    pass

或者,如果您已经有了循环:

Alternatively, if you've already got your loop:

while True:
    if time.time() > future:
        break
    # do other stuff

此方法适用于 pygame,因为它几乎需要你有一个大的主循环.

This method works well with pygame, since it pretty much requires you to have a big main loop.

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

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