/
world.py
378 lines (306 loc) · 13.3 KB
/
world.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
import pygame
import random
import sys
from ctrlAltDel import *
from virus import Virus, DoubleVirus, TripleVirus
from popup import Popup
from worm import Worm
from entity import Entity
from player import Player
from hud import Hud
from boss import Boss
from safeMode import SafeMode
from background import Bkg
from constants import *
class World (object):
def __init__(self):
super(World, self).__init__()
self.frames = 1
# this is normal difficulty by default
# easy would prolly be .7ish and hard.. 1.3 or so?
self.difficulty = 0.7
self.spawnFreq = 10
self.scrollPosition = 0
self.scrollSpeed = 3
self.endPosition = FRAMES_UNTIL_BOSS * self.scrollSpeed
self.startScreen = True
self.helpScreen = False
self.difficultyScreen = False
self.bossMode = False
self.gameOver = False
self.winScreen = False
self.score = 0
self.call_popcorn = 0
self.boss_music=False
self.holdingLeftMouseButton = False
#for keeping track of for the win screen!
self.numEnemiesAppeared = 0
self.numEnemiesDestroyed = 0
self.numViruses = 0
self.numWorms = 0
self.numPopUps = 0
self.playerGroup = pygame.sprite.GroupSingle()
self.enemies = pygame.sprite.Group()
self.pickups = pygame.sprite.Group()
self.bullets = pygame.sprite.Group()
self.mines = pygame.sprite.Group()
self.backgrounds = pygame.sprite.Group()
self.spawnBkg()
# sprite groups listed in draw order (lowest sprite get drawn first)
self.spriteGroups = [self.backgrounds, self.mines, self.pickups, self.bullets, self.enemies, self.playerGroup]
self.hud = Hud()
def spawnWorld(self):
self.StartScreen = False
playerPos = (PLAY_WIDTH / 2, SCREEN_HEIGHT - 50)
pygame.mouse.set_pos(playerPos)
self.player = Player(self, playerPos)
self.playerGroup.add(self.player)
pygame.mixer.music.load("data/music/Main.mp3")
pygame.mixer.music.play()
self.hud.createHudElements()
self.frames = 1
self.scrollPosition = 0
self.scrollSpeed = 3
def spawnBkg(self):
bkg = Bkg()
self.backgrounds.add(bkg)
def spawnCtrl(self):
ctrl = Ctrl()
self.pickups.add(ctrl)
def spawnAlt(self):
alt = Alt()
self.pickups.add(alt)
def spawnDel(self):
d = Del()
self.pickups.add(d)
def spawnVirus(self, position):
self.numEnemiesAppeared += 1
enemy = Virus(position)
self.enemies.add(enemy)
def spawnDoubleVirus(self, position):
self.numEnemiesAppeared += 1
enemy = DoubleVirus(self, position)
self.enemies.add(enemy)
def spawnTripleVirus(self):
self.numEnemiesAppeared += 1
enemy = TripleVirus(self)
self.enemies.add(enemy)
def spawnWorm(self):
self.numEnemiesAppeared += 1
position1 = random.randint(195,PLAY_WIDTH-50)
position2 = 10
position = position1, position2
enemy = Worm(position,'head')
self.enemies.add(enemy)
for i in range(random.randint(1,5)):
position1 -= 32
position = position1, position2
enemy = Worm(position,'body')
self.enemies.add(enemy)
position1 -= 32
position = position1, position2
enemy = Worm(position,'tail')
self.enemies.add(enemy)
def spawnPopup(self):
self.numEnemiesAppeared += 1
enemy = Popup()
while len(pygame.sprite.spritecollide(enemy, self.playerGroup, False)) > 0:
enemy = Popup()
self.enemies.add(enemy)
def spawnBoss(self):
self.boss_music=True
self.numEnemiesAppeared += 1
pygame.mixer.music.load("data/music/Boss.mp3")
pygame.mixer.music.play(-1)
self.boss = Boss((PLAY_WIDTH / 2, -200))
self.enemies.add(self.boss)
for tentacle in self.boss.tentacles:
for link in tentacle.links:
self.enemies.add(link)
def spawnSafe(self):
safe = SafeMode()
self.pickups.add(safe)
def leftMouseButtonDown(self):
self.holdingLeftMouseButton = True
self.player.shoot(self.bullets)
def leftMouseButtonUp(self):
self.holdingLeftMouseButton = False
def rightMouseButtonDown(self):
self.player.quarantine(self.mines)
sound = pygame.mixer.Sound("data/sounds/minedeploy.wav")
sound.set_volume(.25)
sound.play()
def enemy_popcorn(self):
if len(self.enemy_list)==1:
self.call_popcorn=0
else:
self.enemy_list[1].takeHit(self.enemy_list[1].health)
self.enemy_list.remove(self.enemy_list[1])
def destroy_all_enemies(self):
self.enemy_list=[0]
if self.player.destroyAllEnemies:
self.player.after_destroy_all()
sound = pygame.mixer.Sound("data/sounds/destroyall.wav")
sound.play()
for enemy in self.enemies:
if enemy.typeofenemy == 'boss' or enemy.typeofenemy == 'link':
enemy.takeHit(1)
else:
if enemy.typeofenemy is 'virus':
self.numViruses += 1
if enemy.typeofenemy is 'worm':
self.numWorms += 1
if enemy.typeofenemy is 'pop up window':
self.numPopUps += 1
self.numEnemiesDestroyed += 1
self.enemy_list.insert(1,enemy)
#enemy.takeHit(enemy.health)
self.call_popcorn=self.frames
def update(self):
if self.holdingLeftMouseButton:
if self.frames % 5 == 0:
self.player.shoot(self.bullets)
for group in self.spriteGroups:
group.update()
self.hud.update(self)
# Test player-enemy collisions
for enemy in pygame.sprite.spritecollide(self.player, self.enemies, False):
if not enemy.dead and not enemy.animName == 'spawn':
if not enemy.typeofenemy == 'boss':
enemy.takeHit(enemy.health)
if not self.player.invincible:
self.player.decrease_life()
self.score -= 100
if enemy.dead:
if enemy.typeofenemy is 'virus':
self.numViruses += 1
self.numEnemiesDestroyed += 1
if enemy.typeofenemy is 'worm':
self.numWorms += 1
self.numEnemiesDestroyed += 1
if enemy.typeofenemy is 'pop up window':
self.numPopUps += 1
self.numEnemiesDestroyed += 1
# Test player-pickup collisions
for pickup in pygame.sprite.spritecollide(self.player, self.pickups, False):
pickup.on_collision(self.player)
sound = pygame.mixer.Sound("data/sounds/keypickup.wav")
sound.play()
# Test enemy-playerBullet collisions
for enemy, bullets in pygame.sprite.groupcollide(self.enemies, self.bullets, False, False).items():
for bullet in bullets:
enemy.collideWithBullet(bullet)
if enemy.dead:
self.player.increase_powerup(5)
if enemy.typeofenemy == "worm":
self.numWorms += 1
self.numEnemiesDestroyed += 1
self.score += 25
elif enemy.typeofenemy == "virus":
self.numViruses += 1
self.numEnemiesDestroyed += 1
self.score += 10
elif enemy.typeofenemy == "popup":
self.numEnemiesDestroyed += 1
self.numPopUps += 1
self.score += 15
# Test enemy-mine collisions
for mine in self.mines:
if mine.exploding:
for enemy in pygame.sprite.spritecollide(mine, self.enemies, False):
enemy.takeHit(1)
if enemy.dead:
if enemy.typeofenemy is 'virus':
self.numViruses += 1
self.numEnemiesDestroyed += 1
if enemy.typeofenemy is 'worm':
self.numWorms += 1
self.numEnemiesDestroyed += 1
if enemy.typeofenemy is 'pop up window':
self.numPopUps += 1
self.numEnemiesDestroyed += 1
# Check enemies offscreen, popups doing damage
for enemy in self.enemies:
if enemy.rect.top > SCREEN_HEIGHT and not enemy.typeofenemy == 'link':
enemy.kill()
self.score -= 10
self.player.decrease_health(1)
sound = pygame.mixer.Sound("data/sounds/CPUload.wav")
sound.play()
if enemy.typeofenemy == "popup":
if enemy.frame % 20 == 0:
self.score -= 1
# Check bullets offscreen
for bullet in self.bullets:
if bullet.rect.bottom < 0:
bullet.kill()
# Check backgrounds offscreen
for bkg in self.backgrounds:
if bkg.rect.top > SCREEN_HEIGHT:
bkg.kill()
if self.frames % 360 == 0:
self.spawnBkg()
if self.spawnFreq is not 1:
if self.frames % (500) == 0:
if self.spawnFreq <= 1:
self.spawnFreq = 1
elif self.spawnFreq > 1:
self.spawnFreq -= self.difficulty
# Spawn more enemies
baseSpawnRate = (self.spawnFreq * 5 + random.randint(1, 5))
#print self.difficulty, baseSpawnRate, self.spawnFreq
if not self.bossMode:
if self.frames % baseSpawnRate < 1:
seed = random.randint(1, 10)
if(seed < 7):
self.spawnVirus(0)
elif(seed < 10):
self.spawnDoubleVirus(0)
else:
self.spawnTripleVirus()
if self.frames % (baseSpawnRate * random.randint(3, 6)) < 1:
self.spawnWorm()
if self.frames % (baseSpawnRate * random.randint(2, 5)) < 1:
self.spawnPopup()
if self.frames % (baseSpawnRate * random.randint(18, 21)) < 1:
self.player.destroyAllEnemies = True
if self.frames % (baseSpawnRate * random.randint(1, 4)) < 1:
self.spawnCtrl()
if self.frames % (baseSpawnRate * random.randint(5, 8)) < 1:
self.spawnAlt()
if self.frames % (baseSpawnRate * random.randint(9, 12)) < 1:
self.spawnDel()
if self.frames % (baseSpawnRate * random.randint(4, 7)) < 1:
self.spawnSafe()
# Check if main music has ended, and loop music should start
if not pygame.mixer.music.get_busy():
if not self.boss_music:
pygame.mixer.music.load("data/music/Mainloop.mp3")
pygame.mixer.music.play(-1)
elif self.frames == FRAMES_UNTIL_BOSS + MUSIC_LENGTH_BOSS:
pygame.mixer.music.load("data/music/Bossloop.mp3")
pygame.mixer.music.play(-1)
#Check for calling enemy_popcorn
if self.call_popcorn != 0 and self.call_popcorn<self.frames-10:
self.enemy_popcorn()
# Check gameover
if self.player.lives == 0:
self.gameOver = True
pygame.mixer.music.stop()
# Check win screen!
if self.bossMode and self.boss.dead and self.boss.anim.done:
if not self.winScreen:
self.numEnemiesDestroyed += 1
self.winScreen = True
# Scroll level
self.scrollPosition += self.scrollSpeed
self.scrollPosition = min(self.scrollPosition, self.endPosition)
if self.scrollPosition == self.endPosition and not self.bossMode:
self.bossMode = True
self.spawnBoss()
self.frames += 1
def draw(self, screen):
for group in self.spriteGroups:
for sprite in group:
sprite.draw(screen)
self.hud.draw(screen, PLAY_WIDTH)