Published on

Memory Game using Python and Pygame – Part 2(Coding Display & Animation Functions)

Welcome back to the Memory Game using Python and Pygame series. This is part 2 of the series where we show you the implementation of the Display and Animation functions.

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

Video for Part 2

Here are the timestamps for this video, for your convenience:

  • 0:00 Introduction to Part 2
  • 0:28 Coding Display Function
  • 7:38 Coding Animation Functions
  • 15:46 Closing Notes

Along with all the supporting links for the video:

Video Summary

Here is a quick recap of each of the functions covered in the video:

  • generateRevealedBoxedData(val): This function outputs an array containing boolean values, each of which represents a box in the game board. A False value indicates that the corresponding box should be closed while a True value indicates that it should be opened
  • getRandomizedBoard(): This function creates the 2d array representing the game board. Then the 2d array is shuffled and transposed multiple times in order to ensure randomization of the sprites contained in it. Note: To transposed a 2d array means reverse the axes of the array so that it is basically flipped on it’s head, metaphorically speaking of course 😀
  • splitIntoGroupsOf(groupSize, theList): This function segregates the 2d array into smaller groups of arrays, with the group size being decided by the groupSize argument
  • leftTopCoordsOfBox(boxx, boxy): This function gets the left and top pixel coordinates of any pixel coordinate values passed in as boxx(for the x axis) and boxy(for the y-axis). It is useful for know where to position each box when it is drawn onto the game board
  • getBoxAtPixel(x, y): This function get the box at which a mouse click occurred, notified using the x and y arguments. It is used to identify which box the user clicks when the game is in progress
  • drawIcon(shape, color, boxx, boxy): This function draws sprites on to the game board using the arguments passed into it, namely shape(for chosing the shape), color(for choosing the color, boxx & boxy(for specifying the sprite’s position)
  • getShapeAndColor(board, boxx, boxy): This function gets the shape and color for an box that is passed into it. It is used extensively in order to compare the first and second box selections made by the user
  • drawBoxCovers(board, boxes, coverage): This function draws box covers, with the extent of the cover being decided by the coverage variable. The boxes that are to be covered are decreed by the boxes argument
  • revealBoxesAnimation(board, boxesToReveal): This function reveals any box that is contained in the boxesToReveal array argument for 1 second, predominately done using the coverage variable
  • coverBoxesAnimation(board, boxesToCover): This function is the opposite of the revealBoxesAnimation(board, boxesToReveal) function in that it covers boxes instead of reveals them
  • drawBoard(board, boxesToCover): This function is responsible for drawing boxes onto the game board, done using the blit() method. A Boolean value is used to decide whether to cover a given box or to reveal it(False to cover, True to reveal)
  • startGameAnimation(board): This function is used to start the game animation process. It repeatedly calls the revealedBoxesAnimation() and coverBoxesAnimation() functions to accomplish this goal
  • gameWonAnimation(board): This function initiates the animation for when a level up condition has been met. It also calls the drawBoard() function in order to draw the board for the next level
  • hasWon(revealedBoxes): This function checks if a level up condition has been met. If a box matching event occurs with other boxes still being covered, returns False. If all boxes are currently revealed then it returns True

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 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.