-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.py
68 lines (54 loc) · 2.18 KB
/
menu.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
from typing import Callable
from pygame import Surface
import pygame
from CONSTS import GAME_NAME
class Button:
def __init__(self, x: int, y: int, image: Surface, scale: float, action: Callable):
width = image.get_width()
height = image.get_height()
self.image = pygame.transform.scale(image, (int(width * scale), int(height * scale)))
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
self.action = action
def draw(self, screen: Surface):
screen.blit(self.image, (self.rect.x, self.rect.y))
def on_click(self, mouse: tuple[int, int]):
if self.rect.collidepoint(mouse):
self.action()
class Text:
def __init__(self, x: int, y: int, text: str, font: pygame.font.Font, shadow=False, color=(255, 255, 255)):
self.x = x
self.y = y
self.text = text
self.font = font
self.shadow = shadow
self.color = color
def draw(self, screen: Surface):
if self.shadow:
shadow_surface = self.font.render(self.text, False, (0, 0, 0))
screen.blit(shadow_surface, (self.x+5, self.y+5))
text_surface = self.font.render(self.text, False, self.color)
screen.blit(text_surface, (self.x, self.y))
class Menu:
def __init__(self, title: str, width: int, height: int, background: Surface, buttons: list[Button] = [],
texts: list[Text] = []):
self.title = title
self.width = width
self.height = height
self.background = pygame.transform.scale(background, (width, height))
self.buttons: list[Button] = buttons
self.texts: list[Text] = texts
def draw(self, screen: Surface):
pygame.display.set_caption(GAME_NAME + " - " + self.title)
screen.blit(self.background, (0, 0))
for button in self.buttons:
button.draw(screen)
for text in self.texts:
text.draw(screen)
def add_button(self, button: Button):
self.buttons.append(button)
def add_text(self, text: Text):
self.texts.append(text)
def on_click(self, mouse: tuple[int, int]):
for button in self.buttons:
button.on_click(mouse)