-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
253 lines (212 loc) · 7.89 KB
/
test.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import pygame
import random
import sys
maze_size = 35 # taille du labyrinth
cote = 25 # coté d'une cellule
# Créer les matrices
case = [[0 for lig in range(maze_size)] for col in range(maze_size)]
pixel = [[ 0 for lig in range(maze_size)] for col in range(maze_size)]
# Données initiales
def main():
for y in range(maze_size):
for x in range(maze_size):
case[x][y] = pygame.Rect(x * cote, y * cote, cote, cote)
# placer les pixels et créer le labyrinthe
init_pixel()
create_maze()
complexe()
distance, _ = find_shortest_path((0, 1), (maze_size - 1, maze_size - 2))
path = retrace_path((0, 1), (maze_size - 1, maze_size - 2), distance)
draw_path(path)
print("FINI")
def init_pixel(): # placer les pixels
for x in range(maze_size):
pixel[x][0] = "WALL"
pixel[x][maze_size-1] = "WALL"
pixel[0][x] = "WALL"
pixel[maze_size-1][x] = "WALL"
for i in range(1,maze_size-1):
for j in range(1,maze_size-1):
pixel[i][j] = False
def create_maze(): # Exploration exhaustive / Depth-first search
x = 0
y = 1
x_position=[]
y_position=[]
while x != maze_size-1 or y != maze_size-2:
if len(check(x,y)) >= 1:
Dir = random.choice(check(x,y))
else:
Dir = []
if Dir == "EAST":
pixel[x+2][y] = True
pixel[x+1][y] = True
x_position.append(x)
y_position.append(y)
x += 2
elif Dir == "WEST":
pixel[x-2][y] = True
pixel[x-1][y] = True
x_position.append(x)
y_position.append(y)
x -= 2
elif Dir == "SOUTH":
pixel[x][y+2] = True
pixel[x][y+1] = True
x_position.append(x)
y_position.append(y)
y += 2
elif Dir == "NORTH":
pixel[x][y-2] = True
pixel[x][y-1] = True
x_position.append(x)
y_position.append(y)
y -= 2
if Dir == [] :
x_position.reverse()
y_position.reverse()
if len(x_position) != 0 :
x = x_position[0]
y = y_position[0]
x_position.pop(0)
y_position.pop(0)
x_position.reverse()
y_position.reverse()
else:
x = maze_size-1
y = maze_size-2
pixel[maze_size-2][maze_size-2] = True
draw()
pixel[0][1] = True #départ
pixel[maze_size-1][maze_size-2] = True
def check(x,y): # regarder les cases autour
Direction_possible = []
if x+2 <= maze_size-1:
if pixel[x+2][y] == False:
Direction_possible.append("EAST")
if x-2 >= 0:
if pixel[x-2][y] == False:
Direction_possible.append("WEST")
if y+2 <= maze_size-1:
if pixel[x][y+2] == False:
Direction_possible.append("SOUTH")
if y-2 >= 0:
if pixel[x][y-2] == False:
Direction_possible.append("NORTH")
return Direction_possible
def complexe(): # rends le labyrinthe complexe en cassant des murs au hasard
for i in range(maze_size):
x = random.randint(1, maze_size-1)
y = random.randint(1, maze_size-1)
if pixel[x][y] == False:
if pixel[x][y+1] == False and pixel[x][y-1] == False and pixel[x-1][y] == True and pixel[x+1][y] == True:
pixel[x][y] = True
draw()
if pixel[x-1][y] == False and pixel[x-1][y] == False and pixel[x][y-1] == True and pixel[x][y+1] == True:
pixel[x][y] = True
draw()
def check_chemin(x,y): # regarder les cases autour pour chemin
Direction_possible = []
if x+1 <= maze_size-1:
if pixel[x+1][y] == False:
Direction_possible.append("E")
if x-1 >= 0:
if pixel[x-1][y] == False:
Direction_possible.append("W")
if y+1 <= maze_size-1:
if pixel[x][y+1] == False:
Direction_possible.append("S")
if y-1 >= 0:
if pixel[x][y-1] == False:
Direction_possible.append("N")
return Direction_possible
def find_shortest_path(start, end): # TROUVER LE CHEMIN LE PLUS COURT
distance = [[float('inf')] * maze_size for _ in range(maze_size)]
distance[end[0]][end[1]] = 0
queue = [end]
visited = set()
max_distance = 0
max_possible_distance = (maze_size - 1) * 2 # la distance maximale possible dans le labyrinthe
while queue:
x, y = queue.pop(0)
if (x, y) == start:
break
if (x, y) not in visited:
visited.add((x, y))
for dx, dy in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
nx, ny = x + dx, y + dy
if 0 <= nx < maze_size and 0 <= ny < maze_size and pixel[nx][ny] != "WALL" and pixel[nx][ny] != False:
if distance[nx][ny] > distance[x][y] + 1:
distance[nx][ny] = distance[x][y] + 1
max_distance = max(max_distance, distance[nx][ny])
queue.append((nx, ny))
# Affiche la couleur pendant la recherche du chemin
color = lerp_color((255, 255, 0), (0, 0, 139), distance[nx][ny] / max_possible_distance)
screen.fill(color, (nx * cote, ny * cote, cote, cote))
pygame.display.update()
pygame.time.delay(5) # ajustez cette valeur pour changer la vitesse de l'animation
return distance, max_distance
def draw_path(path): # affiche le chemin le plus court
for x, y in path:
screen.fill((0, 255, 0), (x * cote, y * cote, cote, cote))
pygame.display.update()
def draw_progress(x, y):
screen.fill((0, 0, 255), (x * cote, y * cote, cote, cote))
pygame.display.update()
pygame.time.delay(5) # ajustez cette valeur pour changer la vitesse de l'animation
def retrace_path(start, end, distance):
x, y = start
path = []
while (x, y) != end:
for dx, dy in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
nx, ny = x + dx, y + dy
if 0 <= nx < maze_size and 0 <= ny < maze_size and distance[nx][ny] == distance[x][y] - 1:
path.append((nx, ny))
x, y = nx, ny
break
return path
def clamp_color_value(value):
return max(0, min(255, int(value)))
def lerp_color(color1, color2, t):
r = clamp_color_value(color1[0] * (1 - t) + color2[0] * t)
g = clamp_color_value(color1[1] * (1 - t) + color2[1] * t)
b = clamp_color_value(color1[2] * (1 - t) + color2[2] * t)
return (r, g, b)
def draw(): #affiche le labyrinthe
for y in range(maze_size):
for x in range(maze_size):
if pixel[x][y] == True:
coul = (255, 255, 255)
elif pixel[x][y] == False:
coul = (0, 0, 0)
elif pixel[x][y] == "WALL":
coul = (0, 0, 0)
screen.fill(coul, (x * cote, y * cote, cote, cote))
screen.fill((0, 255, 0), (0 * cote, 1 * cote, cote, cote)) #départ
screen.fill((255, 0, 0), ((maze_size - 1) * cote, (maze_size - 2) * cote, cote, cote)) #arrivé
pygame.display.update()
def replay():
init_pixel()
create_maze()
complexe()
print("FINI")
def quit_game():
pygame.quit()
sys.exit()
# Lancement du programme
pygame.init()
screen = pygame.display.set_mode((cote * maze_size, cote * maze_size))
pygame.display.set_caption("Labyrinth")
pygame.display.set_mode((cote * maze_size, cote * maze_size))
if __name__ == '__main__':
main()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
replay()
elif event.key == pygame.K_q:
quit_game()
elif event.type == pygame.QUIT:
quit_game()