2

I am struggling to find a way to make a timer that can blit this image every 2 seconds. It's an image of a net and is meant to trap my player on my platform game. The net is dropped from a helicopter which I wont go into details about. At the moment the net drops once and I want it to drop continuously ever 2 seconds. Any help would be appreciated.

class Net:
def __init__(self,x,y):
    self.x = x
    self.y = y
    self.width = 60
    self.height = 30
    self.velocity = 0
    self.netimage = pygame.image.load("Images/net.png")
    self.GroundCollision = False

def drop(self, heli, player, gravity):
    screen.blit(self.netimage, (self.x,self.y))
    self.velocity += gravity
    self.y += self.velocity
CubeJockey
  • 2,209
  • 8
  • 24
  • 31
Jim
  • 109
  • 9
  • in `mainloop` call `Net.update()` which compare current time with `Net.time_to_blit_image` and after blit it sets `Net.time_to_blit_image = current_time + 2seconds`. You can use `pygame.time.get_ticks` to get current time. – furas Jan 06 '16 at 16:18
  • or use `pygame.time.set_timer()` – furas Jan 06 '16 at 16:23
  • Possible duplicate of [Move an object every few seconds in Pygame](http://stackoverflow.com/questions/23368999/move-an-object-every-few-seconds-in-pygame) – sloth Jan 07 '16 at 10:36

1 Answers1

1

This is what I do with my games:

  1. I create a variable called timer, and set it to 0.
  2. Every loop, I check the timer variable with modulo.
  3. If timer%something==0, do the thing.

It is also helpful to have a variable called "fps", in case you want to change your framerate.

Just type timer=0 before your while loop.

Then type the following code inside of your while loop:

clock.tick(fps)
timer += 1
if timer % (fps * 2) == 0: #executes the following every 2 seconds
    Net.drop()

And that is all that is needed! Have fun Pygaming!