Skip to content

Commit bb00b7e

Browse files
committed
added betrayal.py
A test implementation for a table top game I'm working on developing with JT Booth and John Phillpot.
1 parent 57b1955 commit bb00b7e

File tree

1 file changed

+288
-0
lines changed

1 file changed

+288
-0
lines changed

betrayal.py

+288
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
import graphics
2+
import time
3+
4+
VoffSet = 80
5+
HoffSet = 40
6+
cellSize = 40
7+
8+
class Maze:
9+
10+
def __init__(self, rows, columns):
11+
self.rows = rows
12+
self.columns = columns
13+
self.cells = [] #2D array of cell objects
14+
self.Vwalls = [] #2D array of all possible vertical walls
15+
self.Hwalls = [] #2D array of all possible horizontal walls
16+
self.win = graphics.GraphWin("Maze",2*HoffSet+cellSize*columns,2*VoffSet+cellSize*rows)
17+
self.win.setBackground("black")
18+
self.initialize()
19+
20+
def __repr__(self):
21+
return str(self.cells)
22+
23+
def initialize(self):
24+
for r in range(self.rows):
25+
self.cells += [[]]
26+
for c in range(self.columns):
27+
self.cells[r] += [Cell(r,c)]
28+
self.cells[r][c].cellGraphic.draw(self.win)
29+
for r in range(self.rows):
30+
self.Vwalls += [[]]
31+
for c in range(self.columns+1):
32+
if c == 0:
33+
self.Vwalls[r] += [Wall(r,c,'W',True,'v')]
34+
elif c == self.columns:
35+
self.Vwalls[r] += [Wall(r,c,'E',True,'v')]
36+
else:
37+
self.Vwalls[r] += [Wall(r,c,False,True,'v')]
38+
self.Vwalls[r][c].wallGraphic.draw(self.win)
39+
for r in range(self.rows+1):
40+
self.Hwalls += [[]]
41+
for c in range(self.columns):
42+
if r == 0:
43+
self.Hwalls[r] += [Wall(r,c,'N',True,'h')]
44+
elif r == self.rows:
45+
self.Hwalls[r] += [Wall(r,c,'S',True,'h')]
46+
else:
47+
self.Hwalls[r] += [Wall(r,c,False,True,'h')]
48+
self.Hwalls[r][c].wallGraphic.draw(self.win)
49+
50+
def insertObj(self, row, column, obj):
51+
"""draws the given object in the maze any objects must
52+
have a createFigure() member function that provides a
53+
drawable .figure data member and take .maze data member
54+
that stores the maze in which the object exists"""
55+
obj.row = row
56+
obj.column = column
57+
obj.maze = self #this sets the object to know what maze it is in
58+
anchorPt = self.cells[row][column].cellGraphic.getCenter()
59+
obj.createFigure(anchorPt)
60+
obj.figure.draw(self.win)
61+
62+
def getWall(self, cell, direction):
63+
"""returns the wall in the given direction from
64+
the given cell"""
65+
return self.getWall(cell.row, cell.column, direction)
66+
67+
def getWall(self, row, column, direction):
68+
"""returns the wall in the given direction from
69+
the given cell"""
70+
if direction == 'N':
71+
return self.Hwalls[row][column]
72+
elif direction == 'W':
73+
return self.Vwalls[row][column]
74+
elif direction == 'E':
75+
return self.Vwalls[row][column+1]
76+
elif direction == 'S':
77+
return self.Hwalls[row+1][column]
78+
79+
def setWall(self, row, column, orientation, solid):
80+
if orientation == 'v':
81+
self.Vwalls[row][column].setWall(solid)
82+
else:
83+
self.Hwalls[row][column].setWall(solid)
84+
85+
def close(self):
86+
self.win.close()
87+
88+
class Cell:
89+
90+
def __init__(self, row, column):
91+
self.row = row
92+
self.column = column
93+
self.obj = None
94+
p1 = graphics.Point(HoffSet+cellSize*column,VoffSet+cellSize*row)
95+
p2 = graphics.Point(HoffSet+cellSize*(column+1),VoffSet+cellSize*(row+1))
96+
self.cellGraphic = graphics.Rectangle(p1, p2)
97+
self.cellGraphic.setOutline("black")
98+
99+
def __repr__(self):
100+
return str((self.row, self.column))
101+
102+
def setColor(self, color):
103+
self.cellGraphic.setFill(color)
104+
105+
106+
class Wall:
107+
108+
def __init__(self, row, column, edge, solid, orientation):
109+
self.row = row
110+
self.column = column
111+
self.solid = solid
112+
self.edge = edge
113+
self.orientation = orientation
114+
if orientation == 'v':
115+
p1 = graphics.Point(HoffSet+column*cellSize,VoffSet+row*cellSize)
116+
p2 = graphics.Point(HoffSet+column*cellSize,VoffSet+(row+1)*cellSize)
117+
else:
118+
p1 = graphics.Point(HoffSet+column*cellSize,VoffSet+row*cellSize)
119+
p2 = graphics.Point(HoffSet+(column+1)*cellSize,VoffSet+row*cellSize)
120+
self.wallGraphic = graphics.Line(p1, p2)
121+
self.refreshWall()
122+
123+
124+
def __repr__(self):
125+
if self.edge != False:
126+
return str(self.edge)+" edge of the maze"
127+
if self.orientation == 'v':
128+
s = "Vertical wall between "
129+
s += str((self.row, self.column))
130+
s += " and "
131+
s += str((self.row+1, self.column))
132+
else:
133+
s = "Horizontal wall between "
134+
s += str((self.row, self.column))
135+
s += " and "
136+
s += str((self.row, self.column+1))
137+
return s
138+
139+
def setWall(self, solid):
140+
self.solid = solid
141+
self.refreshWall()
142+
143+
def refreshWall(self):
144+
if self.solid and self.edge == False:
145+
self.wallGraphic.setOutline("white")
146+
elif self.solid:
147+
self.wallGraphic.setOutline("red")
148+
else:
149+
self.wallGraphic.setOutline("black")
150+
151+
class Player:
152+
153+
def __init__(self, number):
154+
self.number = number
155+
self.row = None
156+
self.maze = None
157+
self.column = None
158+
self.figure = None
159+
self.heading = None
160+
self.standing = True
161+
self.alive = True
162+
163+
def __repr__(self):
164+
return "Player " + str(self.number)
165+
166+
def createFigure(self, anchor):
167+
if self.heading == None:
168+
self.figure = graphics.Circle(anchor,cellSize/2.5)
169+
self.figure.setOutline("yellow")
170+
else:
171+
if self.heading == 'N':
172+
p1x = anchor.getX()
173+
p1y = anchor.getY() - cellSize/3.0
174+
p2x = anchor.getX() - cellSize/5.0
175+
p2y = anchor.getY() + cellSize/3.0
176+
p3x = anchor.getX() + cellSize/5.0
177+
p3y = anchor.getY() + cellSize/3.0
178+
elif self.heading == 'S':
179+
p1x = anchor.getX()
180+
p1y = anchor.getY() + cellSize/3.0
181+
p2x = anchor.getX() - cellSize/5.0
182+
p2y = anchor.getY() - cellSize/3.0
183+
p3x = anchor.getX() + cellSize/5.0
184+
p3y = anchor.getY() - cellSize/3.0
185+
elif self.heading == 'E':
186+
p1x = anchor.getX() + cellSize/3.0
187+
p1y = anchor.getY()
188+
p2x = anchor.getX() - cellSize/3.0
189+
p2y = anchor.getY() - cellSize/5.0
190+
p3x = anchor.getX() - cellSize/3.0
191+
p3y = anchor.getY() + cellSize/5.0
192+
elif self.heading == 'W':
193+
p1x = anchor.getX() - cellSize/3.0
194+
p1y = anchor.getY()
195+
p2x = anchor.getX() + cellSize/3.0
196+
p2y = anchor.getY() - cellSize/5.0
197+
p3x = anchor.getX() + cellSize/3.0
198+
p3y = anchor.getY() + cellSize/5.0
199+
p1 = graphics.Point(p1x, p1y)
200+
p2 = graphics.Point(p2x, p2y)
201+
p3 = graphics.Point(p3x, p3y)
202+
self.figure = graphics.Polygon(p1, p2, p3)
203+
self.figure.setOutline("yellow")
204+
205+
def move(self, direction, newHeading):
206+
"""this function attempts to move the player
207+
in the desired direction, and gives them a new
208+
heading, it also assumes they are already inserted
209+
into the maze"""
210+
self.figure.undraw()
211+
self.heading = newHeading
212+
r = self.row
213+
c = self.column
214+
if direction == 'N' and not self.maze.Hwalls[r][c].solid:
215+
nextPt = self.getAnchor(r-1, c)
216+
self.row -= 1
217+
elif direction == 'S' and not self.maze.Hwalls[r+1][c].solid:
218+
nextPt = self.getAnchor(r+1, c)
219+
self.row += 1
220+
elif direction == 'W' and not self.maze.Vwalls[r][c].solid:
221+
nextPt = self.getAnchor(r, c-1)
222+
self.column -= 1
223+
elif direction == 'E' and not self.maze.Vwalls[r][c+1].solid:
224+
nextPt = self.getAnchor(r, c+1)
225+
self.column += 1
226+
else:
227+
nextPt = self.getAnchor(r, c)
228+
self.createFigure(nextPt)
229+
self.figure.draw(self.maze.win)
230+
231+
def shoot(self, direction):
232+
"""shoots in a direction, marking the death squares
233+
red as the shot moves"""
234+
r = self.row
235+
c = self.column
236+
wallCount = 0
237+
while True: #not at the end!
238+
if direction == 'N':
239+
if self.maze.getWall(r,c,'N').solid: wallCount += 1
240+
if wallCount > 1: break
241+
r -= 1
242+
self.maze.cells[r][c].cellGraphic.setFill('red')
243+
elif direction == 'W':
244+
if self.maze.getWall(r,c,'W').solid: wallCount += 1
245+
if wallCount > 1: break
246+
c -= 1
247+
self.maze.cells[r][c].cellGraphic.setFill('red')
248+
elif direction == 'E':
249+
if self.maze.getWall(r,c,'E').solid: wallCount += 1
250+
if wallCount > 1: break
251+
c += 1
252+
self.maze.cells[r][c].cellGraphic.setFill('red')
253+
elif direction == 'S':
254+
if self.maze.getWall(r,c,'S').solid: wallCount += 1
255+
if wallCount > 1: break
256+
r += 1
257+
self.maze.cells[r][c].cellGraphic.setFill('red')
258+
time.sleep(0.5)
259+
260+
261+
def getAnchor(self, row, column):
262+
return self.maze.cells[row][column].cellGraphic.getCenter()
263+
264+
265+
n = "N"
266+
e = "E"
267+
w = "W"
268+
s = "S"
269+
N = n
270+
E = e
271+
W = w
272+
S = s
273+
274+
m = Maze(7,8)
275+
p = Player(1)
276+
m.insertObj(2,3,p)
277+
m.setWall(2,3,'v',False)
278+
m.setWall(2,5,'v',False)
279+
m.setWall(3,4,'h',False)
280+
m.setWall(2,1,'v',False)
281+
m.setWall(1,0,'h',False)
282+
m.setWall(2,1,'v',False)
283+
m.setWall(2,3,'v',False)
284+
m.setWall(2,2,'v',False)
285+
m.setWall(3,4,'v',False)
286+
m.setWall(2,0,'h',False)
287+
m.setWall(3,3,'h',False)
288+

0 commit comments

Comments
 (0)