--- /dev/null
+import pygame
+from color import Color
+from text import Label
+
+
+class Button(Label):
+ cooldown = 0.2 # seconds
+ def __init__(self, x, y, text, font, size, text_color=Color.BLACK, background=Color.WHITE, border_color=Color.BLACK,
+ border_width=1, border_radius=0, padding=10, func=None):
+ super().__init__(x, y, text, font, size, text_color, background, border_color, border_width, border_radius, padding)
+ self.is_hover = False
+ self.is_pressed = False
+ self.is_clicked = False
+
+ self.func = func
+
+ self.hover_color = Color.darken(self.background_color, 20)
+ self.pressed_color = Color.darken(self.background_color, 40)
+
+ self.last_click = 0
+ self.still_pressed = False
+
+
+ def update(self):
+ mouse_pressed = pygame.mouse.get_pressed()[0]
+ mouse_pos = pygame.mouse.get_pos()
+
+ self.is_hover = self.rect.collidepoint(mouse_pos)
+ self.is_clicked = self.is_hover and not mouse_pressed and self.is_pressed
+ self.is_pressed = self.is_hover and mouse_pressed
+
+ if self.still_pressed and not self.is_pressed:
+ self.still_pressed = False
+
+ if self.is_pressed:
+ if pygame.time.get_ticks() - self.last_click > self.cooldown * 1000 and not self.still_pressed:
+ self.still_pressed = True
+ self.last_click = pygame.time.get_ticks()
+
+ self.rect.set_background_color(self.pressed_color)
+ if self.func is not None:
+ self.func()
+
+ elif self.is_hover:
+ self.rect.set_background_color(self.hover_color)
+ else:
+ self.rect.set_background_color(self.background_color)
+
+ def draw(self, screen):
+ self.rect.draw(screen)
+ super().draw(screen)
class Color:
BLACK = (0, 0, 0)
+ DARK_GRAY = (64, 64, 64)
GRAY = (128, 128, 128)
+ LIGHT_GRAY = (192, 192, 192)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
+
+ @staticmethod
+ def darken(color, amount):
+ return max(0, color[0] - amount), max(color[1] - amount, 0), max(color[2] - amount, 0)
+
+ @staticmethod
+ def lighten(color, amount):
+ return min(255, color[0] + amount), min(color[1] + amount, 255), min(color[2] + amount, 255)
from color import Color
from rectangle import Rectangle
from text import Text, Label
+from button import Button
pygame.init()
a = Text(100, 100, "Hello World", "Arial", 20, color=Color.RED)
b = Label(100, 200, "Word hunt", "Imprint Shadow", 20, Color.RED, Color.BLACK, Color.BLACK, 1, 15)
c = Rectangle(100, 300, 100, 100, (255, 0, 0))
+d = Button(100, 200, "Play", "Imprint Shadow", 40, Color.BLUE, Color.BLACK, Color.BLACK, 1, 15, 20, lambda: print("Hello World"))
is_running = True
while is_running:
b.draw(screen)
c.draw(screen)
+ d.update()
+ d.draw(screen)
+
pygame.display.update()
delta = clock.tick(60) / 1000 # Seconds since last frame
def set_border_color(self, border_color):
self.border_color = border_color
+ def collidepoint(self, point):
+ return self.x <= point[0] <= self.x + self.width and self.y <= point[1] <= self.y + self.height
+
def draw(self, screen):
pygame.draw.rect(screen, self.background_color, (self.x, self.y, self.width, self.height),
border_radius=self.border_radius)
def get_height(self):
return self.text_surface.get_height()
- def update(self):
+ def update_text(self):
self.text_surface = self.font.render(self.text, True, self.text_color)
def set_position(self, x, y):
def set_text(self, text):
self.text = text
- self.update()
+ self.update_text()
def set_font(self, font, font_size):
self.font_size = font_size
self.font = pygame.font.SysFont(font, font_size)
- self.update()
+ self.update_text()
def set_color(self, text_color):
self.text_color = text_color
- self.update()
+ self.update_text()
def draw(self, screen):
screen.blit(self.text_surface, (self.x, self.y))
class Label(Text):
- padding = 10
- def __init__(self, x, y, text, font, font_size, background_color, text_color, border_color=Color.BLACK, border_width=0,
- border_radius=0):
+
+ def __init__(self, x, y, text, font, font_size, background_color, text_color, border_color=Color.BLACK,
+ border_width=0, border_radius=0, padding=10):
super().__init__(x, y, text, font, font_size, text_color)
self.background_color = background_color
self.border_color = border_color
self.border_width = border_width
self.border_radius = border_radius
+ self.padding = padding
self.width = self.text_surface.get_width() + self.padding * 2
self.height = self.text_surface.get_height() + self.padding * 2
- self.rect = Rectangle(self.x - self.padding, self.y - self.padding, self.width, self.height, self.background_color, self.border_color, self.border_width,self.border_radius)
+ self.rect = Rectangle(self.x - self.padding, self.y - self.padding, self.width, self.height,
+ self.background_color, self.border_color, self.border_width, self.border_radius)
def set_position(self, x, y):
super().set_position(x, y)
super().move(dx, dy)
self.rect.move(dx, dy)
- def update(self):
- super().update()
+ def update_text(self):
+ super().update_text()
self.width = self.text_surface.get_width() + self.padding * 2
self.height = self.text_surface.get_height() + self.padding * 2
self.rect.set_size(self.width, self.height)