mirror of
https://github.com/kristoferssolo/2048.git
synced 2025-10-21 15:20:35 +00:00
feat(game): create main menu
This commit is contained in:
parent
b0240d1b10
commit
61976e40aa
@ -7,6 +7,7 @@ from .board import Board
|
|||||||
from .config import Config
|
from .config import Config
|
||||||
from .logger import setup_logger
|
from .logger import setup_logger
|
||||||
from .screens.header import Header
|
from .screens.header import Header
|
||||||
|
from .screens.menu import Menu
|
||||||
from .utils import Direction
|
from .utils import Direction
|
||||||
|
|
||||||
|
|
||||||
@ -20,6 +21,7 @@ class Game:
|
|||||||
pygame.display.set_caption("2048")
|
pygame.display.set_caption("2048")
|
||||||
self.board = Board()
|
self.board = Board()
|
||||||
self.header = Header()
|
self.header = Header()
|
||||||
|
self.menu = Menu()
|
||||||
self.score = 0
|
self.score = 0
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
@ -31,13 +33,14 @@ class Game:
|
|||||||
|
|
||||||
def _update(self) -> None:
|
def _update(self) -> None:
|
||||||
"""Update the game."""
|
"""Update the game."""
|
||||||
self.board.update()
|
# self.board.update()
|
||||||
|
|
||||||
def _render(self) -> None:
|
def _render(self) -> None:
|
||||||
"""Render the game."""
|
"""Render the game."""
|
||||||
self.screen.fill(Config.COLORSCHEME.BG)
|
self.screen.fill(Config.COLORSCHEME.BG)
|
||||||
self.board.draw(self.screen)
|
# self.board.draw(self.screen)
|
||||||
self.header.draw(self.screen, self.score)
|
# self.header.draw(self.screen, self.score)
|
||||||
|
self.menu.draw(self.screen)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
def _hande_events(self) -> None:
|
def _hande_events(self) -> None:
|
||||||
@ -56,6 +59,7 @@ class Game:
|
|||||||
self.move_down()
|
self.move_down()
|
||||||
elif event.key == pygame.K_q:
|
elif event.key == pygame.K_q:
|
||||||
self.exit()
|
self.exit()
|
||||||
|
self.menu._handle_events(event)
|
||||||
|
|
||||||
def move_up(self) -> None:
|
def move_up(self) -> None:
|
||||||
self.score += self.board.move(Direction.UP)
|
self.score += self.board.move(Direction.UP)
|
||||||
|
|||||||
@ -12,7 +12,7 @@ class Label:
|
|||||||
font_family: str
|
font_family: str
|
||||||
font_color: ColorScheme
|
font_color: ColorScheme
|
||||||
font_size: int
|
font_size: int
|
||||||
font: pygame.font.Font = field(init=False)
|
font: pygame.Font = field(init=False)
|
||||||
rendered_text: pygame.Surface = field(init=False)
|
rendered_text: pygame.Surface = field(init=False)
|
||||||
rect: pygame.Rect = field(init=False)
|
rect: pygame.Rect = field(init=False)
|
||||||
|
|
||||||
@ -30,5 +30,5 @@ class Label:
|
|||||||
self.text = new_text
|
self.text = new_text
|
||||||
self._draw_text()
|
self._draw_text()
|
||||||
|
|
||||||
def draw(self, screen):
|
def draw(self, surface: pygame.Surface) -> None:
|
||||||
screen.blit(self.rendered_text, self.position)
|
surface.blit(self.rendered_text, self.position)
|
||||||
|
|||||||
53
src/py2048/screens/menu.py
Normal file
53
src/py2048/screens/menu.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import pygame
|
||||||
|
from loguru import logger
|
||||||
|
from py2048.config import Config
|
||||||
|
|
||||||
|
from .elements.button import Button
|
||||||
|
|
||||||
|
|
||||||
|
class Menu:
|
||||||
|
def __init__(self):
|
||||||
|
self.buttons = [
|
||||||
|
Button(
|
||||||
|
"Play",
|
||||||
|
Config.FONT_FAMILY,
|
||||||
|
Config.FONT_SIZE,
|
||||||
|
Config.COLORSCHEME.LIGHT_TEXT,
|
||||||
|
(Config.SCREEN_WIDTH / 2 - 50, Config.SCREEN_HEIGHT / 2 - 100),
|
||||||
|
100,
|
||||||
|
50,
|
||||||
|
self.play,
|
||||||
|
Config.COLORSCHEME.BOARD_BG,
|
||||||
|
Config.COLORSCHEME.BLOCK_0,
|
||||||
|
),
|
||||||
|
Button(
|
||||||
|
"Exit",
|
||||||
|
Config.FONT_FAMILY,
|
||||||
|
Config.FONT_SIZE,
|
||||||
|
Config.COLORSCHEME.LIGHT_TEXT,
|
||||||
|
(Config.SCREEN_WIDTH / 2 - 50, Config.SCREEN_HEIGHT / 2),
|
||||||
|
100,
|
||||||
|
50,
|
||||||
|
self.exit,
|
||||||
|
Config.COLORSCHEME.BOARD_BG,
|
||||||
|
Config.COLORSCHEME.BLOCK_0,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
def _handle_events(self, event: pygame.event.Event) -> None:
|
||||||
|
if event.type == pygame.MOUSEMOTION:
|
||||||
|
for button in self.buttons:
|
||||||
|
button.check_hover(event.pos)
|
||||||
|
elif event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
|
for button in self.buttons:
|
||||||
|
button.check_click(event.pos)
|
||||||
|
|
||||||
|
def draw(self, surface: pygame.Surface) -> None:
|
||||||
|
for button in self.buttons:
|
||||||
|
button.draw(surface)
|
||||||
|
|
||||||
|
def play(self) -> None:
|
||||||
|
logger.debug("Play")
|
||||||
|
|
||||||
|
def exit(self) -> None:
|
||||||
|
logger.debug("Exit")
|
||||||
0
src/py2048/screens/settings.py
Normal file
0
src/py2048/screens/settings.py
Normal file
Loading…
Reference in New Issue
Block a user