2

I am trying to add some buttons to my program and I have managed to put them on there. However, they keep flickering and I'm not sure how to stop that problem. The sizes and positioning are going to be changed later I just used random positions for now so ignore that. I put pygame.display.flip() everywhere hoping that would fix it. However, it did not. Thank you

import pygame
import pygame.freetype
from pygame.sprite import Sprite
from pygame.rect import Rect

PINK = (250, 100, 100)
WHITE = (255, 255, 255)
BLACK = (0,0,0)

#initialize pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))

#images
loginBut = pygame.image.load('loginBut.png').convert_alpha()
signupBut = pygame.image.load('signupBut.png').convert_alpha()

pygame.display.flip()

#variables
xbut = 300
ybut1 = 350
ybut2 = 450

def create_surface_with_text(text, font_size, text_rgb, bg_rgb):
    font = pygame.freetype.SysFont("Arial", font_size, bold=True)
    surface, _ = font.render(text=text, fgcolor=text_rgb, bgcolor=bg_rgb)
    return surface.convert_alpha()

def loginButton(xbut,ybut1):
    screen.blit(loginBut,(xbut,ybut1))
    pygame.display.update()

def signupButton(xbut,ybut2):
    screen.blit(signupBut,(xbut,ybut2))
    pygame.display.update()

    pygame.display.flip()

class UIElement(Sprite):
   
    def __init__(self, center_position, text, font_size, bg_rgb, text_rgb):
     
        self.mouse_over = False  

        # what happens when the mouse is not over the element
        default_image = create_surface_with_text(
            text=text, font_size=font_size, text_rgb=text_rgb, bg_rgb=bg_rgb
        )

        # what happens when the mouse is over the element
        highlighted_image = create_surface_with_text(
            text=text, font_size=font_size * 1.1, text_rgb=text_rgb, bg_rgb=bg_rgb
        )

        self.images = [default_image, highlighted_image]
        self.rects = [
            default_image.get_rect(center=center_position),
            highlighted_image.get_rect(center=center_position),
        ]

        super().__init__()

    @property
    def image(self):
        return self.images[1] if self.mouse_over else self.images[0]

    @property
    def rect(self):
        return self.rects[1] if self.mouse_over else self.rects[0]

    def update(self, mouse_pos):
        if self.rect.collidepoint(mouse_pos):
            self.mouse_over = True
        else:
            self.mouse_over = False

    def draw(self, surface):
        surface.blit(self.image, self.rect)

        pygame.display.flip()


def main():
    pygame.init()
    
    screen = pygame.display.set_mode((800, 600))

    uielement = UIElement(
        center_position=(400, 100),
        font_size=40,
        bg_rgb=PINK,
        text_rgb=BLACK,
        text="Welcome to the Automated Timetable Program",
    )

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        screen.fill(PINK)

        uielement.update(pygame.mouse.get_pos())
        uielement.draw(screen)
        pygame.display.flip()
        loginButton(xbut,ybut1)
        signupButton(xbut,ybut2)
        pygame.display.flip()

if __name__ == "__main__":
    main()

MW03
  • 61
  • 1

1 Answers1

1

The issue is caused by the multiple calles of pygame.display.flip(). Multiple calls to pygame.display.update() or pygame.display.flip() cause flickering. Remove all the calls of pygame.display.flip() from the code, but call it once at the end of the application loop.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174