- 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:
- Here is the source code for the game, in case you get stuck: https://github.com/ShehanAT/PythonMiniProjects/tree/master/memoryGame
- If you need to download the image files for the gems used in this game, here is the GitHub repo link to do so: https://github.com/ShehanAT/PythonMiniProjects/tree/master/memoryGame/assets
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 pygameSurface
object along with its correspondingrect
object - The
button(msg, x, y, w, h, ic, ac, action=None)
method is used to create the buttons in the Pause menu. Themsg
is for the button text, thex & y
arguments are for the left and top coordinates of which the button will be positioned. Thew & h
arguments are for the width and height of the button, respectively. Lastly theic
argument is a string value representing the color the button should be in when it is hovered on, while theac
argument is a color value that the button should take when it is clicked on. Finally, the button is drawn onto the screen via theblit()
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 thedrawBoard(mainBoard, revealedBoxes)
function) - Display the game score & level text on the top left of the screen
- Listen for events from
pygame.events.get()
. If theESC
key is pressed then the pause menu will pop up while moving and/or clicking of the mouse will be tracked themousex, mousey
andmouseClicked
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()
anddrawBoard()
methods while adding an extra row & column via thelevelUp()
function. Finally, thestartGameAnimation()
function is called in order display the starting animation
- If it's for a first selection then just note the appropriate sprite in the
- Finally, continually update the pygame window using the
pygame.display.update()
function after setting the frame rate for the game as 30 using theFPSCLOCK.tick(FPS)
function
Conclusion
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.