Built in 2019
Circular Tetris
Stack circles for points
A canvas game where you drop circles into scoring bands and clear a band when it holds four of them.
Made with
The repository has a file called oldCode.js. It opens with a note in Spanish. It says I wrote that code a long time ago, when I was just starting to program. I improved its structure, but the game works the same. The old code stays in the repository “in honor of the creativity” I had back then.
That old code was a small game. On April 14, 2019, I came back to it and rewrote it in one day. The rules stayed the same. The code underneath is new.
I called it Circular Tetris. There are no blocks and no rows. You stack circles instead.
Circles instead of blocks
The game lives in a tall 240x600 pixel canvas. The top area shows your score. Below it, seven gray bands fill the bottom of the screen. Each band has a label: +0, +100, +200, and so on up to +600. The higher the band, the more it pays.
A circle appears near the top. Its size and color are random. There are six sizes, from a 9 pixel radius to a 48 pixel radius, and six colors.
You move the circle left and right with the arrow keys or with A and D. The down arrow drops it. When a band holds four circles, it pays out and empties. Music plays on a loop in the background.
The challenge is the pile. Circles land on top of each other, so the pile grows. To reach the expensive bands at the top, you must build a stack of circles that rises into them.
A guide shows the landing
Below the falling circle, a second circle of the same size and color shows where it will land. It moves with you. Before you drop, you already know which band the circle will join.
Finding that spot is the most interesting part of the code. A circle that falls onto another circle stops when the distance between their centers equals the sum of their radii. With that and the Pythagorean theorem, the code finds the landing height:
let y=b.y-Math.sqrt((b.radius+guide.radius)**2-(b.x-guide.x)**2);
Here b is a circle already on the board. guide is the circle you are about to drop. The code repeats this for every circle that overlaps the guide from left to right. The highest result wins, because that is the first circle the falling one would touch. If nothing is below, the circle rests on the floor.
Each key press moves the circle one pixel. That feels slow, but it gives fine control. A few pixels decide if a circle lands on top of another one or drops into the gap next to it.
Bands that get thinner
The bottom band takes 15 percent of the canvas height. Every band above it is 8 percent shorter than the one below. The top bands are thin and worth the most.
This is one thing the rewrite changed. In the old code, all seven bands had the same height. Now the thin top bands are harder to hit, so the game pays the most where it asks for the most precision.
When you drop a circle, the game looks at where its center lands. It checks the bands from the bottom up and adds the circle to the first band that contains that point.
Four circles clear a band
After each drop, the game checks the band that got the circle. If it holds exactly four circles, it scores and empties.
The points are the band value plus a bonus for every circle. Bigger circles give a bigger bonus. The bonus for each circle is its radius divided by 0.6.
That 0.6 has a story. The old game used a 400x1000 canvas, and its circles ranged from 15 to 80 pixels. The rewrite shrank everything by 0.6 to fit a 240x600 canvas. Dividing by 0.6 gives back the old sizes, so a circle is worth the same points as it was in the original game.
When a band clears, the circles in other bands do not fall into the gap. They stay where they are. The pile only grows, and the empty space becomes new room to fill.
From one script to classes
The old version was one file of global variables and functions. A timer redrew the whole screen every 30 milliseconds.
The rewrite splits the game into files with classes. A Ball holds its size and color. A Guide computes the landing spot. Each Section (one band) keeps its circles and knows when to pay out. A Board puts it all together and draws the scene.
The new version has no game loop. The screen only redraws when you press a key. Nothing moves on its own, so there is no reason to draw 60 times a second.
There is also no game over. If you drop a circle so high that it lands above the top band, the game does not store it. It just disappears, and a new circle appears.
Respecting the first version
I could have deleted the old code. I kept it in the repository, next to the new one, because it was the first time I solved this problem. The rewrite shows what I learned since then: classes, smaller files, and drawing only when something changes. The old file shows where that started.
Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build