11
11
RED = (255 , 0 , 0 )
12
12
GREEN = (0 , 255 , 0 )
13
13
14
+
14
15
class Cell :
15
16
WALL = 0
16
17
PATH = 1
17
18
VISITED = 2
18
19
20
+
19
21
maze = [[Cell .WALL for _ in range (MAZE_WIDTH )] for _ in range (MAZE_HEIGHT )]
20
22
23
+
21
24
def generate_maze (x , y ):
22
25
maze [y ][x ] = Cell .VISITED
23
26
directions = [(0 , 1 ), (1 , 0 ), (0 , - 1 ), (- 1 , 0 )]
@@ -29,6 +32,7 @@ def generate_maze(x, y):
29
32
maze [y + dy ][x + dx ] = Cell .PATH
30
33
generate_maze (nx , ny )
31
34
35
+
32
36
class Player :
33
37
def __init__ (self , x , y ):
34
38
self .x = x
@@ -40,6 +44,7 @@ def move(self, dx, dy):
40
44
self .x = new_x
41
45
self .y = new_y
42
46
47
+
43
48
player = Player (1 , 1 )
44
49
45
50
pygame .init ()
@@ -59,7 +64,8 @@ def move(self, dx, dy):
59
64
running = False
60
65
elif event .type == pygame .KEYDOWN :
61
66
if event .key == pygame .K_r :
62
- maze = [[Cell .WALL for _ in range (MAZE_WIDTH )] for _ in range (MAZE_HEIGHT )]
67
+ maze = [[Cell .WALL for _ in range (
68
+ MAZE_WIDTH )] for _ in range (MAZE_HEIGHT )]
63
69
generate_maze (0 , 0 )
64
70
start_point = (1 , 1 )
65
71
end_point = (MAZE_WIDTH - 2 , MAZE_HEIGHT - 2 )
@@ -79,22 +85,29 @@ def move(self, dx, dy):
79
85
for y in range (MAZE_HEIGHT ):
80
86
for x in range (MAZE_WIDTH ):
81
87
if maze [y ][x ] == Cell .WALL :
82
- pygame .draw .rect (window , BLACK , (x * CELL_SIZE , y * CELL_SIZE , CELL_SIZE , CELL_SIZE ))
88
+ pygame .draw .rect (window , BLACK , (x * CELL_SIZE ,
89
+ y * CELL_SIZE , CELL_SIZE , CELL_SIZE ))
83
90
elif maze [y ][x ] == Cell .PATH :
84
- pygame .draw .rect (window , WHITE , (x * CELL_SIZE , y * CELL_SIZE , CELL_SIZE , CELL_SIZE ))
91
+ pygame .draw .rect (window , WHITE , (x * CELL_SIZE ,
92
+ y * CELL_SIZE , CELL_SIZE , CELL_SIZE ))
85
93
elif maze [y ][x ] == Cell .VISITED :
86
- pygame .draw .rect (window , GREEN , (x * CELL_SIZE , y * CELL_SIZE , CELL_SIZE , CELL_SIZE ))
94
+ pygame .draw .rect (window , GREEN , (x * CELL_SIZE ,
95
+ y * CELL_SIZE , CELL_SIZE , CELL_SIZE ))
87
96
88
97
for x , y in path :
89
- pygame .draw .rect (window , RED , (x * CELL_SIZE , y * CELL_SIZE , CELL_SIZE , CELL_SIZE ))
98
+ pygame .draw .rect (window , RED , (x * CELL_SIZE , y *
99
+ CELL_SIZE , CELL_SIZE , CELL_SIZE ))
90
100
91
- pygame .draw .rect (window , GREEN , (end_point [0 ] * CELL_SIZE , end_point [1 ] * CELL_SIZE , CELL_SIZE , CELL_SIZE ))
101
+ pygame .draw .rect (
102
+ window , GREEN , (end_point [0 ] * CELL_SIZE , end_point [1 ] * CELL_SIZE , CELL_SIZE , CELL_SIZE ))
92
103
93
104
if (player .x , player .y ) == end_point :
94
105
elapsed_time = time .time () - start_time
95
- pygame .draw .rect (window , GREEN , (end_point [0 ] * CELL_SIZE , end_point [1 ] * CELL_SIZE , CELL_SIZE , CELL_SIZE ))
106
+ pygame .draw .rect (
107
+ window , GREEN , (end_point [0 ] * CELL_SIZE , end_point [1 ] * CELL_SIZE , CELL_SIZE , CELL_SIZE ))
96
108
font = pygame .font .SysFont (None , 40 )
97
- text = font .render (f"Congratulations! Time: { elapsed_time :.2f} seconds" , True , BLACK )
109
+ text = font .render (
110
+ f"Congratulations! Time: { elapsed_time :.2f} seconds" , True , BLACK )
98
111
window .blit (text , (10 , WINDOW_HEIGHT - 40 ))
99
112
else :
100
113
path = []
@@ -118,4 +131,4 @@ def move(self, dx, dy):
118
131
119
132
pygame .display .update ()
120
133
121
- pygame .quit ()
134
+ pygame .quit ()
0 commit comments