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
|
#! /usr/bin/env python
"""Setup the maze so it could be drawn in the screen
@license: U{http://creativecommons.org/licenses/by-sa/3.0/us/}
@var FILLED: whether or nott the filled wall images are used
"""
import pygame
from maze import Maze
FILLED = True # Determines the set of maze wall images used, filled or unfilled
class mazeSetup:
"""
Interprets the maze object made in maze.py, draws it all on the screen
"""
def __init__ (self, screen, MAZE_SIZE):
"""
Initialize the maze setup
@param screen: the screen in which we will draw
@param MAZE_SIZE: the size of the maze
"""
#Create the maze
self.maze = Maze(MAZE_SIZE, MAZE_SIZE)
# compute the size of the tiles given the screen size, etc.
size = screen.get_size()
self.tileSizeX = (625 / MAZE_SIZE)
self.tileSizeY = (625 / MAZE_SIZE)
#call the draw function
self.drawMaze(screen, MAZE_SIZE)
def drawPoint(self, screen, x, y):
"""
Draw a certain point of the maze on the screen
@param screen: the screen to draw in
@param x: the x coordinate of the maze map
@param y: the y coordinate of the maze map
"""
rect = pygame.Rect(x*self.tileSizeX,y*self.tileSizeY, self.tileSizeX, self.tileSizeY)
tile = self.maze.map[x][y]
if tile == self.maze.EMPTY:
#0
pygame.draw.rect(screen, (0, 0, 0), rect, 0)
elif tile == self.maze.SOLID:
#1
image = self.getWallImage(x,y)
if image == False:
pygame.draw.rect(screen, (0,0,0) , rect, 0)
else:
screen.blit( image, rect )
elif tile == self.maze.OFFL:
#2
pygame.draw.rect(screen, (0,0,0), rect, 0)
elif tile == self.maze.PELLET:
#3
pygame.draw.rect(screen, (0, 0, 0), rect, 0)
dot = rect.inflate(-self.tileSizeX/1.25, -self.tileSizeY/1.25)
pygame.draw.ellipse(screen, (255, 255, 0), dot, 0)
elif tile == self.maze.PPL:
#4
pygame.draw.rect(screen, (0, 0, 0), rect, 0)
dot = rect.inflate(-self.tileSizeX/2, -self.tileSizeY/2)
pygame.draw.ellipse(screen, (94,246,0), dot, 0)
elif tile == self.maze.PELLET_INT:
#5
pygame.draw.rect(screen, (0, 0, 0), rect, 0)
dot = rect.inflate(-self.tileSizeX/1.25, -self.tileSizeY/1.25)
pygame.draw.ellipse(screen, (255, 255, 0), dot, 0)
elif tile == self.maze.EMPTY_INT:
#6
pygame.draw.rect(screen, (0, 0, 0), rect, 0)
else:
#same as off limits
pygame.draw.rect(screen, (0,0,0), rect, 0)
def drawMaze(self, screen, MAZE_SIZE):
"""
Draw the whole maze
@param screen: the screen to draw into
@param MAZE_SIZE: the maze size
"""
screen.fill((0,0,0)) #black screen
for a in range(0, MAZE_SIZE):
for b in range(0, MAZE_SIZE):
self.drawPoint(screen, a, b)
def getWallImage(self, x, y):
"""
Choses the correct image for wall positions based on the surrounding
plots
@param x: x coordinate
@param y: y coordinate
@return a converted image
"""
file = 'EMPTY'
sides = [ self.maze.getPoint(x,y-1) == self.maze.SOLID, # top
self.maze.getPoint(x+1,y) == self.maze.SOLID, # right
self.maze.getPoint(x,y+1) == self.maze.SOLID, # bottom
self.maze.getPoint(x-1,y) == self.maze.SOLID ] # left
if sides == [0,0,0,0]: # draw box
print "We need a box!"
elif sides == [0,0,0,1]: # draw 180 right
file = './images/25_c_r'
elif sides == [0,0,1,0]: # draw 180 up
file = './images/25_c_u'
elif sides == [0,0,1,1]: # draw 90 top right
file = './images/50_c_rt'
elif sides == [0,1,0,0]: # draw 180 left
file = './images/25_c_l'
elif sides == [0,1,0,1]: # draw parallel left right
file = './images/25_lines_lr'
elif sides == [0,1,1,0]: # draw 90 top left
file = './images/50_c_lt'
elif sides == [0,1,1,1]: # draw line top
file = './images/50_top'
elif sides == [1,0,0,0]: # draw 180 bottom
file = './images/25_c_d'
elif sides == [1,0,0,1]: # draw 90 bottom right
file = './images/50_c_rb'
elif sides == [1,0,1,0]: # draw parallel top bottom
file = './images/25_lines_ud'
elif sides == [1,0,1,1]: # draw line right
file = './images/50_right'
elif sides == [1,1,0,0]: # draw 90 bottom left
file = './images/50_c_lb'
elif sides == [1,1,0,1]: # draw line bottom
file = './images/50_bottom'
elif sides == [1,1,1,0]: # draw line left
file = './images/50_left'
elif sides == [1,1,1,1]: # draw empty
file = 'EMPTY'
else:
print "MAZE FAIL"
if FILLED == True: # uses the filled image blocks instead
if file == 'EMPTY':
file = './images/25_box'
file += '_filled'
if not file == 'EMPTY': # empty is treated as 'use a black box'
return pygame.image.load(file+'.png').convert_alpha()
else:
return False
def redrawMaze(self, screen, mazeSize = 25):
self.drawMaze( screen, mazeSize )
pygame.display.update(0,0,625,625)
|