我在游戏中有一个计时器,我用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)
我的时间不是递减但是保持不变!
有什么建议吗?
答案 0 :(得分: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)