A gull cannot land on you under an umbrella: one new function, one changed line and a hint on the panel. Two small steps.
▶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 6 ends with this: a hotdog on an umbrella or a picnic table is not hurt when a gull lands on it. A dive that lands on the next tiles still hurts, so the player has to choose where to be, and the hint on the panel says so. It is a short episode: the maze already has the umbrellas and tables, they were drawn from the first episode, and now they do something.
You will need: your project from episode 5 (a game.py, an art folder and the .venv), or the starter files below.
You will make: a function that says whether a tile is cover, and one changed rule.
You will learn: how one small rule changes how a whole game plays, and how to say it on the screen.
Starter files: hotdog-dash-ep05.zip is where this episode begins, hotdog-dash-ep06.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 and the hotdog loses lives. The file is 372 lines. We add one function and change one line of check_gulls.
Step 1: cover
Umbrellas are U in the maze, and picnic tables are T and t. The hotdog can already walk on them, because is_open lets everything except a hedge through. Make these changes to game.py:
Add this after line 69 of your file (return 0 <= col < COLS and 0 <= row < ROWS and MAZE[row][col] != "#")
def is_cover(col, row):
"""Umbrellas and picnic tables: a gull cannot dive onto a hotdog standing under one."""
return MAZE[row][col] in "UTt"
- - -
Replace line 290
if on_it:
with
if on_it and not is_cover(*tile):
What is new:
MAZE[row][col] in "UTt" the character on that tile, and the test 'is it one of these three characters'. in works on strings: "U" in "UTt" is true, "." in "UTt" is false
is_cover(*tile) tile is the pair (col, row) from self.player.tile(). The star unpacks it into the two arguments, the way KEYS[...] did in episode 2
on_it and not is_cover(...) the hotdog is hit when it is on the landing patch, not safe, and not on cover. If it is on cover, nothing happens: the gull lands, stands for a second and leaves
Note what it does not do: it protects only the tile the hotdog stands on. The dive still covers a 3 by 3 patch, so a hotdog under an umbrella is safe, and a hotdog beside one is not.
Run it:
(.venv) $ python game.py
Run onto an umbrella or a picnic table. A hotdog keeps running until it meets a hedge, so it cannot stand still in the middle of a lane: you hide by running onto the cover and turning back and forth on it, and by timing it so you are on the cover when the gull lands. When the gull lands on you there, nothing happens: the lives stay the same. Be on the open floor next to it and you are hit. (Checked in the lab: is_cover is true for an umbrella, the two halves of a table and false for a hedge and for floor. With a gull landed on the hotdog under an umbrella the lives stayed at 3; the same dive on the open floor next to it took one life, 3 to 2.)
Step 2: tell the player
A rule the player does not know is no good. The panel has room under the packet counters. Make these changes:
Add this after line 253 of your file (self.score_font = pygame.font.Font(None, 76))
self.small_font = pygame.font.Font(None, 30)
- - -
Add this after line 353 of your file (surf.blit(self.font.render(f"x {left}", True, FLAVOR_COLORS[flavor]), (MAZE_W + 70, y + 4)))
surf.blit(self.small_font.render("Duck under an umbrella", True, (255, 243, 160)), (MAZE_W + 26, 596))
surf.blit(self.small_font.render("to hide from a dive.", True, (255, 243, 160)), (MAZE_W + 26, 626))
What is new:
small_font a font of size 30, smaller than the one used for the counters
two lines of hint a short text, in a light yellow, at the bottom of the panel. The text is split in two so that it fits the 320 pixels of the panel; font.render gives a picture, and a long text would be cut off at the edge of the window
Run it:
(.venv) $ python game.py
At the bottom of the panel: 'Duck under an umbrella to hide from a dive.' The whole file after step 2 is also hotdog-dash-ep06.zip.
Episode 6 running (a screenshot of the finished step).
Tuning the feel
Cover is what makes the maze a puzzle, and it is easy to break:
too many umbrellas the game is easy, because there is always one close
too few the gull's dive is a lottery
where they are change the MAZE and see how much it matters
Try it yourself
Three small changes before episode 7:
1 Make the hotdog safe on the hot sauce tile too: add S to the string in is_cover. How does that change where the player runs?
2 Remove all but two umbrellas from MAZE. Is it still fun?
3 Make the hint text blink. (Hint: the blink in Hotdog.draw.)
Episode 7 gives the hotdog a way to fight back: hot sauce.
Check your work
If something is different:
NameError: name 'is_cover' is not defined the function goes above class Hotdog, next to is_open
TypeError: is_cover() takes 2 positional arguments but 3 were given in check_gulls it is is_cover(*tile), with the star
the hotdog is always safe 'not' is missing: it is on_it and not is_cover(...)
AttributeError: 'Game' object has no attribute 'small_font' the small_font line goes in Game.__init__, next to the other fonts
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.