I grew up in a video-game-deprived household. I received my first console until I was nearly 16 years old: a joint Christmas gift to my sister and me of… a Nintendo Wii. *sobs* Hey, what can I say? You can keep your fancy graphics and story lines; Wii Golf is as good as it gets! As a result of my minimal exposure to video games, I’ve never spent much time on them, and the few games that have caught my attention are simple, lo-fi affairs: Wii Golf, Wii Tennis, Pokemon (Generations 1–3), 2048… and lately, Tetris! This Soviet classic, developed by Alexey Pajitnov in 1984, has been surpassed in plot, graphics over the past 36 years but still lives on. I have frittered away shameful amounts of free time at FreeTetris.org over the past month or so. My recent forays into football and baseball simulations made me think: how would I go about implementing this simple little game? What sort of unexpected complexities and edge cases does it contain? After a few days tinkering around, my answer is: many!

Basic Rules

Here are the basic rules of Tetris:

  • Board consists of 18 rows and 10 columns
  • Pieces (7 different available shapes) fall vertically down from the sky (i.e. the top of the screen)
  • They continue to fall until landing on top of another piece or hitting the bottom of the screen
  • When an entire row is filled by pieces, it disappears, and the player receives points
  • Every 10 rows, the level increases: pieces fall faster, and each completed row yields more points
  • Pieces can be rotated clockwise; thus, each piece can, in effect, take 4 different shapes
  • The game ends when the board is filled and no new pieces can descend onto the screen

Object-Oriented Implementation

The game consists of several main classes:

  • Piece: A piece can fit into a 2x2, 3x3 or 4x4 grid. It can be rotated 90 clockwise.

One of our main goals will be to program the falling of tetris pieces and to regulate their movement. Pieces will continue to fall until hitting another piece or the ground, at which point it will freeze in place, and a new piece will begin to fall from the top. We also need to make sure that pieces cannot be moved horizontally off the screen.

  • Game: A game contains several main objects
  1. Board: The aforementioned 18x10 grid, each of whose spots will either be occupied by a piece or not. We’ll implement it as an 18x10 array with each member being 0 (empty) or 1 (taken).
  2. Score: A player’s score starts at 0 and increases with each completed row. The number of points earned per completed row increases; therefore, score is a 2nd-degree polynomial function of the number of rows completed.
  3. Rows completed & level: These increment upwards with each finished row.
  4. Current piece: At any given moment, only one pieces is falling & can be moved. Once a piece “stops”, it freezes permanently in place, and another piece is randomly generated at the top of the board.
  5. Game over or not: The game essentially exists as a while loop: while game is not over, it continues. Once the board is filled, game over switches to true (or game on becomes false), and the game ends.

Piece

Here is our code for the Piece class.

First, we create a dictionary that holds all of the possible piece shapes. These pieces will be randomly generated and inserted into a game board:

Now, we’ll write some code to print out each piece; ideally, we should see seven pieces that correspond to the above photo:

Hooray! It ain’t pretty, but we can see all seven of the desired shapes.

Now, let’s code out the Piece class itself:

Let’s test out our rotate() function on one of the pieces; it should rotate 90 degrees clockwise each time:

Hooray again! Our piece is rotating 90 degrees clockwise each time, eventually returning to its original position.

Now, let’s move on to the Tetris class.

Tetris

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store