0

I'm making a game using Turtle, and need a throwing mechanic with a cooldown. I've tried to use the time module, but trying to classify the variables as int() or float() doesn't work.

def throw(throwcdstart,throwcd):
  if throwcd >= 2:
    throwcdstart = int(time.time())
    print('Thrown!')    #this is put here as a placeholder

throwcd = int(time.time()) - throwcdstart #this part is in a while true at the bottom, by the way

I've tried searching on here and Google, but the only results I got were for discord.py.

Edit: I forgot to say that I did set the throwcd variable as 2 at the start of my code.

Edit 2: There are also other things happening, like moving and enemy AI. I've tried to use things like time.sleep(), but it stalls the entire game when I do. I haven't tried math yet, because it seems pretty complicated.

YOVLYX
  • 1
  • 1
  • 1
    what's a cooldown? – Paul H Apr 06 '21 at 17:03
  • @PaulH a cooldown is where you aren't able to use an ability of some sort for a specific amount of time – YOVLYX Apr 06 '21 at 17:09
  • Does the game just "hang" for the duration of the cooldown, or are there other things happening? If freezing-up is OK, just call `time.sleep(delay_in_seconds)`. – martineau Apr 06 '21 at 17:28
  • There are other things happening like moving. – YOVLYX Apr 06 '21 at 18:07
  • In that case you might be able to use the [`turtle.ontimer()`](https://docs.python.org/3/library/turtle.html#turtle.ontimer) function to do it without causing everything to freeze-up. There's an example of its use in my [answer](https://stackoverflow.com/a/60919146/355230) to another question. (There currently isn't enough code in your question for me to be more specific.) – martineau Apr 06 '21 at 18:37

1 Answers1

0

quite cheekily formulated, because the question was asked very unclearly:

def throw(throwcdstart,throwcd):
  if throwcd >= 2:
    throwcdstart = int(time.time())
    mycooldown = 0.5 # seconds <==============
    time.sleep(mycooldown)
    print('Thrown!')    #this is put here as a placeholder

throwcd = int(time.time()) - throwcdstart #this part is in a while true at the bottom, by the way
Sunsheep
  • 52
  • 1
  • 6