[Python] hovering only works on first button

Discussão em 'Python' iniciado por Stack, Fevereiro 9, 2025.

  1. Stack

    Stack Membro Participativo

    In my code:

    class text:
    def __init__(self, size, message, color, position, button = False, action = None):
    self.size = size
    self.message = message
    self.color = color
    self.position = position
    self.text_size = pygame.font.SysFont(None, int(size*displaywidth))
    self.Textsurface = self.text_size.render(self.message, True, self.color)
    Textrect = self.Textsurface.get_rect()
    self.Textrect = Textrect
    self.Textwidth = Textrect[2]
    self.Textheight = Textrect[3]
    self.second_x_pos = Textrect[2] + position[0]
    self.second_y_pos = Textrect[3] + position[1]
    self.button = button
    self.action = action



    def display(self):
    self.Textrect.topleft = (self.position)
    gameWindow.blit(self.Textsurface, self.Textrect)




    if self.button == True:
    self.Textrect.topleft = (self.position)
    gameWindow.blit(self.Textsurface, self.Textrect)
    for event in pygame.event.get():
    if event.type == pygame.MOUSEMOTION:
    if self.position[0] < event.pos[0] < self.second_x_pos and self.position[1] < event.pos[1] < self.second_y_pos:
    print("yee")
    self.color = white
    self.Textsurface = self.text_size.render(self.message, True, self.color)
    gameWindow.blit(self.Textsurface, self.Textrect)
    else:

    self.Textsurface = self.text_size.render(self.message, True, self.color)


    if event.type == pygame.MOUSEBUTTONUP :

    self.action()

    # menu screen
    def menu_screen():
    global wine
    global purple
    menu = True
    global displaywidth
    global displayheight
    global gameWindow
    global compltely_red
    global brown
    global red




    # Texts

    menu_txt = text(0.2,"Timm", red, (displaywidth/2,displayheight/9))
    Play_txt = text(0.04, "Play ", wine, (displaywidth/7, displayheight/1.5), True, game_loop)
    parallel_button = displaywidth - (displaywidth/7) - Play_txt.Textwidth
    Quit_txt = text(0.04, "Quit ", compltely_red, (parallel_button, displayheight/1.5), True, quit_Everything)




    #loop

    while menu == True:


    #the loop
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    quit_Everything()


    if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_ESCAPE:
    quit_Everything()
    if event.key == pygame.K_f:
    displaywidth = 1920
    displayheight = 1080
    gameWindow = pygame.display.set_mode((displaywidth,displayheight), pygame.FULLSCREEN)
    if event.key == pygame.K_g:
    displaywidth = 960
    displayheight = 960
    gameWindow = pygame.display.set_mode((displaywidth,displayheight))
    gameWindow.fill(green)

    menu_txt.display()
    Play_txt.display()
    Quit_txt.display()
    pygame.display.update()


    The first button that I call (The "Play" button) works perfectly (I am not yet concerned about the actions, what matters to me at the moment is the hovering). But the second button is very glitchy (when I inverted the order of how I called the buttons, the first one being called always works perfectly)

    Continue reading...

Compartilhe esta Página