Nico
Nico

Reputation: 101

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.

Upvotes: 3

Views: 67

Answers (1)

Luc
Luc

Reputation: 1491

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)

Upvotes: 1

Related Questions