forked from ryukinix/labyrinth-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabyrinth_explorer.py
164 lines (137 loc) · 4.24 KB
/
labyrinth_explorer.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
import os
import pygame
from labyrinth_generator import generate
from random import randint
from time import time, sleep
import mazeFinder
BLOCKSIZE = 16
WIDTH = 590
HEIGHT = 480
DELAY = 20
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
# Class for the orange dude
class Player(object):
'''
Creates a player based on x and y values
'''
def __init__(self, x, y):
'''
initialises a player rectangle in the pygame
'''
self.x = x
self.y = y
self.rect = pygame.Rect(x, y, BLOCKSIZE, BLOCKSIZE)
@property
def position(self):
'''
returns the position of the rectangle
'''
return self.rect.x, self.rect.y
# Nice class to hold a wall rect
class Wall(object):
'''
Used to create walls that determine the maze layout
'''
def __init__(self, pos):
'''
initialises walls based on pos
'''
walls.append(self)
self.rect = pygame.Rect(pos[0], pos[1], BLOCKSIZE, BLOCKSIZE)
def setup(width,height):
'''
Used to call the labryinth generator functions to make a maze
for the search algorithm to search in
'''
global walls, player, clock, end_rect, screen
# Initialise pygame
os.environ["SDL_VIDEO_CENTERED"] = "1"
pygame.init()
# Set up the display
pygame.display.set_caption("Wait for the program to calculate the route")
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
walls = []
level = generate(width,height)
x = y = 0
for row in level:
for col in row:
if col == "W":
Wall((x, y))
elif col == "E":
end_rect = pygame.Rect(x, y, BLOCKSIZE, BLOCKSIZE)
elif col == "P":
player = Player(x, y)
x += BLOCKSIZE
y += BLOCKSIZE
x = 0
return screen, clock, walls, player, end_rect
def playOut(actions):
'''
Moves the player based on the action
'''
for action in actions:
if(action == "Move Up"):
player.rect.y -= BLOCKSIZE
elif(action == "Move Down"):
player.rect.y += BLOCKSIZE
elif(action == "Move Right"):
player.rect.x += BLOCKSIZE
elif(action == "Move Left"):
player.rect.x -= BLOCKSIZE
updateScreen(player, end_rect, walls)
sleep(0.1)
def updateScreen(player, end_rect, walls):
'''
Updates the screeen
'''
screen.fill(BLACK)
for wall in walls:
pygame.draw.rect(screen, WHITE, wall.rect)
pygame.draw.rect(screen, RED, end_rect)
pygame.draw.rect(screen, GREEN, player.rect)
pygame.display.flip()
def recordResutls(time, width, height, method,filterActionsValue):
'''
Used to record results in the results text file
'''
string = "Solver took "+str(time)+", for " +str(width)+" by "+str(height)+" maze using method ID "+str(method)+" with filter actions set to "+str(filterActionsValue) +"\n\r"
f= open("results.txt","a")
f.write(string)
f.close()
def game(method, filterActionsValue, width,height):
'''
Used to initialise a maze and find the path
Also records the time required to find the path
'''
setup(width,height)
running = True
updateScreen(player, end_rect, walls)
t0 = time()
path = mazeFinder.findSolution(method,filterActionsValue,player, end_rect, walls)
t1 = time()
totalTime = t1-t0
print ("Solver took ",totalTime, ' seconds')
recordResutls(totalTime, width, height, method, filterActionsValue)
pygame.display.set_caption("Press space to start the route")
while running:
clock.tick(5)
for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
pygame.quit()
if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
running = False
pygame.quit()
key = pygame.key.get_pressed()
if key[pygame.K_SPACE]:
pygame.display.set_caption("Following route")
playOut(path)
running = False
pygame.quit()
updateScreen(player, end_rect, walls)
if __name__ == '__main__':
game()