Fixed crash: pygame.Surface has no collidepoint().

This commit is contained in:
The Wobbler 2023-11-05 19:43:41 +01:00
parent 9f8c4ce1a7
commit b758d46139

23
pg.py
View file

@ -23,8 +23,9 @@ class Hover:
def __init__(self, function=None, ca=None, position: tuple=None, size: tuple=None): def __init__(self, function=None, ca=None, position: tuple=None, size: tuple=None):
self.function = function self.function = function
self.ca = ca self.ca = ca
self.x, self.y = position if not position is None and not size is None:
self.w, self.h = size self.x, self.y = position
self.w, self.h = size
self.active = True self.active = True
def check(self, mouse_pos: tuple): def check(self, mouse_pos: tuple):
@ -32,7 +33,7 @@ class Hover:
mx, my = mouse_pos mx, my = mouse_pos
if not self.ca is None: if not self.ca is None:
if self.ca.position((mx, my)): if self.ca.collidepoint((mx, my)):
if self.function is None: if self.function is None:
return True return True
@ -59,8 +60,9 @@ class Hover:
class Button: 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 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 if not position is None and not size is None:
self.width, self.height = size self.x, self.y = position
self.width, self.height = size
self.function = function self.function = function
self.active = True self.active = True
self.ca = ca self.ca = ca
@ -80,8 +82,9 @@ class Button:
class TextButton: 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.text = text
self.pos = pos
self.function = function self.function = function
self.text_color = text_color self.text_color = text_color
self.bg_color = bg_color self.bg_color = bg_color
@ -102,12 +105,12 @@ class TextButton:
w, h = self.text_object.get_size() w, h = self.text_object.get_size()
w, h = w + self.padding * 2, h + self.padding * 2 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): def blit(self, surface: pygame.Surface):
x, y = pos 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)) surface.blit(self.text_object, (x + self.padding, y + self.padding))
def check(self, mouse_pos, pressed): def check(self, mouse_pos, pressed):