你如何在 Python 2.7 中创建一个计时器? [英] How do you create a timer in Python 2.7?

查看:92
本文介绍了你如何在 Python 2.7 中创建一个计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Pyglet 库编写一个破砖机克隆程序,我想为游戏的奖励"(即更长的桨、更快的桨运动、更大的球)制作一个计时器,最多可计算 20 秒.我一直在努力挖掘互联网,但我找不到答案.

I'm currently programming a brick-breaker clone using the Pyglet library and I would like to make a timer that counts up to 20 seconds for the game's 'bonuses'(i.e. longer paddle, faster paddle movement, a larger ball). I've been digging the internet as hard as I can but I couldn't find the answer.

推荐答案

import threading

bonuses_count = 0

def count_bonuses():
    global bonuses_count
    # paddle = count(paddle) # something your logic part here
    bonuses_count += 20
    print "counting bonuses :- ", (bonuses_count) 
    t = threading.Timer(20.0, count_bonuses).start()

t = threading.Timer(20.0, count_bonuses)
t.start()

好吧,我不知道您计算奖金的逻辑,但我认为您可以通过创建一个 线程 来实现 20 秒计时器,该线程将在每 20 秒后执行一次.

Well i don't know your logic of counting bonuses but i think you can acheive 20 seconds timer by creating a thread which will executing after every 20 seconds.

这里我创建了函数 count_bonuses,它将包含您的游戏逻辑并在每 20 秒后执行一次.

Here i have created function count_bonuses that will contain your game logic and get executed after every 20 seconds.

如果您想停止此线程,您可以创建自己的停止标志,或者创建一个 KeyboardInterrupt 以根据您的游戏逻辑通过键盘中断来停止线程.

You can create your own stopflag if you want to stop this thread or create an KeyboardInterrupt to stop the thread with keyboard intruption based on your gamming logic.

counting bonuses :-  20
counting bonuses :-  40
counting bonuses :-  60
counting bonuses :-  80

这篇关于你如何在 Python 2.7 中创建一个计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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