Loading
Tetris. It's a classic, right? Everyone's played it, seen it, or at least heard of it, so, I decided to make my own version. The idea to make this actually sparked when I downloaded the mobile version of Tetris and got fed up with the constant invasive ads. Pay 2 bucks to remove the ads, or just spend a month making the entire game yourself so you don't have to deal with ads. I mean... it's clearly a no-brainer.
The game mechanics are pretty standard for Tetris. Tetrominos fall from the top to the bottom of the play area, and you can move them sideways and rotate them. When a tetromino lands, if it completes a row, that row clears, and you earn points. As you clear more rows, the game speeds up, making it more challenging. The goal is simple: keep the tetrominos from stacking up to the top of the play area for as long as possible. Classic Tetris fun, full of speed and strategy!
Working on this project was a blast. It was fun, for sure, but it also opened my eyes to the fact that even seemingly small projects can present some major challenges.
At the core of the game is the fundamental gameplay loop: a tetromino spawns, falls, and locks in place when it reaches the bottom or another piece. This triggers the appearance of the next randomized piece, and the cycle repeats. Mastering this loop was crucial—it’s the heartbeat of Tetris, driving everything from movement to row clearing to increasing difficulty levels. Every mechanic builds on this foundation, shaping the rhythm and strategy that make the game so engaging.
With a variety of mechanics to implement, each offering its own set of challenges, I tackled some of the standout features in Unity. From the classic tetromino movement to row clearing and increasing difficulty levels, I dove into the intricacies of game development to bring these elements to life.
Ensuring consistency in the spawning of tetromino pieces is crucial for maintaining fairness and predictability in gameplay. To achieve this, I meticulously calculated the dimensions of the play area and determined an optimal vector location for tetromino spawning. By anchoring the spawn point within the confines of the play area, I guaranteed that each piece enters the game world with precision and uniformity.
In cases where a piece inadvertently spawned too high up, potentially breaching the boundaries of the play area, I implemented a fail-safe mechanism. This mechanism automatically detects such occurrences and swiftly adjusts the position of the piece downward, ensuring it remains within the designated playable space. This proactive approach not only mitigates potential disruptions to gameplay but also enhances the overall fluidity and integrity of the Tetris experience.
The locking of tetrominos in place is crucial for maintaining the Tetris game loop, ensuring its continuous flow and challenge. Each tetromino is constructed by combining four single block prefabs, each representing a 1x1 unit block. The play area is a 10x20 grid surrounded by bounds implemented as 2D box colliders labeled as 'bounds'.
Collision detection is performed before every movement, and if a downward collision is detected, the tetromino locks in place. Once a tetromino is locked, all its blocks are labeled as 'bounds' to prevent overlap. I also implemented a last-second move delay of 16 milliseconds, allowing for last-minute player adjustments before locking occurs.
Handling the rotational movement of tetrominos presented a significant challenge. Left and right movements were straightforward using casts, but rotations required additional checks to prevent pieces from moving out of bounds. For example, the I-piece had to be adjusted if it extended beyond the play area's limits.
I implemented a series of checks to attempt the desired rotation, try the opposite direction if blocked, and ultimately cancel the rotation if no valid position was found. This intricate system involved approximately 150 lines of code to handle all possible rotation scenarios smoothly.
The point system detects completed rows through ray casts after a tetromino locks in place. If 10 blocks fill a row, it is removed, and any blocks above it are shifted downwards. Points are awarded based on the number of rows cleared at once (up to 4 rows with the I-piece). For every 10 rows cleared, the game level increases by 1, accelerating the tetromino's descent.
I used GitHub for version control while working on the Tetris project. This allowed me to track changes, revert to previous versions when necessary, and maintain a clean, organized development process.
All of the game files are on GitHub, including a downloadable build that you can try out if you'd like!