我正在使用带有pygame的Python 2.7.6,我仍然是编程和pygame的新手。有一种方法我可以使它在哪里如果我有数字,它将从0到1到2到3到4到5而不是0到5?就像地球上的卫生系统一样,它不仅会下降,而且会一次下降一个数字。我尝试在for循环中使用time.sleep(),但它暂停了整个游戏一秒钟。
for i in range(7):
time.sleep(.5)
dodged += 1
我在没有time.sleep()的情况下感到疲惫,但是看起来太快了。
for i in range(7):
dodged += 1
有人知道怎么做吗?
答案 0 :(得分:1)
您应该在游戏中使用pygame.time.set_timer
定期事件(在这种情况下,数字会增加。在事件循环之前运行此代码:
increase_event = pygame.USEREVENT + 1
pygame.time.set_timer(increase_event, number_change_time_in_milliseconds)
在循环中运行此代码:
if event.type == increase_event
dodged += 1
答案 1 :(得分:0)
像这样:
#upcount
import pygame, sys
from pygame.locals import *
pygame.init()
size = width, height = 200, 200
screen = pygame.display.set_mode(size)
font = pygame.font.SysFont('arial', 20)
increase_event = pygame.USEREVENT + 1
number_change_time = 1000 # 1 second
def main():
pygame.time.set_timer(increase_event, number_change_time)
global dodged
dodged = 0
while True:
event_loop()
update()
def event_loop():
global dodged
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
pygame.time.set_timer(increase_event, 0) # stop the timer on keypress
if event.type == increase_event:
dodged += 1
def update():
global dodged
screen.fill((0,0,0))
numstr = str(dodged)
mytext = font.render(numstr, True, (255,255,0))
mytext = mytext.convert_alpha()
screen.blit(mytext, (20, 30))
pygame.display.flip()
if __name__=="__main__":
main()