-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_entities.py
270 lines (231 loc) · 10.9 KB
/
game_entities.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
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
import pygame
import math
import random
import time
from config import *
from utilities import *
class GameObject:
"""Base class for all game entities"""
def __init__(self, x, y, image):
self.original_image = image
self.image = image
self.rect = image.get_rect(topleft=(x, y))
self.active = True
def update(self, *args):
"""Update logic to be overridden by subclasses"""
pass
def draw(self, screen, scroll):
"""Draw the entity with scroll offset"""
screen.blit(self.image, (self.rect.x - scroll[0], self.rect.y - scroll[1]))
class Player(GameObject):
def __init__(self, x, y, image, asset_manager):
super().__init__(x, y, image)
self.flipped = False
self.last_direction = "right"
self.health = START_HEALTH
self.garlic_count = 0
self.carrot_juice_count = 0 # Initialize counter
self.death_effect_active = False
self.death_effect_start_time = 0
self.asset_manager = asset_manager
self.health_changed = False
self.garlic_changed = False
self.juice_changed = False
def move(self, dx, dy, world_bounds):
speed = PLAYER_SPEED
self.rect.x = max(0, min(world_bounds[0] - self.rect.width, self.rect.x + dx * speed))
self.rect.y = max(0, min(world_bounds[1] - self.rect.height, self.rect.y + dy * speed))
if dx < 0 and not self.flipped:
self.image = pygame.transform.flip(self.original_image, True, False)
self.flipped = True
elif dx > 0 and self.flipped:
self.image = self.original_image
self.flipped = False
def take_damage(self, amount=1):
self.health = max(0, self.health - amount)
self.health_changed = True
if self.health > 0:
self.asset_manager.sounds['hurt'].play()
def reset(self):
self.health = START_HEALTH
self.garlic_count = 0
self.carrot_juice_count = 0
self.rect.x = 200
self.rect.y = 200
def draw(self, screen, scroll):
screen.blit(self.image, (self.rect.x - scroll[0], self.rect.y - scroll[1]))
def draw_ui(self, screen, hp_image, garlic_image, max_garlic):
# Health display
for i in range(self.health):
screen.blit(hp_image, (10 + i * (hp_image.get_width() + 5), 10))
# Garlic display
if self.garlic_count > 0:
screen_width = screen.get_width()
garlic_width = garlic_image.get_width()
spacing = 5
for i in range(self.garlic_count):
x = screen_width - 10 - (i + 1) * (garlic_width + spacing)
screen.blit(garlic_image, (x, 10))
# Carrot juice counter at bottom right (always visible when count > 0)
if self.carrot_juice_count > 0:
juice_image = self.asset_manager.images['carrot_juice']
digits = str(self.carrot_juice_count)
spacing = 5
original_digit = self.asset_manager.images['digit_0']
scaled_digit_width = original_digit.get_width() * 5
scaled_digit_height = original_digit.get_height() * 5
# Calculate total width needed for digits and spacing
total_width = len(digits) * (scaled_digit_width + spacing)
# Start position (left side of juice image)
x = screen.get_width() - 10 - juice_image.get_width() - total_width
y = screen.get_height() - 10 - juice_image.get_height()
# Calculate vertical position to align bottoms
digit_y = y + juice_image.get_height() - scaled_digit_height
# Draw digits first
for i, digit in enumerate(digits):
digit_img = self.asset_manager.images[f'digit_{digit}']
scaled_digit = pygame.transform.scale(digit_img, (scaled_digit_width, scaled_digit_height))
screen.blit(scaled_digit, (x + i * (scaled_digit_width + spacing), digit_y))
# Draw juice image to the right of the digits
screen.blit(juice_image, (x + total_width + spacing, y))
class Bullet(GameObject):
def __init__(self, x, y, target_x, target_y, image):
super().__init__(x, y, image)
dir_x, dir_y = get_direction_vector(x, y, target_x, target_y)
self.velocity = (dir_x * BULLET_SPEED, dir_y * BULLET_SPEED)
self.angle = math.degrees(math.atan2(-dir_y, dir_x))
def update(self):
self.rect.x += self.velocity[0]
self.rect.y += self.velocity[1]
@property
def rotated_image(self):
return pygame.transform.rotate(self.original_image, self.angle)
class Carrot(GameObject):
def __init__(self, x, y, image):
super().__init__(x, y, image)
self.speed = CARROT_SPEED
self.active = True
self.respawn_timer = 0
self.direction = pygame.math.Vector2(random.uniform(-1, 1),
random.uniform(-1, 1)).normalize()
self.spawn_position = (x, y) # Store initial spawn position
def respawn(self, world_size, player_rect):
"""Reset carrot to initial position and state"""
self.rect.center = self.spawn_position
self.active = True
self.direction = pygame.math.Vector2(random.uniform(-1, 1),
random.uniform(-1, 1)).normalize()
def update(self, player_rect, world_bounds):
if self.active:
player_center = pygame.math.Vector2(player_rect.center)
carrot_center = pygame.math.Vector2(self.rect.center)
direction = carrot_center - player_center
dist = direction.length()
max_distance = CARROT_DETECTION_RADIUS
speed_multiplier = min(max(1, 1 + (max_distance - dist)/max_distance * (3 - 1)), 3)
if dist < CARROT_CHASE_RADIUS and dist > 0:
direction.normalize_ip()
self.direction = direction
else:
self.direction += pygame.math.Vector2(random.uniform(-0.2, 0.2), random.uniform(-0.2, 0.2))
self.direction.normalize_ip()
movement = self.direction * self.speed * speed_multiplier
self.rect.x += movement.x
self.rect.y += movement.y
self.rect.x = max(0, min(world_bounds[0] - self.rect.width, self.rect.x))
self.rect.y = max(0, min(world_bounds[1] - self.rect.height, self.rect.y))
class GarlicShot(GameObject):
def __init__(self, start_x, start_y, target_x, target_y, image):
super().__init__(start_x, start_y, image)
dir_x, dir_y = get_direction_vector(start_x, start_y, target_x, target_y)
self.direction = pygame.math.Vector2(dir_x, dir_y)
self.rotation_angle = 0
self.speed = GARLIC_SHOT_SPEED
self.max_travel = GARLIC_SHOT_MAX_TRAVEL
self.traveled = 0
self.active = True
def update(self):
if self.active:
self.rect.x += self.direction.x * self.speed
self.rect.y += self.direction.y * self.speed
self.traveled += self.speed
self.rotation_angle = (self.rotation_angle + 5) % 360
if self.traveled >= self.max_travel:
self.active = False
class Explosion:
def __init__(self, x, y, image):
self.image = image
self.rect = image.get_rect(center=(x, y))
self.start_time = time.time()
self.flash_count = 0
self.max_flashes = EXPLOSION_MAX_FLASHES
self.flash_interval = EXPLOSION_FLASH_INTERVAL
self.active = True
def update(self, current_time):
if self.active:
elapsed = current_time - self.start_time
if elapsed > self.flash_interval:
self.flash_count += 1
self.start_time = current_time
if self.flash_count >= self.max_flashes:
self.active = False
return True
return False
def draw(self, screen, scroll):
"""Draw the explosion with scrolling offset"""
if self.active and (self.flash_count % 2 == 0):
screen.blit(self.image,
(self.rect.x - scroll[0],
self.rect.y - scroll[1]))
class Collectible(GameObject):
def __init__(self, x, y, image, item_type, scale=0.5):
scaled_image = pygame.transform.scale(image,
(int(image.get_width() * scale),
int(image.get_height() * scale)))
super().__init__(x, y, scaled_image)
self.rect = self.image.get_rect(center=(x, y)) # Center the rect
self.active = True
self.item_type = item_type
class Vampire(GameObject):
def __init__(self, x, y, image):
super().__init__(x, y, image)
self.active = False
self.respawn_timer = 0
self.death_effect_active = False
self.death_effect_start_time = 0
self.death_effect_duration = VAMPIRE_DEATH_DURATION
self.speed = VAMPIRE_SPEED
def update(self, player, world_bounds, current_time):
if self.active:
# Movement logic
move_x, move_y = calculate_movement_towards(self.rect, player.rect, self.speed, world_bounds)
self.rect.x += move_x
self.rect.y += move_y
# Boundary check
self.rect.x = max(0, min(world_bounds[0] - self.rect.width, self.rect.x))
self.rect.y = max(0, min(world_bounds[1] - self.rect.height, self.rect.y))
# Death effect check
if self.death_effect_active and (current_time - self.death_effect_start_time >= self.death_effect_duration):
self.death_effect_active = False
else:
# Respawn check when NOT active
if (current_time - self.respawn_timer > VAMPIRE_RESPAWN_TIME):
self.respawn(
random.randint(0, world_bounds[0] - self.rect.width),
random.randint(0, world_bounds[1] - self.rect.height)
)
def respawn(self, x, y):
self.rect.topleft = (x, y)
self.active = True
self.death_effect_active = False
self.death_flash_count = 0
def draw(self, screen, scroll, current_time):
if self.death_effect_active:
time_since_flash = current_time - self.death_effect_start_time
if time_since_flash <= self.death_effect_duration:
if int(time_since_flash / 0.1) % 2 == 0:
tinted_image = self.image.copy()
tinted_image.fill((0, 255, 0, 128), special_flags=pygame.BLEND_RGBA_MULT)
screen.blit(tinted_image, (self.rect.x - scroll[0], self.rect.y - scroll[1]))
elif self.active:
screen.blit(self.image, (self.rect.x - scroll[0], self.rect.y - scroll[1]))