-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
445 lines (404 loc) · 19.3 KB
/
Game.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
import random # seed generation
import time # Making sure tings are based on time not loops
from tkinter import *
from tkinter import colorchooser, simpledialog
import winSize
import infoWindow
import enemyCtrl
import shopCtrl
pygameInstalled = True
try:
from pygame import mixer
except ModuleNotFoundError as er:
print("Pygame not installed, required for audio, game will not use any audio.")
pygameInstalled = False
class Game(object): # Allows for things like getters and setters I think
# Init (a bit more than) some vars
ogHealth = lastHealth = health = 200
gameRunning = gamePaused = dispMenuHidden = waitForResize = stopBetweenRounds = lastMidRound = False
clickChecked = midRound = shrinkWin = schoolFriendly = True
clickX = clickY = volumeSet = rainbowTime = points = menubar = shopMenu = dispMenu = gameMenu = rainbowOn = 0
bullets = clipSize = seedTxt = 6
refillTimer = 1.5
RAINBOW_LIST = ('#ff0000', '#ff3700', '#ff6a00', '#ffa200', '#ffd500', '#f2ff00', '#bfff00', '#88ff00',
'#55ff00', '#1eff00', '#00ff15', '#00ff4d', '#00ff80', '#00ffb7', '#00ffea', '#00ddff',
'#00aaff', '#0073ff', '#0040ff', '#0009ff', '#2b00ff', '#6200ff', '#9500ff', '#cc00ff',
'#ff00ff')
difficulties = []
currColor = "Default"
def __init__(self):
self.tk = Tk()
if pygameInstalled:
mixer.init()
self.difficulty = IntVar(self.tk, 2)
# Get sizes then setup window
self.window = winSize.WinSize(self)
self.pygameInstalled = pygameInstalled
self.tk.title("Protect The Base!")
self.canvas = Canvas(self.tk, width=self.window.x, height=self.window.y)
self.tk.resizable(width=False, height=False) # Window can't be manually resized
self.canvas.pack()
self.tk.geometry('+' + str(self.window.offx) + '+' + str(self.window.offy))
self.defBackground = self.canvas["background"] # Sets defualt background for system compatibility
self.enemyCtrl = enemyCtrl.EnemyCtrl(self, self.window)
self.tower = Tower(self, self.window)
self.infoManager = infoWindow.InfoManager(self, self.window, self.enemyCtrl) # Will be setup after title screen
self.shopCtrl = shopCtrl.ShopCtrl(self, self.infoManager)
self.waveNoticeText = self.canvas.create_text(self.window.midx, self.window.midy / 2, text='Wave #1',
font=('Helvetica', 20))
self.canvas.itemconfigure(self.waveNoticeText, state="hidden")
self.bindKeypressesAndMenu()
self.window.readConfig(2) # read rest of config
self.seed = random.randrange(sys.maxsize)
self.titleScreen() # Runs the title screen
def rename_menubar(self, shop_name="Shop", game_name="Game", view_name="View"):
self.menubar = Menu(self.tk)
self.menubar.add_cascade(label=game_name, menu=self.gameMenu)
self.menubar.add_cascade(label=view_name, menu=self.dispMenu)
self.menubar.add_cascade(label=shop_name, menu=self.shopCtrl.shopMenu)
self.tk.config(menu=self.menubar)
def bindKeypressesAndMenu(self):
# Game Menu
self.gameMenu = Menu(self.tk, tearoff=0) # Tearoff keeps menus from being able to detach
if pygameInstalled:
self.gameMenu.add_command(label="Set Volume %", command=self.changeVolume)
self.gameMenu.add_command(label="Resize Max Sized Window", command=self.queueResize)
self.gameMenu.add_command(label="Pause/Unpause Game", command=self.pauseGame)
self.gameMenu.add_command(label="Toggle stop between rounds", command=self.toggleBetweenRoundsPause)
self.gameMenu.add_command(label="Restart", command=lambda: restart(self))
self.gameMenu.add_command(label="Quit", command=self.quit)
# Display menu
self.dispMenu = Menu(self.tk, tearoff=0)
self.dispMenu.add_command(label="Set Background Color", command=self.setBackgroundColor)
self.dispMenu.add_command(label="Set Background Rainbow", command=self.setBackgroundRainbow)
self.dispMenu.add_command(label="Toggle Enemy Leg Motion", command=self.enemyCtrl.toggleLegMotion)
self.dispMenu.add_command(label="Toggle Information Window", command=self.toggleInfoWin)
# Shop menu
self.shopCtrl.make_shop("Clear All Enemies", 30, self.shopCtrl.kill_all_enemies, keypress="q")
self.shopCtrl.make_shop("Expand Window-20s", 20, self.shopCtrl.expandWindow)
self.shopCtrl.make_shop("Expand Clip Size (+1)", 5, self.shopCtrl.expand_clip, 1.1, "w")
self.rename_menubar() # Add them all to the menubar
# Add all the keypresses and mouse clicks
self.tk.bind("<Button 1>", self.mouseClick) # Left click
self.tk.bind("<Button 3>", self.startReload) # Right click
self.tk.bind("<space>", self.startRound)
self.tk.bind("<Escape>", self.stopGame)
self.tk.bind('p', self.pauseGame)
self.tk.bind('e', self.easterEgg)
def titleScreen(self):
# Should eventually make text based on window size
self.canvas.create_text(self.window.midx, self.window.midy - 200, text='Protect the base!',
font=('Helvetica', 35), tag="titleItem")
self.canvas.create_text(self.window.midx, self.window.midy - 140, text='Right click anywhere to continue.',
font=('Helvetica', 20), tag="titleItem")
seedBtn = Button(self.tk, text="Set Game Seed", command=self.setSeed)
seedBtn.pack()
seedBtn.place(x=self.window.midx, y=self.window.midy - 110, anchor=CENTER)
self.seedTxt = self.canvas.create_text(self.window.midx, self.window.midy - 85,
text='Current Seed: ' + str(self.seed), font=('Helvetica', 15),
tag="titleItem")
self.canvas.create_text(self.window.midx, self.window.midy - 60, text='Difficulty:',
font=('Helvetica', 15), tag="titleItem")
options = {"Easy": 1, "Medium": 2, "Hard": 3, "Impossible": 4}
for (text, value) in options.items():
self.difficulties.append(Radiobutton(self.tk, text=text, variable=self.difficulty, value=value))
self.difficulties[value - 1].pack()
self.difficulties[value - 1].place(x=self.window.midx - 40, y=self.window.midy - 70 + 20 * value)
if pygameInstalled:
# Title screen music
mixer.music.load("Sounds/Background1.wav")
mixer.music.play(-1) # -1 means until new one or stopped
if self.currColor == "Rainbow":
self.handleRainbow()
elif self.currColor != "Default":
[item.config(bg=self.currColor) for item in self.difficulties]
while self.clickChecked:
try:
self.tk.update()
except TclError as e:
print(e)
print("You have closed the window!")
self.quit()
self.clickChecked = True
if pygameInstalled:
# Game music
mixer.music.load("Sounds/Background2.wav")
mixer.music.play(-1)
self.canvas.delete("titleItem")
[item.destroy() for item in self.difficulties]
self.difficulties = []
if self.difficulty.get() == 1:
self.ogHealth = self.lastHealth = self.health = 300
elif self.difficulty.get() == 2:
self.ogHealth = self.lastHealth = self.health = 200
elif self.difficulty.get() == 3:
self.ogHealth = self.lastHealth = self.health = 150
else:
self.ogHealth = self.lastHealth = self.health = 100
seedBtn.destroy()
random.seed(self.seed)
print("The seed for this game was: " + str(self.seed))
self.infoManager.start_game()
self.gameRunning = True
self.tower.startGame()
self.tk.update_idletasks()
self.handleGame()
def endScreen(self):
self.infoManager.gameOver()
self.tower.remove()
self.enemyCtrl.killAllEnemies()
if pygameInstalled:
# End screen/Title screen music
mixer.music.load("Sounds/Background1.wav")
mixer.music.play(-1)
t1 = self.canvas.create_text(self.window.midx, self.window.midy - 200, text='You Have Lost!',
font=('Helvetica', 35))
t2 = self.canvas.create_text(self.window.midx, self.window.midy - 140,
text='You got to round %i.' % self.enemyCtrl.round, font=('Helvetica', 20))
# Expand window to full size in a way that looks cool
for x in range(0, 101):
self.window.shrinkWindow(x / 100)
if self.window.x > 140 and self.dispMenuHidden:
self.rename_menubar()
self.dispMenuHidden = False
self.canvas.coords(t1, (self.window.midx, self.window.midy - 200))
self.canvas.coords(t2, (self.window.midx, self.window.midy - 140))
self.tk.update()
try:
while True:
self.tk.update()
except TclError as e:
print(e)
print("You have closed the window!")
self.quit()
def handleGame(self):
while self.gameRunning:
try:
while self.gamePaused:
self.tk.update()
self.enemyCtrl.handleEnemies()
self.enemyCtrl.handleBullets()
self.infoManager.updateItems()
# Check if request to resize
if self.waitForResize and (not self.midRound):
self.window.customiseButton()
# Check if round over and show click space to continue, or show new round text
if self.midRound != self.lastMidRound:
if not (self.stopBetweenRounds or self.midRound):
self.midRound = True
self.lastMidRound = self.midRound
if self.midRound:
self.enemyCtrl.roundCleanup()
self.canvas.itemconfig(self.waveNoticeText, text="Round #{0}".format(self.enemyCtrl.round + 1),
state="normal")
self.canvas.coords(self.waveNoticeText, (self.window.midx, self.window.midy / 2))
self.bullets = self.clipSize
self.canvas.after(2000, lambda: self.canvas.itemconfigure(self.waveNoticeText, state="hidden"))
self.tower.showTower()
self.enemyCtrl.startRound()
else:
self.tower.hideTower()
self.canvas.itemconfig(self.waveNoticeText, text="Press space to start next round.",
state="normal")
self.canvas.coords(self.waveNoticeText, (self.window.midx, self.window.midy / 2))
# Check if need to resize
if self.health != self.lastHealth:
self.lastHealth = self.health
if self.shrinkWin:
self.window.shrinkWindow()
self.updateLocations()
# Check if dead
if self.health <= 0:
self.gameRunning = False
self.endScreen()
self.tk.update() # Update everything
except TclError as e: # handle closed window
print(e)
print("You have closed the window!")
self.gameRunning = False
self.quit()
def setBackgroundRainbow(self): # Menu sets rainbow
if self.currColor == "Rainbow":
self.setBackgroundColor("Default")
else:
self.currColor = "Rainbow"
self.handleRainbow()
def handleRainbow(self): # Checks and updates rainbow
if self.currColor == "Rainbow":
self.setBackgroundColor(self.RAINBOW_LIST[self.rainbowOn])
self.rainbowOn += 1
self.rainbowOn %= 25
self.canvas.after(1000, self.handleRainbow)
def pauseGame(self, *args): # Pause/Unpause
if self.gamePaused:
self.gamePaused = False
self.enemyCtrl.unpauseEnemies()
else:
self.gamePaused = True
def updateLocations(self): # Done after window shrink to move things properly
self.tower.moveTower()
self.enemyCtrl.winShrinkMove()
def queueResize(self):
if self.gameRunning:
self.waitForResize = True
self.infoManager.displayInfo("Window will be resized after", "this round is completed.")
else:
self.window.customiseButton()
def setSeed(self):
seed = simpledialog.askinteger("Pick Seed", "Please put in a seed to replicate the randomness in the game.")
if seed is not None:
self.seed = seed
self.canvas.itemconfigure(self.seedTxt, text='Current Seed: ' + str(seed))
def toggleInfoWin(self, *args):
if self.gameRunning:
self.infoManager.toggleWin()
if len(args) == 0:
self.enemyCtrl.unpauseEnemies()
def toggleBetweenRoundsPause(self):
self.stopBetweenRounds = not self.stopBetweenRounds
self.window.editConfig("settings", "stopBetweenRounds", self.stopBetweenRounds)
self.enemyCtrl.unpauseEnemies()
def easterEgg(self, *args):
print('How DARE you press the "e" key!')
if self.gameRunning:
self.infoManager.displayInfo('How DARE you press the "e" key!')
def expandWindow(self, *args):
if self.points < 20:
print("That item is too expensive.")
self.infoManager.displayInfo("That item is too expensive.")
return
self.points -= 20
self.shrinkWin = False
self.window.shrinkWindow(1) # sets window to full size
self.updateLocations()
self.tk.after(20000, self.stopExpandWindow) # Currently for 20 secs
def stopExpandWindow(self):
self.shrinkWin = True
if self.gameRunning:
self.window.shrinkWindow()
self.updateLocations()
def startReload(self, clickloc):
if self.gameRunning:
self.refillTimer = time.time() + 1.5
self.bullets = 0
self.infoManager.reloading = True
else:
self.clickChecked = False
self.clickX = clickloc.x
self.clickY = clickloc.y
def startRound(self, *args):
self.midRound = True
def setBackgroundColor(self, colorIn=None, readingConfig=False):
if colorIn is None: # Called from menu
color = colorchooser.askcolor()
else: # Called in code
if colorIn == "Default":
color = ["eh", None]
else:
color = ["eh", colorIn]
if color[1] is None:
self.canvas.configure(bg=self.defBackground)
if len(self.difficulties) != 0:
[item.config(bg=self.defBackground) for item in self.difficulties]
self.currColor = "Default"
else:
self.canvas.configure(bg=color[1])
if len(self.difficulties) != 0:
[item.config(bg=color[1]) for item in self.difficulties]
if self.currColor != "Rainbow" or colorIn is None:
self.currColor = color[1]
if not readingConfig:
self.window.updateConfig()
self.enemyCtrl.unpauseEnemies()
self.tk.update_idletasks()
def changeVolume(self):
self.volumeSet = -1
tk2 = Tk() # Make window for volume controls
canvas = Canvas(tk2, width=150, height=45)
tk2.resizable(width=False, height=False)
tk2.geometry('+' + str(int(self.window.midox)) + '+' + str(int(self.window.midoy)))
canvas.pack()
# Make volume controls
mv = Scale(tk2, from_=0, to=100, orient=HORIZONTAL, label="Music Volume %")
mv.set(mixer.music.get_volume() * 100)
mv.pack()
ev = Scale(tk2, from_=0, to=100, orient=HORIZONTAL, label="Effects Volume %")
ev.set(mixer.Sound.get_volume(self.enemyCtrl.shotSound) * 100)
ev.pack()
tv = Scale(tk2, from_=0, to=100, orient=HORIZONTAL, label="Master Volume %")
tv.set(mixer.music.get_volume() * 100)
tv.pack()
Button(tk2, text='Set', command=self.setVolume).pack()
lastTv = tv.get()
while self.volumeSet == -1: # set by button press command
try:
self.tk.update()
tk2.update()
if lastTv != tv.get(): # Master controls both
lastTv = tv.get()
ev.set(lastTv)
mv.set(lastTv)
except TclError: # If window was closed, leave the while loop
self.volumeSet = -2
if self.volumeSet != -2: # -2 is closed, /100 to make 0-1
mixer.music.set_volume(mv.get() / 100)
self.setEffectsVolume(ev.get() / 100)
self.window.editConfig("settings", "musicVolume", str(mv.get()))
self.window.editConfig("settings", "musicVolume", str(ev.get()))
try:
tk2.destroy()
except TclError: # Window was destroyed before, can't be destroyed again
pass
self.enemyCtrl.unpauseEnemies()
def setVolume(self):
self.volumeSet = 0 # changes the value to allow the volume setting loop to escape
def setEffectsVolume(self, volume): # volume must be a float from 0 to 1 inclusive
mixer.Sound.set_volume(self.enemyCtrl.stepSound, volume)
mixer.Sound.set_volume(self.enemyCtrl.shotSound, volume)
def mouseClick(self, clickloc):
if self.gameRunning:
if not self.gamePaused:
self.enemyCtrl.checkClickedLocation(clickloc.x, clickloc.y)
def stopGame(self, *args):
if self.gameRunning:
self.gameRunning = False
self.tk.destroy()
def quit(self, restarting=False): # Handles being quit
try:
if self.gameRunning:
self.infoManager.gameOver()
self.gameRunning = False
self.enemyCtrl.killAllEnemies()
self.canvas.destroy()
self.tk.destroy()
except TclError as e:
print(e)
if pygameInstalled:
mixer.quit()
if not restarting:
sys.exit()
class Tower:
def __init__(self, gamein, windowIn):
self.game = gamein
self.window = windowIn
self.towerImg = PhotoImage(file='Images/towerImg.gif')
self.tower = self.game.canvas.create_image(self.window.midx, self.window.midy, image=self.towerImg)
self.hideTower()
def moveTower(self):
self.game.canvas.coords(self.tower, (self.window.midx, self.window.midy))
def startGame(self):
self.showTower()
def remove(self):
self.game.canvas.delete(self.tower)
def hideTower(self):
self.game.canvas.itemconfigure(self.tower, state='hidden')
def showTower(self):
self.game.canvas.itemconfigure(self.tower, state='normal')
def restart(gameIn):
global game
if gameIn.window.windowCustomised:
gameIn.quit(True)
gameIn.tk = Tk()
game = Game()
if __name__ == "__main__":
game = Game()