简体   繁体   中英

How to set time limit using python

I am trying to create a little game with python. I already set everything but I would like to set a time limit. I found something on the internet but it is not working with this project but it works with another project.

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()

But I get this:

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

If you know an easier way to set a time limit that would be super.

Did you import time? I think you used the "time" name as an integer variable somewhere in your code and you have ambiguous code. Try this to get current time:

import time as time_module
now = time_module.time()

Try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM