-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade.py
139 lines (107 loc) · 6.07 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
import pygame
from settings import *
class Upgrade:
def __init__(self,player):
# general setup
self.display_surface = pygame.display.get_surface()
self.player = player
self.attribute_number = len(player.stats)
self.attribute_names = list(player.stats.keys())
self.max_values = list(player.max_stats.values())
self.font = pygame.font.Font(UI_FONT, UI_FONT_SIZE)
# selection system
self.selection_index = 0 # 0 - 4 to select between the 0 - 5 index for upgrade boxes in self.index = index, inside the Item class
self.selection_time = None
self.can_move = True
# item creation
self.height = self.display_surface.get_size()[1] * 0.8
self.width = self.display_surface.get_size()[0] // 6
self.create_items()
def input(self):
keys = pygame.key.get_pressed()
if self.can_move:
if keys[pygame.K_RIGHT] and self.selection_index < self.attribute_number - 1: # -1 because of len(player.stats)
self.selection_index += 1
self.can_move = False
self.selection_time = pygame.time.get_ticks()
elif keys[pygame.K_LEFT] and self.selection_index >= 1:
self.selection_index -= 1
self.can_move = False
self.selection_time = pygame.time.get_ticks()
if keys[pygame.K_SPACE]:
self.can_move = False
self.selection_time = pygame.time.get_ticks()
self.item_list[self.selection_index].trigger(self.player)
def selection_cooldown(self):
if not self.can_move:
current_time = pygame.time.get_ticks()
if current_time - self.selection_time >= 300:
self.can_move = True
def create_items(self):
self.item_list = []
for item, index in enumerate(range(self.attribute_number)):
#horizontal position
full_width = self.display_surface.get_size()[0]
increment = full_width // self.attribute_number
left = (item * increment) + (increment - self.width) // 2
#vertical position
top = self.display_surface.get_size()[1] * 0.1
#create the object
item = Item(left, top, self.width, self.height, index, self.font)
self.item_list.append(item)
def display(self):
# self.display_surface.fill('black') #fill the whole screen black
self.input()
self.selection_cooldown()
for index, item in enumerate(self.item_list): # display the 5 rectangles for the upgrade menu # use enumerate to know which index of each number
# get attributes
name = self.attribute_names[index]
value = self.player.get_value_by_index(index)
max_value = self.max_values[index]
cost = self.player.get_cost_by_index(index)
item.display(self.display_surface, self.selection_index, name, value, max_value, cost)
class Item:
def __init__(self, l, t, w, h, index, font):
self.rect = pygame.Rect(l,t,w,h)
self.index = index # the index of each upgrade box 0 - 4
self.font = font
def display_names(self, surface, name, cost, current_selected_box): # current_selected_box is True or False
color = TEXT_COLOR_SELECTED if current_selected_box else TEXT_COLOR
#title
title_surf = self.font.render(name, False, color)
title_rect = title_surf.get_rect(midtop = self.rect.midtop + pygame.math.Vector2(0, 20))
#cost
cost_surf = self.font.render(f'{int(cost)}', False, color) # convert to int in case cost becomes a floating point number
cost_rect = cost_surf.get_rect(midbottom = self.rect.midbottom - pygame.math.Vector2(0, 20))
#draw
surface.blit(title_surf, title_rect)
surface.blit(cost_surf, cost_rect)
def display_bar(self, surface, value, max_value, selected):
#drawing setup
top = self.rect.midtop + pygame.math.Vector2(0, 60) #offset to go down 60
bottom = self.rect.midbottom - pygame.math.Vector2(0, 60)
color = BAR_COLOR_SELECTED if selected else BAR_COLOR
#bar setup
full_height = bottom[1] - top[1] # 1 is the y postion only
relative_number = (value / max_value) * full_height
value_rect = pygame.Rect(top[0] - 15, bottom[1] - relative_number, 30, 10) # 15 is half of the width
pygame.draw.rect(surface, color, value_rect)
#draw elements
pygame.draw.line(surface, color, top, bottom, 5)
def trigger(self, player):
upgrade_attribute = list(player.stats.keys())[self.index]
if player.exp >= player.upgrade_cost[upgrade_attribute] and player.stats[upgrade_attribute] < player.max_stats[upgrade_attribute]: # player can only upgrade if they have the exp AND current stats lower than max stats
player.exp -= player.upgrade_cost[upgrade_attribute]
player.stats[upgrade_attribute] *= 1.2
player.upgrade_cost[upgrade_attribute] *= 1.4 # increase upgrade cost after every upgrade to make upgrades more expensive
if player.stats[upgrade_attribute] > player.max_stats[upgrade_attribute]: # set it to max stats so that player can't over upgrade their stats
player.stats[upgrade_attribute] = player.max_stats[upgrade_attribute]
def display(self, surface, selection_num, name, value, max_value, cost): # selection_num is the selection index from Upgrade class
if self.index == selection_num:
pygame.draw.rect(surface, UPGRADE_BG_COLOR_SELECTED, self.rect) # draw basic rectangles
pygame.draw.rect(surface, UI_BORDER_COLOR, self.rect, 4) # draw basic rectangles, 4 is the width
else:
pygame.draw.rect(surface, UI_BG_COLOR, self.rect) # draw basic rectangles
pygame.draw.rect(surface, UI_BORDER_COLOR, self.rect, 4) # draw basic rectangles, 4 is the width
self.display_names(surface, name, cost, self.index == selection_num)
self.display_bar(surface, value, max_value, self.index == selection_num)