Nov-22-2021, 07:25 PM
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
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
#! /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
Download my project scripts
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts