-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgrade.py
171 lines (150 loc) · 6.11 KB
/
upgrade.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
import pygame
import os
from settings import PROJECT_DIR
class Upgrade:
def __init__(self, player: pygame.sprite.Sprite):
self.display_surface = pygame.display.get_surface()
self.player = player
self.max_player_attrs = len(player.STATS)
self.font = pygame.font.Font(
os.path.join(PROJECT_DIR, "graphics/font/joystix.ttf"), 24
)
self.selection_index = 0
self.selection_time = None
self.cooldown = 100
self.can_move = True
font = pygame.font.Font(
os.path.join(PROJECT_DIR, "graphics/font/joystix.ttf"), 25
)
UpgradeBox.set_font(font=font)
self.upgrade_box_height = self.display_surface.get_height() * 0.8
self.upgrade_box_width = (
self.display_surface.get_width() - 100
) // self.max_player_attrs
self.start_pos_x = (
self.display_surface.get_width()
- self.upgrade_box_width * self.max_player_attrs
- self.max_player_attrs * 10
) // 2
self.create_upgrade_box()
def get_input(self):
keys = pygame.key.get_pressed()
if self.can_move:
if keys[pygame.K_LEFT] or keys[pygame.K_RIGHT] or keys[pygame.K_SPACE]:
self.can_move = False
self.selection_time = pygame.time.get_ticks()
if keys[pygame.K_RIGHT]:
self.selection_index += 1
elif keys[pygame.K_LEFT]:
self.selection_index -= 1
if keys[pygame.K_SPACE]:
self.upgrade_box_list[self.selection_index].trigger_action(self.player)
self.selection_index = max(
0,
min(self.selection_index, self.max_player_attrs - 1),
)
def selection_cooldown(self):
if not self.can_move:
current_time = pygame.time.get_ticks()
if current_time - self.selection_time >= self.cooldown:
self.can_move = True
def create_upgrade_box(self):
self.upgrade_box_list = []
attrs_names = list(self.player.STATS.keys())
attrs_values = list(self.player.STATS.values())
for i in range(self.max_player_attrs):
top = self.display_surface.get_height() * 0.1
left = self.start_pos_x + i * self.upgrade_box_width + (i * 10)
upgrade_box = UpgradeBox(
(top, left),
(self.upgrade_box_width, self.upgrade_box_height),
i,
)
self.upgrade_box_list.append(upgrade_box)
def display(self):
self.get_input()
self.selection_cooldown()
for index, upgrade_box in enumerate(self.upgrade_box_list):
attribute_stat = self.player.get_stat_by_index(index)
upgrade_box.display(
self.display_surface, self.selection_index, attribute_stat
)
class UpgradeBox:
def __init__(
self,
topleft: tuple[int, int],
size: tuple[int, int],
index: int,
):
self.image = pygame.Surface(size, pygame.SRCALPHA)
self.image.fill((96, 125, 139, 200))
self.position = topleft[1], topleft[0]
self.rect = self.image.get_rect(topleft=(self.position))
self.radius_pos = topleft[1] - 5, topleft[0] - 5
self.radius_size = size[0] + 10, size[1] + 10
self.index = index
@classmethod
def set_font(self, font: pygame.font.Font):
self.font = font
def trigger_action(self, player):
selected_attr = player.get_stat_by_index(self.index)
attr = selected_attr["attr"]
if player.exp < selected_attr["value"]["cost"]:
return
if selected_attr["value"]["amount"] < selected_attr["value"]["max_value"]:
player.update_attribute(attr, selected_attr["value"]["increment"])
def display(
self,
display_surface: pygame.Surface,
selection_index: int,
attribute_stat: dict,
):
self.display_names(display_surface, attribute_stat)
if self.index == selection_index:
pygame.draw.rect(
surface=display_surface,
color=(0, 255, 208),
rect=(self.radius_pos, self.radius_size),
width=5,
border_radius=5,
)
self.image.fill(color=(96, 125, 139))
else:
self.image.fill(color=(96, 125, 139, 200))
def display_bar(self, display_surface: pygame.Surface, attribute_stat: dict):
midtop = self.rect.centerx, self.rect.top + 60
midbottom = self.rect.centerx, self.rect.bottom - 60
full_height = midbottom[1] - midtop[1]
relative_height = (
attribute_stat["amount"] / attribute_stat["max_value"]
) * full_height
# just hardcoded since code is already long
value_rect = (midtop[0] - 15, midbottom[1] - relative_height - 5, 30, 10)
pygame.draw.rect(display_surface, "white", value_rect)
pygame.draw.line(display_surface, "white", midtop, midbottom, 4)
def display_names(self, display_surface: pygame.Surface, attribute_stat: dict):
label = self.font.render(str(attribute_stat["attr"]), True, "yellow")
value = self.font.render(str(attribute_stat["value"]["cost"]), True, "yellow")
max_value = self.font.render(
str(attribute_stat["value"]["max_value"]), True, "red"
)
display_surface.blit(self.image, self.position)
self.display_bar(display_surface, attribute_stat["value"])
display_surface.blit(
source=label,
dest=(
self.position[0] + (self.image.get_width() - label.get_width()) // 2,
self.position[1] - label.get_height(),
),
)
display_surface.blit(
source=value,
dest=(
self.position[0] + (self.image.get_width() - value.get_width()) // 2,
self.rect.bottom - value.get_height(),
),
)
display_surface.blit(
source=max_value,
dest=((self.rect.centerx - max_value.get_width() // 2), self.rect.top),
)