sand/button.py
2024-02-25 11:35:01 +01:00

39 lines
1.4 KiB
Python

import pygame
class Button():
def __init__(self, screen, color, pos, width, height, text) -> None:
self.pos = pos
self.größe = width,height
self.color = color
self.screen = screen
self.text = text
def draw(self):
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])
)
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
)
font = pygame.font.Font(None, 30)
text_surface = font.render(self.text, True, (0,0,0))
text_rect = text_surface.get_rect()
text_rect.center = (self.pos[0],
self.pos[1])
self.screen.blit(text_surface, text_rect)
def check_hovered(self, mouse):
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):
return True
else:
return False