2012-04-26 77 views
0

我为我的pygame创建了一个简单的评分系统。但暂停游戏。我知道这是因为时间的问题,但我不知道如何整理。Pygame简单评分系统

的评分系统是+100每5秒,同时开始是真实的,代码:

while start == True: 
    time.sleep(5) 
    score = score + 100 

与缩进全码:在行http://pastebin.com/QLd3YTdJ 代码:156-158

谢谢

+1

'x == True'永远不是你想要的。刚开始时:' – habnabit 2012-04-26 20:15:35

+0

您可能对[pygame.time]感兴趣(http://www.pygame.org/docs/ref/time.html)。 – James 2012-04-26 20:15:46

回答

2

如果我正确理解你,你不想让while True: score += 100循环阻止你的整个程序?

你应该通过移动得分增加了一个单独的功能 解决它,使用APScheduler http://packages.python.org/APScheduler/intervalschedule.html的intervalfunction

from apscheduler.scheduler import Scheduler 

# Start the scheduler 
sched = Scheduler() 
sched.start() 

# Schedule job_function to be called every 5 seconds 
@sched.interval_schedule(seconds=5) 
def incr_score(): 
    score += 100 

这将导致APScheduler为您创建运行功能每5秒一个线程。

您可能需要对函数进行一些更改才能使其正常工作,但它至少会使您开始工作:)。

+0

,看起来像使用理想的解决方案,但我得到这个错误导入调度器 ImportError:没有模块命名调度,任何想法? – ErHunt 2012-04-26 21:42:40

+0

你必须安装它,即。 'pip安装apscheduler' – DMan 2012-08-27 05:23:47

3

而不是使用sleep,直到时间流逝,它会停止游戏,您想要计数一个已经过去的秒数的内部计时器。当您点击5秒钟时,增加分数,然后重置计时器。

事情是这样的:

scoreIncrementTimer = 0 
lastFrameTicks = pygame.time.get_ticks() 
while start == True: 
    thisFrameTicks = pygame.time.get_ticks() 
    ticksSinceLastFrame = thisFrameTicks - lastFrameTicks 
    lastFrameTicks = thisFrameTicks 

    scoreIncrementTimer = scoreIncrementTimer + ticksSinceLastFrame 
    if scoreIncrementTimer > 5000: 
     score = score + 100 
     scoreIncrementTimer = 0 

这很容易得到改善(如果你的帧率非常低,有帧之间超过5秒?),但总体思路。这通常被称为“增量时间”游戏计时器实现。