0

i am working through a pygame code and am in the process of creating a second menu sequentially, however unlike the first menu the second one differs in that it only works when holding down on the position of the button.

i am wondering if anyone can help differentiate what makes the two work and if there is a fix for it.

import random
import time
import pygame

pygame.init()

WIDTH = 500
HEIGHT = 500
screen = pygame.display.set_mode([WIDTH, HEIGHT])
fps = 60
timer = pygame.time.Clock()
main_menu = False
font = pygame.font.Font('freesansbold.ttf', 24)
menu_command = 0
p1score = 0
p2score = 0

class Button:
    def __init__(self, txt, pos):
        self.text = txt
        self.pos = pos
        self.button = pygame.rect.Rect((self.pos[0], self.pos[1]), (200, 40))

    def draw(self):
        pygame.draw.rect(screen, 'light gray', self.button, 0, 5)
        pygame.draw.rect(screen, 'dark gray', self.button, 5, 5)
        text = font.render(self.text, True, 'black')
        screen.blit(text, (self.pos[0] + 15, self.pos[1] + 7))

    def check_clicked(self):
        if self.button.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]:
            return True
        else:
            return False

def draw_game():
    button = Button('Main Menu', (120, 450))
    button.draw()
    menu = button.check_clicked()
    return menu


def draw_menu():
    command = -1
    pygame.draw.rect(screen, 'black', [100, 100, 300, 300])
    #exit menu button
    menu_button = Button('Exit Menu', (120, 350))
    btn1 = Button('Local Match', (120, 180))
    btn2 = Button('Online Match', (120, 240))
    btn3 = Button('Settings', (120, 300))
    menu_button.draw()
    btn1.draw()
    btn2.draw()
    btn3.draw()
    if menu_button.check_clicked():
        command = 0
    if btn1.check_clicked():
        command = 1
    if btn2.check_clicked():
        command = 2
    if btn3.check_clicked():
        command = 3
    return command


def turndecide():
    time.sleep(0.1)
    firstturn = -1
    p1button = Button('player 1', (120, 120))
    p2button = Button('player 2', (120, 180))
    ranbutton = Button('Random', (120, 240))
    firstturntext = font.render(f'please select who will take the first turn', True, 'black')
    screen.blit(firstturntext, (20, 20))
    p1button.draw()
    p2button.draw()
    ranbutton.draw()
    if p1button.check_clicked():
        firstturn = 1
    if p2button.check_clicked():
        firstturn = 2
    if ranbutton.check_clicked():
        firstturn = random.randint(1, 2)
    return firstturn

def localgame():
    screen.fill('white')
    turn = turndecide()
    if turn > 0:
        screen.fill('red')
        outputtext = font.render(f'player {turn} will move first', True, 'black')
        screen.blit(outputtext, (20, 20))


run = True
while run:
    screen.fill('white')
    timer.tick(fps)
    if main_menu:
        menu_command = draw_menu()
        if menu_command != -1:
            main_menu = False
    else:
        main_menu = draw_game()
        if menu_command > 0:
            if menu_command == 1:
                localgame()
            if menu_command == 2:
                onlinegame()
            if menu_command == 3:
                settings()

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

    pygame.display.flip()
pygame.quit()

i tried adding a while loop to ensure the code does not move backward as it does

def localgame():
    screen.fill('white')
    turn = turndecide()
    testvariable = True
    while testvariable:
        if turn > 0:
            screen.fill('red')
            outputtext = font.render(f'player {turn} will move first', True, 'black')
            screen.blit(outputtext, (20, 20))

but this causes the program to crash.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

0

In your check_clicked function, it will return True only if the user is actively clicking on the button. Whether or not you show the menu is, therefore, also linked to whether or not the user is actively clicking/holding the button.

Instead, each time the button is clicked, toggle the menu option. Only toggle the menu again once the player releases the mouse and presses it again. This avoids the menu being toggled really quickly constantly while holding the button down.

Perhaps, have a can_click variable that is set to False every time a click is registered and reset to True when the mouse is released.

Telan
  • 281
  • 1
  • 5