Build a flappy bird copy with TypeScript & Pixi.js

David Guan
Aug 27, 2017 · 4 min read

Online demo link and the code repo(160 lines of TypeScript code).

Sorry for no background image & music in the game

This article is about what I learned from building a small project while learning Typescript & Pixi.js and having fun.


Before talking details of the implementation, here are my two cents about TypeScript and Pixi.js

The first benefit TypeScript provided strong type checking, the code below is an example:

If we are lucky, we can find the typo with locale in the development stage, but sometimes this kind of errors are being pushed into codebase’s master branch without being noticed and will waste a lot of time on debugging in the production environment.

The second benefit TypeScript provided is easing suffering from with developing a javascript library I’m unfamiliar with, very often I worry a lot whether I’m using the right signatures, and wasting a lot of time in checking the docs.
Now, I can rely on what TypeScript tells me:

Pixi.js is a lightweight 2D library.

The first benefit is it’s a WebGL wrapper (in short, WebGL allows us rendering graphics using GPU directly), it lets us using WebGL without worry touching WebGL API. (In my last article WebGL and Image Filter 101 I mentioned the power of WebGL, and there are a lot of boilerplate code we have to provide to make WebGL working since it’s a low-level API).

The second benefit is it unified all kinds of 2D content(image, text, shapes) to “Sprite”, we can apply the transformation and adding interactivity to these content the same way with the easy-to-use API.
We also can group different content with containers, and use batch effects quickly.


Rendering a flying bird

Let’s start from creating a scene, after this setup, any changes happen inside the stage container can be rendered by using renderer.render(stage).

const renderer = PIXI.autoDetectRenderer(512, 512);
document.body.appendChild(renderer.view);
const stage = new PIXI.Container();
Result: an empty Canvas

To create the flying bird, I found these assets from this link (Please use texture atlas if you have much more images in your project, which is also supported well by Pixi.js).

Then, what I did is creating a “Sprite” and update the Sprite’s texture with an interval timer.

With the code below, you will get an animating bird looks like flying.

In the reset, we use x and y to control the position.
If you have the speedX and speedY, you can also update bird’s flying orientation by this.sptire.rotation=Math.atan(speedY / speedX).

Now we have a bird and can apply different transformation to it; you can try adding a new field speedY to Bird class and update bird’s position by updating sprite’s y each draw based on that field .

Rending Tubes

As I mentioned before, by using Pixi.js, all kinds of 2D content(image, text, shapes) can be treated as “Sprite”, for the tube, I’m simply drawing some rectangles.

Pixi.js provided 2d drawing API with PIXI.Graphics, for example, to draw a “tube” and moving it horizontally:

const tube = new PIXI.Graphics();tube.beginFill(0xffffff);
//drawRect(x, y, width, height)
tube.drawRect(100, 0, 50, 120);
tube.drawRect(100, 200, 50, 512);
tube.endFill();
tube.x = 200;
stage.addChild(tube);
renderer.render(stage);

And the tube class I made is like:

Checking Collision

if((x1 + w1 < x2) || (x2 + w2 < x1) || (y1 + h1 < y2) || (y2 + h2 < y1))
No collision

So, that’s basically all parts of making a simple flappy bird copy.


Thanks for your reading :)

David Guan

Written by

I program machines to do web and graphics stuff, also write about them.

More From Medium

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade