/
level.py
384 lines (329 loc) · 12.5 KB
/
level.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
from Enemy import *
from Platform import *
from Enemy_Sprite import *
from Heart_Sprites import *
from Coin_Sprite import *
from Projectile_Sprite import *
class Level:
world_shift = 0
level_limit = 0
def __init__(self, player):
self.background = None
self.enemy_list = pygame.sprite.Group()
self.platform_list = pygame.sprite.Group()
self.health_bar = pygame.sprite.Group()
self.coins = pygame.sprite.Group()
self.coins_needed = pygame.sprite.Group()
self.player = player
self.hardmode = False
self.coinsNeeded
def getEnemies(self):
return self.enemy_list
def getPlatforms(self):
return self.platform_list
def getPlayer(self):
return self.player
def isHardMode(self):
return self.hardmode
# Define number of coins needed to win
def coinsNeeded(self, num):
self.coinsNeeded = num
def draw(self, screen):
screen.fill((0, 0, 255))
screen.blit(self.background, (self.world_shift // 3, 0))
self.platform_list.draw(screen)
self.enemy_list.draw(screen)
self.health_bar.draw(screen)
self.coins.draw(screen)
self.coins_needed.draw(screen)
def update(self):
self.platform_list.update()
self.enemy_list.update()
self.coins.update()
def shift_world(self, shift_x):
self.world_shift += shift_x
for platform in self.platform_list:
platform.rect.x += shift_x
for enemy in self.enemy_list:
enemy.rect.x += shift_x
for coin in self.coins:
coin.rect.x += shift_x
def addEnemies(self, enemies):
for enemy in enemies:
if enemy[0] == BASIC_ENEMY:
type = BasicEnemy()
elif enemy[0] == SHOOTING_ENEMY:
type = ShootingEnemy()
p = Projectile_Sprite(PROJECTILE, type)
if type.getDirection() == "L":
p.rect.x = enemy[1] - 30
p.boundary = p.rect.y - 20
else:
p.rect.x = enemy[1] + 50
p.boundary = p.rect.y + 20
p.origPos = p.rect.x
p.rect.y = enemy[2] + 55
p.player = self.player
p.level = self
self.enemy_list.add(p)
elif enemy[0] == BOSS_ENEMY:
type = BossEnemy()
elif enemy[0] == BOMB:
type = Bomb()
en = Enemy_Sprite(enemy[0], type)
if len(enemy) > 3 and enemy[3] == "R":
en.image = pygame.transform.flip(en.image, True, False)
en.rect.x = enemy[1]
en.rect.y = enemy[2]
self.enemy_list.add(en)
def addCoins(self, lv_coins):
for coin in lv_coins:
c = Coin_Sprite(coin[0])
c.rect.x = coin[1]
c.rect.y = coin[2]
self.coins.add(c)
def addHearts(self):
hearts = []
if self.isHardMode():
for i in range(0, 1):
hearts.append([LITTLE_HEART, (60 * i), 0])
else:
for i in range(0, 3):
hearts.append([LITTLE_HEART, (60 * i), 0])
for heart in hearts:
h = Heart_Sprite(heart[0])
h.rect.x = heart[1]
h.rect.y = heart[2]
self.health_bar.add(h)
def addCoinsNeeded(self):
coins_need = []
for i in range(0, self.coinsNeeded):
if i < 5:
coins_need.append([COIN, (60 * i), 60])
elif 10 > i >= 5:
coins_need.append([COIN, (60 * (i - 5)), 120])
else:
coins_need.append([COIN, (60 * (i - 10)), 180])
for coin in coins_need:
c = Coin_Sprite(coin[0])
c.rect.x = coin[1]
c.rect.y = coin[2]
self.coins_needed.add(c)
def addPlatforms(self, platforms):
for platform in platforms:
block = Platform(platform[0])
block.rect.x = platform[1]
block.rect.y = platform[2]
block.player = self.player
self.platform_list.add(block)
def reset(self):
self.enemy_list = pygame.sprite.Group()
self.platform_list = pygame.sprite.Group()
self.health_bar = pygame.sprite.Group()
self.coins = pygame.sprite.Group()
self.coins_needed = pygame.sprite.Group()
class LevelOne(Level):
def __init__(self, player):
Level.__init__(self, player)
Level.hardmode = False
if self.isHardMode():
self.coinsNeeded(5)
else:
self.coinsNeeded(5)
self.background = pygame.image.load("level1.png").convert()
self.background.set_colorkey((255, 255, 255))
self.level_limit = -2500
platforms = [[GRASS_LEFT, 500, 500],
[GRASS_MIDDLE, 570, 500],
[GRASS_RIGHT, 640, 500],
[GRASS_LEFT, 800, 400],
[GRASS_MIDDLE, 870, 400],
[GRASS_RIGHT, 940, 400],
[GRASS_LEFT, 1000, 500],
[GRASS_MIDDLE, 1070, 500],
[GRASS_RIGHT, 1140, 500],
[STONE_PLATFORM_LEFT, 1120, 280],
[STONE_PLATFORM_MIDDLE, 1190, 280],
[STONE_PLATFORM_RIGHT, 1260, 280]]
enemies = [[BASIC_ENEMY, 600, 420], # Add optional "R" or "L" argument to change facing direction.
[BASIC_ENEMY, 750, 500], # Defaults to left-facing
[BOMB, 1000, 420],
[BOMB, 1200, 500]]
lv_coins = [[COIN, 500, 420],
[COIN, 750, 300],
[COIN, 1200, 200],
[COIN, 1025, 350],
[COIN, 1220, 450]]
self.addEnemies(enemies)
self.addCoins(lv_coins)
self.addCoinsNeeded()
self.addHearts()
self.addPlatforms(platforms)
class LevelTwo(Level):
def __init__(self, player):
Level.__init__(self, player)
Level.hardmode = False
if self.isHardMode():
self.coinsNeeded(7)
else:
self.coinsNeeded(5)
self.background = pygame.image.load("level2.png").convert()
self.background.set_colorkey((255, 255, 255))
self.level_limit = -2500
platforms = [[GRASS_LEFT, 500, 500],
[GRASS_MIDDLE, 570, 500],
[GRASS_RIGHT, 640, 500],
[GRASS_LEFT, 800, 400],
[GRASS_MIDDLE, 870, 400],
[GRASS_RIGHT, 940, 400],
[GRASS_LEFT, 1000, 500],
[GRASS_MIDDLE, 1070, 500],
[GRASS_RIGHT, 1140, 500],
[STONE_PLATFORM_LEFT, 1120, 280],
[STONE_PLATFORM_MIDDLE, 1190, 280],
[STONE_PLATFORM_RIGHT, 1260, 280]]
enemies = [[BASIC_ENEMY, 600, 420], # Add optional "R" or "L" argument to change facing direction.
[BASIC_ENEMY, 750, 500], # Defaults to left-facing
[BOMB, 1000, 420],
[BOSS_ENEMY, 1200, 500]]
lv_coins = [[COIN, 500, 420],
[COIN, 750, 300],
[COIN, 1200, 200],
[COIN, 1025, 350],
[COIN, 1220, 450],
[COIN, 1000, 250],
[COIN, 850, 325]]
self.addEnemies(enemies)
self.addCoins(lv_coins)
self.addCoinsNeeded()
self.addHearts()
self.addPlatforms(platforms)
# Add a custom moving platform
block = MovingPlatform([648, 648, 70, 40])
block.rect.x = 1350
block.rect.y = 280
block.boundary_left = 1350
block.boundary_right = 1600
block.change_x = 1
block.player = self.player
block.level = self
self.platform_list.add(block)
class LevelThree(Level):
def __init__(self, player):
Level.__init__(self, player)
Level.hardmode = False
if self.isHardMode():
self.coinsNeeded(10)
else:
self.coinsNeeded(7)
self.background = pygame.image.load("level3.png").convert()
self.background.set_colorkey((255, 255, 255))
self.level_limit = -2500
platforms = [[GRASS_LEFT, 500, 500],
[GRASS_MIDDLE, 570, 500],
[GRASS_RIGHT, 640, 500],
[GRASS_LEFT, 800, 400],
[GRASS_MIDDLE, 870, 400],
[GRASS_RIGHT, 940, 400],
[GRASS_LEFT, 1000, 500],
[GRASS_MIDDLE, 1070, 500],
[GRASS_RIGHT, 1140, 500],
[STONE_PLATFORM_LEFT, 1120, 280],
[STONE_PLATFORM_MIDDLE, 1190, 280],
[STONE_PLATFORM_RIGHT, 1260, 280]]
enemies = [[BASIC_ENEMY, 600, 420],
[BASIC_ENEMY, 750, 500],
[BOMB, 1000, 420], #add more enemies
[BOMB, 1200, 500]]
#[BOSS_ENEMY , , ],
#[SHOOTING_ENEMY, , ]]
lv_coins = [[COIN, 500, 420],
[COIN, 750, 300],
[COIN, 1200, 200],
[COIN, 1025, 350],
[COIN, 1220, 450],
[COIN, 1000, 250], #fix these numbers
[COIN, 850, 325],
[COIN, 400, 530],
[COIN, 900, 210],
[COIN, 600, 480]]
self.addEnemies(enemies)
self.addCoins(lv_coins)
self.addCoinsNeeded()
self.addHearts()
self.addPlatforms(platforms)
# Add a custom moving platform
block = MovingPlatform([648, 648, 70, 40])
block.rect.x = 1350
block.rect.y = 280
block.boundary_left = 1350
block.boundary_right = 1600
block.change_x = 1
block.player = self.player
block.level = self
self.platform_list.add(block)
# Add a 2nd custom moving platform
block = MovingPlatform([648, 648, 70, 40])
block.rect.x = 1350
block.rect.y = 280
block.boundary_left = 1350
block.boundary_right = 1600
block.change_x = 1
block.player = self.player
block.level = self
self.platform_list.add(block)
class LevelFour(Level):
def __init__(self, player):
Level.__init__(self, player)
Level.hardmode = False
if self.isHardMode():
self.coinsNeeded(10)
else:
self.coinsNeeded(10)
self.background = pygame.image.load("level4.png").convert()
self.background.set_colorkey((255, 255, 255))
self.level_limit = -2500
platforms = [[GRASS_LEFT, 500, 550],
[GRASS_MIDDLE, 570, 550],
[GRASS_RIGHT, 640, 550],
[GRASS_LEFT, 780, 525],
[GRASS_MIDDLE, 850, 525],
[GRASS_RIGHT, 920, 525],
[STONE_PLATFORM_LEFT, 1400, 400],
[STONE_PLATFORM_RIGHT, 1470, 400],
[STONE_PLATFORM_MIDDLE, 1600, 320],
[STONE_PLATFORM_LEFT, 1650, 200],
[STONE_PLATFORM_RIGHT, 1720, 200],
[STONE_PLATFORM_LEFT, 1400, 100],
[STONE_PLATFORM_RIGHT, 1470, 100],
[STONE_PLATFORM_MIDDLE, 1250, 100]]
enemies = [[BASIC_ENEMY, 500, 470], # Add optional "R" or "L" argument to change facing direction.
[BOMB, 780, 445], # Defaults to left-facing
[BOMB, 1470, 320],
[SHOOTING_ENEMY, 1720, 100],
[BOSS_ENEMY, 1400, 10]]
lv_coins = [[COIN, 600, 400],
[COIN, 750, 300],
[COIN, 1400, 330],
[COIN, 1600, 260],
[COIN, 1470, 50],
[COIN, 750, 300],
[COIN, 1300, 530],
[COIN, 1500, 460],
[COIN, 1000, 175],
[COIN, 1250, 50]]
self.addEnemies(enemies)
self.addCoins(lv_coins)
self.addCoinsNeeded()
self.addHearts()
self.addPlatforms(platforms)
# Add a custom moving platform
block = MovingPlatform([648, 648, 70, 40])
block.rect.x = 1000
block.rect.y = 450
block.boundary_left = 1000
block.boundary_right = 1350
block.change_x = 2
block.player = self.player
block.level = self
self.platform_list.add(block)