-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.py
166 lines (126 loc) · 5.13 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
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
import pygame
import random
from typing import List, Union
from game_object import GameObject
class Enemy(GameObject):
"""
Represents an enemy in the game, including movement, collision handling,
health management, and rendering.
Attributes:
pos (pygame.Vector2): The current position of the enemy.
radius (int): The radius of the enemy's representation.
speed (float): The speed of the enemy's movement.
dmg (int): The damage dealt by the enemy to the player.
hp (int): The current health points of the enemy.
"""
def __init__(self, x: float, y: float, radius: int) -> None:
"""
Initializes an Enemy object with a position, radius, speed, damage, and health.
Args:
x (float): The x-coordinate of the enemy's position.
y (float): The y-coordinate of the enemy's position.
radius (int): The radius of the enemy.
"""
self.pos = pygame.Vector2(x, y)
self.radius = radius
self.speed = 200 / 60 # Speed per frame
self.dmg = 10
self.hp = 50
def update(self, player: "Player", enemies: List["Enemy"], screen: pygame.Surface) -> None:
"""
Updates the enemy's state, including movement, collisions, and death handling.
Args:
player (Player): The player object.
enemies (List[Enemy]): The list of all enemies in the game.
screen (pygame.Surface): The game screen surface.
"""
self.handle_movement(player)
self.handle_collision(player, enemies)
self.handle_death(enemies)
def handle_movement(self, player: "Player") -> None:
"""
Handles the movement logic of the enemy to chase the player.
Args:
player (Player): The player object.
"""
self.chase_player(player.pos)
def chase_player(self, player_pos: pygame.Vector2) -> None:
"""
Moves the enemy toward the player's position.
Args:
player_pos (pygame.Vector2): The position of the player.
"""
direction = player_pos - self.pos
direction.normalize_ip()
self.pos += direction * self.speed
def handle_collision(self, player: "Player", enemies: List["Enemy"]) -> None:
"""
Handles collisions with the player and other enemies.
Args:
player (Player): The player object.
enemies (List[Enemy]): The list of all enemies in the game.
"""
if self.check_collision(player):
player.take_damage(self.dmg)
self.take_damage(10)
self.knockback(player.pos)
for enemy in enemies:
if enemy != self and self.check_collision(enemy):
self.knockback(enemy.pos, 10)
def check_collision(self, entity: Union["Player", "Enemy"]) -> bool:
"""
Checks if the enemy collides with another entity.
Args:
entity (Union[Player, Enemy]): The other entity to check collision with.
Returns:
bool: True if a collision occurred, False otherwise.
"""
return (self.pos - entity.pos).length() < self.radius + entity.radius
def take_damage(self, dmg: int) -> None:
"""
Reduces the enemy's health by a specified amount.
Args:
dmg (int): The damage amount to inflict on the enemy.
"""
self.hp -= dmg
def knockback(self, entity_pos: pygame.Vector2, force: float = 50) -> None:
"""
Moves the enemy away from another entity as a result of a collision.
Args:
entity_pos (pygame.Vector2): The position of the other entity.
force (float): The force of the knockback. Defaults to 50.
"""
direction = self.pos - entity_pos
direction.normalize_ip()
self.pos += direction * force
def handle_death(self, enemies: List["Enemy"]) -> None:
"""
Checks if the enemy's health is zero or below and removes it from the game.
Args:
enemies (List[Enemy]): The list of all enemies in the game.
"""
if self.hp <= 0:
self.die(enemies)
def die(self, enemies: List["Enemy"]) -> None:
"""
Removes the enemy from the game.
Args:
enemies (List[Enemy]): The list of all enemies in the game.
"""
enemies.remove(self)
def handle_display(self, screen: pygame.Surface) -> None:
"""
Renders the enemy as a red circle on the screen.
Args:
screen (pygame.Surface): The game screen surface.
"""
pygame.draw.circle(screen, "red", self.pos, self.radius)
def draw_with_offset(self, screen: pygame.Surface, offset_x: float, offset_y: float) -> None:
"""
Draws the enemy with an offset, useful for camera systems.
Args:
screen (pygame.Surface): The game screen surface.
offset_x (float): The horizontal offset.
offset_y (float): The vertical offset.
"""
pygame.draw.circle(screen, "red", self.pos - pygame.Vector2(offset_x, offset_y), self.radius)