Skip to main content
Engineering LibreTexts

6.7: Drawing the Board and Handling Input

  • Page ID
    14537
  •     while True: # main game loop
            clickedButton = None # button that was clicked (set to YELLOW, RED, GREEN, or BLUE)
            DISPLAYSURF.fill(bgColor)
            drawButtons()
    
            scoreSurf = BASICFONT.render('Score: ' + str(score), 1, WHITE)
            scoreRect = scoreSurf.get_rect()
            scoreRect.topleft = (WINDOWWIDTH - 100, 10)
            DISPLAYSURF.blit(scoreSurf, scoreRect)
    
            DISPLAYSURF.blit(infoSurf, infoRect)
    

    Line 1 [68] is the start of the main game loop. The clickedButton will be reset to None at the beginning of each iteration. If a button is clicked during this iteration, then clickedButton will be set to one of the color values to match the button (YELLOW, RED, GREEN, or BLUE).

    The fill() method is called on line 3 [70] to repaint the entire display Surface so that we can start drawing from scratch. The four colored buttons are drawn with a call to the drawButtons() (explained later). Then the text for the score is created on lines 6 [73] to 9 [76].

    There will also be text that tells the player what their current score is. Unlike the call to the render() method on line 51 for the instruction text, the text for the score changes. It starts off as 'Score: 0' and then becomes 'Score: 1' and then 'Score: 2' and so on. This is why we create new Surface objects by calling the render() method on line 6 [73] inside the game loop. Since the instruction text ("Match the pattern by...") never changes, we only need one call to render() outside the game loop on line 50.