-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
338 lines (268 loc) · 13.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
import pygame
import sys
from pygame.locals import *
from server import *
from virus import *
from menu import *
import time
from maps import *
class Game(object):
def __init__ (self):
done = False
width = 1300
height = 700
screen = pygame.display.set_mode((width, height))
background_colour = (150,150,150)
score_red = 200
score_blue = 200
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}
score_red_t = 0.0
score_blue_t = 0.0
score_dec_b = 0.0
score_dec_r = 0.0
paused = False
#Map init
gamemap = Map(screen, menu.map)
Player1 = gamemap.Player1
Player2 = gamemap.Player2
comps = gamemap.comps
#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]
def play(self):
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("Score = " + 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("Score = " + 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), "Increase Virus Limit = 2000","Firewall = " + 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>")
pygame.display.flip()
##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
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:
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:
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:
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
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
#Quitting
if event.type == pygame.QUIT:
done = True
elif event.type == KEYDOWN and event.key== K_ESCAPE:
done = True
#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()
comp.onoff()
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
for comp in comps:
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))
score_red += int(score_red_t + score_dec_r)
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)