Published on

Memory Game using Python and Pygame - Part 3(Coding Main method)

Welcome back to the Memory Game using Python and Pygame series. This is part 3 of the series where we show you the implementation of the Main method along with some extra functions.

Here is the YouTube video for the part 3 where we go into much more depth on how to build this game:

Video for Part 3

Here are the timestamps for this video:

  • 0:00 Intro to Part 3
  • 0:26 Coding Helper Functions
  • 4:59 Coding Main Method
  • 12:50 Info about Part 4

Along with all the supporting links for the video:

Video Summary

Here is a quick recap of the important sections covered in the video above:

  • The text_objects(text, font) method is used to return a pygame Surface object along with its corresponding rect object
  • The button(msg, x, y, w, h, ic, ac, action=None) method is used to create the buttons in the Pause menu. The msg is for the button text, the x & y arguments are for the left and top coordinates of which the button will be positioned. The w & h arguments are for the width and height of the button, respectively. Lastly the ic argument is a string value representing the color the button should be in when it is hovered on, while the ac argument is a color value that the button should take when it is clicked on. Finally, the button is drawn onto the screen via the blit() method
  • The main() can be regarded as the control center for the entire application as it controls the flow and state of the game. Here is a brief overview of what it does:
    • Initialize pygame and the main display window
    • Initialize the game board via the getRandomizedBoard() & generateRevealedBoxesData(val)
    • Start the main game loop(which is a while loop) by drawing the the game board onto the screen(via the drawBoard(mainBoard, revealedBoxes) function)
    • Display the game score & level text on the top left of the screen
    • Listen for events from If the ESC key is pressed then the pause menu will pop up while moving and/or clicking of the mouse will be tracked the mousex, mousey and mouseClicked variables, respectively
    • Check whether the registered click event was for a first or second selection of the boxes in the game board
      • If it's for a first selection then just note the appropriate sprite in the firstSelection tuple.
      • If it's for a second selection then compare the second sprite to the first sprite. If there is a match then leave the matched boxes open while the hasWon(revealedBoxes) method is used to check for a level up condition. If there isn't a match then simply cover the boxes back up
      • If a level up condition is reached(where all the boxes are revealed) then display the end game animation and restart the game board initialization process using the getRandomizedBoard() and drawBoard() methods while adding an extra row & column via the levelUp() function. Finally, the startGameAnimation() function is called in order display the starting animation
    • Finally, continually update the pygame window using the pygame.display.update() function after setting the frame rate for the game as 30 using the FPSCLOCK.tick(FPS) function


Thanks for following along in this tutorial and if you have any questions or concerns please feel free to post a comment in this article and I will get back to you if I find the time.

Well that’s all for today, I hope you found this article helpful. Thanks so much for reading my article! Feel free to follow me on Twitter and GitHub, connect with me on LinkedIn and subscribe to my YouTube channel.