-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemy.py
86 lines (69 loc) · 2.34 KB
/
enemy.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
"""
Enemy classes for PyDoom game
"""
from cmu_graphics import Image
from dataclasses import dataclass
import math
from image_utils import get_png_dimensions
@dataclass
class SpriteFrame:
"""Class to hold sprite frame data."""
image: Image
width: int
height: int
class Enemy:
"""Base enemy class."""
def __init__(self):
self.health = 100
self.x = 0
self.y = 0
self.speed = 0.1
self.angle = 0
self.visible = False
def move(self) -> None:
"""Update enemy position."""
pass
def render(self) -> None:
"""Update enemy visibility."""
pass
class Imp(Enemy):
"""Imp enemy class."""
def __init__(self, x: float, y: float):
super().__init__()
self.x = x
self.y = y
self.frame_counter = 0
self.current_frame = 0
self.frames_per_state = 8
self.sprites = []
# Load all animation frames with actual dimensions
for i in range(4): # frame0 to frame3
filepath = f'assets/Imp/frame{i}.png'
width, height = get_png_dimensions(filepath)
sprite = Image(filepath, self.x, self.y,
align='bottom', width=width, height=height, visible=False)
self.sprites.append(SpriteFrame(sprite, width, height))
self.sprite = self.sprites[0] # Current visible sprite
self.visible = True
def update_animation(self) -> None:
"""Update the imp's animation state."""
if not self.visible:
return
self.frame_counter += 1
if self.frame_counter >= self.frames_per_state:
self.frame_counter = 0
self.current_frame = (self.current_frame + 1) % len(self.sprites)
# Update current sprite
self.sprite = self.sprites[self.current_frame]
def move(self) -> None:
"""Update Imp position."""
self.x += self.speed * math.cos(self.angle)
self.y += self.speed * math.sin(self.angle)
self.update_animation()
def move_to(self, to) -> None:
self.x = to[0]
self.y = to[1]
print(f"moved to {self.x} {self.y}")
def render(self) -> None:
"""Update Imp visibility."""
self.sprite.image.visible = self.visible