0

有人可以帮我在python中制作计时器吗?我需要帮助的是'Handle_caught'方法应该包含一个计时器,使玩家速度增加五秒钟,当全局变量改变时玩家速度增加为真。

速度令牌类:

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
4

1 回答 1

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:

于 2013-11-12T17:52:19.680 回答