0

I want to have a delay between each shot or maybe a limited bulled number and then a reload time.

def input (key):
    if key == 'space':
        e = Entity(y=zeri.y, x=zeri.x+2, model='quad', collider='box', texture="textures/bum.png")
        e.animate_x(30, duration=2, curve=curve.linear)
        invoke(destroy, e, delay=2)

This is the bullet code.

I tried by using time.sleep() and expected it to stop the player from using commands but it just stops everything. For the bulled reload I tried:

def input (key):
    bullets=10
    if key == 'space' and if bullets>0:
        e = Entity(y=zeri.y, x=zeri.x+2, model='quad', collider='box', texture="textures/bum.png")
        e.animate_x(30, duration=2, curve=curve.linear)
        invoke(destroy, e, delay=2)
        bullets=bullets-1

I expected it to stop shooting after 10 bullets, but you can still shoot for as long as you want.

dm2
  • 4,053
  • 3
  • 17
  • 28
  • You should not stop anything but read and store the timestamp after 10 bullets then whenever user tries to shot again, check how much seconds/minutes /whatever passed since you stored the stamp. If more less than your threshold you do nothing, if more then you lift the shoting ban and cycle repeats. – Marcin Orlowski Nov 29 '22 at 21:03
  • how do i store it? if i use the x=time.time( ) it just updates as time goes on so if i check how much time passed it just remains 0. Is there a way i can store it withut i changing over time? – Paolo Gangemi Nov 30 '22 at 21:19

1 Answers1

0

This would give you delay after each shot (untested pseudo code out of my head but you should get the picture)

from time import time

shot_threshold = 20 # Each shot no sooner than 20ms from last one
last_shot = None

if key == 'space':
   if not last_shot or time.time_ns() - last_shot > shot_threshold:
      # store the shot stamp to check when fired again
      last_shot = time.time_ns()
      # handle your shot normal way

Similar way logic would be for reload delay. Just add another variable to hold the stamp when you start the reload and extend the condition to check for that (or reorganize your code as you like, as you i.e. may want to add some visual effect for the period of reloading etc). The only difference would be that that stamp would be set after your bullet count drops to 0.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • i tried like this: def input(key): shot_threshold = 20 last_shot= None if key=="space": if not last_shot or time.time() - last_shot > shot_threshold: #mycode last_shot=time.time(). But it doen't encrase the delay even if i encrase the shot threshhold variable, how can i change it to make it work? PS with from time import time or from time import * the time.time_ns( ) doen't work, is it maybe because i used import time instead? – Paolo Gangemi Dec 01 '22 at 16:09