Make a Frogger-style arcade game

these child objects. The two subclasses override a number of methods – one key example being **check_collision**. The **Bunner.update** method calls this to find out if the player has collided with something, and if so, what to do about it. In the case of **Road**, colliding with a car kills the player and replaces the player sprite with the ‘splat’ image (see the sprites below left). For **Water**, however, colliding with a log is a good thing, whereas not colliding with a log ends the game and replaces the player sprite with the ‘splash’ animation. The **Game** class maintains a reference to the player object, and is also responsible for creating, updating, and deleting rows. The final part of the code uses a simple state machine system to update and draw the game objects.

returns zero, resulting in no movement. In the **Water** class, however, this method is overridden, and causes the player to move on the X axis if they’re currently sitting on a log. The **ActiveRow** class inherits from **Row**, and is the base class for **Water** and **Road**. What those two classes have in common is that they both feature moving objects (logs and cars) which start just off-screen and move horizontally. **ActiveRow** deals with creating and destroying

HEDGE MASKS

**Grass** rows sometimes contain **Hedges**. These block certain parts of the row, requiring the player to make a detour around them. The function **generate_hedge_mask** decides the layout of hedge segments in a row. A mask is a series of Boolean values which allow or prevent parts of an underlying image from showing through. This function creates a mask representing the presence or absence of hedges in a **Grass** row. **False** means a hedge is present; **True** represents a gap. Initially we create a list of twelve elements. For each element there is a small chance of a gap, but often all elements will be **False**, representing a hedge with no gaps. We then randomly set one element to **True**, to ensure that there is always at least one gap that the player can get through. Single-tile gaps aren’t wide enough for the player to fit through, so the next step is to widen any gaps to a minimum of three tiles. Once the mask has been generated, the function **classify_hedge_segment** is used to determine which sprite to use for each hedge segment.

Infinite Bunner

The code here is just a small sample – the full program and assets can be downloaded directly from our GitHub: **wfmag.cc/CTC1-bunner**. Open the bunner.py file in a Python editor, such as IDLE, and select Run > Run Module.

**from pygame import mixer** **from random import*** **from enum import Enum** **import pygame, pgzrun, pzgero, sys** **if sys.version_info < (3,6):** **print**("This game requires at least version 3.6 of Python. Please download it \ from www.python.org") **sys.exit()** **if pgzero.__version__ != “1.2”:** **print**(f"This game requires at least version 1.2 of Pygame Zero. You are using \ version {pgzero.version}. If this is older than 1.2, please upgrade using the \ command 'pip install --upgrade pgzero'") **WIDTH** = 480 **HEIGHT** = 800 **TITLE** = “Infinite Bunner” **ROW_HEIGHT** = 40 **DEBUG_SHOW_ROW_BOUNDARIES** = **False** **class MyActor(Actor):** **def __init__(self, image, pos, anchor=(“center”, “bottom”)):** **super().__init__(image, pos, anchor)** **self.children = []** **def draw(self, offset_x, offset_y):** **self.x += offset_x** **self.y += offset_y** **super().draw()** **for child_obj in self.children:** **child_obj.draw(self.x, self.y)** **self.x -= offset_x** **self.y -= offset_y** **def update(self):** **for child_obj in self.children:** **child_obj.update()** **class Eagle(MyActor):** **def __init__(self, pos):** **super().__init__(“eagles”, pos)** **self.children.append(MyActor(“eagle”, (0, -32)))** **def update(self):** **self.y += 12**

**class PlayerState(Enum):** **ALIVE** = 0, **SPLAT** = 1, **SPLASH** = 2, **EAGLE** = 3 **DIRECTION_UP** = 0 **DIRECTION_RIGHT** = 1 **DIRECTION_DOWN** = 2 **DIRECTION_LEFT** = 3 **direction_keys** = [**keys.UP, keys.RIGHT, keys.DOWN, keys.LEFT**] **DX** = [0,4,0,-4] **DY** = [-4,0,4,0] **class Bunner(MyActor):** **MOVE_DISTANCE** = 10 **def __init__(self, pos):** **super().__init__(“blank”, pos)** **self.state** = PlayerState.ALIVE **self.direction** = 2 **self.timer** = 0 **self.input_queue** = [] **self.min_y** = self.y **def handle_input(self, dir):** **for row in game.rows:** **if row.y == self.y + Bunner.MOVE_DISTANCE * DY[dir]:** **new_row = row** **break** **if new_row.allow_movement(self.x + Bunner.MOVE_DISTANCE * DX[dir]):** **self.direction = dir** **self.timer = Bunner.MOVE_DISTANCE** **game.play_sound(“jump”, 1)** **def update(self):** **for direction in range(4):** **if key_just_pressed(direction_keys[direction]):** **self.input_queue.append(direction)** **if self.state == PlayerState.ALIVE:** **if self.timer == 0 and len(self.input_queue) > 0:** **self.handle_input(self.input_queue.pop(0))** **land = False**

**if self.timer > 0:** **self.x += DX[self.direction]** **self.y += DY[self.direction]** **self.timer -= 1** **land = self.timer == 0** **current_row = None** **for row in game.rows:** **if row.y == self.y:** **current_row = row** **break** **if current_row:** **self.state, dead_obj_y_offset = current_row.check_collision(self.x)** **if self.state == PlayerState.ALIVE:** **self.x += current_row.push()** **if self.y > game.scroll_pos + HEIGHT + 80:** **game.eagle = Eagle((self.x, game.scroll_pos))** **self.state = PlayerState.EAGLE** **self.timer = 150** **game.play_sound(“eagle”)** **if land:** **current_row.play_sound()** **else:** **if self.state == PlayerState.SPLAT:** **current_row.children.insert(0, MyActor(“splat” + \** **str(self.direction), (self.x, dead_obj_y_offset)))** **self.timer = 100** **self.x = max(16, min(WIDTH - 16, self.x))** **else:** **self.timer -= 1** **self.min_y = min(self.min_y, self.y)** **self.image = “blank”** **if self.state == PlayerState.ALIVE:** **if self.timer > 0:** **self.image = “jump” + str(self.direction)** **else:** **self.image = “sit” + str(self.direction)** **elif self.state == PlayerState.SPLASH and self.timer > 84:** **self.image = “splash” + str(int((100 - self.timer) / 2))** **class Mover(MyActor):** **def __init__(self, dx, image, pos):** **super().__init__(image, pos)** **self.dx = dx**

**def update(self):** **self.x += self.dx** **class Car(Mover):** **SOUND_ZOOM = 0** **SOUND_HONK = 1** **def __init__(self, dx, pos):** **image = “car” + str(randint(0, 3)) + (“0” if dx < 0 else “1”)** **super().__init__(dx, image, pos)** **self.played = [False, False]** **self.sounds = [(“zoom”, 6), (“honk”, 4)]** **def play_sound(self, num):** **if not self.played[num]:** **game.play_sound(*self.sounds[num])** **self.played[num] = True** **class Log(Mover):** **def __init__(self, dx, pos):** **image = “log” + str(randint(0, 1))** **super().__init__(dx, image, pos)** **class Train(Mover):** **def __init__(self, dx, pos):** **image = “train” +str(randint(0, 2)) + (“0” if dx < 0 else “1”)** **super().__init__(dx, image, pos)** **class Row(MyActor):** **def __init__(self, base_image, index, y):** **super().__init__(base_image + str(index), (0, y), (“left”, “bottom”))** **self.index = index** **self.dx = 0** **def next(self):** **return** **def collide(self, x, margin=0):** **for child_obj in self.children:** **if x >= child_obj.x - (child_obj.width / 2) - margin \** **and x < child_obj.x + (child_obj.width / 2) + margin:** **return child_obj** **return None** **def push(self):** **return 0** ...