Getting hit, a moment of safety and three lives. Three small steps, each one run before it was written down.
▶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: when a shake touches the burger, a splat appears, the burger goes back to the middle and blinks for two seconds, during which nothing can hurt it. After the third hit the whole game starts again. The lives are not on the screen yet; that is episode 7.
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 collision test between the burger and the shakes, a safe-time timer with a blinking draw, and a new_game method with three lives.
You will learn: another timer with dt, drawing nothing on purpose to make a blink, and one method that resets the whole game.
Starter files: burger-blaster-ep05.zip is where this episode begins, burger-blaster-ep06.zip is where it ends. Both have the art folder.
The code on this page was run on a clean Ubuntu 24.04 desktop with Python 3.12.3 and pygame 2.6.1. Each of the three steps was run on its own, 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 burger-blaster
$ source .venv/bin/activate
(.venv) $ python game.py
The burger shoots, the shakes split and splat, and a new wave comes when the screen is clear. But the shakes cannot hurt the burger, so nothing is at stake. The file is 227 lines.
Step 1: getting hit
The test is the same circle test as for the fries, between the burger and each shake. Add a method check_player_hit to class Game and call it after check_fry_hits. The whole file after step 1:
#!/usr/bin/env python3
"""Burger Blaster: a hamburger shoots french fries at angry milkshakes."""
import random
import sys
from pathlib import Path
import pygame
WIDTH, HEIGHT = 1280, 720
FPS = 60
HERE = Path(__file__).resolve().parent
ART = HERE / "art"
TURN_SPEED = 220 # degrees per second
THRUST = 420 # pixels per second, every second
FRICTION = 0.55 # share of speed left after one second of coasting
MAX_SPEED = 480
FRY_SPEED = 720
FRY_LIFE = 0.9 # seconds before a fry disappears
SHOOT_DELAY = 0.22 # seconds between shots
FLAVORS = ["strawberry", "chocolate", "mint"]
IMG = {}
def load_images():
return {p.stem: pygame.image.load(p).convert_alpha() for p in ART.glob("*.png")}
def wrap(pos, margin=40):
"""Leave one side of the screen, come back on the other."""
pos.x = (pos.x + margin) % (WIDTH + 2 * margin) - margin
pos.y = (pos.y + margin) % (HEIGHT + 2 * margin) - margin
def draw_rotated(surf, image, pos, angle):
rotated = pygame.transform.rotate(image, -angle) # pygame turns counter-clockwise
surf.blit(rotated, rotated.get_rect(center=pos))
class Player:
radius = 34
def __init__(self):
self.reset()
def reset(self):
self.pos = pygame.Vector2(WIDTH / 2, HEIGHT / 2)
self.vel = pygame.Vector2()
self.angle = 0 # degrees, 0 = up, clockwise
self.cooldown = 0
def direction(self):
return pygame.Vector2(0, -1).rotate(self.angle)
def update(self, dt, keys):
if keys[pygame.K_LEFT]:
self.angle -= TURN_SPEED * dt
if keys[pygame.K_RIGHT]:
self.angle += TURN_SPEED * dt
if keys[pygame.K_UP]:
self.vel += self.direction() * THRUST * dt
self.vel *= FRICTION**dt
if self.vel.length() > MAX_SPEED:
self.vel.scale_to_length(MAX_SPEED)
self.pos += self.vel * dt
wrap(self.pos)
self.cooldown -= dt
def shoot(self, keys):
if keys[pygame.K_SPACE] and self.cooldown <= 0:
self.cooldown = SHOOT_DELAY
return Fry(self.pos + self.direction() * 44, self.direction(), self.angle)
def draw(self, surf):
draw_rotated(surf, IMG["burger"], self.pos, self.angle)
class Fry:
radius = 8
def __init__(self, pos, direction, angle):
self.pos = pygame.Vector2(pos)
self.vel = direction * FRY_SPEED
self.angle = angle
self.life = FRY_LIFE
def update(self, dt):
self.pos += self.vel * dt
wrap(self.pos)
self.life -= dt
def draw(self, surf):
draw_rotated(surf, IMG["fry"], self.pos, self.angle)
class Shake:
def __init__(self, pos, size, flavor, boost=1.0):
self.pos = pygame.Vector2(pos)
self.size = size # "big" or "small"
self.flavor = flavor
self.boost = boost
self.radius = 50 if size == "big" else 25
speed = random.uniform(60, 130) if size == "big" else random.uniform(110, 200)
self.vel = pygame.Vector2(speed * boost, 0).rotate(random.uniform(0, 360))
self.angle = random.uniform(0, 360)
self.spin = random.uniform(-90, 90)
self.image = IMG[f"shake_{flavor}" if size == "big" else f"shake_{flavor}_small"]
def update(self, dt):
self.pos += self.vel * dt
self.angle += self.spin * dt
wrap(self.pos, 70)
def draw(self, surf):
draw_rotated(surf, self.image, self.pos, self.angle)
class Splat:
"""A short animation where a shake used to be."""
DURATION = 0.4
def __init__(self, pos, flavor):
self.pos = pygame.Vector2(pos)
self.flavor = flavor
self.age = 0
def update(self, dt):
self.age += dt
def draw(self, surf):
frame = min(int(self.age / self.DURATION * 5), 4)
image = IMG[f"splat_{self.flavor}_{frame}"]
surf.blit(image, image.get_rect(center=self.pos))
class Game:
def __init__(self):
pygame.init()
pygame.display.set_caption("Burger Blaster")
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.clock = pygame.time.Clock()
IMG.update(load_images())
self.running = True
self.player = Player()
self.fries = []
self.shakes = []
self.splats = []
self.spawn_shakes(4)
def spawn_shakes(self, count):
for _ in range(count):
while True: # keep new shakes away from the burger
pos = pygame.Vector2(random.uniform(0, WIDTH), random.uniform(0, HEIGHT))
if pos.distance_to(self.player.pos) > 300:
break
self.shakes.append(Shake(pos, "big", random.choice(FLAVORS)))
def pop(self, shake):
"""A fry hit a shake: big ones split in two, small ones splat."""
self.shakes.remove(shake)
self.splats.append(Splat(shake.pos, shake.flavor))
if shake.size == "big":
for _ in range(2):
self.shakes.append(Shake(shake.pos, "small", shake.flavor, shake.boost))
def check_fry_hits(self):
for fry in self.fries:
if fry.life <= 0:
continue
for shake in self.shakes:
if fry.pos.distance_to(shake.pos) < shake.radius + fry.radius:
fry.life = 0
self.pop(shake)
break
def check_player_hit(self):
player = self.player
for shake in self.shakes:
if player.pos.distance_to(shake.pos) < player.radius + shake.radius * 0.8:
self.splats.append(Splat(player.pos, "chocolate"))
player.reset()
return
def handle_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.running = False
def update(self, dt):
keys = pygame.key.get_pressed()
self.player.update(dt, keys)
fry = self.player.shoot(keys)
if fry:
self.fries.append(fry)
for fry in self.fries:
fry.update(dt)
self.fries = [f for f in self.fries if f.life > 0]
for shake in self.shakes:
shake.update(dt)
if not self.shakes:
self.spawn_shakes(4)
self.check_fry_hits()
self.check_player_hit()
for splat in self.splats:
splat.update(dt)
self.splats = [s for s in self.splats if s.age < Splat.DURATION]
def draw(self):
surf = self.screen
surf.blit(IMG["background"], (0, 0))
for splat in self.splats:
splat.draw(surf)
for shake in self.shakes:
shake.draw(surf)
for fry in self.fries:
fry.draw(surf)
self.player.draw(surf)
pygame.display.flip()
def run(self):
while self.running:
dt = self.clock.tick(FPS) / 1000
self.handle_events()
self.update(dt)
self.draw()
pygame.quit()
if __name__ == "__main__":
Game().run()
sys.exit(0)
What is new:
player.radius = 34 the burger's radius, set in episode 2 and never used until now
shake.radius * 0.8 only 80 percent of the shake counts. A circle test is rougher than the picture, and a hit that looks like a near miss feels unfair, so the test is a little generous to the player
Splat(player.pos, "chocolate") the splat from episode 5, on the burger. Chocolate is a good color for it
player.reset() the reset from episode 2 puts the burger back in the middle of the screen, standing still, pointing up
return at most one hit per frame, so two shakes at once do not cost two lives
Run it, and fly into a shake:
(.venv) $ python game.py
A splat, and the burger is back in the middle. The problem shows up when a shake is near the middle: the burger is hit again in the next frame, and in the one after that, for as long as the shake is there. (Checked in the lab, with a shake parked on the respawn point: 30 hits in half a second, one on every frame.)
Step 2: a moment of safety
After a hit the burger needs a few seconds in which it cannot be hit. A timer does it, the same kind as the gun's cooldown from episode 3. The whole file after step 2:
#!/usr/bin/env python3
"""Burger Blaster: a hamburger shoots french fries at angry milkshakes."""
import random
import sys
from pathlib import Path
import pygame
WIDTH, HEIGHT = 1280, 720
FPS = 60
HERE = Path(__file__).resolve().parent
ART = HERE / "art"
TURN_SPEED = 220 # degrees per second
THRUST = 420 # pixels per second, every second
FRICTION = 0.55 # share of speed left after one second of coasting
MAX_SPEED = 480
FRY_SPEED = 720
FRY_LIFE = 0.9 # seconds before a fry disappears
SHOOT_DELAY = 0.22 # seconds between shots
FLAVORS = ["strawberry", "chocolate", "mint"]
IMG = {}
def load_images():
return {p.stem: pygame.image.load(p).convert_alpha() for p in ART.glob("*.png")}
def wrap(pos, margin=40):
"""Leave one side of the screen, come back on the other."""
pos.x = (pos.x + margin) % (WIDTH + 2 * margin) - margin
pos.y = (pos.y + margin) % (HEIGHT + 2 * margin) - margin
def draw_rotated(surf, image, pos, angle):
rotated = pygame.transform.rotate(image, -angle) # pygame turns counter-clockwise
surf.blit(rotated, rotated.get_rect(center=pos))
class Player:
radius = 34
def __init__(self):
self.reset()
def reset(self):
self.pos = pygame.Vector2(WIDTH / 2, HEIGHT / 2)
self.vel = pygame.Vector2()
self.angle = 0 # degrees, 0 = up, clockwise
self.cooldown = 0
self.invulnerable = 0
def direction(self):
return pygame.Vector2(0, -1).rotate(self.angle)
def update(self, dt, keys):
if keys[pygame.K_LEFT]:
self.angle -= TURN_SPEED * dt
if keys[pygame.K_RIGHT]:
self.angle += TURN_SPEED * dt
if keys[pygame.K_UP]:
self.vel += self.direction() * THRUST * dt
self.vel *= FRICTION**dt
if self.vel.length() > MAX_SPEED:
self.vel.scale_to_length(MAX_SPEED)
self.pos += self.vel * dt
wrap(self.pos)
self.cooldown -= dt
self.invulnerable -= dt
def shoot(self, keys):
if keys[pygame.K_SPACE] and self.cooldown <= 0:
self.cooldown = SHOOT_DELAY
return Fry(self.pos + self.direction() * 44, self.direction(), self.angle)
def draw(self, surf):
if self.invulnerable > 0 and int(self.invulnerable * 10) % 2:
return # blink while safe
draw_rotated(surf, IMG["burger"], self.pos, self.angle)
class Fry:
radius = 8
def __init__(self, pos, direction, angle):
self.pos = pygame.Vector2(pos)
self.vel = direction * FRY_SPEED
self.angle = angle
self.life = FRY_LIFE
def update(self, dt):
self.pos += self.vel * dt
wrap(self.pos)
self.life -= dt
def draw(self, surf):
draw_rotated(surf, IMG["fry"], self.pos, self.angle)
class Shake:
def __init__(self, pos, size, flavor, boost=1.0):
self.pos = pygame.Vector2(pos)
self.size = size # "big" or "small"
self.flavor = flavor
self.boost = boost
self.radius = 50 if size == "big" else 25
speed = random.uniform(60, 130) if size == "big" else random.uniform(110, 200)
self.vel = pygame.Vector2(speed * boost, 0).rotate(random.uniform(0, 360))
self.angle = random.uniform(0, 360)
self.spin = random.uniform(-90, 90)
self.image = IMG[f"shake_{flavor}" if size == "big" else f"shake_{flavor}_small"]
def update(self, dt):
self.pos += self.vel * dt
self.angle += self.spin * dt
wrap(self.pos, 70)
def draw(self, surf):
draw_rotated(surf, self.image, self.pos, self.angle)
class Splat:
"""A short animation where a shake used to be."""
DURATION = 0.4
def __init__(self, pos, flavor):
self.pos = pygame.Vector2(pos)
self.flavor = flavor
self.age = 0
def update(self, dt):
self.age += dt
def draw(self, surf):
frame = min(int(self.age / self.DURATION * 5), 4)
image = IMG[f"splat_{self.flavor}_{frame}"]
surf.blit(image, image.get_rect(center=self.pos))
class Game:
def __init__(self):
pygame.init()
pygame.display.set_caption("Burger Blaster")
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.clock = pygame.time.Clock()
IMG.update(load_images())
self.running = True
self.player = Player()
self.fries = []
self.shakes = []
self.splats = []
self.spawn_shakes(4)
def spawn_shakes(self, count):
for _ in range(count):
while True: # keep new shakes away from the burger
pos = pygame.Vector2(random.uniform(0, WIDTH), random.uniform(0, HEIGHT))
if pos.distance_to(self.player.pos) > 300:
break
self.shakes.append(Shake(pos, "big", random.choice(FLAVORS)))
def pop(self, shake):
"""A fry hit a shake: big ones split in two, small ones splat."""
self.shakes.remove(shake)
self.splats.append(Splat(shake.pos, shake.flavor))
if shake.size == "big":
for _ in range(2):
self.shakes.append(Shake(shake.pos, "small", shake.flavor, shake.boost))
def check_fry_hits(self):
for fry in self.fries:
if fry.life <= 0:
continue
for shake in self.shakes:
if fry.pos.distance_to(shake.pos) < shake.radius + fry.radius:
fry.life = 0
self.pop(shake)
break
def check_player_hit(self):
player = self.player
if player.invulnerable > 0:
return
for shake in self.shakes:
if player.pos.distance_to(shake.pos) < player.radius + shake.radius * 0.8:
self.splats.append(Splat(player.pos, "chocolate"))
player.reset()
player.invulnerable = 2.0
return
def handle_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.running = False
def update(self, dt):
keys = pygame.key.get_pressed()
self.player.update(dt, keys)
fry = self.player.shoot(keys)
if fry:
self.fries.append(fry)
for fry in self.fries:
fry.update(dt)
self.fries = [f for f in self.fries if f.life > 0]
for shake in self.shakes:
shake.update(dt)
if not self.shakes:
self.spawn_shakes(4)
self.check_fry_hits()
self.check_player_hit()
for splat in self.splats:
splat.update(dt)
self.splats = [s for s in self.splats if s.age < Splat.DURATION]
def draw(self):
surf = self.screen
surf.blit(IMG["background"], (0, 0))
for splat in self.splats:
splat.draw(surf)
for shake in self.shakes:
shake.draw(surf)
for fry in self.fries:
fry.draw(surf)
self.player.draw(surf)
pygame.display.flip()
def run(self):
while self.running:
dt = self.clock.tick(FPS) / 1000
self.handle_events()
self.update(dt)
self.draw()
pygame.quit()
if __name__ == "__main__":
Game().run()
sys.exit(0)
What is new:
self.invulnerable = 0 a timer in seconds, for the safe time. 0 means vulnerable
self.invulnerable -= dt counts down in Player.update, every frame
player.invulnerable = 2.0 set after each hit, together with the respawn
if player.invulnerable > 0: return check_player_hit does nothing while the burger is safe
int(self.invulnerable * 10) % 2 the blink. The time left, times 10, rounded down: 19, 18, 17 and so on, one number per tenth of a second. % 2 is the remainder after dividing by 2, which is 1 for odd numbers and 0 for even ones. 1 counts as true, so every other tenth of a second draw returns without drawing the burger
Run it:
(.venv) $ python game.py
A hit, a splat, and the burger comes back blinking. For two seconds the shakes pass right through it, and then it is solid again. (Checked, with a shake parked on the respawn point: one hit in the first two seconds, and the next one only after the safe time ran out.)
Step 3: lives and a reset
The last step: three lives, and the whole game starts again when they run out. The whole file after step 3, which is also the file in burger-blaster-ep06.zip:
#!/usr/bin/env python3
"""Burger Blaster: a hamburger shoots french fries at angry milkshakes."""
import random
import sys
from pathlib import Path
import pygame
WIDTH, HEIGHT = 1280, 720
FPS = 60
HERE = Path(__file__).resolve().parent
ART = HERE / "art"
TURN_SPEED = 220 # degrees per second
THRUST = 420 # pixels per second, every second
FRICTION = 0.55 # share of speed left after one second of coasting
MAX_SPEED = 480
FRY_SPEED = 720
FRY_LIFE = 0.9 # seconds before a fry disappears
SHOOT_DELAY = 0.22 # seconds between shots
FLAVORS = ["strawberry", "chocolate", "mint"]
IMG = {}
def load_images():
return {p.stem: pygame.image.load(p).convert_alpha() for p in ART.glob("*.png")}
def wrap(pos, margin=40):
"""Leave one side of the screen, come back on the other."""
pos.x = (pos.x + margin) % (WIDTH + 2 * margin) - margin
pos.y = (pos.y + margin) % (HEIGHT + 2 * margin) - margin
def draw_rotated(surf, image, pos, angle):
rotated = pygame.transform.rotate(image, -angle) # pygame turns counter-clockwise
surf.blit(rotated, rotated.get_rect(center=pos))
class Player:
radius = 34
def __init__(self):
self.reset()
def reset(self):
self.pos = pygame.Vector2(WIDTH / 2, HEIGHT / 2)
self.vel = pygame.Vector2()
self.angle = 0 # degrees, 0 = up, clockwise
self.cooldown = 0
self.invulnerable = 0
def direction(self):
return pygame.Vector2(0, -1).rotate(self.angle)
def update(self, dt, keys):
if keys[pygame.K_LEFT]:
self.angle -= TURN_SPEED * dt
if keys[pygame.K_RIGHT]:
self.angle += TURN_SPEED * dt
if keys[pygame.K_UP]:
self.vel += self.direction() * THRUST * dt
self.vel *= FRICTION**dt
if self.vel.length() > MAX_SPEED:
self.vel.scale_to_length(MAX_SPEED)
self.pos += self.vel * dt
wrap(self.pos)
self.cooldown -= dt
self.invulnerable -= dt
def shoot(self, keys):
if keys[pygame.K_SPACE] and self.cooldown <= 0:
self.cooldown = SHOOT_DELAY
return Fry(self.pos + self.direction() * 44, self.direction(), self.angle)
def draw(self, surf):
if self.invulnerable > 0 and int(self.invulnerable * 10) % 2:
return # blink while safe
draw_rotated(surf, IMG["burger"], self.pos, self.angle)
class Fry:
radius = 8
def __init__(self, pos, direction, angle):
self.pos = pygame.Vector2(pos)
self.vel = direction * FRY_SPEED
self.angle = angle
self.life = FRY_LIFE
def update(self, dt):
self.pos += self.vel * dt
wrap(self.pos)
self.life -= dt
def draw(self, surf):
draw_rotated(surf, IMG["fry"], self.pos, self.angle)
class Shake:
def __init__(self, pos, size, flavor, boost=1.0):
self.pos = pygame.Vector2(pos)
self.size = size # "big" or "small"
self.flavor = flavor
self.boost = boost
self.radius = 50 if size == "big" else 25
speed = random.uniform(60, 130) if size == "big" else random.uniform(110, 200)
self.vel = pygame.Vector2(speed * boost, 0).rotate(random.uniform(0, 360))
self.angle = random.uniform(0, 360)
self.spin = random.uniform(-90, 90)
self.image = IMG[f"shake_{flavor}" if size == "big" else f"shake_{flavor}_small"]
def update(self, dt):
self.pos += self.vel * dt
self.angle += self.spin * dt
wrap(self.pos, 70)
def draw(self, surf):
draw_rotated(surf, self.image, self.pos, self.angle)
class Splat:
"""A short animation where a shake used to be."""
DURATION = 0.4
def __init__(self, pos, flavor):
self.pos = pygame.Vector2(pos)
self.flavor = flavor
self.age = 0
def update(self, dt):
self.age += dt
def draw(self, surf):
frame = min(int(self.age / self.DURATION * 5), 4)
image = IMG[f"splat_{self.flavor}_{frame}"]
surf.blit(image, image.get_rect(center=self.pos))
class Game:
def __init__(self):
pygame.init()
pygame.display.set_caption("Burger Blaster")
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.clock = pygame.time.Clock()
IMG.update(load_images())
self.running = True
self.player = Player()
self.fries = []
self.shakes = []
self.splats = []
self.new_game()
def new_game(self):
self.score = 0
self.lives = 3
self.player.reset()
self.fries.clear()
self.shakes.clear()
self.splats.clear()
def spawn_shakes(self, count):
for _ in range(count):
while True: # keep new shakes away from the burger
pos = pygame.Vector2(random.uniform(0, WIDTH), random.uniform(0, HEIGHT))
if pos.distance_to(self.player.pos) > 300:
break
self.shakes.append(Shake(pos, "big", random.choice(FLAVORS)))
def pop(self, shake):
"""A fry hit a shake: big ones split in two, small ones splat."""
self.shakes.remove(shake)
self.splats.append(Splat(shake.pos, shake.flavor))
if shake.size == "big":
for _ in range(2):
self.shakes.append(Shake(shake.pos, "small", shake.flavor, shake.boost))
def check_fry_hits(self):
for fry in self.fries:
if fry.life <= 0:
continue
for shake in self.shakes:
if fry.pos.distance_to(shake.pos) < shake.radius + fry.radius:
fry.life = 0
self.pop(shake)
break
def check_player_hit(self):
player = self.player
if player.invulnerable > 0:
return
for shake in self.shakes:
if player.pos.distance_to(shake.pos) < player.radius + shake.radius * 0.8:
self.splats.append(Splat(player.pos, "chocolate"))
self.lives -= 1
player.reset()
player.invulnerable = 2.0
if self.lives == 0:
self.new_game()
return
def handle_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.running = False
def update(self, dt):
keys = pygame.key.get_pressed()
self.player.update(dt, keys)
fry = self.player.shoot(keys)
if fry:
self.fries.append(fry)
for fry in self.fries:
fry.update(dt)
self.fries = [f for f in self.fries if f.life > 0]
for shake in self.shakes:
shake.update(dt)
if not self.shakes:
self.spawn_shakes(4)
self.check_fry_hits()
self.check_player_hit()
for splat in self.splats:
splat.update(dt)
self.splats = [s for s in self.splats if s.age < Splat.DURATION]
def draw(self):
surf = self.screen
surf.blit(IMG["background"], (0, 0))
for splat in self.splats:
splat.draw(surf)
for shake in self.shakes:
shake.draw(surf)
for fry in self.fries:
fry.draw(surf)
self.player.draw(surf)
pygame.display.flip()
def run(self):
while self.running:
dt = self.clock.tick(FPS) / 1000
self.handle_events()
self.update(dt)
self.draw()
pygame.quit()
if __name__ == "__main__":
Game().run()
sys.exit(0)
What is new:
new_game() one method that puts everything back to the start: score, lives, the burger, and all the lists emptied
self.fries.clear() list.clear() empties a list in place. It is not the same as self.fries = [], which would make a new list and leave the old one alone. Either works here, but clear() keeps the same list object
self.lives = 3 three lives to start with
self.lives -= 1 in check_player_hit, one life for each hit
if self.lives == 0: self.new_game() out of lives: start again
self.new_game() in __init__ the game starts through the same method, so there is one place that knows what 'the start' means. It replaces the self.spawn_shakes(4) line, because the empty shakes list makes update spawn the first wave anyway
self.score = 0 not used yet. Episode 7 adds the score, and it is reset here already
Run it:
(.venv) $ python game.py
Hit, blink, hit, blink, and on the third hit everything resets: no shakes, no fries, a fresh wave. There is no lives counter on the screen yet; that is the next episode. (Checked, with a shake parked on the respawn point: the lives went 3, 2, 1 and back to 3 after the third hit, one hit at a time, two seconds apart.)
Episode 6 running.
Tuning the feel
Three numbers decide how forgiving the game is:
player.invulnerable = 2.0 the safe time. Shorter is harder
self.lives = 3 more lives, longer games
shake.radius * 0.8 the generosity of the hit test. 1.0 is the picture's size
And where the burger comes back matters: in the middle of the screen it can land on top of a shake, which the safe time fixes. A better respawn point could look for an empty place, the way spawn_shakes looks for a place far from the burger.
Try it yourself
Three small changes before episode 7:
1 Start with five lives, and make the safe time five seconds. Is the game too easy now?
2 Make the burger blink faster by changing the 10 in the blink.
3 Let the burger keep its position after a hit instead of going back to the middle (hint: do not call reset, only set the safe time).
Episode 7 makes the game a game: a score, waves that get harder, a title screen and a saved high score.
Check your work
If something is different:
AttributeError: 'Player' object has no attribute 'invulnerable' reset() has to set self.invulnerable = 0 in step 2
the burger never blinks draw must be the Player.draw method, and the test must use the player's timer
the burger is hit again at once, over and over the 'if player.invulnerable > 0: return' lines or the 2.0 are missing
lives never run out the 'if self.lives == 0' check is missing, or it is outside the loop
NameError: name 'new_game' it is a method: write self.new_game()
Not run in the lab: other Linux distributions, Wayland-only sessions and pygame older than 2.6. The numbers in the checks come from parking a shake on the respawn point and running the game for twelve simulated seconds at 60 frames per second.