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
|
#!/usr/bin/env python
"""
Maze generates the maze in a map so it could be accessed.
@license: U{http://creativecommons.org/licenses/by-sa/3.0/us/}
@var grid: The map to store the maze
"""
from pygame import Rect
#GAME VARIABLES
grid = []
class Maze:
"""
@cvar EMPTY: An empty space in the maze where a regular pellet would be
@cvar SOLID: A wall representation in the maze
@cvar OFFL: A space off limits to pacmath character
@cvar PELLET: Where you could move and collect a regular pellet
@cvar PPL: The power pellet that would give a question
@cvar PELLET_INT: The intersection of a pellet
@cvar EMPTY_INT: An intersection of a pellet
"""
EMPTY = 0 #You can move around
SOLID = 1 #Wall
OFFL = 2 #Off limits to pacman
PELLET = 3 #You can move and collect a Regular Pellet
PPL = 4 #The Power Pellet or Question Pellet
PELLET_INT = 5 #Intersection with Regular Pellet
EMPTY_INT = 6 #Intersection after Regular Pellet has been eaten
#Constructor for object
def __init__(self, width, height):
"""
Build a maze
@param width: the width of the maze
@param height: the height of the maze
"""
self.width, self.height = width, height
self.generate()
#Generate the Maze
def generate (self):
"""
Generate the maze with walls and empty spaces
"""
self.map = []
for x in range(0, self.width-1):
if (x == 0):
# draw left border
self.map.append([self.SOLID] * self.height)
else:
# draw right border
self.map.append([self.EMPTY] * self.height)
self.map.append([self.SOLID] * self.height)
for x in range(1, self.width-1):
self.map[x][0] = self.SOLID # draw top border
self.map[x][self.height - 1] = self.SOLID # draw bottom border
f = open("maze.txt")
for y in range (0, 25):
line = f.readline()
for x in range (0, 25):
if (line[x] == "0"):
self.map[x][y] = self.EMPTY
elif (line[x] == "1"):
self.map[x][y] = self.SOLID
elif (line[x] == "2"):
self.map[x][y] = self.OFFL
elif (line[x] == "3"):
self.map[x][y] = self.PELLET
elif (line[x] == "4"):
self.map[x][y] = self.PPL
elif (line[x] == "5"):
self.map[x][y] = self.PELLET_INT
elif (line[x] == "6"):
self.map[x][y] = self.EMPTY_INT
else:
self.map[x][y] = self.OFFL
f.close()
#set the map to the global variable gird
global grid
grid = self.map
#access the grid (or map)
def getGrid(self):
"""Returns the grid instance
@rtype: 2d list
@return: the grid
"""
#simply returns the grid (or map)
return grid
def getPoint(self, x, y):
"""
Used by mazeSetup.py to get the type of block at a given set of
coordinates, returns solid if it is off the grid
@param x: the X parameter, may be off the grid
@param y: the Y parameter, may be off the grid
@rtype: number
@return: the integer representing block type, use the class constants to determine what that means.
"""
if x < 0 or y < 0 or x >= self.width or y >= self.height:
return self.SOLID
else:
return self.map[x][y]
|