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()

```[/CODE]
 
Joined
Dec 10, 2022
Messages
94
Reaction score
26
Maybe this will help
Python:
import pygame
from os import sys

pygame.init()
fps = 60
clock = pygame.time.Clock()

font = pygame.font.SysFont('Arial', 20)

window_size = (640,480)

screen = pygame.display.set_mode(window_size)

objects = []

def myfunc():
    print('Clicked Button')

def anotherfunc():
    print('Another button click')

class Button:
    def __init__(self, pos_x, pos_y, width, height, text='Button', click_func=None):
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.width = width
        self.height = height
        self.click_func = click_func

        self.state = {
            'normal': 'steelblue',
            'hover': 'skyblue',
            'press': 'royalblue'
        }

        self.btn_surface = pygame.Surface((self.width, self.height))
        self.btn_rect = pygame.Rect(self.pos_x, self.pos_y, self.width, self.height)

        self.btn_surf = font.render(text, True, (20,20,20))

        objects.append(self)

    def process(self):
        mouse_pos = pygame.mouse.get_pos()
        self.btn_surface.fill(self.state['normal'])

        if self.btn_rect.collidepoint(mouse_pos):
            self.btn_surface.fill(self.state['hover'])
            
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                self.click_func()

        self.btn_surface.blit(self.btn_surf, [self.btn_rect.width/2-self.btn_surf.get_rect().width/2,
        self.btn_rect.height/2-self.btn_surf.get_rect().height/2])

        screen.blit(self.btn_surface, self.btn_rect)


Button(30,30,400,100, 'Button One', myfunc)
Button(30,180,400,100, 'Button 2', anotherfunc)

running = True
while running:

    screen.fill('antiquewhite')

    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        sys.exit()

    for obj in objects:
        obj.process()

    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,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top