-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_objects.py
More file actions
574 lines (546 loc) · 23.2 KB
/
game_objects.py
File metadata and controls
574 lines (546 loc) · 23.2 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
import pygame
import math
import random
import os
from enum import Enum
from game_engine import Dimension, load_image, load_sound, Animation, ParticleSystem, TILE_SIZE
# Player class
class Player:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = TILE_SIZE - 4
self.height = TILE_SIZE - 4
self.vel_x = 0
self.vel_y = 0
self.max_vel_x = 6
self.max_vel_y = 10
self.acceleration = 0.5
self.friction = 0.85
self.can_jump = False
self.dimension = Dimension.NORMAL
self.ethereal_objects = [] # Objects player can pass through in ethereal dimension
self.metal_objects = [] # Objects player is attracted to in magnetic dimension
self.shift_cooldown = 0
self.health = 100
self.max_health = 100
self.invincible = 0
self.score = 0
self.facing_right = True
self.is_jumping = False
self.is_falling = False
self.is_idle = True
self.particle_system = ParticleSystem()
self.character_type = "default" # Can be "default", "mario", "ninja", "robot"
# Load sounds
self.jump_sound = load_sound("jump.wav")
self.collect_sound = load_sound("collect.wav")
self.hurt_sound = load_sound("hurt.wav")
self.dimension_shift_sound = load_sound("dimension_shift.wav")
# Load animations
self.animations = {}
self.load_animations()
self.current_animation = "idle_normal"
def load_animations(self):
dimensions = ["normal", "inverse", "ethereal", "time", "magnetic"]
states = ["idle", "run", "jump", "fall"]
try:
base_path = os.path.dirname(os.path.abspath(__file__))
character_file = f"{self.character_type}.png"
character_path = os.path.join(base_path, "assets", "images", character_file)
if os.path.exists(character_path):
print(f"Loading character sprite: {character_path}")
character_sheet = load_image(character_file)
for dimension_idx, dimension in enumerate(dimensions):
for state_idx, state in enumerate(states):
key = f"{state}_{dimension}"
frames = []
for i in range(4):
x = i * 40
y = (state_idx * len(dimensions) + dimension_idx) * 40
frame = pygame.Surface((40, 40), pygame.SRCALPHA)
frame.blit(character_sheet, (0, 0), (x, y, 40, 40))
frames.append(frame)
self.animations[key] = Animation(frames, 8)
print(f"Successfully loaded {self.character_type} animations")
return
except Exception as e:
print(f"Failed to load character sprite sheet: {e}")
# Fallback: try to load individual animation files or use colored rectangles
for dimension in dimensions:
for state in states:
key = f"{state}_{dimension}"
try:
frames = []
for i in range(1, 5):
frame_path = f"player/{self.character_type}_{key}_{i}.png"
frames.append(load_image(frame_path))
if frames:
self.animations[key] = Animation(frames, 8)
continue
except Exception as e:
print(f"Failed to load animation {frame_path}: {e}")
try:
frames = []
for i in range(1, 5):
frame_path = f"player/{key}_{i}.png"
frames.append(load_image(frame_path))
if frames:
self.animations[key] = Animation(frames, 8)
continue
except Exception as e:
print(f"Failed to load animation {frame_path}: {e}")
# Placeholder colored rectangles
color = self.get_dimension_color(dimension)
frames = []
for i in range(4):
surf = pygame.Surface((self.width, self.height), pygame.SRCALPHA)
pygame.draw.rect(surf, color, (0, 0, self.width, self.height))
if state == "idle" and i == 2:
pygame.draw.rect(surf, (255, 255, 255), (self.width//4, self.height//4, 2, 2))
pygame.draw.rect(surf, (255, 255, 255), (3*self.width//4, self.height//4, 2, 2))
elif state == "run":
leg_height = self.height // 3
leg_offset = (i % 2) * 4 - 2
pygame.draw.rect(surf, (0, 0, 0), (self.width//4, self.height-leg_height+leg_offset, 4, leg_height))
pygame.draw.rect(surf, (0, 0, 0), (3*self.width//4, self.height-leg_height-leg_offset, 4, leg_height))
elif state == "jump":
pygame.draw.ellipse(surf, (0, 0, 0), (self.width//4, self.height-10, self.width//2, 8))
elif state == "fall":
pygame.draw.ellipse(surf, (0, 0, 0), (self.width//4, self.height-6, self.width//2, 4))
frames.append(surf)
self.animations[key] = Animation(frames, 8)
def change_character(self, character_type):
self.character_type = character_type
self.animations = {}
self.load_animations()
def get_dimension_color(self, dimension_name):
if dimension_name == "normal":
return (0, 0, 255)
elif dimension_name == "inverse":
return (255, 0, 0)
elif dimension_name == "ethereal":
return (128, 0, 128)
elif dimension_name == "time":
return (255, 255, 0)
elif dimension_name == "magnetic":
return (0, 255, 255)
return (255, 255, 255)
def update_animation_state(self):
dimension_name = self.dimension.name.lower()
if abs(self.vel_x) > 0.5:
self.is_idle = False
state = "run"
elif self.is_jumping:
self.is_idle = False
state = "jump"
elif self.is_falling:
self.is_idle = False
state = "fall"
else:
self.is_idle = True
state = "idle"
self.current_animation = f"{state}_{dimension_name}"
if self.current_animation not in self.animations:
self.current_animation = f"{state}_normal"
def update(self, walls, platforms, dimension_portals, collectibles, hazards=None, powerups=None):
if hazards is None:
hazards = []
if powerups is None:
powerups = []
if self.dimension == Dimension.NORMAL:
gravity = 0.5
self.max_vel_x = 6
elif self.dimension == Dimension.INVERSE:
gravity = -0.5
self.max_vel_x = 6
elif self.dimension == Dimension.ETHEREAL:
gravity = 0.5
self.max_vel_x = 5
elif self.dimension == Dimension.TIME:
gravity = 0.25
self.max_vel_x = 3
elif self.dimension == Dimension.MAGNETIC:
gravity = 0.5
self.max_vel_x = 5
self.apply_magnetic_attraction()
self.vel_y += gravity
if self.vel_y > self.max_vel_y:
self.vel_y = self.max_vel_y
elif self.vel_y < -self.max_vel_y:
self.vel_y = -self.max_vel_y
self.vel_x *= self.friction
if abs(self.vel_x) < 0.1:
self.vel_x = 0
self.x += self.vel_x
self.check_collision_x(walls, platforms)
self.y += self.vel_y
self.check_collision_y(walls, platforms)
if self.vel_x > 0:
self.facing_right = True
elif self.vel_x < 0:
self.facing_right = False
if self.vel_y < 0:
self.is_jumping = True
self.is_falling = False
elif self.vel_y > 0:
self.is_jumping = False
self.is_falling = True
else:
self.is_jumping = False
self.is_falling = False
for portal in dimension_portals:
if self.collides_with(portal) and self.shift_cooldown <= 0:
old_dimension = self.dimension
self.dimension = portal.target_dimension
self.shift_cooldown = 30
self.dimension_shift_sound.play()
self.particle_system.create_explosion(
self.x + self.width // 2,
self.y + self.height // 2,
self.get_dimension_color(self.dimension.name.lower()),
30, 4
)
for collectible in collectibles[:]:
if self.collides_with(collectible):
collectibles.remove(collectible)
self.score += collectible.value
self.collect_sound.play()
self.particle_system.create_explosion(
collectible.rect.centerx,
collectible.rect.centery,
collectible.color,
15, 3
)
if self.invincible <= 0:
for hazard in hazards:
if self.collides_with(hazard):
self.take_damage(hazard.damage)
if self.x < hazard.rect.centerx:
self.vel_x = -5
else:
self.vel_x = 5
self.vel_y = -5
for powerup in powerups[:]:
if self.collides_with(powerup):
powerups.remove(powerup)
powerup.apply_effect(self)
if self.shift_cooldown > 0:
self.shift_cooldown -= 1
if self.invincible > 0:
self.invincible -= 1
self.update_animation_state()
self.particle_system.update()
def apply_magnetic_attraction(self):
for obj in self.metal_objects:
dx = obj.rect.centerx - (self.x + self.width // 2)
dy = obj.rect.centery - (self.y + self.height // 2)
distance = math.sqrt(dx * dx + dy * dy)
if distance > 20:
force = 100 / (distance * distance)
self.vel_x += (dx / distance) * force
self.vel_y += (dy / distance) * force
def check_collision_x(self, walls, platforms):
player_rect = pygame.Rect(self.x, self.y, self.width, self.height)
for wall in walls:
if self.dimension == Dimension.ETHEREAL and wall in self.ethereal_objects:
continue
if player_rect.colliderect(wall.rect):
if self.vel_x > 0:
self.x = wall.rect.left - self.width
elif self.vel_x < 0:
self.x = wall.rect.right
self.vel_x = 0
def check_collision_y(self, walls, platforms):
self.can_jump = False
player_rect = pygame.Rect(self.x, self.y, self.width, self.height)
for wall in walls:
if self.dimension == Dimension.ETHEREAL and wall in self.ethereal_objects:
continue
if player_rect.colliderect(wall.rect):
if self.vel_y > 0:
self.y = wall.rect.top - self.height
self.can_jump = True
self.is_falling = False
elif self.vel_y < 0:
self.y = wall.rect.bottom
self.vel_y = 0
for platform in platforms:
if player_rect.colliderect(platform.rect):
if self.dimension == Dimension.NORMAL and self.vel_y > 0:
self.y = platform.rect.top - self.height
self.can_jump = True
self.is_falling = False
self.vel_y = 0
elif self.dimension == Dimension.INVERSE and self.vel_y < 0:
self.y = platform.rect.bottom
self.can_jump = True
self.is_falling = False
self.vel_y = 0
def jump(self):
if self.can_jump:
if self.dimension == Dimension.NORMAL:
self.vel_y = -32
elif self.dimension == Dimension.INVERSE:
self.vel_y = 28
elif self.dimension == Dimension.ETHEREAL:
self.vel_y = -36
elif self.dimension == Dimension.TIME:
self.vel_y = -18
elif self.dimension == Dimension.MAGNETIC:
self.vel_y = -34
self.jump_sound.play()
self.is_jumping = True
self.can_jump = False
def move_left(self):
if self.vel_x > -self.max_vel_x:
self.vel_x -= self.acceleration
if self.vel_x < -self.max_vel_x:
self.vel_x = -self.max_vel_x
def move_right(self):
if self.vel_x < self.max_vel_x:
self.vel_x += self.acceleration
if self.vel_x > self.max_vel_x:
self.vel_x = self.max_vel_x
def take_damage(self, amount):
if self.invincible <= 0:
self.health -= amount
self.invincible = 60
self.hurt_sound.play()
self.particle_system.create_explosion(
self.x + self.width // 2,
self.y + self.height // 2,
(255, 0, 0, 128),
20, 3
)
def heal(self, amount):
self.health = min(self.health + amount, self.max_health)
def collides_with(self, obj):
player_rect = pygame.Rect(self.x, self.y, self.width, self.height)
return player_rect.colliderect(obj.rect)
def draw(self, screen, camera=None):
# Get current animation frame
if self.current_animation in self.animations:
current_frame = self.animations[self.current_animation].update()
else:
# Fallback if animation not found
current_frame = self.animations["idle_normal"].update()
# Flip if facing left
if not self.facing_right:
current_frame = pygame.transform.flip(current_frame, True, False)
# Draw with or without camera
if camera:
rect = camera.apply(pygame.Rect(self.x, self.y, self.width, self.height))
screen.blit(current_frame, rect)
# Draw particles with camera offset
for particle in self.particle_system.particles:
pos = (int(particle['x'] - camera.rect.x), int(particle['y'] - camera.rect.y))
pygame.draw.circle(screen, particle['color'], pos, particle['size'])
else:
screen.blit(current_frame, (self.x, self.y))
self.particle_system.draw(screen)
# Flash when invincible
if self.invincible > 0 and self.invincible % 6 < 3:
if camera:
rect = camera.apply(pygame.Rect(self.x, self.y, self.width, self.height))
pygame.draw.rect(screen, (255, 255, 255, 128), rect, 2)
else:
pygame.draw.rect(screen, (255, 255, 255, 128), (self.x, self.y, self.width, self.height), 2)
# Wall class
class Wall:
def __init__(self, x, y, width, height, color=(255, 255, 255)):
self.rect = pygame.Rect(x, y, width, height)
self.color = color
self.is_ethereal = False
self.is_metal = False
try:
self.image = load_image("wall.png")
self.image = pygame.transform.scale(self.image, (width, height))
except:
self.image = None
def draw(self, screen, camera=None):
if camera:
rect = camera.apply(self.rect)
else:
rect = self.rect
if self.image:
screen.blit(self.image, rect)
else:
pygame.draw.rect(screen, self.color, rect)
# Platform class
class Platform(Wall):
def __init__(self, x, y, width, height=10):
super().__init__(x, y, width, height, (0, 255, 0))
try:
self.image = load_image("platform.png")
self.image = pygame.transform.scale(self.image, (width, height))
except:
self.image = None
# Dimension Portal class
class DimensionPortal:
def __init__(self, x, y, target_dimension):
self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
self.target_dimension = target_dimension
self.animation_timer = 0
if target_dimension == Dimension.NORMAL:
self.color = (0, 0, 255)
elif target_dimension == Dimension.INVERSE:
self.color = (255, 0, 0)
elif target_dimension == Dimension.ETHEREAL:
self.color = (128, 0, 128)
elif target_dimension == Dimension.TIME:
self.color = (255, 255, 0)
elif target_dimension == Dimension.MAGNETIC:
self.color = (0, 255, 255)
try:
self.image = load_image(f"portal_{target_dimension.name.lower()}.png")
except:
self.image = None
if not self.image:
self.frames = []
for i in range(8):
surf = pygame.Surface((TILE_SIZE, TILE_SIZE), pygame.SRCALPHA)
size = TILE_SIZE - 4 + math.sin(i / 4 * math.pi) * 4
pygame.draw.circle(surf, self.color, (TILE_SIZE // 2, TILE_SIZE // 2), int(size // 2))
pygame.draw.circle(surf, (255, 255, 255), (TILE_SIZE // 2, TILE_SIZE // 2), int(size // 4))
self.frames.append(surf)
self.animation = Animation(self.frames, 5)
def update(self):
self.animation_timer = (self.animation_timer + 1) % 360
def draw(self, screen, camera=None):
if camera:
rect = camera.apply(self.rect)
else:
rect = self.rect
if self.image:
angle = self.animation_timer
rotated_image = pygame.transform.rotate(self.image, angle)
new_rect = rotated_image.get_rect(center=rect.center)
screen.blit(rotated_image, new_rect)
else:
current_frame = self.animation.update()
screen.blit(current_frame, rect)
# Collectible class
class Collectible:
def __init__(self, x, y, value=10):
self.rect = pygame.Rect(x + TILE_SIZE//4, y + TILE_SIZE//4, TILE_SIZE//2, TILE_SIZE//2)
self.color = (255, 215, 0)
self.value = value
self.animation_timer = random.randint(0, 360)
try:
self.image = load_image("collectible.png")
except:
self.image = None
def update(self):
self.animation_timer = (self.animation_timer + 1) % 360
def draw(self, screen, camera=None):
offset_y = math.sin(self.animation_timer / 30) * 3
if camera:
rect = camera.apply(self.rect)
rect.y += offset_y
else:
rect = pygame.Rect(self.rect.x, self.rect.y + offset_y, self.rect.width, self.rect.height)
if self.image:
screen.blit(self.image, rect)
else:
pygame.draw.rect(screen, self.color, rect)
shine_pos = (rect.x + rect.width * 0.7, rect.y + rect.height * 0.3)
pygame.draw.circle(screen, (255, 255, 255), shine_pos, 2)
# Hazard class
class Hazard:
def __init__(self, x, y, width, height, damage=10):
self.rect = pygame.Rect(x, y, width, height)
self.color = (255, 50, 50)
self.damage = damage
self.animation_timer = 0
try:
self.image = load_image("hazard.png")
self.image = pygame.transform.scale(self.image, (width, height))
except:
self.image = None
def update(self):
self.animation_timer = (self.animation_timer + 1) % 60
def draw(self, screen, camera=None):
if camera:
rect = camera.apply(self.rect)
else:
rect = self.rect
if self.image:
screen.blit(self.image, rect)
else:
pulse = abs(math.sin(self.animation_timer / 10)) * 50
color = (255, 50 + pulse, 50)
pygame.draw.rect(screen, color, rect)
for i in range(0, rect.width, 10):
pygame.draw.line(screen, (0, 0, 0), (rect.x + i, rect.y), (rect.x + i + 5, rect.y + rect.height), 2)
# Powerup class
class Powerup:
def __init__(self, x, y, powerup_type="health"):
self.rect = pygame.Rect(x, y, TILE_SIZE, TILE_SIZE)
self.powerup_type = powerup_type
self.animation_timer = random.randint(0, 360)
if powerup_type == "health":
self.color = (0, 255, 0)
elif powerup_type == "speed":
self.color = (0, 255, 255)
elif powerup_type == "jump":
self.color = (255, 255, 0)
elif powerup_type == "invincibility":
self.color = (255, 255, 255)
try:
self.image = load_image(f"powerup_{powerup_type}.png")
except:
self.image = None
def update(self):
self.animation_timer = (self.animation_timer + 1) % 360
def draw(self, screen, camera=None):
offset_y = math.sin(self.animation_timer / 30) * 3
scale = 1.0 + math.sin(self.animation_timer / 15) * 0.1
if camera:
base_rect = camera.apply(self.rect)
else:
base_rect = self.rect.copy()
rect = pygame.Rect(base_rect.x, base_rect.y + offset_y, base_rect.width, base_rect.height)
if self.image:
scaled_size = (int(self.image.get_width() * scale), int(self.image.get_height() * scale))
scaled_image = pygame.transform.scale(self.image, scaled_size)
scaled_rect = scaled_image.get_rect(center=rect.center)
screen.blit(scaled_image, scaled_rect)
else:
pygame.draw.circle(screen, self.color, rect.center, int(rect.width // 2 * scale))
pygame.draw.circle(screen, (255, 255, 255), rect.center, int(rect.width // 4 * scale))
def apply_effect(self, player):
if self.powerup_type == "health":
player.heal(25)
elif self.powerup_type == "speed":
player.max_vel_x *= 1.5
elif self.powerup_type == "jump":
pass # Implement jump boost if needed
elif self.powerup_type == "invincibility":
player.invincible = 300
# Moving Platform class
class MovingPlatform(Platform):
def __init__(self, x, y, width, height, x_range=0, y_range=100, speed=1):
super().__init__(x, y, width, height)
self.start_x = x
self.start_y = y
self.x_range = x_range
self.y_range = y_range
self.speed = speed
self.progress = 0
def update(self):
self.progress = (self.progress + self.speed) % 360
if self.x_range > 0:
self.rect.x = self.start_x + math.sin(math.radians(self.progress)) * self.x_range
if self.y_range > 0:
self.rect.y = self.start_y + math.sin(math.radians(self.progress)) * self.y_range
# Metal Wall class for magnetic dimension
class MetalWall(Wall):
def __init__(self, x, y, width, height):
super().__init__(x, y, width, height, (192, 192, 192))
self.is_metal = True
try:
self.image = load_image("metal_wall.png")
self.image = pygame.transform.scale(self.image, (width, height))
except:
self.image = None