diff --git a/hotbar.py b/hotbar.py new file mode 100644 index 0000000..98a5735 --- /dev/null +++ b/hotbar.py @@ -0,0 +1,127 @@ +import pygame +from settings import * +from items import get_item + + +class Hotbar: + """Hotbar UI for quick access to items and weapons""" + + def __init__(self, inventory, weapon_manager): + self.inventory = inventory + self.weapon_manager = weapon_manager + self.selected_slot = 0 + self.visible_slots = HOTBAR_SLOT_COUNT + + # UI positioning + self.slot_size = 50 + self.slot_padding = 8 + self.background_color = (40, 40, 40) + self.selected_color = (100, 150, 255) + self.border_color = (150, 150, 150) + + # Center hotbar at bottom of screen + total_width = (self.slot_size * self.visible_slots) + (self.slot_padding * (self.visible_slots - 1)) + 20 + self.x = (SCREEN_WIDTH - total_width) // 2 + self.y = SCREEN_HEIGHT - 80 + + self.font = pygame.font.SysFont("consolas", 14) + + def handle_input(self, event): + """Handle hotbar input (number keys 1-8 and mouse clicks)""" + if event.type == pygame.KEYDOWN: + # Number keys 1-8 + if pygame.K_1 <= event.key <= pygame.K_8: + slot_index = event.key - pygame.K_1 + if slot_index < self.visible_slots: + self.selected_slot = slot_index + self.switch_weapon_at_slot() + return True + + if event.type == pygame.MOUSEBUTTONDOWN: + if event.button == 4: # Scroll up + self.selected_slot = (self.selected_slot - 1) % self.visible_slots + self.switch_weapon_at_slot() + return True + elif event.button == 5: # Scroll down + self.selected_slot = (self.selected_slot + 1) % self.visible_slots + self.switch_weapon_at_slot() + return True + + # Click on hotbar slots + if event.button == 1: + self.handle_click(event.pos) + return True + + return False + + def handle_click(self, mouse_pos): + """Handle clicking on hotbar slots""" + for i in range(self.visible_slots): + slot_x = self.x + i * (self.slot_size + self.slot_padding) + slot_y = self.y + slot_rect = pygame.Rect(slot_x, slot_y, self.slot_size, self.slot_size) + + if slot_rect.collidepoint(mouse_pos): + self.selected_slot = i + self.switch_weapon_at_slot() + break + + def switch_weapon_at_slot(self): + """Switch to weapon in selected slot""" + items_list = list(self.inventory.slots.items()) + if self.selected_slot < len(items_list): + item_id, _ = items_list[self.selected_slot] + # Check if it's a weapon + if item_id in WEAPON_STATS: + self.weapon_manager.switch_weapon(item_id) + + def get_selected_item(self): + """Get the item in the selected hotbar slot""" + items_list = list(self.inventory.slots.items()) + if self.selected_slot < len(items_list): + return items_list[self.selected_slot] + return None, None + + def draw(self, screen): + """Draw hotbar on screen""" + # Get inventory items + items_list = list(self.inventory.slots.items()) + + for i in range(self.visible_slots): + # Calculate slot position + slot_x = self.x + i * (self.slot_size + self.slot_padding) + slot_y = self.y + slot_rect = pygame.Rect(slot_x, slot_y, self.slot_size, self.slot_size) + + # Draw background + if i == self.selected_slot: + pygame.draw.rect(screen, self.selected_color, slot_rect) + else: + pygame.draw.rect(screen, self.background_color, slot_rect) + + # Draw border + pygame.draw.rect(screen, self.border_color, slot_rect, 2) + + # Draw item if exists + if i < len(items_list): + item_id, amount = items_list[i] + item = get_item(item_id) + + if item: + # Draw item icon (colored square) + icon_rect = pygame.Rect( + slot_x + 6, + slot_y + 6, + self.slot_size - 12, + self.slot_size - 12 + ) + pygame.draw.rect(screen, item.color, icon_rect) + + # Draw amount text + if amount > 1: + amount_text = self.font.render(str(amount), True, (255, 255, 255)) + screen.blit(amount_text, (slot_x + 4, slot_y + self.slot_size - 20)) + + # Draw slot number + num_text = self.font.render(str(i + 1), True, (200, 200, 200)) + screen.blit(num_text, (slot_x + 4, slot_y + 4)) \ No newline at end of file