Fix menu crash: - Add back MenuItem.draw method - Remove duplicate MenuItem class - Fix menu item imports

This commit is contained in:
Rbanh 2025-02-23 13:06:09 -05:00
parent d1fb3a58b4
commit 251822ec35
2 changed files with 5 additions and 30 deletions

View File

@ -3,7 +3,7 @@ import sys
from enum import Enum, auto
from src.snake import Snake, Direction
from src.food import Food
from src.menu import Menu, GameMode
from src.menu import Menu, GameMode, MenuItem
class GameState(Enum):
MENU = auto()
@ -19,35 +19,6 @@ class GameRules:
self.min_move_cooldown = 50 # Minimum movement delay in milliseconds
self.initial_move_cooldown = 100 # Initial movement delay
class MenuItem:
def __init__(self, text, position, action, size=36, color=(255, 255, 255)):
self.text = text
self.position = position
self.action = action
self.size = size
self.default_color = color
self.color = color
self.hover = False
self._setup_font()
def _setup_font(self):
self.font = pygame.font.Font(None, self.size)
self.surface = self.font.render(self.text, True, self.color)
self.rect = self.surface.get_rect(center=self.position)
def update_hover(self, mouse_pos):
old_hover = self.hover
self.hover = self.rect.collidepoint(mouse_pos)
if self.hover:
self.color = (255, 255, 0) # Yellow on hover
else:
self.color = self.default_color
if old_hover != self.hover:
self._setup_font()
def draw(self, screen):
screen.blit(self.surface, self.rect)
class SettingsMenu:
def __init__(self, width, height, rules):
self.width = width

View File

@ -35,6 +35,10 @@ class MenuItem:
if old_hover != self.hover:
self._setup_font()
def draw(self, screen):
"""Draw the menu item on the screen"""
screen.blit(self.surface, self.rect)
class Menu:
def __init__(self, width, height):
self.width = width