Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sprites appearing too fast

I'm trying to create a program where balloons appear for the user to pop, however balloons are appearing so fast it becomes unmanageable. I took a screenshot about half a second into running the program: enter image description here

Here is the code for time between balloons appearing:

timeTillNextBalloon = random.randint(100000, 200000)

while done == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    if pygame.time.get_ticks() > timeTillNextBalloon:
        timeTillNextBalloon = random.randint(30000, 250000)
        yCoord = random.randint(50,350)
        balloonType = random.randint(1,4)
        balloon = Balloon(0, yCoord, "right", balloonType)
        if balloonType >= 1 and balloonType <= 3:
            otherBalloons.add(balloon)
        else:
            blueBalloons.add(balloon)
        allBalloons.add(balloon)

I've tried to increase the timeTillNextBaloon variable, but it just shows a black screen if I try to make it any bigger than this.

like image 569
Nico Avatar asked Dec 08 '25 06:12

Nico


1 Answers

Get_ticks gets the current time, timeTillNextBalloon should be the current to + the random value. Now every the loop repeats a balloon is added:

timeTillNextBalloon = pygame.time.get_ticks() + random.randint(30000, 250000)
like image 176
Luc Avatar answered Dec 09 '25 20:12

Luc