0

I have created a programme which has a home menu, when the user clicks on engine or multiplayer it should take them to a chess board graphic. However as soon as the mouse is released the chess board graphic disappears and the home menu is shown again. I have attached code below.

There is a variable, numberButton, which is used to see which button has been pressed and display the appropriate screen ie. the chess graphic. I am aware that as soon as the user stops clicking on the button that this variable is set back to zero which is why the main menu shows again however I do not know how to fix. Any suggestions would be appreciated :)

class button:
    def __init__(self, txt, pos):
        self.txt = txt 
        self.pos = pos
        self.button = pygame.rect.Rect((self.pos[0], self.pos[1]), (200,40))
    
    def draw(self, SCREEN):
        pygame.draw.rect(SCREEN, 'light gray', self.button, 0 ,5)
        pygame.draw.rect(SCREEN, 'dark gray', self.button, 5, 5)
        txt = FONT.render(self.txt , True, 'black')
        SCREEN.blit(txt, (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 drawMenu(SCREEN):
    titleText = FONT2.render('CHESS',True,  'black')
    SCREEN.blit(titleText, (70,70))
    knightImage = pygame.transform.scale(pygame.image.load("Images/bK.png"),(200,200))
    SCREEN.blit(knightImage, (300,575))
    numberButton = 0
    #Inizializes all of the buttons and draws them onto the screen 
    engineButton = button('Multiplayer', (300, 300))
    engineButton.draw(SCREEN)
    multiplayerButton = button('Engine', (300, 400))
    multiplayerButton.draw(SCREEN)
    exitButton = button('QUIT', (300, 500))
    exitButton.draw(SCREEN)
    #Checks to see if the button has been pressed. Assigns a corresponding number to variable numberButton which can be used later when displaying next screen.
    if engineButton.check_clicked():
        numberButton = 1
    elif multiplayerButton.check_clicked():
        numberButton = 2 
    elif exitButton.check_clicked():
        numberButton = 3

    return numberButton


    while RUNNING:
        
        SCREEN.fill('light blue')
        drawMenu(SCREEN)
        
        if drawMenu(SCREEN) == 1:
            drawBoard(SCREEN, board)
        elif drawMenu(SCREEN) == 2:
            drawBoard(SCREEN, board)
        elif drawMenu(SCREEN) == 3:
            RUNNING = False
        
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                RUNNING = False
        

i did try and use a while loop instead of the if loop to say while numberButton == 1: display the chess graphics, however this just crashed the programme.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Max
  • 1
  • 1

0 Answers0