-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUI_components.py
More file actions
240 lines (180 loc) · 8.68 KB
/
Copy pathUI_components.py
File metadata and controls
240 lines (180 loc) · 8.68 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
import pygame
import random
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
def draw_health_bar(surface, current_hp, max_hp, scale_x = 2 , scale_y = 2):
bg_rect = pygame.Rect(140* scale_x, 5 * scale_y, 300 * scale_x , 30 * scale_y)
pygame.draw.rect(surface, WHITE, bg_rect)
health_width = int((current_hp / max_hp) * 300 * scale_x)
health_rect = pygame.Rect(140 * scale_x, 5* scale_y, health_width, 30 * scale_y )
pygame.draw.rect(surface, RED, health_rect)
pygame.draw.rect(surface, BLACK, bg_rect, 2)
class Menu_option (pygame.sprite.Sprite):
def __init__(self, x, y, width, height, color, hover_color,text):
super().__init__()
self.image = pygame.Surface((width , height))
self.image.fill(color)
self.rect = self.image.get_rect(topleft=(x, y))
self.color = color
self.hover_color = hover_color
self.original_width = width
self.original_height = height
self.hover_width = width + 100
self.hover_height = height
self.active = False
self.text = text
self.width = width
self.height = height
def update(self , x , y, screen):
font = pygame.font.SysFont("Bauhaus 93", 42)
shadow = font.render(self.text, True, BLACK)
shadow.set_alpha(150,150)
# Smoothly transition the size
target_width = self.hover_width if self.active else self.original_width
target_height = self.hover_height if self.active else self.original_height
self.width += (target_width - self.width) * 0.05
self.height += (target_height - self.height) * 0.2
self.image = pygame.Surface((int(self.width), int(self.height)), pygame.SRCALPHA) # Use SRCALPHA for transparency
self.image.fill((0, 0, 0, 0))
pygame.draw.rect(
self.image,
self.hover_color if self.active else self.color,
(0, 0, int(self.width), int(self.height)),
border_radius = 15 # Adjust the border radius for rounded corners
)
# Update the button's image and rect
self.rect = self.image.get_rect(topleft=(x, y))
# Draw the text
text_surface = font.render(self.text, True, BLACK if not self.active else WHITE)
text_rect = text_surface.get_rect(center=(self.width / 2, self.height / 2))
if self.active == True :
self.image.blit(shadow, (text_rect.x + 10, text_rect.y + 5))
self.image.blit(text_surface, text_rect)
# Blit the button to the screen
screen.blit(self.image, self.rect.topleft)
def toogle (self):
self.active = not self.active
class DustParticle:
def __init__(self, x, y):
self.x = x
self.y = y
self.radius = random.randint(1, 3)
self.speed_y = random.uniform(-0.2, -0.5)
self.alpha = random.randint(50, 100)
self.lifetime = random.randint(100, 200)
self.surface = pygame.Surface((self.radius * 4, self.radius * 4), pygame.SRCALPHA)
def update(self):
self.y += self.speed_y
self.alpha -= 0.2
self.lifetime -= 1
def draw(self, screen):
self.surface.fill((0, 0, 0, 0)) # Clear the surface with full transparency
pygame.draw.circle(
self.surface,
(255, 255, 255, max(0, int(self.alpha))),
(self.surface.get_width() // 2, self.surface.get_height() // 2),
self.radius
)
screen.blit(self.surface, (self.x, self.y))
def is_dead(self):
return self.lifetime <= 0 or self.alpha <= 0
def draw_reload_bar(screen, x, y, scale_x, scale_y, reload_progress,
bg_color=(50, 50, 50), fill_color=(255, 150, 0),
glow_color=(255, 223, 0), text_color=(255, 255, 200)):
"""
Draws an animated reload progress bar
Parameters:
- screen: Target surface to draw on
- x, y: Top-left position (base coordinates before scaling)
- scale_x, scale_y: Scaling factors
- reload_progress: 0.0-1.0 value indicating reload completion
- colors: Optional color overrides
"""
# Calculate scaled dimensions
bar_width = int(200 * scale_x)
bar_height = int(24 * scale_y)
pos_x = int(x * scale_x)
pos_y = int(y * scale_y)
border_radius = int(5 * scale_x)
# Animated glow background
glow_alpha = abs(pygame.time.get_ticks() % 1000 - 500) / 5
glow_surface = pygame.Surface((bar_width, bar_height), pygame.SRCALPHA)
pygame.draw.rect(glow_surface, (*glow_color, int(glow_alpha)),
glow_surface.get_rect(), border_radius=border_radius)
screen.blit(glow_surface, (pos_x, pos_y))
# Background container
pygame.draw.rect(screen, bg_color, (pos_x, pos_y, bar_width, bar_height),
border_radius=border_radius)
pygame.draw.rect(screen, (100, 100, 100), (pos_x, pos_y, bar_width, bar_height),
2, border_radius=border_radius)
# Progress bar
current_width = bar_width * reload_progress
gradient = pygame.Surface((current_width, bar_height))
for i in range(int(current_width)):
alpha = int(255 * (i/current_width)) if current_width > 0 else 0
pygame.draw.line(gradient, (fill_color[0], fill_color[1] + alpha//4, fill_color[2]),
(i, 0), (i, bar_height))
# Clip the gradient to rounded rectangle
mask = pygame.Surface((current_width, bar_height), pygame.SRCALPHA)
pygame.draw.rect(mask, (255,255,255,255), (0, 0, current_width, bar_height),
border_radius=border_radius)
gradient.blit(mask, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
screen.blit(gradient, (pos_x, pos_y))
# Percentage text
font = pygame.font.SysFont("Arial Bold", int(14 * scale_x))
text = font.render(f"{int(reload_progress*100)}%", True, text_color)
text_rect = text.get_rect(center=(pos_x + bar_width//2, pos_y + bar_height + 10 * scale_y))
screen.blit(text, text_rect)
# Reload icon (optional - needs image file)
try:
icon_size = int(20 * scale_x)
reload_icon = pygame.transform.scale(pygame.image.load("images/reload_icon.png"),
(icon_size, icon_size))
screen.blit(reload_icon, (pos_x + bar_width + 5 * scale_x, pos_y - 2 * scale_y))
except FileNotFoundError:
pass
def draw_minimap(screen, player, Rooms, boss_room_rect=None, scale_x=1, scale_y=1):
minimap_width = int(200 * scale_x)
minimap_height = int(150 * scale_y)
minimap_surface = pygame.Surface((minimap_width, minimap_height))
minimap_surface.fill((30, 30, 30))
map_scale = 50
all_rects = [room.rect for room in Rooms]
if boss_room_rect:
all_rects.append(boss_room_rect)
min_x = min(rect.x for rect in all_rects)
min_y = min(rect.y for rect in all_rects)
def to_minimap_coords(x, y):
return (
int((x - min_x) / map_scale),
int((y - min_y) / map_scale)
)
for room in Rooms:
x, y = to_minimap_coords(room.rect.x, room.rect.y)
w = max(1, int(room.rect.width / map_scale))
h = max(1, int(room.rect.height / map_scale))
pygame.draw.rect(minimap_surface, (100, 100, 255), pygame.Rect(x, y, w, h))
if boss_room_rect:
x, y = to_minimap_coords(boss_room_rect.x, boss_room_rect.y)
w = max(1, int(boss_room_rect.width / map_scale))
h = max(1, int(boss_room_rect.height / map_scale))
pygame.draw.rect(minimap_surface, (255, 0, 0), pygame.Rect(x, y, w, h), 2)
px, py = to_minimap_coords(player.rect.centerx, player.rect.centery)
pygame.draw.circle(minimap_surface, (0, 255, 0), (px, py), int(3 * scale_x))
screen.blit(minimap_surface, (screen.get_width() - minimap_width - 10, int(50 * scale_y)))
class StopButton :
def __init__(self, screen, x, y, width, height):
self.screen = screen
self.x = x
self.y = y
self.width = width
self.height = height
self.image = pygame.image.load('images/button.webp').convert_alpha()
self.image = pygame.transform.scale(self.image, (width, height))
self.rect = self.image.get_rect(topleft=(x, y))
def draw(self,screen):
screen.blit(self.image, (self.x, self.y))
pygame.draw.rect(screen, WHITE, self.rect, 2)