-
Notifications
You must be signed in to change notification settings - Fork 1
/
level.py
376 lines (304 loc) · 15.6 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
import pygame
from support import import_csv_layout, import_cut_graphics, resource_path
from settings import tile_size, screen_height, screen_width
from tiles import Tile, StaticTile, Crate, Coin, Palm, WindTurbine
from enemy import Enemy
from decoration import Sky, Water, Clouds, Level1Background, Level2Background
from player import Player
from particles import ParticleEffect
from game_data import levels
class Level:
def __init__(self, current_level, surface, create_overworld, change_coins, change_health):
# general setup
self.display_surface = surface
self.world_shift = 0
self.current_x = None
# audio
self.coin_sound = pygame.mixer.Sound(resource_path('./audio/pickupCoin.ogg'))
self.stomp_sound = pygame.mixer.Sound(resource_path('./audio/land.ogg'))
# overworld connection
self.create_overworld = create_overworld
self.current_level = current_level
level_data = levels[self.current_level]
self.new_max_level = level_data['unlock']
# player
player_layout = import_csv_layout(level_data['player'])
self.player = pygame.sprite.GroupSingle()
self.goal = pygame.sprite.GroupSingle()
self.player_setup(player_layout, change_health)
# user interface
self.change_coins = change_coins
# dust
self.dust_sprite = pygame.sprite.GroupSingle()
self.player_on_ground = False
# explosion particles
self.explosion_sprites = pygame.sprite.Group()
# terrain setup
terrain_layout = import_csv_layout(level_data['terrain'])
self.terrain_sprites = self.create_tile_group(terrain_layout, 'terrain')
# grass setup
grass_layout = import_csv_layout(level_data['grass'])
self.grass_sprites = self.create_tile_group(grass_layout, 'grass')
# crates
crate_layout = import_csv_layout(level_data['crates'])
self.crate_sprites = self.create_tile_group(crate_layout, 'crates')
# coins
coin_layout = import_csv_layout(level_data['coins'])
self.coin_sprites = self.create_tile_group(coin_layout, 'coins')
# foreground palms
fg_palm_layout = import_csv_layout(level_data['fg palms'])
self.fg_palm_sprites = self.create_tile_group(fg_palm_layout, 'fg palms')
# background palms
bg_palm_layout = import_csv_layout(level_data['bg palms'])
self.bg_palm_sprites = self.create_tile_group(bg_palm_layout, 'bg palms')
# enemy
enemy_layout = import_csv_layout(level_data['enemies'])
self.enemy_sprites = self.create_tile_group(enemy_layout, 'enemies')
# constraint
constraint_layout = import_csv_layout(level_data['constraints'])
self.constraint_sprites = self.create_tile_group(constraint_layout, 'constraints')
# turbines
if self.current_level == 1:
turbine_layout = import_csv_layout(level_data['wind_turbines'])
self.turbine_sprites = self.create_tile_group(turbine_layout, 'wind_turbines')
if self.current_level == 2:
terrain_deco_layout = import_csv_layout(level_data['terrain_deco'])
self.terrain_deco_sprites = self.create_tile_group(terrain_deco_layout, 'terrain_deco')
# decoration
self.sky = Sky(8)
self.level1background = Level1Background()
self.level2background = Level2Background()
level_width = len(terrain_layout[0]) * tile_size
self.water = Water(screen_height - 20, level_width)
self.clouds = Clouds(400, level_width, 30)
def create_tile_group(self, layout, type):
sprite_group = pygame.sprite.Group()
for row_index, row in enumerate(layout):
for col_index, val in enumerate(row):
if val != '-1':
x = col_index * tile_size
y = row_index * tile_size
if type == 'terrain' and self.current_level == 0:
terrain_tile_list = import_cut_graphics(resource_path('./assets/Terrain/terrain_tiles.png'))
tile_surface = terrain_tile_list[int(val)]
sprite = StaticTile(tile_size, x, y, tile_surface)
sprite_group.add(sprite)
if type == 'terrain' and self.current_level == 1:
terrain_tile_list = import_cut_graphics(resource_path('./assets/Terrain/terrain_tiles_2.png'))
tile_surface = terrain_tile_list[int(val)]
sprite = StaticTile(tile_size, x, y, tile_surface)
sprite_group.add(sprite)
if type == 'terrain' and self.current_level == 2:
terrain_tile_list = import_cut_graphics(resource_path('./assets/Terrain/terrain_tiles.png'))
tile_surface = terrain_tile_list[int(val)]
sprite = StaticTile(tile_size, x, y, tile_surface)
sprite_group.add(sprite)
if type == 'grass':
grass_tile_list = import_cut_graphics(resource_path('./assets/decoration/grass/grass.png'))
tile_surface = grass_tile_list[int(val)]
sprite = StaticTile(tile_size, x, y, tile_surface)
sprite_group.add(sprite)
if type == 'crates':
sprite = Crate(tile_size, x, y)
sprite_group.add(sprite)
if type == 'coins':
if val == '0': sprite = Coin(tile_size, x, y, resource_path('./assets/coins/gold'), 5)
if val == '1': sprite = Coin(tile_size, x, y, resource_path('./assets/coins/silver'), 1)
sprite_group.add(sprite)
if type == 'fg palms':
if val == '0': sprite = Palm(tile_size, x, y, resource_path('./assets/Terrain/palm_small'), 38)
if val == '1': sprite = Palm(tile_size, x, y, resource_path('./assets/Terrain/palm_large'), 64)
sprite_group.add(sprite)
if type == 'bg palms':
sprite = Palm(tile_size, x, y, resource_path('./assets/Terrain/palm_bg'), 64)
sprite_group.add(sprite)
if type == 'enemies':
sprite = Enemy(tile_size, x, y)
sprite_group.add(sprite)
if type == 'constraints':
sprite = Tile(tile_size, x, y)
sprite_group.add(sprite)
if type == 'wind_turbines':
sprite = WindTurbine(tile_size, x, y, resource_path('./assets/Terrain/wind_turbine'), 64)
sprite_group.add(sprite)
if type == 'terrain_deco' and self.current_level == 2:
terrain_deco_tile_list = import_cut_graphics(resource_path('./assets/Terrain/terrain_tiles_3.png'))
tile_surface = terrain_deco_tile_list[int(val)]
sprite = StaticTile(tile_size, x, y, tile_surface)
sprite_group.add(sprite)
return sprite_group
def player_setup(self, layout, change_health):
for row_index, row in enumerate(layout):
for col_index, val in enumerate(row):
x = col_index * tile_size
y = row_index * tile_size
if val == '0':
sprite = Player((x, y), self.display_surface, self.create_jump_particles, change_health)
self.player.add(sprite)
if val == '1' and self.current_level == 0:
hat_surface = pygame.image.load(
resource_path('./assets/dog/dog1.png')).convert_alpha()
sprite = StaticTile(tile_size, x, y, hat_surface)
self.goal.add(sprite)
elif val == '1' and self.current_level == 1:
hat_surface = pygame.image.load(
resource_path('./assets/dog/dog2.png')).convert_alpha()
sprite = StaticTile(tile_size, x, y, hat_surface)
self.goal.add(sprite)
elif val == '1' and self.current_level == 2:
hat_surface = pygame.image.load(
resource_path('./assets/dog/dog3.png')).convert_alpha()
sprite = StaticTile(tile_size, x, y, hat_surface)
self.goal.add(sprite)
def enemy_collision_reverse(self):
for enemy in self.enemy_sprites.sprites():
if pygame.sprite.spritecollide(enemy, self.constraint_sprites, False):
enemy.reverse()
def create_jump_particles(self, pos):
if self.player.sprite.facing_right:
pos -= pygame.math.Vector2(10, 5)
else:
pos += pygame.math.Vector2(10, -5)
jump_particle_sprite = ParticleEffect(pos, 'jump')
self.dust_sprite.add(jump_particle_sprite)
def horizontal_movement_collision(self):
player = self.player.sprite
player.collision_rect.x += player.direction.x * player.speed
collidable_sprites = self.terrain_sprites.sprites() + self.crate_sprites.sprites() + self.fg_palm_sprites.sprites()
for sprite in collidable_sprites:
if sprite.rect.colliderect(player.collision_rect):
if player.direction.x < 0:
player.collision_rect.left = sprite.rect.right
player.on_left = True
self.current_x = player.rect.left
elif player.direction.x > 0:
player.collision_rect.right = sprite.rect.left
player.on_right = True
self.current_x = player.rect.right
def vertical_movement_collision(self):
player = self.player.sprite
player.apply_gravity()
collidable_sprites = self.terrain_sprites.sprites() + self.crate_sprites.sprites() + self.fg_palm_sprites.sprites()
for sprite in collidable_sprites:
if sprite.rect.colliderect(player.collision_rect):
if player.direction.y > 0:
player.collision_rect.bottom = sprite.rect.top
player.direction.y = 0
player.on_ground = True
elif player.direction.y < 0:
player.collision_rect.top = sprite.rect.bottom
player.direction.y = 0
player.on_ceiling = True
if player.on_ground and player.direction.y < 0 or player.direction.y > 1:
player.on_ground = False
def scroll_x(self):
player = self.player.sprite
player_x = player.rect.centerx
direction_x = player.direction.x
if player_x < screen_width / 4 and direction_x < 0:
self.world_shift = 8
player.speed = 0
elif player_x > screen_width - (screen_width / 4) and direction_x > 0:
self.world_shift = -8
player.speed = 0
else:
self.world_shift = 0
player.speed = 8
def get_player_on_ground(self):
if self.player.sprite.on_ground:
self.player_on_ground = True
else:
self.player_on_ground = False
def create_landing_dust(self):
if not self.player_on_ground and self.player.sprite.on_ground and not self.dust_sprite.sprites():
if self.player.sprite.facing_right:
offset = pygame.math.Vector2(10, 15)
else:
offset = pygame.math.Vector2(-10, 15)
fall_dust_particle = ParticleEffect(self.player.sprite.rect.midbottom - offset, 'land')
self.dust_sprite.add(fall_dust_particle)
def check_death(self):
if self.player.sprite.rect.top > screen_height:
self.create_overworld(self.current_level, 0)
def check_win(self):
if pygame.sprite.spritecollide(self.player.sprite, self.goal, False):
self.create_overworld(self.current_level, self.new_max_level)
def check_coin_collisions(self):
collided_coins = pygame.sprite.spritecollide(self.player.sprite, self.coin_sprites, True)
if collided_coins:
self.coin_sound.play()
for coin in collided_coins:
self.change_coins(coin.value)
def check_enemy_collisions(self):
enemy_collisions = pygame.sprite.spritecollide(self.player.sprite, self.enemy_sprites, False)
if enemy_collisions:
for enemy in enemy_collisions:
enemy_center = enemy.rect.centery
enemy_top = enemy.rect.top
player_bottom = self.player.sprite.rect.bottom
if enemy_top < player_bottom < enemy_center and self.player.sprite.direction.y >= 0:
self.player.sprite.direction.y = -35
self.stomp_sound.play()
explosion_sprite = ParticleEffect(enemy.rect.center, 'enemy_death')
self.explosion_sprites.add(explosion_sprite)
enemy.kill()
else:
self.player.sprite.get_damage()
def run(self):
# run the entire game / level
# sky
self.sky.draw(self.display_surface)
self.clouds.draw(self.display_surface, self.world_shift)
if self.current_level == 1:
self.level1background.draw(self.display_surface, self.world_shift)
self.turbine_sprites.draw(self.display_surface)
self.turbine_sprites.update(self.world_shift)
elif self.current_level == 2:
self.level2background.draw(self.display_surface, self.world_shift)
# background palms
self.bg_palm_sprites.update(self.world_shift)
self.bg_palm_sprites.draw(self.display_surface)
# dust particles
self.dust_sprite.update(self.world_shift)
self.dust_sprite.draw(self.display_surface)
# terrain
self.terrain_sprites.update(self.world_shift)
self.terrain_sprites.draw(self.display_surface)
if self.current_level == 2:
self.terrain_deco_sprites.update(self.world_shift)
self.terrain_deco_sprites.draw(self.display_surface)
# enemy
self.enemy_sprites.update(self.world_shift)
self.constraint_sprites.update(self.world_shift)
self.enemy_collision_reverse()
self.enemy_sprites.draw(self.display_surface)
self.explosion_sprites.update(self.world_shift)
self.explosion_sprites.draw(self.display_surface)
# crate
self.crate_sprites.update(self.world_shift)
self.crate_sprites.draw(self.display_surface)
# grass
self.grass_sprites.update(self.world_shift)
self.grass_sprites.draw(self.display_surface)
# coins
self.coin_sprites.update(self.world_shift)
self.coin_sprites.draw(self.display_surface)
# foreground palms
self.fg_palm_sprites.update(self.world_shift)
self.fg_palm_sprites.draw(self.display_surface)
# player sprites
self.player.update()
self.horizontal_movement_collision()
self.get_player_on_ground()
self.vertical_movement_collision()
self.create_landing_dust()
self.scroll_x()
self.player.draw(self.display_surface)
self.goal.update(self.world_shift)
self.goal.draw(self.display_surface)
self.check_death()
self.check_win()
self.check_coin_collisions()
self.check_enemy_collisions()
# water
self.water.draw(self.display_surface, self.world_shift)