如何在for循环中增加数据和时间

时间:2018-07-11 22:02:02

标签: python python-3.x

我正在尝试创建一个游戏时间,即使您没有在游戏中主动活跃,这个游戏世界也会运行得更快,并且有所不同。

所以我要设置一个纪元作为所有计算的参考。

我不得不说,每60秒钟增加游戏分钟,每60分钟增加游戏小时,依此类推,我只是在增加部分上受了限制,请问有什么可以帮助的吗?

import time

#Earth Time
seconds = int(time.time())
mins = seconds / 60
hours = mins/ 60
days = hours / 24
years = days / 365

print("Seconds:",seconds)
print("Mins:",mins)
print("Hours:",hours)
print("Days:",days)
print("Years:",years)

#Game Time
gseconds = seconds * 25
gmins = 0
ghours = 0
gdays = 0
gyears = 0

print("GSeconds:",gseconds)
print("GMins:",gmins)
print("GHours:",ghours)
print("GDays:",gdays)
print("GYears:",gyears)

count = 0

for var in gseconds: #stuck here
        count = count +1

print(count)

类似:

for var in range (gseconds): #stuck here
        count = count +1
        if count >=60:
                gmins= gmins +1
                count = 0
        if gmins >= 60:
                ghours = ghours +1
                gmins = 0
        if ghours >= 24:
                gdays = gdays +1

1 个答案:

答案 0 :(得分:1)

只需每秒使用一次来增加适当的时间值:

for var in range (gseconds):
    count += 1
    if count >= 60:
            gmins += 1
    if gmins >= 3600:
            ghours += 1
    if ghours >= (24 * 3600):
            gdays += 1