In total, there are three Sparky's that can be on the screen at any given time. To keep game play interesting, each of their appearances on screen are randomized.

The first way that they're randomized is by having varying delays using time.monotonic(), similar to how Blinka's animation is delayed without slowing down the loop. Each Sparky's generation is delayed by 0.03 seconds, 0.07 seconds and 0.12 seconds respectively. This allows their appearances to be staggered.

Once the defined time has elapsed, then there is a check to see if a random integer matches with a predefined integer: 3, 7 and 12 respectively.

If the random integer matches, then the sparky_state for the corresponding Sparky sprite is updated to True, which allows them to appear on screen. If the random integer is not a match, then the time.monotonic() count begins again.

#  Sparkys are generated using a staggered delay
        #  and matching an int to a random int
        #  1st Sparky
        if (blue + 0.03) < time.monotonic():
            if randint(1, 15) == 3:
                sparky_states[0] = True
            blue = time.monotonic()
        #  2nd Sparky
        if (smoke + 0.07) < time.monotonic():
            if randint(1, 15) == 7:
                sparky_states[1] = True
            smoke = time.monotonic()
        #  3rd Sparky
        if (monster + 0.12) < time.monotonic():
            if randint(1, 15) == 12:
                sparky_states[2] = True
            monster = time.monotonic()

When a Sparky's state is True, their x coordinate location updates by 1 pixel continuously, allowing them to smoothly move across the screen.

The corresponding index in the sparky_x array is also updated to hold the x coordinate. This is used later in the loop for discerning whether Blinka and a Sparky have a collision. 

Finally, when a Sparky's x location is -16 pixels, which is off-screen, their x coordinate is reset to 100 to prepare them for the next time they have a random integer match. 

if sparky_states[0] is True:
            sparky0_grid.x -= 1
            sparky_x[0] = sparky0_grid.x
            display.refresh(target_frames_per_second=120)
            #  when a Sparky is 16 pixels off the display,
            #  it goes back to its starting position
            if sparky0_grid.x is -16:
                sparky_states[0] = False
                sparky0_grid.x = 100
                sparky_x[0] = sparky0_grid.x
        #  2nd Sparky
        if sparky_states[1] is True:
            sparky1_grid.x -= 1
            sparky_x[1] = sparky1_grid.x
            display.refresh(target_frames_per_second=120)
            if sparky1_grid.x is -16:
                sparky_states[1] = False
                sparky1_grid.x = 100
                sparky_x[1] = sparky1_grid.x
        #  3rd Sparky
        if sparky_states[2] is True:
            sparky2_grid.x -= 1
            sparky_x[2] = sparky2_grid.x
            display.refresh(target_frames_per_second=120)
            if sparky2_grid.x is -16:
                sparky_states[2] = False
                sparky2_grid.x = 100
                sparky_x[2] = sparky2_grid.x

This guide was first published on Jul 01, 2020. It was last updated on Mar 14, 2024.

This page (Randomly Generate Blue Smoke Monsters) was last updated on Mar 08, 2024.

Text editor powered by tinymce.