有人可以帮我解决在python中制作计时器的问题吗?我需要帮助的是'Handle_caught'方法应该包含一个计时器,让玩家的速度提高5秒,玩家的速度在全球范围内增加变量更改为True。
速度令牌类:
class Addspeed(games.Sprite):
image = games.load_image('addspeed.png')
speed = 2
def __init__(self,x,y = 10):
super(Addspeed, self).__init__(image = Addspeed.image,
x = x, y = y,
dy = Addspeed.speed)
def update(self):
if self.bottom>games.screen.height:
self.destroy()
def handle_caught(self):
self.destroy()
def increasespeed(self):
global speedbuff
speedbuff = True
def decreasespeed(self):
global speedbuff
speedbuff = False
球员速度如何增加:
speedbuff = False
def update(self):
global inventory
global speed
""" uses A and D keys to move the ship """
if games.keyboard.is_pressed(games.K_a):
if speedbuff == False:
self.x -= 3
elif speedbuff == True:
self.x -= 6
if games.keyboard.is_pressed(games.K_d):
if speedbuff == False:
self.x+= 3
elif speedbuff == True:
self.x+=6
答案 0 :(得分:1)
您可以将speedbuff
设置为整数值(上电时的帧数),而不是布尔值,并且在播放器的更新方法中将其递减直到0.像这样:
if games.keyboard.is_pressed(games.K_a):
if speedbuff > 0:
self.x -= 6
else:
self.x -= 3
...
if speedbuff > 0: speedbuff -= 1
由于0被解释为False
,如果您愿意,则if语句可能只是if speedbuff:
。