2

I am doing a kind of bomberman, and I am trying to do that the bomb explodes after a while. Sorry if this question already exists. I have been looking for any answer but I didnt find.

This should be like: 1. I put a bomb somewhere 2. The bomb waits 5 seconds 3. The bomb explodes

I dont know how to give the 5 seconds before to explode.

class bomb(object):
def __init__(self, aposX, aposY, bombRange = 5):
    self.posX = aposX 
    self.posY = aposY
    self.bombRange = bombRange
    self.timeToExplode = 5000
    pygame.draw.circle(ventana,(200,0,0),(self.posX,self.posY),20)


def update(self):
    pygame.draw.circle(ventana,(200,0,0),(self.posX,self.posY),20)

    #Here should wait 5 seconds and then call the explde method
    self.explode()

def explode(self):
    pygame.draw.line(ventana,(200,0,0),(self.posX,self.posY),(self.posX+20+(40*self.bombRange),self.posY),40)
    pygame.draw.line(ventana,(200,0,0),(self.posX,self.posY),(self.posX-20-(40*self.bombRange),self.posY),40)
    pygame.draw.line(ventana,(200,0,0),(self.posX,self.posY),(self.posX,self.posY+20+(40*self.bombRange)),40)
    pygame.draw.line(ventana,(200,0,0),(self.posX,self.posY),(self.posX,self.posY-20-(40*self.bombRange)),40)

I hope you can help me.I am going to appreciate that.

MatiasCL
  • 25
  • 3
  • could you paste your game's main loop code? or where you calling bomb's update method? – SungJin Steve Yoo Oct 21 '17 at 07:36
  • Check out these [answers](https://stackoverflow.com/questions/30720665/countdown-timer-in-pygame) to learn how you can implement a timer in pygame. – skrx Oct 21 '17 at 08:17

1 Answers1

1

Here's a little example with the dt variant. I pass the dt to the update method where I use it to decrement the timer attribute. In the main loop I just draw the lines of the explosion if the timer is below 0. To remove the instances I put the exploded bombs into a set which I subtract from the bomb_set that contains all bomb instances.

import pygame


class Bomb(object):
    def __init__(self, aposX, aposY, bombRange=5):
        self.posX = aposX 
        self.posY = aposY
        self.bombRange = bombRange
        self.timeToExplode = 3000

    def update(self, dt):
        # Subtract the passed time `dt` from the timer each frame.
        self.timeToExplode -= dt

    def explode(self, screen):
        pygame.draw.line(screen,(200,0,0),(self.posX,self.posY),(self.posX+20+(40*self.bombRange),self.posY),40)
        pygame.draw.line(screen,(200,0,0),(self.posX,self.posY),(self.posX-20-(40*self.bombRange),self.posY),40)
        pygame.draw.line(screen,(200,0,0),(self.posX,self.posY),(self.posX,self.posY+20+(40*self.bombRange)),40)
        pygame.draw.line(screen,(200,0,0),(self.posX,self.posY),(self.posX,self.posY-20-(40*self.bombRange)),40)

    def draw(self, screen):
        pygame.draw.circle(screen,(200,0,0),(self.posX,self.posY),20)


def main():
    screen = pygame.display.set_mode((640, 480))
    clock = pygame.time.Clock()
    bomb_set = set()  # This set holds the bomb instances.

    done = False

    while not done:
        # Get the passed time since last clock.tick call.
        dt = clock.tick(30)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    bomb_set.add(Bomb(*event.pos))

        # Game logic.
        to_remove = set()

        # Update bombs. Pass the `dt` to the bomb instances.
        for bomb in bomb_set:
            bomb.update(dt)
            # Add old bombs to the to_remove set.
            if bomb.timeToExplode <= -3000:
                to_remove.add(bomb)

        # Remove bombs fromt the bomb_set.
        if to_remove:
            bomb_set -= to_remove

        # Draw everything.
        screen.fill((30, 30, 30))
        for bomb in bomb_set:
            bomb.draw(screen)
            # I'm just drawing the explosion lines each
            # frame when the time is below 0.
            if bomb.timeToExplode <= 0:
                bomb.explode(screen)

        pygame.display.flip()


if __name__ == '__main__':
    pygame.init()
    main()
    pygame.quit()
skrx
  • 19,980
  • 5
  • 34
  • 48
  • I recommend to take a look at [pygame sprites and sprite groups](http://programarcadegames.com/index.php?chapter=introduction_to_sprites&lang=en#section_13). – skrx Oct 21 '17 at 12:40