-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmazeGame.py
77 lines (61 loc) · 1.95 KB
/
mazeGame.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
import pygame
import sys
import maze as mz
# initialize pygame
pygame.init()
clock = pygame.time.Clock()
# get the maze and initialize it
mazeFile = sys.argv[1]
maze = mz.Maze(mazeFile)
maze.print()
# set up the screen
S_WIDTH = maze.width
S_HEIGHT = maze.height
BLOCK_SIZE = 10 # size of a block in pixels
screen = pygame.display.set_mode(
(BLOCK_SIZE*S_WIDTH, BLOCK_SIZE*S_HEIGHT))
pygame.display.set_caption('Maze Game')
icon = pygame.image.load('mazeGameIcon.ico')
pygame.display.set_icon(icon)
# draw the maze
walls = maze.walls
# blocks = []
for i in range(maze.height):
for j in range(maze.width):
if walls[i][j]:
block = pygame.Rect(
(BLOCK_SIZE*j, BLOCK_SIZE*i, BLOCK_SIZE, BLOCK_SIZE))
pygame.draw.rect(screen, "blue", block)
running = True
# set up the player, start and goal blocks
player = pygame.Rect(
(BLOCK_SIZE*maze.start[1], BLOCK_SIZE*maze.start[0], BLOCK_SIZE, BLOCK_SIZE))
startBlock = pygame.Rect(
(BLOCK_SIZE*maze.start[1], BLOCK_SIZE*maze.start[0], BLOCK_SIZE, BLOCK_SIZE))
goalBlock = pygame.Rect(
(BLOCK_SIZE*maze.goal[1], BLOCK_SIZE*maze.goal[0], BLOCK_SIZE, BLOCK_SIZE))
pygame.display.update()
count = 0
maze.solve() # solve the maze
# main loop
while running:
pygame.draw.rect(screen, "green", player) # draw the player
pygame.draw.rect(screen, "purple", startBlock) # draw the start block
pygame.draw.rect(screen, "red", goalBlock) # draw the goal block
# key = pygame.key.get_pressed()
# event listner for quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# move the player through the solution
if count < len(maze.solution[1]):
action = maze.solution[1][count]
print(action)
player.y = action[0]*BLOCK_SIZE
player.x = action[1]*BLOCK_SIZE
count += 1
# update the screen
pygame.display.update()
# set the fps
clock.tick(20)
pygame.quit()