91 lines
3.0 KiB
Python
91 lines
3.0 KiB
Python
import pygame
|
|
from settings import *
|
|
from items import ITEMS, get_item
|
|
|
|
|
|
class Inventory:
|
|
def __init__(self):
|
|
self.slots = {}
|
|
|
|
# UI settings
|
|
self.width = 420
|
|
self.height = 300
|
|
self.x = (SCREEN_WIDTH - self.width) // 2
|
|
self.y = (SCREEN_HEIGHT - self.height) // 2
|
|
|
|
self.grid_cols = 8
|
|
self.grid_rows = 4
|
|
self.cell_size = 40
|
|
self.padding = 10
|
|
self.cell_padding = 4
|
|
|
|
def add_item(self, item_id, amount=1):
|
|
if item_id in self.slots:
|
|
self.slots[item_id] += amount
|
|
else:
|
|
self.slots[item_id] = amount
|
|
|
|
def draw(self, screen):
|
|
font = pygame.font.SysFont("consolas", 18)
|
|
title_font = pygame.font.SysFont("consolas", 24)
|
|
|
|
# Panel background
|
|
panel_rect = pygame.Rect(self.x, self.y, self.width, self.height)
|
|
pygame.draw.rect(screen, (30, 30, 30), panel_rect)
|
|
pygame.draw.rect(screen, (255, 255, 255), panel_rect, 2)
|
|
|
|
# Title
|
|
title = title_font.render("Inventory", True, (255, 255, 255))
|
|
screen.blit(title, (self.x + 15, self.y + 10))
|
|
|
|
# Close hint
|
|
hint = font.render("Press I to close", True, (200, 200, 200))
|
|
screen.blit(hint, (self.x + 15, self.y + 40))
|
|
|
|
# Draw grid
|
|
start_x = self.x + self.padding
|
|
start_y = self.y + 70
|
|
|
|
# Convert slots to list for display
|
|
items_list = list(self.slots.items())
|
|
|
|
index = 0
|
|
mouse = pygame.mouse.get_pos()
|
|
|
|
for row in range(self.grid_rows):
|
|
for col in range(self.grid_cols):
|
|
cell_rect = pygame.Rect(
|
|
start_x + col * (self.cell_size + self.cell_padding),
|
|
start_y + row * (self.cell_size + self.cell_padding),
|
|
self.cell_size,
|
|
self.cell_size
|
|
)
|
|
|
|
pygame.draw.rect(screen, (60, 60, 60), cell_rect)
|
|
pygame.draw.rect(screen, (120, 120, 120), cell_rect, 2)
|
|
|
|
if index < len(items_list):
|
|
item_id, amount = items_list[index]
|
|
item = get_item(item_id)
|
|
|
|
if item:
|
|
# Draw item icon
|
|
icon_rect = pygame.Rect(
|
|
cell_rect.x + 6,
|
|
cell_rect.y + 6,
|
|
28,
|
|
28
|
|
)
|
|
pygame.draw.rect(screen, item.color, icon_rect)
|
|
|
|
# Draw amount
|
|
amount_text = font.render(str(amount), True, (255, 255, 255))
|
|
screen.blit(amount_text, (cell_rect.x + 2, cell_rect.y + 2))
|
|
|
|
# Tooltip on hover
|
|
if cell_rect.collidepoint(mouse):
|
|
tooltip = font.render(item.name, True, (255, 255, 0))
|
|
screen.blit(tooltip, (mouse[0] + 10, mouse[1] + 10))
|
|
|
|
index += 1
|