-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerm Project.py
323 lines (296 loc) · 12.4 KB
/
Term Project.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from cmu_112_graphics import *
from tkinter import *
import random
#TP1 Submission
#CITE cmu graphics and tkinter
# CITATION: http://www.cs.cmu.edu/~112/notes/notes-animations-part2.html
# #subclassingModalApp
class SplashScreenMode(Mode):
def redrawAll(mode,canvas):
canvas.create_rectangle(0,0,mode.width,mode.height,fill="black")
font = 'Arial 26 bold'
canvas.create_text(mode.width/2, mode.height/5, \
text='Pac Man', font=font, fill="white")
canvas.create_text(mode.width/2, mode.height/3, \
text='Press space to start!',font=font,fill="white")
def keyPressed(mode,event):
if event.key=="Space":
mode.app.setActiveMode(mode.app.gameMode)
#will add buttons to determine mode
def mousePressed(mode,event):
pass
# Pac-Man class defines characteristics of Pac-Man and draws Pac-Man
class PacMan(Mode):
def __init__(self,x,y):
self.x=x
self.y=y
self.radius=10
self.color="yellow"
self.url="http://labs.phaser.io/assets/games/pacman/sprites32.png"
def drawPacMan(self,canvas):
canvas.create_oval(self.x-self.radius,self.y-self.radius,\
self.x+self.radius,self.y+self.radius,fill=self.color)
# Ghost class defines characteristics of ghosts, draws ghosts,
# and defines movement
class Ghost(Mode):
def __init__(self,x,y):
self.x=x
self.y=y
self.color=None
self.radius=10
self.url="http://labs.phaser.io/assets/games/pacman/sprites32.png"
def drawGhost(self,canvas):
canvas.create_oval(self.x-self.radius,self.y-self.radius,\
self.x+self.radius,self.y+self.radius,fill=self.color)
def moveGhost(self):
# each ghost will have its own algorithm to determine its
# path around the maze, can be implemented in this method in each
# ghost subclass
pass
class Blinky(Ghost):
def __init__(self,x,y):
super().__init__(x,y)
self.color="red"
def moveGhost(self):
pass
# follows the shortest path to get to Pac-Man
class Pinky(Ghost):
def __init__(self,x,y):
super().__init__(x,y)
self.color="pink"
def moveGhost(self):
pass
# follows the path to get to four tiles to the right or left of Pac-Man
class Inky(Ghost):
def __init__(self,x,y):
super().__init__(x,y)
self.color="aqua"
def moveGhost(self):
pass
# follows the shortest path from Blinky to two tiles next to Pac-Man and
# doubles length in that direction
class Clyde(Ghost):
def __init__(self,x,y):
super().__init__(x,y)
self.color="orange"
def moveGhost(self):
pass
# if less than 8 tiles away from Pac-Man, random mode but if not, same algorithm as Blinky
# Wall class defines characteristics of walls and draws walls
class Wall(Mode):
def __init__(self,x,y,width,height):
self.x=x
self.y=y
self.width=width
self.height=height
self.color="blue"
def drawWall(self,canvas):
canvas.create_rectangle(self.x,self.y,\
self.x+self.width,self.y+self.height,fill=self.color,width=0)
# OriginalBoard class draws walls based on given dimensions
# original board is static, and the dimensions and coordinates are predetermined
class OriginalBoard(Wall):
def __init__(self,mode,x,y,width,height):
super().__init__(x,y,width,height)
self.ratio1=50/mode.width
#right wall
rightWall=Wall(2*self.x,2*self.y,self.width,\
mode.height-4*self.height)
#left wall
leftWall=Wall(mode.width-3*self.x,2*self.y,self.width,\
mode.height-4*self.height)
#top wall
topWall=Wall(2*self.x,2*self.y,mode.width-4*self.width,\
self.height)
#bottom wall
bottomWall=Wall(2*self.x,mode.height-3*self.y,mode.width-4*self.width,\
self.height)
#1
wall11=Wall(10*self.x,20*self.y,10*self.width,50*self.height)
#5
wall51=Wall(10*self.x+2*self.ratio1*mode.width,20*self.y,20*self.width,\
10*self.height)
wall52=Wall(10*self.x+2*self.ratio1*mode.width,20*self.y+10*self.height,\
10*self.width,20*self.height)
wall53=Wall(20*self.x+2*self.ratio1*mode.width,20*self.y+20*self.height,10*self.width,\
20*self.height)
wall54=Wall(10*self.x+2*self.ratio1*mode.width,20*self.y+40*self.height,20*self.width,\
10*self.height)
#dash
wallDash=Wall(10*self.x+45*self.width,20*self.y+20*self.width,\
20*self.width,10*self.height)
#1
wall12=Wall(80*self.x,20*self.y,10*self.width,50*self.height)
#1
wall13=Wall(100*self.x,20*self.y,10*self.width,50*self.height)
#2
wall21=Wall(100*self.x+2*self.ratio1*mode.width,20*self.y,20*self.width,\
10*self.height)
wall22=Wall(110*self.x+2*self.ratio1*mode.width,20*self.y+10*self.height,\
10*self.width,20*self.height)
wall23=Wall(100*self.x+2*self.ratio1*mode.width,20*self.y+20*self.height,10*self.width,\
20*self.height)
wall24=Wall(100*self.x+2*self.ratio1*mode.width,20*self.y+40*self.height,20*self.width,\
10*self.height)
self.board=[wall11,wall51,wall52,wall53,wall54,wallDash,wall12,wall13,\
wall21,wall22,wall23,wall24,rightWall,leftWall,topWall,bottomWall]
self.dimensions=set()
for wall in self.board:
self.dimensions.add((wall.x,wall.y,wall.width,wall.height))
def drawBoard(self,canvas):
for wall in self.board:
wall.drawWall(canvas)
# Points class defines characteristics of points and draws points
class Points(object):
def __init__(self,x,y):
self.x=x
self.y=y
self.radius=5
def drawPoints(self,canvas):
canvas.create_oval(self.x-self.radius,self.y-self.radius,\
self.x+self.radius,self.y+self.radius,fill="orange")
# OriginalGameMode establishes original game mode with static board
# if time permits, may add randomly generated board without side scroll
class OriginalGameMode(Mode):
def appStarted(mode):
mode.radius=10
mode.pointRadius=5
mode.pacmanXPos,mode.pacmanYPos=mode.width/2,4*mode.height/5
mode.ghostXPos,mode.ghostYPos=325,225
mode.wallX,mode.wallY=mode.width/150,mode.height/100
mode.wallWidth,mode.wallHeight=mode.width/150,mode.height/100
mode.speed=5
mode.dxPos,mode.dyPos=-1*mode.speed,0
mode.currDirection="left"
mode.direction=mode.currDirection
mode.legalDirections=["up","right","down","left"]
mode.gameBoard=OriginalBoard(mode,mode.wallX,mode.wallY,\
mode.wallWidth,mode.wallHeight)
mode.points=[]
mode.drawCoins()
mode.score=0
mode.gameOver=False
def keyPressed(mode,event):
if mode.gameOver==False:
if event.key=="Right":
mode.direction="right"
elif event.key=="Left":
mode.direction="left"
elif event.key=="Up":
mode.direction="up"
elif event.key=="Down":
mode.direction="down"
if mode.direction in mode.legalDirections:
mode.currDirection=mode.direction
mode.movePacMan(mode.currDirection)
else:
mode.movePacMan(mode.currDirection)
else:
if event.key=="r":
mode.appStarted()
mode.gameOver=False
def movePacMan(mode,direction):
if direction=="right":
mode.dirPacMan(mode.speed,0)
elif direction=="left":
mode.dirPacMan(-1*mode.speed,0)
elif direction=="up":
mode.dirPacMan(0,-1*mode.speed)
elif direction=="down":
mode.dirPacMan(0,mode.speed)
def timerFired(mode):
if mode.gameOver==False:
mode.legalDirections=mode.legalPlaces()
if mode.currDirection in mode.legalDirections:
mode.pacmanXPos+=mode.dxPos
mode.pacmanYPos+=mode.dyPos
i=0
while i<len(mode.points):
if ((mode.points[i].x-mode.pacmanXPos)**2+\
(mode.points[i].y-mode.pacmanYPos)**2)**0.5\
<mode.points[i].radius+mode.radius:
mode.points.pop(i)
mode.score+=1
if len(mode.points)==0:
mode.gameOver=True
i+=1
else:
mode.dirPacMan(0,0)
def dirPacMan(mode,dx,dy):
mode.dxPos=dx
mode.dyPos=dy
def legalPlaces(mode):
mode.legalDirections=["up","right","down","left"]
for wall in mode.gameBoard.dimensions:
if mode.pacmanXPos+mode.radius>wall[0] and \
mode.pacmanXPos-mode.radius<wall[0]+wall[2]:
if (mode.pacmanYPos>wall[1]+wall[3] and mode.pacmanYPos-mode.radius<=wall[1]+wall[3]):
if "up" in mode.legalDirections:
mode.legalDirections.remove("up")
elif (mode.pacmanYPos<wall[1] and mode.pacmanYPos+mode.radius>=wall[1]):
if "down" in mode.legalDirections:
mode.legalDirections.remove("down")
if mode.pacmanYPos+mode.radius>wall[1] and \
mode.pacmanYPos-mode.radius<wall[1]+wall[3]:
if (mode.pacmanXPos>wall[0]+wall[2] and mode.pacmanXPos-mode.radius<=wall[0]+wall[2]):
if "left" in mode.legalDirections:
mode.legalDirections.remove("left")
elif (mode.pacmanXPos<wall[0] and mode.pacmanXPos+mode.radius>=wall[0]):
if "right" in mode.legalDirections:
mode.legalDirections.remove("right")
return mode.legalDirections
def drawCoins(mode):
for x in range(int(mode.wallX*10),int(mode.width-3*mode.wallWidth),int(mode.wallX*10)):
for y in range(int(mode.wallY*10),int(mode.height-3*mode.wallHeight),int(mode.wallY*10)):
mode.points.append(Points(x,y))
i=0
while i < len(mode.points):
for wall in mode.gameBoard.dimensions:
if (mode.points[i].x>wall[0] and \
mode.points[i].x<wall[0]+wall[2] and \
mode.points[i].y<wall[1]+wall[3] and \
mode.points[i].y>wall[1]):
mode.points.pop(i)
i+=1
def redrawAll(mode,canvas):
canvas.create_rectangle(0,0,mode.width,mode.height,fill="black")
if mode.gameOver==False:
mode.gameBoard.drawBoard(canvas)
for coin in mode.points:
coin.drawPoints(canvas)
Blinky(mode.ghostXPos-mode.width/25,mode.ghostYPos).drawGhost(canvas)
Pinky(mode.ghostXPos-mode.width/75,mode.ghostYPos).drawGhost(canvas)
Inky(mode.ghostXPos+mode.width/75,mode.ghostYPos).drawGhost(canvas)
Clyde(mode.ghostXPos+mode.width/25,mode.ghostYPos).drawGhost(canvas)
PacMan(mode.pacmanXPos,mode.pacmanYPos).drawPacMan(canvas)
canvas.create_text(mode.width//2,15,text=f'Score:{mode.score}',\
fill="white")
else:
font = 'Arial 26 bold'
canvas.create_text(mode.width//2,mode.height//2,
text='Game Over!', font=font, fill="white")
canvas.create_text(mode.width//2,3*mode.height//4,
text=f'Your score: {mode.score}',font=font,fill="white")
canvas.create_text(mode.width//2,5*mode.height//6,
text='Press r to restart',font=font,fill="white")
# SidescrollGameMode will establish a randomly generated board every single time
# a new game starts, will allow the user to travel off screen
class SidescrollGameMode(Mode):
pass
# CreativeMode will allow the user to create their own map, will play like
# original mode
class CreativeMode(Mode):
pass
# MultiplayerMode will allow two users to player, one as ghost
# can play in original mode
class MultiplayerMode(Mode):
pass
# from http://www.cs.cmu.edu/~112/notes/notes-animations-part2.html
# #subclassingModalApp
class MyModalApp(ModalApp):
def appStarted(app):
app.splashScreenMode=SplashScreenMode()
app.gameMode=OriginalGameMode()
app.setActiveMode(app.splashScreenMode)
app.timerDelay=50
app = MyModalApp(width=750, height=500)