0

I have this game where a balloon needs to explode every 3 seconds, but the explosion needs to be delayed for about 1 second (right now, it's almost immediate). I have the balloon every 3 seconds part down, but I'm having trouble on the delay part. If I use sleep or wait, all the other animations I have going on stop, and that's not what I want. Does anyone have any tips?

sy89
  • 195
  • 2
  • 14

2 Answers2

0

pygame.time.get_ticks() gives current time in milliseconds.

You can set (3s = 3000ms)

 next_baloon = pygame.time.get_ticks() + 3*1000

and in every loop you have to check if it is time to start ballon

 if next_baloon <= pygame.time.get_ticks():
       start_ballon()
       next_baloon = pygame.time.get_ticks() + 3*1000
       next_explosion = pygame.time.get_ticks() + 1*1000

 if next_explosion <= pygame.time.get_ticks():
       explode_ballon()
furas
  • 134,197
  • 12
  • 106
  • 148
0

Here's a complete example. I highly recommend using object-oriented programming, pygame sprites and sprite groups. These sprites have a timer attribute which is decreased by the delta time, dt, each frame. When the time of a balloon is up, it is removed from the containing sprite groups balloon.kill() and an explosion instance is added instead. The explosion also has a timer and removes itself, self.kill(), when the timer is below 0. (Press a mouse button to add more balloons.)

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
# Images. Balloon = blue, explosion = orange.
BALLOON_IMAGE = pg.Surface((50, 50), pg.SRCALPHA)
pg.draw.circle(BALLOON_IMAGE, pg.Color('steelblue2'), (25, 25), 25)
EXPLOSION_IMAGE = pg.Surface((80, 80), pg.SRCALPHA)
pg.draw.circle(EXPLOSION_IMAGE, pg.Color('sienna1'), (40, 40), 40)


class Balloon(pg.sprite.Sprite):

    def __init__(self, pos):
        super().__init__()
        self.image = BALLOON_IMAGE
        self.rect = self.image.get_rect(center=pos)
        self.timer = 3

    def update(self, dt):
        self.timer -= dt


class Explosion(pg.sprite.Sprite):

    def __init__(self, pos):
        super().__init__()
        self.image = EXPLOSION_IMAGE
        self.rect = self.image.get_rect(center=pos)
        self.timer = 1

    def update(self, dt):
        self.timer -= dt
        if self.timer <= 0:
            self.kill()


balloons = pg.sprite.Group(Balloon((300, 300)))
all_sprites = pg.sprite.Group(balloons)

done = False

while not done:
    dt = clock.tick(30) / 1000

    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.MOUSEBUTTONDOWN:
            balloon = Balloon(event.pos)
            balloons.add(balloon)
            all_sprites.add(balloon)

    all_sprites.update(dt)
    for balloon in balloons:
        if balloon.timer <= 0:
            balloon.kill()
            all_sprites.add(Explosion(balloon.rect.center))

    screen.fill((30, 30, 30))
    all_sprites.draw(screen)

    pg.display.flip()
skrx
  • 19,980
  • 5
  • 34
  • 48
  • Chapter 12 and 13 of [Program Arcade Games](http://programarcadegames.com/index.php?chapter=introduction_to_sprites&lang=en#section_13) explain how classes, sprites and sprite groups work, if you don't know them yet. – skrx Nov 24 '17 at 12:22