Back in 2016 I bought a Unity course on Udemy, made a Flappy Bird clone, and then didn’t touch game development again for three years. Then last Christmas I made a whole game in nine days, and I even put it on Google Play! It’s called Gotinha, and it’s about a little drop of water.

Unity and me#

The course was ok, and I did finish the Flappy Bird clone, but the whole time I felt overwhelmed by all the little knobs and buttons of Unity. Drag this object into that panel, click this checkbox in the inspector, turn that thing into a prefab, drag the prefab somewhere else. Before writing a single line of code you have to really KNOW how to use the software, and I didn’t want to learn a software, I’m a developer, I wanted to open a file and write code.

So I never really learned Unity, and I moved on with my life.

Phaser#

Last year I decided I wanted to learn how to make games for real this time, and since I’m already writing JavaScript all day at work, I did a quick research for JavaScript game engines and found Phaser, which lets me make games for the browser using only code, with no editor telling me where to drag things, so it was pretty much perfect for me.

I bought another Udemy course, this time for Phaser, and it was pretty shitty, but it taught me enough of the Phaser API to stop watching videos and start building my own thing.

My very first Phaser game was not Gotinha though, it was a simple brick breaker, and the whole thing is one giant commit called “change bunch of stuff”, so don’t expect a nice history to read. I even drew my own blocks for it with JS Paint and Photopea, and then published them on OpenGameArt as Colorful Blocks.

For Gotinha I had only one rule: I would make everything myself, the code, the sprites, the sound effects, the music, the logo, everything. So no asset packs and no free music from some random website.

The idea#

I wanted to make an infinite game, because that’s pretty much the smallest thing you can make that is still a game, there are no levels, no story, no save files, only a score going up until you die.

So I came up with a kind of endless runner, but instead of a guy running you are a little drop of water, a gotinha in Portuguese, going down the asphalt. The drop sticks to one side of the street, you tap to jump to the other side, and you have to dodge fire hydrants and collect crystals on the way.

Nine days of Christmas holidays#

If you open the Gotinha repo history, it starts in January 2016, the same year as my Unity course, which looks very impressive until you notice those commits are not mine. I used lean/phaser-es6-webpack as the base of the project, so I got its whole history for free.

My real commits start on December 17 (a Tuesday by the way), and the commit messages pretty much tell the whole story:

shell
2019-12-17 22:21 trying to make infinite scrolling and failing
2019-12-18 00:34 holy shit i need to go to bed
2019-12-18 16:43 add first spike
2019-12-18 23:40 add more spikes and randomness
2019-12-21 17:20 new frame yaaaay
2019-12-22 03:05 building android apk
2019-12-23 22:34 add christmas theme
2019-12-25 19:59 remove christmas theme and add share on twitter button

That’s around 50 commits in nine days, and most of them after 22:00. Classic.

Infinite scrolling, the cheap way#

Of course the street is not really infinite, I cheated a little. The drop never moves up or down, it only goes left and right, and what actually moves are the walls. There are four chunks of 320 pixels on each side, all of them going down at the same speed, and when one chunk leaves the bottom of the screen I just teleport it back to the top.

javascript
// calculates the restart of the blocks chunk
for (const blockChunk of this.blocksGroup.children.entries) {
    if (blockChunk.y - 320 >= this.game.config.height) {
        blockChunk.y = -(this.config.chunkSize / 2 + this.config.screenSizeDifference);
    }
}

The game itself runs in a tiny 150x270 canvas with pixelArt: true, and Phaser scales it up to whatever phone you have. Also, the drop is drawn sideways, so its width is actually its height, and that’s why the hitbox code has comments like this.hero.body.width = 30; // actually height.

Spikes, I mean hydrants#

The hydrants started as spikes, and in the code they are still called spikes, because renaming things is not fun when you only have nine days.

Each wave of hydrants comes from an array of ones and zeros, where 1 means there is a hydrant in that position and 0 means there isn’t. At the end of the array there is a -1, which is the trigger to spawn the next wave:

javascript
export const generateRandomSpikePositionsArray = (quantity = 4, factor = 0.2) => {
    const array = [getSpikeOrNot(factor + 0.2), 0];
    for (let i = 0; i < quantity; i++) {
        array.push(0, getSpikeOrNot(factor), 0);
    }

    // -1 is the trigger for spawning new spikes
    array.push(0, -1);

    return array;
};

And the trigger in the game is… a text object with 15 dashes in it. Yes, a text. It’s invisible, it collides with the world bounds, and when it touches the bottom of the screen it destroys itself and the next wave gets generated.

javascript
} else if (spikesArray[spikeIndex] === -1) {
    const text = this.add.text(-10, spikePosition, '---------------')
        .setDepth(100);
    group.add(text);
    text.body.onWorldBounds = true;
    text.body.setCollideWorldBounds(true);
    text.setName('triggerSpikes');
    if (!this.game.config.physics.arcade.debug) {
        text.setVisible(false);
    }
    continue;
}

So basically the whole level generation of Gotinha is a bunch of dashes falling down the screen, and if you turn debug mode on you can actually see them 😅.

There are also four hydrant groups (main, secondary, tertiary and quaternary) taking turns. When one group spawns a new wave, it clears an older group that is already off the screen, so I never have hundreds of hydrants alive at the same time. And I only got the sides right at 00:34, with a commit called “solve the problem of multiple spikes in the same side”.

Gotinha title screenTitle screen
Gotinha gameplayDodging hydrants
Gotinha gameplay with crystalsCrystals!

Crystals and difficulty#

The score goes up by one every 300 milliseconds, and the crystals give you extra points. There are four crystal colors, and the rarity is just how many times each one shows up in an array:

javascript
const crystalTypesArray = [
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    2, 2, 2, 2, 2,
    3, 3, 3,
    4,
];

Ten of the first one, five of the second, three of the third, and only one of the rarest, and each crystal is worth its number times 25 points, so the rare one gives you 100. The laziest loot table ever, but it works.

For the difficulty I didn’t do anything smart either, it’s just a giant if/else checking the score, making things faster and spawning more hydrants as you go:

javascript
if (this.score > 2500) {
    this.config.speed = 200;
    this.doneIncreasingSpikeSpawningSpeed = true;
} else if (this.score > 1500) {
    this.config.speed = 170;
} else if (this.score > 1000) {
    this.config.speed = 150;
} else if (this.score > 700) {
    this.config.spikeGenerationFactor = 0.4;
    this.config.speed = 140;
}
// ... and so on, all the way down to 100 points

The game starts at speed 70 and stops getting harder at 2500 points, with speed 200, and after that it’s only you and your thumb dodging hydrants for as long as you can.

Making everything myself#

This part was the whole point of the project for me.

The first sketches of GotinhaThe first sketches, drawn on the back of a Dutch letter about underground trash containers

Yes, those are the first sketches of the drop, and yes, that’s the back of a Dutch letter about underground trash containers. Paper is paper.

So the drop started on paper. Then I took a photo of the paper and vectorized the drawings in Photopea, and with the vectors as reference I used JS Paint, which is good old Microsoft Paint running in the browser, to draw the actual pixel art for the sprites. The drop has three walking frames and one jumping frame, and each crystal has a couple of frames to make it look like it’s spinning. Then I used ShoeBox to cut the sprites and Atlas Packer Phaser 3 to pack them into the atlas files Phaser wants.

The drop spritesGotinha himself
The crystal spritesThe four crystals
The Gotinha logoThe logo

I made the sound effects (jump, crystal and game over) with sfMaker, and the soundtrack with BeepBox, which is a really fun little tool if you can’t play any instrument, like me.

Funny thing, while writing this post I opened the code again and found this:

javascript
this.playSoundeffects = true;
this.playSoundtrack = false;

The soundtrack gets loaded every time the game starts, and it’s been turned off since December 22. There was supposed to be a button to turn the audio on and off, but that button is still a commented-out TODO. So yeah, the music I made myself, for my first game, because of my “make everything myself” rule, is sitting inside the APK on Google Play and nobody has ever heard it. Awesome.

Putting it on Android#

On December 22, at 00:29, I added Cordova to the project, and at 03:05 the commit says “building android apk”, so you can guess how that night went. Cordova wraps the web game in an Android WebView, then I used the Insomnia plugin to keep the screen from turning off while you play, locked the orientation to portrait, generated the icons with NativeScript Image Builder and signed the APK with Android Studio.

Gotinha iconThe app icon

Then I sent it to Google Play and that was it, I officially have a game on Google Play! Hooray 🎉.

The next afternoon there is a commit called “fix keyboard spacebar jump for killian”, so you know someone tried to play my mobile game on a computer. Classic Killian.

The Christmas theme#

Since it was Christmas, on December 23 I added a Christmas theme, with a Christmas version of the drop, the hydrants, the crystals and the walls, plus snow falling down the screen. In the same commit I also fixed a bug where the drop would sometimes float in the middle of the street after a jump, and as you can see from the comment, the fix is not exactly elegant:

javascript
// hack to fix the bug of flying drop...
if (!this.heroIsMoving) {
    if (this.hero.scale === -1 && this.hero.x !== this.config.tileSize) {
        this.hero.setX(this.config.tileSize);
    } else if (this.hero.scale === 1 && this.hero.x !== this.config.heroXPosition) {
        this.hero.setX(this.config.heroXPosition);
    }
}

Basically, if the drop is not jumping and it’s not glued to a wall, then glue it back to the wall, and that’s it.

Then on December 25, at 19:59, on Christmas day itself, I removed the Christmas theme and added a “share on Twitter” button to the game over screen, which tweets I got 1234 points on Gotinha, how many can you get? with a #GotinhaScore hashtag.

Conclusion#

Gotinha is the first game I ever finished. Ok, it’s super small, the levels are generated by a bunch of dashes and nobody can hear the music, but it’s finished and it’s on Google Play, and coming from a Flappy Bird clone in 2016 that’s already a big upgrade.

Now I’m working on the next one, a skateboard platformer (of course), also with Phaser. It’s way bigger than Gotinha, so it will probably take a bit more than nine days to finish (this will bite me in the ass later).

You can play Gotinha in the browser, and the code is all on GitHub. Maybe one day I’ll finally add the audio button so someone can hear the soundtrack.