-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenemy_unicorns.py
More file actions
117 lines (94 loc) · 3.9 KB
/
Copy pathenemy_unicorns.py
File metadata and controls
117 lines (94 loc) · 3.9 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
import os
import pygame
from enemy import Enemy
from spritesheet import SpriteSheet
from assets import Assets # ✅ Import to access unicorn bullet
class UnicornEnemy(Enemy):
def __init__(self, x, y):
super().__init__(x, y)
# Custom stats
self.health = 10
self.max_health = 10
self.damage = 8
self.speed = 1.5
self.score = 15
self.frame_delay = 120
# Sprite settings
self.sprite_cols = 5 # actions
self.sprite_rows = 4 # directions
self.sprite_scale = 0.3
self.frame_width = 32
self.frame_height = 32
# Initial rect (will be updated after first frame)
scaled_w = int(self.frame_width * self.sprite_scale * 1.2)
scaled_h = int(self.frame_height * self.sprite_scale * 1.2)
self.rect = pygame.Rect(0, 0, scaled_w, scaled_h)
self.rect.center = (x, y)
# Load animations
self.animations = self.load_animations()
# Animation state
self.animation_state = {
"direction": "down",
"action": "idle",
"frame_index": 0,
"last_update": pygame.time.get_ticks()
}
# Load bullet image
self.assets = Assets()
self.bullet_image = self.assets.unicorn_bullet
#print("[DEBUG] Bullet image set:", type(self.bullet_image))
self.animate()
def load_animations(self):
animations = {}
direction_order = ["down", "left", "right", "up"]
action_order = ["idle", "idle2", "move", "move2", "shoot"]
sprite_path = os.path.join("assets", "sprites", "characters", "Unicorn.png")
sheet = SpriteSheet(sprite_path)
grid = sheet.load_grid(cols=self.sprite_cols, rows=self.sprite_rows, scale=self.sprite_scale)
for row, direction in enumerate(direction_order):
for col, action in enumerate(action_order):
key = (direction, action)
try:
animations[key] = [grid[row][col]]
except IndexError:
print(f"[WARN] Missing frame at row {row}, col {col} for key {key}")
animations[key] = []
return animations
def update(self, **kwargs):
players = kwargs["players"]
living_players = [p for p in players.values() if p.health > 0]
if not living_players:
return
target = self.find_closest(living_players)
dx = target.rect.centerx - self.rect.centerx
dy = target.rect.centery - self.rect.centery
direction = self.get_direction(dx, dy)
self.animation_state["direction"] = direction
if abs(dx) > 2 or abs(dy) > 2:
self.rect.x += self.speed if dx > 0 else -self.speed
self.rect.y += self.speed if dy > 0 else -self.speed
self.animation_state["action"] = "move"
else:
self.animation_state["action"] = "idle"
self.animate()
def animate(self):
now = pygame.time.get_ticks()
state = self.animation_state
key = (state["direction"], state["action"])
if key not in self.animations:
print(f"[WARN] Missing animation for {key}, defaulting to ('down', 'idle')")
key = ("down", "idle")
frames = self.animations[key]
if now - state["last_update"] > self.frame_delay:
state["frame_index"] = (state["frame_index"] + 1) % len(frames)
state["last_update"] = now
if frames:
self.image = frames[state["frame_index"]]
# ✅ Update rect to match image frame
self.rect = self.image.get_rect(center=self.rect.center)
def draw(self, screen):
if not isinstance(self.image, pygame.Surface):
print("[ERROR] self.image is not a Surface:", type(self.image))
return
screen.blit(self.image, self.rect.topleft)
self.draw_lifebar(screen)