我爲我的pygame創建了一個簡單的評分系統。但暫停遊戲。我知道這是因爲時間的問題,但我不知道如何整理。Pygame簡單評分系統
的評分系統是+100每5秒,同時開始是真實的,代碼:
while start == True:
time.sleep(5)
score = score + 100
與縮進全碼:在行http://pastebin.com/QLd3YTdJ 代碼:156-158
謝謝
我爲我的pygame創建了一個簡單的評分系統。但暫停遊戲。我知道這是因爲時間的問題,但我不知道如何整理。Pygame簡單評分系統
的評分系統是+100每5秒,同時開始是真實的,代碼:
while start == True:
time.sleep(5)
score = score + 100
與縮進全碼:在行http://pastebin.com/QLd3YTdJ 代碼:156-158
謝謝
如果我正確理解你,你不想讓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秒一個線程。
您可能需要對函數進行一些更改才能使其正常工作,但它至少會使您開始工作:)。
而不是使用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秒?),但總體思路。這通常被稱爲「增量時間」遊戲計時器實現。
'x == True'永遠不是你想要的。剛開始時:' – habnabit 2012-04-26 20:15:35
您可能對[pygame.time]感興趣(http://www.pygame.org/docs/ref/time.html)。 – James 2012-04-26 20:15:46