Who is this Project For?
This is an intermediate-level project for those who are new to Python. Before beginning, you should already have experience with Turtle. You should also understand key concepts such as creating functions and variables, using operators and booleans, and creating loops. These will be the core concepts used in this project. Better for students who completed Python Level 2.
What Will We Learn?
This project focuses on using knowledge of Turtle Graphics, python operators, booleans, and functions. By the end of the project, you will be able to use these concepts to create a fun and functional game.
Features to Consider:
- The game screen should have an 8x8 grid of squares
- There should be an image of some sort behind the grid, which is revealed when the tiles are matched
- When a player clicks on one tile, its number will show but will hide when another tile is selected.
- When the player clicks two tiles in a row that match, the tiles will disappear, showing part of the image.
Main Steps:
This project has 6 main steps:
- General setup
- Setup main game components
- Define and setup grid squares
- Define main game functions
- Create draw function
- Use functions and state final game commands
Step 1: General Setup
The first thing we’ll need to do is set up all the libraries and modules we’re going to use. For this project, we’ll be using turtle, random, and freegames. To do this we’ll type:
Step 2: Setup Main Game Components
Once we've imported all the libraries and modules we need, we'll set up the game's main components. We will need an image for the background (In this case I’ve used a car). We will also need to set up our tile numbers and states and add the code that will hide certain elements when needed. Let’s begin by coding the car image and the tiles:
Hint: We’ve created a list of 31 numbers from 0-31, and then multiplied it by 2 to make two of each tile
Next, we’ll add the code to set the make to None, and set hide to true. By the end of this step, your code will look like this:
Hint: We’ve set ‘mark’ to none. None is a null value, it is not 0 or false, it is a data type of its own.
Step 3: Define and Setup Grid Squares
Now that we’re done with the basic setup, we can start defining the game’s main functions. To do this, we’ll start by creating the tiles. We will create a function called ‘square’ with both x and y coordinates. This will let us decide where to place the tiles. To do this, we’ll type:
Hint: We’ve used a for loop to draw our tiles because it makes the code shorter and more efficient.
Now that we’re done with our tiles, we can move on to defining the functions that will make up the main game.
Step 4: Define Main Game Functions
Now that the setup is complete, it’s time to create 3 of our main functions:
- Index: this function will convert the x and y-coordinates to the tile index
- XY: this function will convert the tile count to x and y-coordinates
- Tap: this function will hide or show the tiles based on which ones are clicked
To create our index and XY functions, we’ll type:
Hint: These two functions will allow us to create a grid over the image and keep track of which ones have been clicked.
Next, we’ll create our tap function. This will hide or show the tiles and their marks depending on when has been clicked. In the end, our code will look like this:
Step 5: Create Draw Function
Now that we’ve finished with the index, xy, and tap functions, we only have one left. We’ll be creating our Draw function, which will draw the image and tiles, and also has the logic part of the game.
The function is made up of 4 main components:
- Clear the canvas and draw the car image
- For loop to draw squares
- If statement to manage the text lapels
- Update and timer functions to continually refresh the game
We’ll start out by typing:
Hint: Notice how we’ve used a for loop before the if-statement to more efficiently manage the statement and to make sure all the tiles are checked.
Now we only have to make the second if-statement to manage the labels on the tiles and create the code to update the game. We’ll be using the update() and ontimer() functions to ensure that the game is constantly updating. In the end the code will look like this:
Step 6: Create Final Game Commands
Finally, we need to add a few more lines of code to shuffle the tiles, draw the car image, and manage what will happen when the player clicks on a tile. To do this we’ll type:
Project Complete!
And now we’re done! Feel free to test your code and see how it works. If you’re stuck or have any issues with your code, try reviewing it to see if you’ve made any mistakes, or check out the code mentioned above as a reference.