-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
334 lines (292 loc) · 13.3 KB
/
main.py
File metadata and controls
334 lines (292 loc) · 13.3 KB
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
import arcade
import random
import math
from enum import Enum
from typing import Optional
SCREEN_WIDTH = 1400
SCREEN_HEIGHT = 900
SCREEN_TITLE = "Epic Platformer - All Assets Edition"
GRAVITY = 1.0
MOVEMENT_SPEED = 5
JUMP_SPEED = 20
TILE_SIZE = 64
PLATFORM_Y_OFFSET = -20
BACKGROUND_FILES = [
"assets/Sprites/Backgrounds/Default/background_color_trees.png",
"assets/Sprites/Backgrounds/Default/background_color_hills.png",
"assets/Sprites/Backgrounds/Default/background_color_desert.png"
]
class CharacterColor(Enum):
BEIGE = "beige"
GREEN = "green"
PINK = "pink"
PURPLE = "purple"
YELLOW = "yellow"
class AnimationState(Enum):
IDLE = "idle"
WALK = "walk"
JUMP = "jump"
class Player(arcade.Sprite):
def __init__(self, x, y, color: CharacterColor):
super().__init__()
self.character_color = color
self.velocity_y = 0
self.is_jumping = False
self.animation_state = AnimationState.IDLE
self.cur_texture_index = 0
self.animation_counter = 0
self.facing_right = True
path = f"assets/Sprites/Characters/Default/character_{color.value}_"
self.idle_textures = [arcade.load_texture(path + "idle.png")]
self.walk_textures = [arcade.load_texture(path + "walk_a.png"),
arcade.load_texture(path + "walk_b.png")]
self.jump_texture = arcade.load_texture(path + "jump.png")
self.idle_textures_flipped = [arcade.load_texture(path + "idle.png")]
self.walk_textures_flipped = [arcade.load_texture(path + "walk_a.png"),
arcade.load_texture(path + "walk_b.png")]
self.jump_texture_flipped = arcade.load_texture(path + "jump.png")
self.texture = self.idle_textures[0]
self.scale = 1.5
self.center_x = x
self.center_y = y
def update_animation(self, delta_time: float = 1/60):
self.animation_counter += 1
if self.facing_right:
if self.animation_state == AnimationState.IDLE:
self.texture = self.idle_textures[0]
elif self.animation_state == AnimationState.WALK:
if self.animation_counter % 10 == 0:
self.cur_texture_index = (self.cur_texture_index + 1) % len(self.walk_textures)
self.texture = self.walk_textures[self.cur_texture_index]
elif self.animation_state == AnimationState.JUMP:
self.texture = self.jump_texture
else:
if self.animation_state == AnimationState.IDLE:
self.texture = self.idle_textures_flipped[0]
elif self.animation_state == AnimationState.WALK:
if self.animation_counter % 10 == 0:
self.cur_texture_index = (self.cur_texture_index + 1) % len(self.walk_textures_flipped)
self.texture = self.walk_textures_flipped[self.cur_texture_index]
elif self.animation_state == AnimationState.JUMP:
self.texture = self.jump_texture_flipped
class Enemy(arcade.Sprite):
def __init__(self, x, y, texture_paths: list[str]):
textures = [arcade.load_texture(p) for p in texture_paths]
super().__init__(textures[0], scale=1.5)
self.textures_list = textures
self.textures_list_flipped = [arcade.load_texture(p) for p in texture_paths]
self.center_x = x
self.center_y = y
self.direction = random.choice([-1,1])
self.speed = 2
self.start_x = x
self.patrol_range = 200
self.cur_texture_index = 0
self.animation_counter = 0
def update(self):
self.center_x += self.speed * self.direction
if abs(self.center_x - self.start_x) > self.patrol_range:
self.direction *= -1
self.animation_counter += 1
if self.animation_counter % 15 == 0:
self.cur_texture_index = (self.cur_texture_index + 1) % len(self.textures_list)
self.texture = self.textures_list[self.cur_texture_index] if self.direction>0 else self.textures_list_flipped[self.cur_texture_index]
class Collectible(arcade.Sprite):
def __init__(self, x, y, texture_path, points, ctype="coin"):
super().__init__(texture_path, scale=1.2)
self.center_x = x
self.center_y = y
self.start_y = y
self.points = points
self.bob_offset = random.uniform(0, math.pi*2)
self.ctype = ctype
def update(self):
self.bob_offset += 0.08
self.center_y = self.start_y + 10 * math.sin(self.bob_offset)
class Platform(arcade.Sprite):
def __init__(self, x, y, platform_type="terrain_grass_block"):
super().__init__(f"assets/Sprites/Tiles/Default/{platform_type}.png", scale=1)
self.center_x = x
self.center_y = y
class Hazard(arcade.Sprite):
def __init__(self, x, y, hazard_type="block_spikes"):
super().__init__(f"assets/Sprites/Tiles/Default/{hazard_type}.png", scale=1)
self.center_x = x
self.center_y = y
self.move_direction = 1
def update(self):
self.center_x += 0.5*self.move_direction
if self.center_x < 50 or self.center_x > SCREEN_WIDTH-50:
self.move_direction *= -1
class Decoration(arcade.Sprite):
def __init__(self, x, y, decoration_type="bush"):
super().__init__(f"assets/Sprites/Tiles/Default/{decoration_type}.png", scale=1.3)
self.center_x = x
self.center_y = y
class Door(arcade.Sprite):
def __init__(self, x, y):
super().__init__("assets/Sprites/Tiles/Default/door_closed.png", scale=1.5)
self.center_x = x
self.center_y = y
class PlatformerGame(arcade.Window):
def __init__(self):
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
arcade.set_background_color(arcade.color.SKY_BLUE)
self.background_index = 0
self.background_sprite_list = arcade.SpriteList()
self.background_sprite: Optional[arcade.Sprite] = None
self.player_list = arcade.SpriteList()
self.platform_list = arcade.SpriteList()
self.enemy_list = arcade.SpriteList()
self.collectible_list = arcade.SpriteList()
self.hazard_list = arcade.SpriteList()
self.decoration_list = arcade.SpriteList()
self.door_list = arcade.SpriteList()
self.physics_engine: Optional[arcade.PhysicsEnginePlatformer] = None
self.player: Optional[Player] = None
self.keys_pressed = set()
self.score = 0
self.lives = 5
self.heart_count = 5
self.level = 1
self.sounds = {}
self.load_sounds()
self.setup()
self.camera = arcade.Camera2D()
def load_sounds(self):
files = {
"coin":"assets/Sounds/sfx_coin.ogg",
"gem":"assets/Sounds/sfx_gem.ogg",
"jump":"assets/Sounds/sfx_jump.ogg",
"hurt":"assets/Sounds/sfx_hurt.ogg",
"magic":"assets/Sounds/sfx_magic.ogg"
}
for k,v in files.items():
try:self.sounds[k]=arcade.load_sound(v)
except:pass
def play_sound(self,name):
if name in self.sounds:
arcade.play_sound(self.sounds[name],volume=0.4)
def setup(self):
self.player_list.clear()
self.platform_list.clear()
self.enemy_list.clear()
self.collectible_list.clear()
self.hazard_list.clear()
self.decoration_list.clear()
self.door_list.clear()
self.background_sprite_list.clear()
# Full-screen background
self.background_sprite = arcade.Sprite(BACKGROUND_FILES[self.background_index])
self.background_sprite.width = SCREEN_WIDTH
self.background_sprite.height = SCREEN_HEIGHT
self.background_sprite.center_x = SCREEN_WIDTH//2
self.background_sprite.center_y = SCREEN_HEIGHT//2
self.background_sprite_list.append(self.background_sprite)
# Player
self.player = Player(150,300,random.choice(list(CharacterColor)))
self.player_list.append(self.player)
self.physics_engine = arcade.PhysicsEnginePlatformer(self.player, self.platform_list, GRAVITY)
# Ground platforms
for x in range(0, SCREEN_WIDTH, TILE_SIZE):
self.platform_list.append(Platform(x + TILE_SIZE//2, TILE_SIZE//2 + PLATFORM_Y_OFFSET))
# Floating platforms
floating_positions = [(300,230),(450,230),(600,230),(200,380),(400,380),(600,380),(800,380),
(300,530),(700,530),(1000,530),(500,680),(900,680)]
for x,y in floating_positions:
self.platform_list.append(Platform(x,y+PLATFORM_Y_OFFSET))
# Enemies
enemy_textures = ["assets/Sprites/Enemies/Default/frog_idle.png",
"assets/Sprites/Enemies/Default/frog_jump.png"]
self.enemy_list.append(Enemy(500,300,enemy_textures))
self.enemy_list.append(Enemy(800,300,enemy_textures))
# Collectibles
self.collectible_list.append(Collectible(400,350,"assets/Sprites/Tiles/Default/coin_gold.png",10))
self.collectible_list.append(Collectible(700,550,"assets/Sprites/Tiles/Default/gem_blue.png",25))
self.collectible_list.append(Collectible(600,250,"assets/Sprites/Tiles/Default/heart.png",0,ctype="heart"))
# Hazards
self.hazard_list.append(Hazard(600,TILE_SIZE//2,"block_spikes"))
# Decorations
self.decoration_list.append(Decoration(350,TILE_SIZE//2,"bush"))
self.decoration_list.append(Decoration(900,TILE_SIZE//2,"bush"))
# Door
self.door_list.append(Door(1200,TILE_SIZE//2))
def on_draw(self):
self.clear()
self.camera.use()
self.background_sprite_list.draw()
self.platform_list.draw()
self.enemy_list.draw()
self.collectible_list.draw()
self.hazard_list.draw()
self.decoration_list.draw()
self.door_list.draw()
self.player_list.draw()
arcade.draw_text(f"LEVEL {self.level}", 20 + self.camera.position[0], SCREEN_HEIGHT-40 + self.camera.position[1], arcade.color.WHITE, 24)
arcade.draw_text(f"SCORE: {self.score}", SCREEN_WIDTH//2-80 + self.camera.position[0], SCREEN_HEIGHT-40 + self.camera.position[1], arcade.color.YELLOW, 24)
arcade.draw_text(f"LIVES: {self.lives}", SCREEN_WIDTH-200 + self.camera.position[0], SCREEN_HEIGHT-40 + self.camera.position[1], arcade.color.RED, 24)
arcade.draw_text(f"HEARTS: {self.heart_count}", SCREEN_WIDTH-200 + self.camera.position[0], SCREEN_HEIGHT-70 + self.camera.position[1], arcade.color.PINK, 24)
arcade.draw_text("W/E to Move | S to Jump", 20 + self.camera.position[0], SCREEN_HEIGHT-70 + self.camera.position[1], arcade.color.LIGHT_GRAY, 14)
def on_update(self, dt):
if not self.player: return
if self.player.center_x < 0:
self.player.center_x = 0
self.player.change_x = 0
if arcade.key.W in self.keys_pressed:
self.player.change_x = MOVEMENT_SPEED
self.player.facing_right = True
self.player.animation_state = AnimationState.WALK
elif arcade.key.E in self.keys_pressed:
self.player.change_x = -MOVEMENT_SPEED
self.player.facing_right = False
self.player.animation_state = AnimationState.WALK
else:
self.player.animation_state = AnimationState.IDLE
if self.physics_engine:
self.physics_engine.update()
self.player.update_animation()
for e in self.enemy_list: e.update()
for c in self.collectible_list: c.update()
for h in self.hazard_list: h.update()
hazards_hit = arcade.check_for_collision_with_list(self.player,self.hazard_list)
if hazards_hit:
self.lives -= 1
self.heart_count -= 1
self.reset_player()
self.play_sound("hurt")
if self.heart_count<=0: arcade.close_window()
collectibles_hit = arcade.check_for_collision_with_list(self.player,self.collectible_list)
for c in collectibles_hit:
self.score+=c.points
c.remove_from_sprite_lists()
if c.ctype=="heart": self.heart_count+=1
self.play_sound("coin" if "coin" in c.texture.path else "gem")
if arcade.check_for_collision_with_list(self.player,self.door_list):
self.level+=1
self.play_sound("magic")
self.setup()
if self.player.center_y<0:
self.lives-=1
self.heart_count-=1
self.reset_player()
self.play_sound("hurt")
if self.heart_count<=0: arcade.close_window()
# Camera follow
cam_x = max(0, self.player.center_x - SCREEN_WIDTH // 3)
self.camera.position = (cam_x, 0)
def on_key_press(self,key,modifiers):
self.keys_pressed.add(key)
if key==arcade.key.S and self.player and self.physics_engine and self.physics_engine.can_jump():
self.player.change_y = JUMP_SPEED
self.player.animation_state = AnimationState.JUMP
self.play_sound("jump")
def on_key_release(self,key,modifiers):
self.keys_pressed.discard(key)
def reset_player(self):
self.player.center_x=150 # pyright: ignore[reportOptionalMemberAccess]
self.player.center_y=300 # pyright: ignore[reportOptionalMemberAccess]
def main():
game = PlatformerGame()
arcade.run()
if __name__=="__main__":
main()