-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlusGame.py
489 lines (350 loc) · 13 KB
/
PlusGame.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# 15-112, Summer 2, TP
######################################
# Full name: Jose Saravia
# Section: A
# Andrew ID: jsaravia
######################################
from tkinter import *
from tkinter import ttk
import time
import math
import GameObject
import GameUI
import GameBoard
import PlusTests
# from threading import Timer
#
# import threading
# Base template for animations was retrieved from course website
# https://pd43.github.io/notes/code/events-example0.py
# photo = photo = PhotoImage(data=img)
# ----------- MODEL -----------
# Initialize the data which will be used to draw on the screen.
# Used for replaying the game
def replay(data, oldScore):
init(data)
data.oldScore = oldScore
def quit(data):
replay(data, data.score)
def init(data):
# Player Data
initPlayer(data)
# Board Data
data.boardCount = 3
## TEST NOTE: Modify second field to change starting level ##
data.boards = GameBoard.createBoards(data.boardCount, 1)
# Game Data
initGameData(data)
# Initialize UI elements
data.ui.initUiElements(data.root, data)
data.ui.removePauseUi()
data.ui.removeSplashUi()
data.ui.removePauseButton()
data.ui.removeHelpButton()
# Print board for testing purposes
# for b in data.boards:
# b.printBoardData()
def initGameData(data):
# Game State
data.isPlaying = False
data.hasExitedPause = False
data.isPaused = False
data.levelHasChanged = False
data.isInHelpMenu = False
data.score = 0
data.oldScore = None
data.level = 1
# Time Data
data.startTime = 0
data.timeOnPause = 0
data.pauseTime = 0
data.timeExitPause = 0
# Powerup data
data.hasReversePower = False
data.hasColorSwitchPower = False
data.hasSlowPower = False
data.hasInvinPower = False
def initPlayer(data):
w, h = data.width, data.height
# 2 player controlled circles
data.player1 = GameObject.Player(("Left", "Right"),
data.ui.playerAMaxRadius,
data.ui.ballAColor,
1, w, h)
data.player2 = GameObject.Player(("Up", "Down"),
data.ui.playerBMaxRadius,
data.ui.ballBColor,
2, w, h)
# Player background
data.player1Pad = GameObject.DirectionPad(data.ui.ballBgColor,
data.ui.ballBgRadius + 2,
1, w, h)
data.player2Pad = GameObject.DirectionPad(data.ui.ballBgColor,
data.ui.ballBgRadius + 2,
2, w, h)
getPlayerControlDirections(data)
def getPlayerControlDirections(data):
data.player1DirA = data.player1.getDirections()[0]
data.player1DirB = data.player1.getDirections()[1]
data.player2DirA = data.player2.getDirections()[0]
data.player2DirB = data.player2.getDirections()[1]
# --------- CONTROLLER --------
def mousePressed(event, data):
pass
def keyReleased(event, data):
key = event.keysym
data.keyPressed.discard(key)
def keyPressed(event, data):
key = event.keysym
# Initialize the game
if (not data.isPlaying and
not data.isPaused and
key == "space"):
data.player2.minimizeAnimation(data.ui.playerBMinRadius, .001)
data.startTime = time.time()
data.isPlaying = True
data.score = 0
# For pausing the game
if (data.isPlaying and key == "p"):
# Call pause button, its id is 5
data.ui.onButton(data, 5)
if(not data.isPlaying or data.isPaused): return
# Only add arrow keys to keyPressed set if pressed
if(key in {"Left", "Right", "Up", "Down"}):
data.keyPressed.add(key)
playerControls(data)
if(key == "m" or data.hasReversePower):
data.player1.invertDirection()
data.player2.invertDirection()
getPlayerControlDirections(data)
data.hasReversePower = False
if(key == "l"):
data.level += 1
def reverseBack(data):
data.hasReversePower = True
def timerFired(data):
if(not data.isPlaying or data.isPaused): return
movePlayersToCenter(data)
removeUsedBoards(data)
# If player has just exited the pause screen, wait
if (not data.hasExitedPause):
moveObstacles(data)
checkForPlayerCollsion(data)
increaseScore(data)
# Removes boards that are not displaying obstacles and adds a new board
def removeUsedBoards(data):
removeOutOfBoundObstacles(data)
if(len(data.boards[0].obstacleList) == 0):
data.boards.pop(0)
data.boards.append(GameBoard.createBoards(1, data.level))
def checkForPlayerCollsion(data):
obstacles = data.boards[0].obstacleList
for obstacle in obstacles:
if(data.player1.checkCollision(obstacle) or
data.player2.checkCollision(obstacle)):
replay(data, data.score)
# if (len(data.boards[0].powerList) != 0):
# item = data.boards[0].powerList[0]
# if (data.player1.checkCollision(item)):
# data.hasReversePower = True
# data.boards[0].powerList = []
# # t = Timer(3.0, reverseBack, args=[data])
# # t.start()
def playerControls(data):
# Player 1 Controls
if (data.player1DirA in data.keyPressed):
data.player1.moveToDirection(data.player1DirA)
data.player1Pad.moveToDirection(data.player1DirA)
elif (data.player1DirB in data.keyPressed):
data.player1.moveToDirection(data.player1DirB)
data.player1Pad.moveToDirection(data.player1DirB)
# Player 2 Controls
if (data.player2DirA in data.keyPressed):
data.player2.moveToDirection(data.player2DirA)
data.player2Pad.moveToDirection(data.player2DirA)
elif (data.player2DirB in data.keyPressed):
data.player2.moveToDirection(data.player2DirB)
data.player2Pad.moveToDirection(data.player2DirB)
def movePlayersToCenter(data):
if (data.player1DirA not in data.keyPressed and
data.player1DirB not in data.keyPressed):
data.player1.moveToCenter()
data.player1Pad.moveToCenter()
if (data.player2DirA not in data.keyPressed and
data.player2DirB not in data.keyPressed):
data.player2.moveToCenter()
data.player2Pad.moveToCenter()
def moveObstacles(data):
obstacles = data.boards[0].obstacleList
for obstacle in obstacles:
obstacle.move()
if(len(data.boards[0].powerList) != 0):
data.boards[0].powerList[0].move()
def removeOutOfBoundObstacles(data):
l = data.boards[0].obstacleList
for obstacle in l:
if(obstacle.x < -60 or obstacle.x > 420 or
obstacle.y < -40 or obstacle.y > 680):
l.remove(obstacle)
def increaseScore(data):
if(not data.isPlaying): return
curTime = time.time()
# Keep track of time spent on pause menu
if (curTime - data.timeOnPause != curTime):
data.pauseTime += curTime - data.timeOnPause
data.timeOnPause = 0
oldScore = data.score
data.score = math.floor(curTime - data.startTime - data.pauseTime)
# Level up when score changes
if(data.score != oldScore):
levelUp(data)
def levelUp(data):
if(data.score % 15 == 0):
data.level += 1
data.levelHasChanged = True
# ----------- VIEW -----------
# It only draws on the canvas.
def redrawAll(canvas, data):
# Draw player
data.player1Pad.draw(canvas)
data.player2Pad.draw(canvas)
data.player1.draw(canvas)
data.player2.draw(canvas)
if(data.isPlaying and not data.isInHelpMenu):
drawPlayingScreen(data, canvas)
elif(not data.isInHelpMenu):
drawSplashScreen(data, canvas)
if(data.isPaused and not data.isInHelpMenu):
drawPauseScreen(data, canvas)
else:
data.ui.removePauseUi()
# Draw Counter when game is unpaused
if(data.hasExitedPause):
drawUnPausedCounter(data, canvas)
if(data.isInHelpMenu):
# data.player2.enlargAnimation(data.ui.playerBMaxRadius*5, .002)
# data.ui.removeSplashUi()
#
# if(data.player2.getRadius() == data.ui.playerBMaxRadius*5):
drawHelpMenu(data, canvas)
def drawHelpMenu(data, canvas):
data.player2.enlargAnimation(data.ui.playerBMaxRadius * 5, .002)
data.ui.removeSplashUi()
data.ui.removePauseButton()
if (data.player2.getRadius() == data.ui.playerBMaxRadius * 5):
cX, cY = (data.width//2, data.height//2)
canvas.create_image(cX, cY, image=data.ui.helpImage)
data.ui.showHelpButton()
def drawPlayingScreen(data, canvas):
data.ui.removeSplashUi()
data.ui.showPauseButton()
if (not data.isPaused):
data.player1.minimizeAnimation(data.ui.playerRadius, .0001)
drawObstacles(data, canvas)
drawScore(data, canvas)
drawLevel(data, canvas)
def drawSplashScreen(data, canvas):
cX, cY = (data.width//2, data.height//2)
y = (cY - data.ui.playerAMaxRadius) // 2
canvas.create_image(cX, y, image=data.ui.halfTitleImage)
data.ui.showSplashUi()
data.ui.removePauseButton()
canvas.create_text(cX, cY, text="SPACE", font="futura 30 bold",
fill=data.ui.bgColor)
# Used for displaying the core of the player
if(data.oldScore is not None):
cx, cy = data.width // 2, data.height // 2
canvas.create_text(cx, cy + 175, text=str(data.oldScore), font="futura 50",
fill="white")
def drawPauseScreen(data, canvas):
if (data.player1.getRadius() != data.ui.playerAMaxRadius):
# data.timeOnPause = time.time()
data.player2.enlargAnimation(data.ui.playerBMaxRadius, .002)
data.player1.enlargAnimation(data.ui.playerAMaxRadius, .001)
data.ui.showPauseUi()
def drawUnPausedCounter(data, canvas):
cX, cY = (data.width//2, data.height//2)
totalSec = 3
seconds = (math.floor(time.time() - data.timeExit))
if (seconds == totalSec):
# Modified for algorithm readability
data.hasExitedPause = False
else:
canvas.create_text(cX, cY, text=str(totalSec - seconds),
font="futura 150 bold",
fill=data.ui.ballBColor)
def drawScore(data, canvas):
cx, y = data.width//2, data.height - 75
canvas.create_text(cx, y, text=str(data.score), font="futura 50",
fill="white")
def drawLevel(data, canvas):
cx, y = data.width // 2, data.height - 25
if(data.levelHasChanged):
canvas.create_text(cx, y, text=str(data.level), font="futura 16",
fill="white")
data.levelHasChanged = True
def drawObstacles(data, canvas):
l = data.boards[0].obstacleList
for o in l:
o.draw(canvas)
if (len(data.boards[0].powerList) != 0):
data.boards[0].powerList[0].draw(canvas)
####################################
####################################
# use the run function as-is
####################################
####################################
def run(width=300, height=300):
def redrawAllWrapper(canvas, data):
canvas.delete(ALL)
redrawAll(canvas, data)
canvas.update()
def mousePressedWrapper(event, canvas, data):
mousePressed(event, data)
redrawAllWrapper(canvas, data)
def keyPressedWrapper(event, canvas, data):
keyPressed(event, data)
redrawAllWrapper(canvas, data)
def keyReleaseWrapper(event, canvas, data):
keyReleased(event, data)
redrawAllWrapper(canvas, data)
def timerFiredWrapper(canvas, data):
timerFired(data)
redrawAllWrapper(canvas, data)
# pause, then call timerFired again
canvas.after(data.timerDelay, timerFiredWrapper, canvas, data)
# Set up data and call init
class Struct(object): pass
data = Struct()
data.width = width
data.height = height
data.ui = GameUI.UIData(width, height)
data.keyPressed = set()
data.timerDelay = 100 # milliseconds
# create the root and the canvas
root = Tk()
root.resizable(width=FALSE, height=FALSE)
# Chanage theme from aqua(Default) to clam
# This allows the button background to be changed in mac
rootThemes = ttk.Style()
rootThemes.theme_use("clam")
# Store Root for Button
data.root = root
canvas = Canvas(root, width=data.width,
height=data.height, bg=data.ui.bgColor)
canvas.pack()
init(data)
root.bind("<Button-1>", lambda event:
mousePressedWrapper(event, canvas, data))
# root.bind("<Key>", lambda event:
# keyPressedWrapper(event, canvas, data))
root.bind("<KeyPress>", lambda event:
keyPressedWrapper(event, canvas, data), add='+')
root.bind("<KeyRelease>", lambda event:
keyReleaseWrapper(event, canvas, data), add='+')
timerFiredWrapper(canvas, data)
# and launch the app
root.mainloop() # blocks until window is closed
print("bye!")
PlusTests.runTests()
run(360, 640)