Creating a “Maze Runner” game in Python using the Turtle graphics library is an exciting way to apply programming concepts while building a fun game. This example outlines a basic version of a maze runner game, where the player navigates a turtle through a maze to reach an endpoint.
Game Overview
The game consists of:
- A maze drawn on the screen using the Turtle graphics library.
- A player-controlled turtle that navigates through the maze.
- An endpoint the player aims to reach.
Preparing the Maze
First, design your maze as a simple grid, where walls are represented by ‘W’ and paths by spaces. You can expand this basic concept with more features as you become comfortable.
1 2 3 4 5 6 7 8 9 10 |
# Define the maze maze = [ "WWWWWWWWWW", "W W W", "W WWW W W", "W W W W W", "W W W W W", "W W W", "WWWWW WWWW", ] |
Python Code for Maze Runner Game
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import turtle # Setup screen wn = turtle.Screen() wn.bgcolor("black") wn.title("Maze Runner Revised") # Create player player = turtle.Turtle() player.shape("square") player.color("white") player.penup() player.speed(0) player.setposition(-144, 144) # Adjusted starting position to align with grid # Define the maze maze = [ "WWWWWWWWWW", "W W W", "W WWW W W", "W W W W W", "W W W W W", "W W W", "WWWWW WWWW", ] # Translate maze to screen and store wall positions walls = [] def draw_maze(maze): for y in range(len(maze)): for x in range(len(maze[0])): character = maze[y][x] screen_x = -168 + (x * 24) # Adjust starting x position screen_y = 168 - (y * 24) # Adjust starting y position if character == "W": draw_wall(screen_x, screen_y) walls.append((screen_x, screen_y)) def draw_wall(x, y): wall = turtle.Turtle() wall.shape("square") wall.color("blue") wall.penup() wall.speed(0) wall.goto(x, y) draw_maze(maze) # Player movement with wall collision detection def move(direction): x, y = player.pos() if direction == "left" and (x - 24, y) not in walls: player.setx(x - 24) if direction == "right" and (x + 24, y) not in walls: player.setx(x + 24) if direction == "up" and (x, y + 24) not in walls: player.sety(y + 24) if direction == "down" and (x, y - 24) not in walls: player.sety(y - 24) def move_left(): move("left") def move_right(): move("right") def move_up(): move("up") def move_down(): move("down") wn.listen() wn.onkey(move_left, "Left") wn.onkey(move_right, "Right") wn.onkey(move_up, "Up") wn.onkey(move_down, "Down") turtle.done() |
Maze Definition and Drawing
- Define the Maze Layout: The maze is represented by a list of strings (
maze
), where each “W” character represents a wall in the maze. - Draw the Maze: The
draw_maze
function iterates over each row (y
) and column (x
) of themaze
layout. For each wall character “W”, it calculates the corresponding screen coordinates (screen_x
,screen_y
) and callsdraw_wall
to draw a blue square at that location. Each wall’s screen coordinates are added to thewalls
list for collision detection. - Wall Drawing: The
draw_wall
function uses aTurtle
object to draw each wall at the calculated screen coordinates.
Player Movement and Wall Collision Detection
- Movement Functions: The
move
function adjusts the player’s position based on the direction (“left”, “right”, “up”, “down”). Before moving, it checks if the next position would collide with a wall by ensuring the intended new coordinates are not in thewalls
list. - Directional Movement: Specific functions (
move_left
,move_right
,move_up
,move_down
) call themove
function with the appropriate direction. These are bound to keyboard inputs (left, right, up, down arrow keys) usingwn.onkey()
, allowing the player to control the movement via the keyboard. - Event Listening:
wn.listen()
tells the program to listen for keyboard inputs, which are linked to the player’s movement functions.
Execution
- The game is executed with
turtle.done()
, which keeps the window open and responsive to the player’s keyboard inputs. - As the player moves, if they attempt to move into a wall, the movement function checks against the
walls
list. If the next position is part of thewalls
list, the move is not made, effectively preventing the player from moving through walls.
This program demonstrates fundamental concepts of game development with Python, including using lists to represent game maps, handling user input for movement, and implementing collision detection to restrict movement within defined boundaries.