🌶️ Hotdog Dash, Episode 7

The hotdog fights back: hot sauce in the four corners, a glowing hotdog, and a gull that lands on it and gets stunned. Two small steps.

Video: Hotdog Dash, Episode 7 (Hotdog Dash video)
Prefer to watch? This short video runs this page's main commands on a real Ubuntu 24.04 desktop, with on-screen captions and a synthetic voiceover (an AI voice trained on the author's own recording). Click to play, or watch it on YouTube.
Episode 7 ends with this: in each corner of the maze is a bottle of hot sauce. The hotdog that runs over one glows orange for six seconds. If a gull lands on a glowing hotdog it is not the hotdog that is hurt: the gull is stunned, sits dazed for two and a half seconds and the player gets 200 points.

You will need: your project from episode 6 (a game.py, an art folder and the .venv), or the starter files below.
You will make: a timer on the hotdog, a set that remembers which bottles are used, and a new gull state.
You will learn: countdown timers, a set, and adding a state to a state machine without touching the others.
Starter files: hotdog-dash-ep06.zip is where this episode begins, hotdog-dash-ep07.zip is where it ends. Both have the art folder.

The code of each step was run headless on Python 3.12 and pygame 2.6.1, and what it does is described below.

Where we start

Open a terminal, go to the project, turn on the virtual environment and run the game:

$ cd hotdog-dash
$ source .venv/bin/activate
(.venv) $ python game.py

The gull dives, the umbrellas protect. The file is 380 lines. The S in the maze, in the four corners, is a hot sauce bottle, and so far it has been drawn as plain floor.

Step 1: hot sauce and a hot hotdog

A hotdog is hot for HOT_TIME seconds after it eats a bottle. That is a number that counts down to zero, like the safe time in episode 5. Make these changes to game.py:

Add this after line 54 of your file (PAUSE = (2.5, 4.0)  # a gull waits this long between dives)

HOT_TIME = 6.0  # seconds a hotdog stays hot after the hot sauce
STUN_TIME = 2.5

- - -

Add this after line 92 of your file (self.safe = 0)

        self.hot = 0

- - -

Add this after line 109 of your file (self.safe -= dt)

        self.hot -= dt

- - -

Replace line 152

        name = f"hotdog_{frame}"

with

        name = f"hotdog_hot_{frame}" if self.hot > 0 else f"hotdog_{frame}"

- - -

Add this after line 258 of your file (self.packets = {})

        self.sauce_eaten = set()

- - -

Add this after line 264 of your file (self.packets = {})

        self.sauce_eaten = set()

- - -

Add this after line 287 of your file (self.score += PACKET_SCORE[flavor])

        if MAZE[row][col] == "S" and (col, row) not in self.sauce_eaten:
            self.sauce_eaten.add((col, row))
            self.player.hot = HOT_TIME

- - -

Add this after line 336 of your file (surf.blit(IMG["table"], (col * TILE, row * TILE)))

                elif char == "S" and (col, row) not in self.sauce_eaten:
                    surf.blit(IMG["hot_sauce"], IMG["hot_sauce"].get_rect(midbottom=(col * TILE + TILE / 2, (row + 1) * TILE)))

What is new:

HOT_TIME = 6.0, STUN_TIME      how long the hotdog stays hot, and (for step 2) how long a stunned gull stays on the ground
self.hot                       seconds of heat left. Counted down by hot -= dt in update, the same as self.safe. Above zero means hot
f"hotdog_hot_{frame}"          the glowing pictures, three frames of chomping, drawn when it is hot
self.sauce_eaten = set()       a set: a collection with no order and no repeats, good for 'have we seen this one'. We put the tile of each bottle in it when it is used
(col, row) not in self.sauce_eaten    this bottle has not been used. A used bottle is not drawn and does not work again. The set is cleared, in fill_packets, when the maze is filled again
midbottom=...                  the bottle is drawn with the bottom edge of its picture on the bottom edge of the tile, so a tall picture does not hang below the floor
the two set lines              one in __init__ and one in fill_packets. The first line is only there so that Game has the name from the start; the second is the one that makes a new maze bring the bottles back

Run it:

(.venv) $ python game.py

A bottle of hot sauce stands in each of the four corners. Run over one: the bottle disappears, the hotdog glows for six seconds and goes back to normal. Run over the same corner again: nothing. (Checked: eating the bottle at the top left corner set hot to 6.0; eating the same tile again after the heat had run out did not heat the hotdog again.)

Step 2: stun the gull

Now the heat has to do something. A stunned gull is the landed gull with a different timer and a different picture. Make these changes:

Replace line 208

        elif self.state == "land":

with

        elif self.state in ("land", "stunned"):

- - -

Replace line 210

            if self.timer >= STAY_TIME:

with

            if self.timer >= (STAY_TIME if self.state == "land" else STUN_TIME):

- - -

Replace line 228

        return self.state == "land"

with

        return self.state in ("land", "stunned")

- - -

Replace line 243

        name = "gull_land" if self.on_ground() else f"gull_{[0, 1, 2, 1][int(self.flap * 9) % 4]}"

with

        name = ("gull_land" if self.state == "land" else "gull_stun") if self.on_ground() else f"gull_{[0, 1, 2, 1][int(self.flap * 9) % 4]}"

- - -

Replace line 305

            if on_it and not is_cover(*tile):

with

            if under and self.player.hot > 0:
                gull.state, gull.timer = "stunned", 0
                self.score += 200
            elif on_it and not is_cover(*tile):

What is new:

"stunned"                      a sixth state. It reuses the code of 'land': the elif tests for either name, and the timer decides how long it stays: STAY_TIME for a normal landing and STUN_TIME for a stun
on_ground()                    true for both, so it is drawn on the ground, with its shadow full size and darkest
gull_stun                      the dazed picture,, drawn for the stunned state
under and self.player.hot > 0  the gull has landed on the hotdog's patch, and the hotdog is hot. Then the gull's state becomes stunned and its timer restarts, and the score gets 200
elif on_it and not ...         if it did not stun the gull, the old rule applies: a hit

One more thing in check_gulls: the stun test comes first, and it does not look at the safe time or at the cover. A hot hotdog under an umbrella still stuns the gull, and a hot hotdog that has just respawned does too.

Run it:

(.venv) $ python game.py

Eat a bottle and stand where the gull is going to land. It lands on you, but it is the gull that is hurt: it sits there dazed for two and a half seconds, flies away, and 200 points are added to the score. The heat runs out after six seconds, and then a landing hurts again. (Checked: a hot hotdog under a landed gull stunned it, the score went up by 200 and the lives stayed at 3.)

The whole file after step 2 is also hotdog-dash-ep07.zip.
Hotdog Dash episode 7 running
Episode 7 running (a screenshot of the finished step).

Tuning the feel

Hot sauce is the reward for taking a risk, so the numbers matter:

HOT_TIME = 6.0     long enough to run from a corner to the middle of the maze and wait for a dive. Too long and the gull is no danger
STUN_TIME = 2.5    the longer it stays, the more time to run past it
200                the score for a stun, more than the 10, 15 or 20 of a packet. That is the push to go for it

There are four bottles in the maze, one in each corner, and the maze is refilled with them at the end of each level.

Try it yourself

Three small changes before episode 8:

1  Make the hot hotdog faster: in Hotdog.update, multiply the speed by 1.3 while self.hot > 0. Does it feel like a reward?
2  Add a sound when the bottle is eaten. (Episode 8 does this, so you can compare.)
3  Put a fifth bottle in the middle of the maze: change a . in MAZE to an S.

Episode 8 is about the game around the game: levels, a title screen, a high score, sound and music.

Check your work

If something is different:

AttributeError: 'Gull' object has no attribute ...   the elif for 'stunned' has to be elif self.state in ("land", "stunned"), with the brackets
the bottles are never drawn                          the 'elif char == "S"' line goes in draw_maze after the table line, not after the packet loop
the bottle comes back on its own                     sauce_eaten must not be cleared anywhere except fill_packets
the hotdog is not stunning                           the stun test comes first in check_gulls, and uses under, not on_it
the gull picture is wrong when stunned               the name in draw: gull_stun for a stunned gull, gull_land for a landed one

Not run in the lab: other Linux distributions, Wayland-only sessions and pygame older than 2.6. The numbers in the checks come from running each stage at 60 frames per second.