GoL/button.py

53 lines
1.4 KiB
Python
Raw Normal View History

2024-02-28 17:44:06 +01:00
import pygame
2024-02-28 19:47:39 +01:00
class Button:
2024-02-28 17:44:06 +01:00
def __init__(self, screen, color, pos, width, height, text) -> None:
self.pos = pos
2024-02-28 19:47:39 +01:00
self.größe = width, height
2024-02-28 17:44:06 +01:00
self.color = color
self.screen = screen
self.text = text
2024-02-28 19:47:39 +01:00
2024-02-28 17:44:06 +01:00
def draw(self):
2024-02-28 19:47:39 +01:00
pygame.draw.rect(
self.screen,
self.color,
(
self.pos[0] - self.größe[0] // 2,
self.pos[1] - self.größe[1] // 2,
self.größe[0],
self.größe[1],
),
2024-02-28 17:44:06 +01:00
)
2024-02-28 19:47:39 +01:00
pygame.draw.rect(
self.screen,
(0, 150, 0),
(
self.pos[0] - self.größe[0] // 2,
self.pos[1] - self.größe[1] // 2,
self.größe[0],
self.größe[1],
),
2,
2024-02-28 17:44:06 +01:00
)
2024-02-28 19:47:39 +01:00
2024-02-28 17:44:06 +01:00
font = pygame.font.Font(None, 30)
2024-02-28 19:47:39 +01:00
text_surface = font.render(self.text, True, (0, 0, 0))
2024-02-28 17:44:06 +01:00
text_rect = text_surface.get_rect()
2024-02-28 19:47:39 +01:00
text_rect.center = (self.pos[0], self.pos[1])
2024-02-28 17:44:06 +01:00
self.screen.blit(text_surface, text_rect)
2024-02-28 19:47:39 +01:00
2024-02-28 17:44:06 +01:00
def check_hovered(self, mouse):
2024-02-28 19:47:39 +01:00
if (
self.pos[0] - self.größe[0] // 2
<= mouse[0]
<= self.pos[0] + self.größe[0] // 2
and self.pos[1] - self.größe[1] // 2
<= mouse[1]
<= self.pos[1] + self.größe[1] // 2
):
2024-02-28 17:44:06 +01:00
return True
else:
return False