-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
476 lines (371 loc) · 16.2 KB
/
main.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
#!usr/bin/env python
import pygame
import sys
from resource import *
from pygame.locals import *
from server import *
from virus import *
from menu import *
import time
from maps import *
pygame.init()
pygame.font.init()
smallfont = pygame.font.Font(None, 20)
scorefont = pygame.font.Font(None, 45)
bigfont = pygame.font.Font(None, 250)
midfont = pygame.font.Font(None, 80)
#Globals
quitting = False
done = False
width = 1300
height = 700
screen = pygame.display.set_mode((width, height))
background_colour = (150,150,150)
score_red = 200
score_blue = 200
score_win = 10000
virus_count_red = 0
virus_count_blue = 0
bomb_count_red = 0
bomb_count_blue = 0
target_dict = {'x_red':0, 'y_red':0, 'w_red':0, 'b_red':0, 'x_blue':0, 'y_blue':0, 'w_blue':0, 'b_blue':0}
x_start = 3
y_start = 0
w_start = 0
b_start = 0
bounds = screen.get_rect()
score_red_t = 0.0
score_blue_t = 0.0
score_dec_b = 0.0
score_dec_r = 0.0
paused = False
victory = False
mute = False
sfx_boom = load_sfx("explosion")
sfx_add = load_sfx("addvirus")
sfx_minus = load_sfx("minusvirus")
boom = load_image("Ultimate_Explosion")
while not quitting:
#Menu
menu = Menu(screen, mute)
if menu.map == None:
sys.exit()
#Map init
gamemap = Map(screen, menu.map)
Player1 = gamemap.Player1
Player2 = gamemap.Player2
comps = gamemap.comps
gates = gamemap.gates
#Virus
red_xvirus = XVirus(1, Player1)
blue_xvirus = XVirus(2, Player2)
red_yvirus = YVirus(1, Player1)
blue_yvirus = YVirus(2, Player2)
red_wvirus = Wall_Virus(1, Player1)
blue_wvirus = Wall_Virus(2, Player2)
red_bvirus = Bomb_Virus(1, Player1)
blue_bvirus = Bomb_Virus(2, Player2)
red_virus_list = [red_xvirus, red_yvirus, red_wvirus, red_bvirus]
blue_virus_list = [blue_xvirus, blue_yvirus, blue_wvirus, blue_bvirus]
#starting virus counts
Player1.red_viruses = {'x':x_start, 'y':y_start, 'w':w_start, 'b':b_start}
Player2.blue_viruses = {'x':x_start, 'y':y_start, 'w':w_start, 'b':b_start}
while not done:
screen.fill(background_colour)
#Connects computers
for com1 in comps:
for com2 in comps:
x1, y1 = com1.rect.center
x2, y2 = com2.rect.center
if (x2-x1)**2 + (y2-y1)**2 <= (2*com1.cnct_range)**2 and com2 not in com1.connected_list and com1 != com2:
com1.connected_list.append(com2)
#Drawing
for comp in comps:
comp.draw_circle()
comp.draw_lines()
for comp in comps:
comp.draw_rect()
pygame.draw.rect(screen, background_colour, pygame.Rect((350,0), (200,50)))
text = scorefont.render("Pings = " + str(score_red), True, (255,0,0))
loc = text.get_rect()
loc.topleft = (300,0)
screen.blit(text,loc)
pygame.draw.rect(screen, background_colour, pygame.Rect((800,0), (200,50)))
text = scorefont.render("Pings = " + str(score_blue), True, (0,0,255))
loc = text.get_rect()
loc.topleft = (750,0)
screen.blit(text,loc)
#Costs
pygame.draw.rect(screen, (0,0,0), pygame.Rect((515, 0),(215,65)), 2)
cost_list = ["Costs:","X-Virus = " + str(red_xvirus.cost), "Y-Virus = " + str(red_yvirus.cost), "Hardware Upgrade = 2000","Fyrewall = " + str(red_wvirus.cost), "Ad Bomb = " + str(red_bvirus.cost)]
for cost in cost_list:
text = smallfont.render(cost, True, (0, 0, 0))
loc = text.get_rect()
if cost_list.index(cost) == 0:
x,y = 620, 10
loc.center = (x, y)
elif cost_list.index(cost)<4 and cost_list.index(cost)>0:
x, y = 520, 5
loc.topleft = (x, y + loc.height*cost_list.index(cost))
else:
x,y = 620, 5
loc.topleft = (x, y + loc.height*(cost_list.index(cost)-3))
screen.blit(text, loc)
Player1.scrn_change()
Player2.scrn_change()
for comp in comps:
comp.draw_text()
comp.draw_limits()
if paused:
draw_controls(screen, background_colour, (0,0,0), "<Paused>")
#Victory by points
if score_red >= score_win:
victory = True
Player1.lose = True
Player2.lose = True
text = bigfont.render("Red Wins!", True, (255,0,0))
loc = text.get_rect()
loc.center = bounds.center
screen.blit(text,loc)
text = midfont.render("Press 'esc' to return to menu", True, (255,0,0))
loc = text.get_rect()
(x, y)= bounds.center
loc.center = (x, y + 100)
screen.blit(text,loc)
if score_blue >= score_win:
victory = True
Player2.lose = True
Player1.lose = True
text = bigfont.render("Blue Wins!", True, (0,0,255))
loc = text.get_rect()
loc.center = bounds.center
screen.blit(text,loc)
text = midfont.render("Press 'esc' to return to menu", True, (0,0,255))
loc = text.get_rect()
(x, y)= bounds.center
loc.center = (x, y + 100)
screen.blit(text,loc)
pygame.display.flip()
##Music
if mute == False:
if Player1.lose or Player2.lose:
if victory == False:
pygame.mixer.music.stop()
play_song("Epic Sax")
victory = True
elif Player1.lose and Player2.lose:
if victory == False:
pygame.mixer.music.stop()
play_song("Epic Sax")
victory = True
elif pygame.mixer.music.get_busy() == False:
play_song("Shadows_Vigilante")
#Muting
if mute == True:
pygame.mixer.music.pause()
if mute == False:
pygame.mixer.music.unpause()
##Keys
for event in pygame.event.get():
#Pause
if event.type == KEYDOWN and event.key == K_SPACE:
if paused == False:
paused = True
elif paused == True:
paused = False
#Quitting
if event.type == pygame.QUIT:
done = True
quitting = True
elif event.type == KEYDOWN and event.key== K_ESCAPE:
done = True
#Muting
if event.type == KEYDOWN and event.key== K_m:
if mute == False:
mute = True
elif mute == True:
mute = False
if not paused:
#Adding XVirus red
if not Player1.lose:
if event.type == KEYDOWN and event.key == K_w:
if score_red >= red_xvirus.cost and sum(Player1.red_viruses.values()) < Player1.virus_max:
# sfx_add.stop()
# sfx_add.play()
Player1.red_viruses['x'] +=1
score_red -= red_xvirus.cost
#Subtracting XVirus red
if event.type == KEYDOWN and event.key == K_s:
if Player1.red_viruses['x'] > 0:
# sfx_minus.stop()
# sfx_minus.play()
Player1.red_viruses['x'] -=1
#Adding YVirus red
if event.type == KEYDOWN and event.key == K_e:
if score_red >= red_yvirus.cost and sum(Player1.red_viruses.values()) < Player1.virus_max:
Player1.red_viruses['y'] += 1
score_red -= red_yvirus.cost
#Subtracting YVirus red
if event.type == KEYDOWN and event.key == K_d:
if Player1.red_viruses['y'] > 0:
Player1.red_viruses['y'] -=1
#Adding Wall_Virus red
if event.type == KEYDOWN and event.key == K_q:
if score_red >= red_wvirus.cost and sum(Player1.red_viruses.values()) < Player1.virus_max:
Player1.red_viruses['w'] += 1
score_red -= red_wvirus.cost
#Subtracting Wall_Virus red
if event.type == KEYDOWN and event.key == K_a:
if Player1.red_viruses['w'] > 0:
Player1.red_viruses['w'] -=1
#Adding Bomb_Virus red
if event.type == KEYDOWN and event.key == K_r:
if score_red >= red_bvirus.cost and bomb_count_red == 0 and sum(Player1.red_viruses.values()) < Player1.virus_max:
Player1.add_virus(red_bvirus)
score_red -= red_bvirus.cost
else:
for comp in comps:
if comp.red_viruses['b'] == 1 and comp.off_state == False:
sfx_boom.stop()
sfx_boom.play()
comp.off_state = True
comp.scrn_colour = (100,100,100)
comp.blue_viruses = {'x':0, 'y':0, 'w':0, 'b':0}
comp.red_viruses ['b'] = 0
#Limit Up Red
if event.type == KEYDOWN and event.key == K_f:
if score_red >= 2000:
Player1.virus_max += 1
score_red -= 2000
#Adding XVirus blue
if not Player2.lose:
if event.type == KEYDOWN and event.key == K_u:
if score_blue >= blue_xvirus.cost and sum(Player2.blue_viruses.values()) < Player2.virus_max:
Player2.blue_viruses['x'] +=1
score_blue -= blue_xvirus.cost
#Subtracting XVirus blue
if event.type == KEYDOWN and event.key == K_j:
if Player2.blue_viruses['x'] > 0:
Player2.blue_viruses['x'] -=1
#Adding YVirus blue
if event.type == KEYDOWN and event.key == K_i:
if score_blue >= blue_yvirus.cost and sum(Player2.blue_viruses.values()) < Player2.virus_max:
Player2.blue_viruses['y'] +=1
score_blue -= blue_yvirus.cost
#Subtracting YVirus blue
if event.type == KEYDOWN and event.key == K_k:
if Player2.blue_viruses['y'] > 0:
Player2.blue_viruses['y'] -=1
#Adding Wall_Virus blue
if event.type == KEYDOWN and event.key == K_y:
if score_blue >= blue_wvirus.cost and sum(Player2.blue_viruses.values()) < Player2.virus_max:
Player2.blue_viruses['w'] +=1
score_blue -= blue_wvirus.cost
#Subtracting Wall_Virus blue
if event.type == KEYDOWN and event.key == K_h:
if Player2.blue_viruses['w'] > 0:
Player2.blue_viruses['w'] -=1
#Adding Bomb_Virus blue
if event.type == KEYDOWN and event.key == K_o:
if score_blue >= blue_bvirus.cost and bomb_count_blue == 0 and sum(Player2.blue_viruses.values()) < Player2.virus_max:
Player2.add_virus(blue_bvirus)
score_blue -= blue_bvirus.cost
else:
for comp in comps:
if comp.blue_viruses['b'] == 1 and comp.off_state == False:
comp.off_state = True
sfx_boom.stop()
sfx_boom.play()
comp.scrn_colour = (100,100,100)
comp.red_viruses = {'x':0, 'y':0, 'w':0, 'b':0}
comp.blue_viruses['b'] = 0
#Up Limit blue
if event.type == KEYDOWN and event.key == K_l:
if score_blue >= 2000:
Player2.virus_max += 1
score_blue -= 2000
#Update
if not paused:
for comp in comps:
target_dict = {'x_red':0, 'y_red':0, 'w_red':0, 'b_red':0, 'x_blue':0, 'y_blue':0, 'w_blue':0, 'b_blue':0}
comp.wipe()
if comp.blue_viruses['w'] == 0 and not Player1.lose:
for virus in red_virus_list:
virus.spread(comp)
for virus in red_virus_list:
if virus.type == 'y' and not Player1.lose:
virus.target(comp, target_dict)
if comp.red_viruses['w'] == 0 and not Player2.lose:
for virus in blue_virus_list:
virus.spread(comp)
for virus in blue_virus_list:
if virus.type == 'y' and not Player2.lose:
virus.target(comp, target_dict)
#Killing the target list
if comp.off_state == False:
comp.red_viruses['x'] -= target_dict['x_red']
if comp.red_viruses['x'] < 0: #So minimum = 0
comp.red_viruses['x'] = 0
comp.red_viruses['y'] -= target_dict['y_red']
if comp.red_viruses['y'] < 0:
comp.red_viruses['y'] = 0
comp.red_viruses['w'] -= target_dict['w_red']
if comp.red_viruses['w'] < 0:
comp.red_viruses['w'] = 0
comp.red_viruses['b'] -= target_dict['b_red']
if comp.red_viruses['b'] < 0:
comp.red_viruses['b'] = 0
comp.blue_viruses['x'] -= target_dict['x_blue']
if comp.blue_viruses['x'] < 0:
comp.blue_viruses['x'] = 0
comp.blue_viruses['y'] -= target_dict['y_blue']
if comp.blue_viruses['y'] < 0:
comp.blue_viruses['y'] = 0
comp.blue_viruses['w'] -= target_dict['w_blue']
if comp.blue_viruses['w'] < 0:
comp.blue_viruses['w'] = 0
comp.blue_viruses['b'] -= target_dict['b_blue']
if comp.blue_viruses['b'] < 0:
comp.blue_viruses['b'] = 0
bred = 0
bblue = 0
#On/off
for gate in gates:
gate.onoff()
for comp in comps:
if comp.off_state:
comp.onoff()
#Bomb Stuff
bred += comp.red_viruses['b']
bblue += comp.blue_viruses['b']
bomb_count_red = bred
bomb_count_blue = bblue
for virus in red_virus_list:
virus.check_home()
for virus in blue_virus_list:
virus.check_home()
for comp in comps:
if comp.off_state == False:
part_score_red, part_score_blue = comp.score_check()
score_red_t += part_score_red
score_blue_t += part_score_blue
score_dec_r += (score_red_t - int(score_red_t))
score_dec_b += (score_blue_t - int(score_blue_t))
if victory == False:
score_red += int(score_red_t + score_dec_r)
if victory == False:
score_blue += int(score_blue_t + score_dec_b)
score_blue_t = 0
score_red_t = 0
if score_dec_r >= .9:
score_dec_r = 0
if score_dec_b >= .9:
score_dec_b = 0
time.sleep(.2)
done = False
victory = False
score_red = 200
score_blue = 200
for event in pygame.event.get():
if event.type == KEYDOWN and event.key == K_ESCAPE:
quit = True