如何使用python

时间:2019-04-09 10:00:01

标签: python python-3.x

我正在尝试使用python创建一个小游戏。我已经设置了所有内容,但我想设置一个时间限制。我在互联网上找到了一些东西,但它不适用于该项目,但可以与另一个项目一起使用。

c.create_text(450, 20, text = 'Time', fill = 'red')
c.create_text(650, 20, text = 'Score', fill = 'red')
time_text = c.create_text(450, 20, text = 'Time', fill = 'red')
score_text = c.create_text(650, 20, text = 'Score', fill = 'red')
def print_score(score) :
    c.itemconfig(score_text, text = str(score))
def print_time(time) :
    c.itemconfig(time_text, text = str(time))


ENNEMY_CHANCE = 10
TIME_LIMIT = 30
SCORE_BONUS = 1000
score = 0
bonus = 0
end = time() + TIME_LIMIT

#Main
while True :
    if randint(1, ENNEMY_CHANCE) == 1:
        create_ennemy()
    move_ennemies()
    hide_ennemies()
    score += ennemy_kill()
    if(int(score/SCORE_BONUS)) > bonus:
        bonus += 1
        end += TIME_LIMIT
    print_score(score)
    print_time(int(end - time()))
    window.update()

但是我明白了:

    end = time() + TIME_LIMIT
TypeError: 'int' object is not callable

如果您知道一种更简单的设置时间限制的方法,那么该时间限制将是超级。

2 个答案:

答案 0 :(得分:0)

您导入时间了吗?我认为您在代码中的某个位置使用了“时间”名称作为整数变量,并且您的代码不明确。试试这个来获取当前时间:

import time as time_module
now = time_module.time()

答案 1 :(得分:0)

尝试

import time   
start = time.time()         #the variable that holds the starting time  
elapsed = 0                 #the variable that holds the number of seconds elapsed.  
while elapsed < 30:         #while less than 30 seconds have elapsed    

elapsed = time.time() - start #update the time elapsed