Built in 2019
Tetris
Tetris Game
Tetris HTML Canvas video-game.
Made with
In 2019, I wanted to know how games draw graphics in the browser. Reading about game engines felt abstract. I had to build something from scratch before it made sense.
Tetris became my test. The rules are simple enough to build in a weekend. The mechanics still hide real work. Collision detection, piece rotation and row clearing each made me think hard about how pixels move on a screen.
I picked vanilla JavaScript and the HTML Canvas API. No frameworks. No libraries. Only the browser’s own drawing tools and my code.
Five pieces, four keys
The game runs in the browser. A 300x500 pixel canvas shows the play area. Five piece types appear at random: the classic Z shapes, the L shape, the I bar and the square. Each piece falls at a fixed interval.
You control pieces with the arrow keys. Left and right move the piece sideways. Up and down rotate it. When a row fills up, it disappears and adds 150 points to your score.
The game ends when a new piece cannot fit on the board. Press Enter to play again.
Pieces as small grids
Each piece stores its shape as a 2D array. A 1 marks a filled block. A 0 marks empty space. This is the Z piece in code:
[[1,1,0],
[0,1,1]]
Grids made rotation simple. Each piece keeps an array with all its rotation states. To rotate, the game switches to the next grid in the list.
Checking every block
Three kinds of collisions matter. Floor collisions stop a piece at the bottom. Side collisions keep pieces out of walls and other blocks. Vertical collisions detect when pieces stack on each other.
The game checks collisions by comparing block coordinates. When a piece tries to move, the code loops through all its blocks. It compares each one with the board edges and the pieces already there. If any block would overlap, the move is rejected.
Rotation makes this harder. A rotated piece can poke through a wall or another block. The game notices and turns the rotation back.
Fifteen blocks clear a row
After a piece lands, the game scans the rows it touched. It counts the blocks in each row, from the piece’s top edge to its bottom edge. A row with 15 blocks is full, because the board is 15 blocks wide at 20 pixels each. That row gets cleared.
To clear a row, the game removes every block at that Y coordinate. Then every block above it moves down one row. This works because the game goes through the rows from bottom to top.
A tick every 200 milliseconds
A simple interval timer runs the game. Every 200 milliseconds, the loop moves the current piece down, checks for collisions and draws everything again.
Key presses draw the board right away, outside the normal loop. The controls feel quick even though pieces fall slowly.
Where it fought back
Canvas coordinates. The Canvas API draws from the top left corner, and Y grows downward. This flipped system confused me at first. I had to turn my coordinate logic upside down in my head.
Rotation near walls. Rotating close to a wall pushed pieces outside the play area. The fix was to check the edges after each rotation and undo it if it was invalid. That sounds simple. It took me several tries to get right.
Broken pieces. When a row clears, a landed piece can lose some of its blocks. The blocks that remain still need to fall correctly. Tracking these partial pieces made me rethink how I stored block positions.
Build first, then read
This project taught me the basics of drawing on a canvas: rectangles, state and the animation loop. Every browser game uses them.
Grids also made collisions easier. Storing shapes as grids made rotation much simpler than working with pixel positions.
Most of all, building beat reading. The Canvas API only made sense after I used it to ship a real game.
Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build