-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeRunner.py
More file actions
100 lines (83 loc) · 2.28 KB
/
MazeRunner.py
File metadata and controls
100 lines (83 loc) · 2.28 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import turtle
# Set up the screen
win = turtle.Screen()
win.title("Turtle Maze Runner")
win.bgcolor("lightblue")
win.setup(width=600, height=600)
# Create the turtle
player = turtle.Turtle()
player.shape("turtle")
player.color("green")
player.penup()
player.goto(-250, 250)
# Create the goal
goal = turtle.Turtle()
goal.shape("circle")
goal.color("red")
goal.penup()
goal.goto(250, -250)
# Define the maze walls
walls = [
(-300, 200, 600, 20), # top wall
(-300, -200, 600, 20), # bottom wall
(-300, 200, 20, 400), # left wall
(280, 200, 20, 400), # right wall
(-150, 100, 300, 20), # horizontal wall
(100, -100, 300, 20) # another horizontal wall
]
def create_wall(x, y, width, height):
wall = turtle.Turtle()
wall.shape("square")
wall.color("black")
wall.shapesize(stretch_wid=height/20, stretch_len=width/20)
wall.penup()
wall.goto(x, y)
return wall
for wall in walls:
create_wall(*wall)
# Functions to control the turtle
def move_up():
player.setheading(90)
player.forward(20)
def move_down():
player.setheading(270)
player.forward(20)
def move_left():
player.setheading(180)
player.forward(20)
def move_right():
player.setheading(0)
player.forward(20)
# Check if the player has reached the goal
def check_goal():
if player.distance(goal) < 20:
print("You win!")
player.goto(-250, 250) # Reset the player position
win.ontimer(check_goal, 100)
# Start the game
def start_game():
# Hide the player and goal initially
player.hideturtle()
goal.hideturtle()
# Display a start message
start_message = turtle.Turtle()
start_message.color("blue")
start_message.penup()
start_message.hideturtle()
start_message.goto(0, 0)
start_message.write("Press 'Space' to Start the Game", align="center", font=("Arial", 16, "bold"))
def begin():
start_message.clear()
player.showturtle()
goal.showturtle()
check_goal()
win.onkey(begin, "space")
# Keyboard bindings
win.listen()
win.onkey(move_up, "w")
win.onkey(move_down, "s")
win.onkey(move_left, "a")
win.onkey(move_right, "d")
# Start the game loop
start_game()
win.mainloop()