Button image duplicating pygame!

Joined
Dec 7, 2022
Messages
2
Reaction score
0
I created class Button with hover method that change button image
I created an instance of Button - btn, indicated button_img andbutton_img_hover.
Unfortunately button image not change it's duplicating and overlaying. Any guess?
:

```
class Button:
def __init__(self, master, img, width, height, x,y,text,color, font = font):
self.font = font
self.surface = pg.Surface((width,height),pg.SRCALPHA, 32)
self.surface = self.surface.convert_alpha()
self.img = img
self.img = pg.transform.scale(self.img, (width, height))
self.rect = self.surface.get_rect()
self.rect.topleft = (x,y)
self.master = master
self.button_text = text
self.text = self.font.render(self.button_text, True, color)
self.text_rect = self.text.get_rect()
self.text_rect.center = self.surface.get_rect().center
self.width = width
self.height = height



def draw(self):
self.img = pg.transform.scale(self.img, (self.width, self.height))
self.surface.blit(self.img, (0,0))
self.surface.blit(self.text, self.text_rect)
self.master.blit(self.surface, self.rect)


def hover(self, img, imgh):
pos = pg.mouse.get_pos()

if self.rect.collidepoint(pos):
self.img = imgh

return True
else:
self.img = img

return False

def click(self, event):

if self.hover():
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
return True
else:
return False

btn= Button(window, button_img, 320,186,900,300,"Text on Button",(255,255,255), font=font)


runScene = True
while runScene:
window.blit(menu_bg,(0,0))
btn.draw()
clock.tick(30)

for event in pygame.event.get():
if event.type == pygame.QUIT:
runScene = False

btn.hover(button_img,button_img_hover)

pygame.display.update()

```
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top