Behind VR Games: Flappy Bird

Ayleen Farnood
6 min readFeb 12, 2019

Everyone has probably heard about Flappy Bird by now; a simple side scroller game of a bird flying through pipes that went viral a few years ago (if you haven’t, the game is sadly no longer available). However, while playing, I always wondered what it was like to experience the game from the perspective of the bird. What would happen if I could be the flappy bird?

This month, I was able to program the Flappy Bird game in virtual reality and fulfill my wish! The game works similar to the app and was programmed in Unity, while the visuals and 3D design was created in Blender.

The setting of the game takes place above a river, with mountains visible off in the distance (because why not) and the player is traveling through a large green roll cage pipe which has smaller pipes inside of it.

When playing in this version, you can either click the trigger on your Google Cardboard headset to jump or press the space bar on your computer. See the video below for the gameplay:

How the Game Works:

  1. Moving the Bird

To ensure that the bird doesn’t move in random locations, it is important to let the rules of gravity impact the bird/you as the player. Rigidbody enables the game objects in Unity to act according to the laws of physics, and is what allows for the player to fall down if they aren’t pressing any buttons to jump.

However, when the bird dies by hitting a pipe, we need to be able to stop everything and freeze the screen until the game restarts (don’t ask me why pipes kill birds, it’s just a game). The code above removes the forces of gravity when the bird dies and sets its velocity to zero.

With the help of Rigidbody, the program lets the bird move forward and jump into the air, as can be seen in the code above. The speed and jump force are all determined by public floats found earlier on in the code, which can also be changed in the Unity editor to alter the difficulty of the game.

One thing which is important to note, however, is that the game is currently programmed so that the bird jumps when the space key is pressed. If you wanted to play this game with a Google Cardboard headset instead of a computer, you would have to replace Input.GetKeyDown(“space”) with Gvr.Viewer.Instance.Triggered instead, which would let you jump every time you pressed the trigger from the headset.

2. Spawning Pipes

What makes Flappy Bird difficult to play is the randomness of the pipes which appear throughout the game. You never know what to expect next and it requires quite a bit of skill (which I lack) to maneuver through the various gaps.

The code above deals with the spawning of these infamous pipes, with the first part ensuring that the type of pipe that appears is randomly picked from the potential formations. In my game I have four different types of pipes however it is possible to add as many different variations as you want. Additionally, the second half of the if-statement allows the pipes to be spaced a certain distance from each other and lets them spawn a specific number of units away from the player.

3. Level Bounds

Another important part of the game are the boundaries which prevent the bird from falling down through the map or going above the ceiling. While these bounds are not visible, they are still present underneath the 3D design. To allow the game to be endless, these bounds must follow the bird as it moves forward or else it may eventually run out and the player would be able to fall through the map. The code above sets the position of the bounds along the z-axis to be equivalent to the position of the player so it can follow the bird forever.

4. On-screen Text

Furthermore, we have the code which deals with the text that appears on the screen during the game. If the bird isn’t dead, the player’s score is shown on screen by itself. In this version, one point is added to the score after every unit the bird travels. Mathf.FloorToInt also ensures that the score is an integer, as it would be fairly confusing (and weird) to have 13.589683 points for example.

When the bird dies, the screen says “Game Over!” and shows the final score of the player. These words appear on the screen for three seconds before the game restarts and the score is set to zero once more.

5. Colliding with Obstacles

Lastly, it would not be possible to lose this game if you couldn’t collide with the obstacles (although it would be fun to win all the time). Since Unity already has Box Collider feature, it can detect when the player comes into contact with the pipes. Thus, when the bird touches the pipe, the public boolean dead is set to True, and the other parts of the code stop the game and show the final score.

3D Design with Blender

My mountain + river landscape

For the visual aspect and design of the game, I used a software called Blender, which is an open-source 3D computer graphics software toolset used for creating animated films, visual effects, art, 3D printed models, interactive 3D applications and video games.

With this tool I was able to create the design of the pipes and the scenery of the game, including the mountains, river, and the pipe which the player is traveling through. Since this was my second time using Blender, each design took me a very long time to create and it’s a skill which I plan on developing more and more. I’ve already seen improvement from my previous virtual reality game and I look forward to seeing how I can get better later on!

And that’s all for my explanation of the coding behind my latest virtual reality game! I plan on building another project this week so stay tuned for another video and article coming out. If you want to learn more about the game watch the video below:

If you enjoyed this article, please give it a like! Follow me on LinkedIn or check out my personal website to learn more about other projects I’ve completed in the past!

Finally, if you have any constructive criticism or feedback, be sure to leave a comment below! I’d love to learn ways to improve my articles/writing style :)

📝 Read this story later in Journal.

🗞 Wake up every Sunday morning to the week’s most noteworthy Tech stories, opinions, and news waiting in your inbox: Get the noteworthy newsletter >

--

--

Ayleen Farnood

machine learning + brain-computer interface + fullstack dev! check out www.ayleenf.com to learn more :)