From b758d46139cb6db9cf1601e1e3a3399fa28bee0c Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Sun, 5 Nov 2023 19:43:41 +0100 Subject: [PATCH] Fixed crash: pygame.Surface has no collidepoint(). --- pg.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/pg.py b/pg.py index 334364a..ccf7d3b 100644 --- a/pg.py +++ b/pg.py @@ -23,8 +23,9 @@ class Hover: def __init__(self, function=None, ca=None, position: tuple=None, size: tuple=None): self.function = function self.ca = ca - self.x, self.y = position - self.w, self.h = size + if not position is None and not size is None: + self.x, self.y = position + self.w, self.h = size self.active = True def check(self, mouse_pos: tuple): @@ -32,7 +33,7 @@ class Hover: mx, my = mouse_pos if not self.ca is None: - if self.ca.position((mx, my)): + if self.ca.collidepoint((mx, my)): if self.function is None: return True @@ -59,8 +60,9 @@ class Hover: class Button: def __init__(self, function=None, ca=None, position: tuple=None, size: tuple=None, key: int=0): # ca = collide able, key: 0 = left 1 = mouse wheel pressed 2 = right - self.x, self.y = position - self.width, self.height = size + if not position is None and not size is None: + self.x, self.y = position + self.width, self.height = size self.function = function self.active = True self.ca = ca @@ -80,8 +82,9 @@ class Button: class TextButton: - def __init__(self, text: str, function=None, key: int=0, text_color: tuple=white, bg_color: tuple=gray, font: pygame.font.Font=default_font, padding: int=8): + def __init__(self, text: str, pos: tuple, function=None, key: int=0, text_color: tuple=white, bg_color: tuple=gray, font: pygame.font.Font=default_font, padding: int=8): self.text = text + self.pos = pos self.function = function self.text_color = text_color self.bg_color = bg_color @@ -102,12 +105,12 @@ class TextButton: w, h = self.text_object.get_size() w, h = w + self.padding * 2, h + self.padding * 2 - return pygame.Surface((w, h)) + return pygame.Rect(self.pos, (w, h)) - def blit(self, surface: pygame.Surface, pos: tuple): - x, y = pos + def blit(self, surface: pygame.Surface): + x, y = self.pos - surface.blit(self.background, pos) + pygame.draw.rect(surface, self.bg_color, self.background) surface.blit(self.text_object, (x + self.padding, y + self.padding)) def check(self, mouse_pos, pressed):