Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Button Instance
#1
I'm playing around with making a button class and have come across problem.
As long as I just have one button the cursor works as expected but, if I place two or more the cursor flickers.

Any insight appreciated.
See video



#! /usr/bin/env python3.10

# Do the imports
import pygame
import os

# Get current working directory
path = os.getcwd()

# Initialize pygame
pygame.init()

# Set screen size
screen = pygame.display.set_mode((800,600))

# Set and display the window icon
# game_icon = pygame.image.load(f'{path}/icons/ratt.ico')
# pygame.display.set_icon(game_icon)

# Set a hand cursor
hand = pygame.SYSTEM_CURSOR_HAND
arrow = pygame.SYSTEM_CURSOR_ARROW

# Set and display window title
pygame.display.set_caption('My Game')

# pygame clock for setting frame rate
clock = pygame.time.Clock()

class Button:
    def __init__(self):
        self.text = 'Button'
        self.x = 0
        self.y = 0
        self.font_size = 30
        self.bgcolor = (110, 110, 255)
        self.fgcolor = (255, 255, 255)
        self.border_color = (90, 90, 90)
        self.shadow_color = (180, 180, 180)
        self.pos = pygame.mouse.get_pos()


    def show(self):

        btn_text = pygame.font.SysFont(None, self.font_size)

        button_shadow = pygame.Rect(self.x+3, self.y+3, btn_text.size(self.text)[0]*2, \
                                    btn_text.size(self.text)[1]*2+1)
        border = pygame.Rect(self.x-1, self.y-1, (btn_text.size(self.text)[0]*2)+2, \
                             (btn_text.size(self.text)[1]*2)+3)
        button = pygame.Rect(self.x, self.y, btn_text.size(self.text)[0]*2, btn_text.size(self.text)[1]*2)
        text = btn_text.render(f'{self.text}', True, self.fgcolor)

        if button.collidepoint(self.pos[0], self.pos[1]):
            pygame.mouse.set_cursor(hand)
            self.bgcolor = (150,150,255)
            self.fgcolor = (250,250,255)
        else:
            pygame.mouse.set_cursor(arrow)


        pygame.draw.rect(screen, self.shadow_color, button_shadow)
        pygame.draw.rect(screen, self.border_color, border)
        pygame.draw.rect(screen, self.bgcolor, button)
        screen.blit(text, (self.x+button.width/4, self.y+button.height/4))


# Main program
def main():
    running = True

    while running:
        screen.fill('ivory')

        btn = Button()
        btn.x = 220
        btn.y = 210
        btn.show()

        btn2 = Button()
        btn2.text = 'Button 2'
        btn2.x = 420
        btn2.y = 210
        btn2.show()

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


        pygame.display.update()
        clock.tick(60)

if __name__ == '__main__':
    main()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
Button Instance - by menator01 - Nov-22-2021, 07:25 PM
RE: Button Instance - by BashBedlam - Nov-22-2021, 10:18 PM
RE: Button Instance - by menator01 - Nov-23-2021, 08:51 AM
RE: Button Instance - by BashBedlam - Nov-23-2021, 04:09 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020