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

15
pg.py
View file

@ -23,6 +23,7 @@ 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
if not position is None and not size is None:
self.x, self.y = position self.x, self.y = position
self.w, self.h = size self.w, self.h = size
self.active = True self.active = True
@ -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,6 +60,7 @@ 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
if not position is None and not size is None:
self.x, self.y = position self.x, self.y = position
self.width, self.height = size self.width, self.height = size
self.function = function self.function = function
@ -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):