1

我在用 python/pygame 编程的游戏中有一个计时器。

当我在主类中拥有所有东西时,计时器工作正常:

time=50

seconds_passed = clock.tick()/1000.0
time-=seconds
draw_time=math.tranc(time)
print(draw_time)

但是,当我将其移至新的班级播放器时

class player():
   .
   .
   .
   set_time(self, draw_time):
        seconds_passed = clock.tick()/1000.0
        time-=seconds_passed
        draw_time=math.tranc(time)
        print(draw_time)

当我在主类中调用此函数时:

class main():
    . 
    .
    .
    draw_time=20
    player = Player()
    print player.set_time(draw_time)

我的时间没有减少,但保持不变!

有什么建议么?

4

1 回答 1

0

当您time在方法中递减时,您只是在修改值的副本。为了能够修改它,您可以改为传递对对象的引用。您可以使用例如timedelta

from datetime import timedelta

class player():
   set_time(self, draw_time):
        seconds_passed = clock.tick()/1000.0
        time -= timedelta(seconds=seconds_passed)
        draw_time=math.tranc(time.seconds)
        print(draw_time)

class main():
    draw_time = timedelta(seconds=20)
    player = Player()
    print player.set_time(draw_time)
于 2013-10-29T14:00:43.023 回答