Pico-8 Bach Inventions Effects ~ Day 13: Make a Tetris-clone! (Patreon)
Content
Hi everyone!
Portrait #13 is all about Tetris. So this post will be about Tetris too and how I would implement it, based on what I made for this portrait!
I'm actually going to focus on the grid-based logic of the game, and mainly on defining the tetraminoes and having them fall down and settle on collision.
Here's the sprite-sheet!
So let's start with how to define tetraminoes!
As you can see, I laid out each of the tetraminoes in all their 4 rotations, on the sprite-sheet. Here, there's one tetramino per sprite, but that's just because I had space to waste, you could definitely have one tetramino per 4x4 pixels and use up only 1/4th of the space I used. (you could even use binary masks and take up only 1/16th of the space I used, but let's not get into that)
When defining a new tetramino, we will first get an id for it, a random number between 0 and 6 (both included - there's 7 different tetraminoes), and a rotation (you can just make it 0 or you can make it random). Then, we make a table-based 4x4 grid and we fill it with the pixels from the sprite-sheet corresponding to our tetramino and its rotation.
Store all those things, plus x and y coordinates, into a neat table object and you have your tetramino defined. Now we need to test if it actually fits on the board where we created it. If it doesn't, that would be because the board is full and that'd be a good time for a game over.
So how to test if it fits into the board? This is obviously a very important part of the game, so I suggest you put it in an isolated function and try to make it as clean and simple as possible. The function should take the tetramino object as parameter and it should return a boolean, true if the piece does fit where it is on the board, false if it doesn't. (or the opposite; you do you)
This function goes like this:
- Take the tetramino's shape-defining grid.
- Compare each cell of the grid with the corresponding cell of the board, relatively to the tetramino's position.
- If there's a 'solid' cell of the grid at the same spot as a 'solid' cell on the board, then return false, the tetramino does not fit!
- If no such case is found, simply return true at the end of the function, the tetramino does fit.
Note that your board might be another table-based grid, or you might use the map, or even a portion of the sprite-sheet. That's your call!
We'll use this function to check for the game over but we will also use it to check for collisions when the tetramino is descending. When that happens, we want the tetramino to return to its previous position (before the collision), burn its shape grid into the board, and get deleted. (and create a new tetramino at the top of the board, the game goes on)
To burn the tetramino into the board, simply take its shape grid again and for any solid cell you find in it, set the corresponding board cell as solid. You might want to use the cell's shape id as 'solid state' value on the board, so that you can draw it with a color or sprite unique to the tetramino shape when drawing the board onto the screen.
Then, check if any line of the board is complete and if it is, empty it and make everything above it on the board drop by one tile. (also, put a nice clearing effect on it and give the player some points)
Last important thing we didn't mention is tetramino rotation. I would simply redefine the piece's shape-defining grid with the new rotation, from the sprite-sheet again. And then check if the piece still fits on the board where it is. If it doesn't, go back to the old rotation, and don't forget to reset the shape grid too!
And that's pretty much it! With all this, you should be able to make your own tetris-clone!
Remember that making clones of existing games is super fine, as long as you go further than the original concept and put a significant something of your own into it. (or if it's just for training, but even then some originality would just make it a better exercise and it would make the result much better as a creation)
Ok, thank you for reading! Ask any questions you might have!
As usual, the downloads for this portrait are available to my 5$+ supporters over there!
Take care and have a nice week-end!
TRASEVOL_DOG