-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory.py
223 lines (178 loc) · 6.6 KB
/
inventory.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
import pygame
import math
from sprites import Spritesheet
from config import WIN_HEIGHT, WIN_WIDTH, BLACK, WHITE
class InventoryItem:
def __init__(self, img, value):
self.img = img
self.value = value
self.rect: pygame.Rect
def set_rect(self, rect):
self.rect = rect
class Consumable(InventoryItem):
def __init__(self, img, value, hp_gain, mp_gain):
InventoryItem.__init__(self, img, value)
self.hp_gain = hp_gain
self.mp_gain = mp_gain
def use(self, target):
target.addHp(self.hp_gain)
target.addMP(self.mp_gain)
class Equipable(InventoryItem):
def __init__(self, img, value):
InventoryItem.__init__(self, img, value)
self.is_equipped = False
self.equipped_to = None
def equip(self, target):
self.is_equipped = True
self.equipped_to = target
def unequip(self):
self.is_equipped = False
self.equipped_to = None
class Weapon(Equipable):
def __init__(self, img, value, atk, wpn_type, name):
Equipable.__init__(self, img, value)
self.atk = atk
self.wpn_type = wpn_type
self.name = name
def equip(self, target):
Equipable.equip(self, target)
target.equip_weapon(self)
def unequip(self):
self.equipped_to.unequip_weapon()
Equipable.unequip(self)
def get_info(self):
return {
'atk': self.atk,
'type': self.wpn_type,
'name': self.name
}
class Armor(Equipable):
def __init__(self, img, value, defence, mdef, slot, name):
Equipable.__init__(self, img, value)
self.defence = defence
self.mdef = mdef
self.slot = slot
self.eq_type = 'armor'
self.name = name
def equip(self, target):
Equipable.equip(self, target)
target.equip_armor(self, self.slot)
def unequip(self):
self.equipped_to.unequip_armor(self.slot)
Equipable.unequip(self)
def get_info(self):
return {
'def': self.defence,
'slot': self.slot,
'type': self.eq_type,
'name': self.name
}
class Inventory:
def __init__(self, hp, game):
self.hp = hp
self.items = [[]]
self.game = game
self.avatar = pygame.image.load('img/avatar.png')
# inventory win
self.width = 500
self.height = 350
# surface sets
self.surf = pygame.Surface((self.width, self.height))
self.surf.fill(BLACK)
self.surf.set_alpha(200)
self.pos_x = (WIN_WIDTH - self.width) / 2
self.pos_y = (WIN_HEIGHT - self.height) / 2
# item sets
self.cell_size = 50
self.cell_size = 50
# font
self.font = pygame.font.Font('arialmt.ttf', 16)
# item info texts
self.texts = []
# win statements
self.invent_open = False
def add_item(self, item):
row = len(self.items) - 1
if len(self.items[row]) == 5:
if row == 2:
return 'Inventory is full'
self.items.append([item])
else:
self.items[row].append(item)
def remove_item(self, item):
for i, sublist in enumerate(self.items):
if item in sublist:
self.items[i].remove(item)
if len(self.items[i]) == 0:
self.items.remove(self.items[i])
def update_hp(self, amount):
self.hp = amount
# get mouse position in surf
def get_act_pos(self, pos):
return pos[0] - self.pos_x, pos[1] - self.pos_y
def is_pressed(self, rect, pos, pressed):
if rect.collidepoint(pos):
if pressed[0]:
return True
return False
return False
def get_cell_render(self, item, lx, ly):
y = 15 + self.cell_size * ly + 15 * ly if ly else 15
x = 135 + self.cell_size * lx + 15 * lx if lx else 135
item_rect = pygame.Rect(x, y, self.cell_size, self.cell_size)
item.set_rect(item_rect)
sprite = pygame.transform.scale(item.img, (50, 50))
return sprite, (x, y)
def get_cell(self, mouse_pos):
cell_x = math.floor((mouse_pos[0] - 135) / self.cell_size)
cell_y = math.floor((mouse_pos[1] - 15) / self.cell_size)
cell_x = math.floor((mouse_pos[0] - 135 - cell_x * 15) / self.cell_size)
cell_y = math.floor((mouse_pos[1] - 15 - cell_y * 15) / self.cell_size)
try:
rect = self.items[cell_y][cell_x].rect
pressed = self.is_pressed(rect, mouse_pos, [1, 0])
if pressed:
return self.items[cell_y][cell_x]
return False
except Exception as e:
return None
def render_invent_win(self, screen, rend_info=False):
self.invent_open = True
self.surf.blit(self.avatar, (15, 15))
for ly, sublist in enumerate(self.items):
for lx, item in enumerate(sublist):
item_sprite, pos = self.get_cell_render(item, lx, ly)
self.surf.blit(item_sprite, (pos[0], pos[1]))
if not rend_info:
screen.blit(self.surf, (self.pos_x, self.pos_y))
def clear_surface(self):
self.game.draw()
self.surf.fill(BLACK)
def render_item_info(self, screen, item):
props = item.get_info()
self.clear_surface()
self.render_invent_win(screen, rend_info=True)
for num, prop in enumerate(props.keys()):
text = self.font.render(f'{prop}: {props[prop]}', True, WHITE)
self.texts.append(text)
self.surf.blit(text, (135, 195 + 20 * num))
screen.blit(self.surf, (self.pos_x, self.pos_y))
def render(self, screen):
self.render_invent_win(screen)
self.invent_open = True
while self.invent_open:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_i:
if self.invent_open:
self.clear_surface()
self.invent_open = False
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
mouse_pressed = pygame.mouse.get_pressed()
item = self.get_cell(self.get_act_pos(mouse_pos))
if item:
self.render_item_info(screen, item)
if not self.is_pressed(self.surf.get_rect(), self.get_act_pos(mouse_pos), mouse_pressed):
self.invent_open = False
pygame.display.update()