【问题标题】:How would I set a timer with Pygame and Livewires?如何使用 Pygame 和 Livewires 设置计时器?
【发布时间】:2013-11-11 16:59:54
【问题描述】:

所以在我正在创建的游戏中,当玩家拿起速度提升精灵时,我希望玩家的速度周期性地提升大约五秒钟,有人可以帮我理解如何做到这一点吗? 'Handle Caught' 方法将包含速度提升的代码,播放器的实际速度也设置为全局变量 'speed' 等于 2。提前谢谢

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):
        global speed

【问题讨论】:

    标签: python pygame livewires


    【解决方案1】:

    如果您的游戏足够大,我建议您使用计时器管理器。 有一个基于 pygame.Clock 的大计时器。创建方法,注册时间和时间过去时要调用的方法。所以像:

    timerMananger.register(self.myMethod,5000) 
    # registers an event to be called after 5 seconds
    

    管理器应包含时钟,并具有要完成的任务的优先级队列。 一个简短的草图:

    def register(myMethod,t):
        pqueue.add(myMethod,time_now+t)
    
    def tick():
        clock.tick()
        if(time_now > pqueue[0]):
            pqueue.pop()[0]()
    

    这样您只检查最近的事件。如果会有很多事件,你可以将 if 更改为 while 循环,以便一次处理它们。

    【讨论】:

    • 我将如何实现这一点,一旦计时器达到 5 秒,速度会降低回 2?
    • 有一个函数将 dy 设置回速度。您也可以将指针发送到对象,以便该函数能够修改播放器的属性。
    • 我更改了 register 参数,因此该参数将是一个绑定函数 - 这样 self 将按预期工作。
    猜你喜欢
    • 2013-11-26
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多