Let us discover the fusion of a classic game and a popular productivity tool – Tetris on Google Sheets! This, admittedly unique, gaming experience showcases the incredible capabilities of Google Sheets and Apps Script.
How to Play Tetris on Google Sheets
- Access the game here.
- Make a copy of the Google Sheet file and add it to your drive.
- Open it, read the instructions, and press “Start” to play.
- Be prepared for some quirks—check out the bugs section for more details.
Here is a video demo.
The Origin of Google Sheets Tetris
In 2018, my PM(Hey Dennis!) assigned me an Apps Script project on Google Sheets. Though hesitant at first, I quickly became captivated by the versatility of Apps Script. After completing two successful projects, I humorously challenged myself to create a Tetris game using Apps Script, inspired by a lighthearted Friday evening. Thus, the project commenced.
Crafting Tetris in Google Sheets
The technical process of developing the game involved multiple steps, including dropping of the tiles, implementing controls for tile movement, stacking tiles, and clearing rows. Initially using a three by three bounding box for tiles, I eventually transitioned to a “hinge” for each shape and rotation, significantly simplifying the code.
Technical Deep Dive
In this section, we delve into the more technical aspects of creating Tetris on Google Sheets. If you’re interested in understanding the behind-the-scenes development, read on! You may find the code here.
Key Components of Tetris on Google Sheets
The development process began by tackling crucial challenges such as moving tiles and managing their stacking. The plan included these primary steps for a single tile (the ‘I’):
- Create a colored range drop (a set of contiguous cells in a straight line).
- Add controllers for moving tiles right and left.
- Design tile stacking on top of each other.
- Implement row clearing.
Overcoming Developmental Hurdles
Initially, placing the tile within a 3×3 bounding box helped move it down while interacting with the bounding box instead of each tile cell. The tile falling mechanism utilized an infinite loop that moved the range down while sleeping for a specific duration. The tile moving process involved checking cell values and adjusting the tile’s position accordingly.
For stacking, a shadow sheet named Board_ recorded 1s where a tile finally rested but not during its fall. If the buffer encountered 1s, it had to stop. The buffer was the row immediately beneath the bounding box. Row clearing involved a simple animation – checking if a row summed up to 24 (the number of board columns) and clearing it if it did. The content above the cleared row would then be copied and pasted into the now-empty row below.
Eventually, all other tetriminos (tiles) and their rotations were added to a new sheet called Tiles. While the 3×3 bounding box initially appeared helpful, it eventually caused issues. For instance, the moving bounding box would override the color stack below it, clearing everything in its path.
Furthermore, the buffer for the bounding box was a straight line under the box. However, with shapes like the “L,” the buffer became uneven when rotated.
The solution was to choose an origin for every shape and rotation, copying and pasting relative to that origin. This method required custom offsets and ranges for each tile, and buffers needed adjustments as the uniform bounding box was abandoned.
A significant breakthrough came when realizing that placing the origins of every shape as nodes on a grid would simplify the data structures and make the code cleaner. This step demonstrated the importance of choosing suitable data structures for efficient code handling.
It would be simpler because set of offsets needed are less than otherwise. If this doesn’t make sense imagine how this part of the code would look like if the origin was at the top-left corner for each tile.
The Potential of Apps Script
Apps Script is a remarkable tool that’s frequently underappreciated by those who could genuinely benefit from it. The user-friendly interface of Google Sheets, coupled with Apps Script, enables the creation of intricate systems without the need for infrastructure maintenance.
The concept of an MVC (Model-View-Controller) framework on top of Apps Script is also compelling. By saving states in another sheet and reading from and writing to it, we could potentially develop more sophisticated applications.
While Google Sheets and Apps Script might not rival the power of a SQL database, they could be a more suitable option for specific use cases. In my experience, people often undervalue the capabilities of Apps Script for everyday tasks.
Go ahead and give it a try—you won’t be disappointed!
Leave a Reply