PROJECT NAME:

FLAPPY BIRD

INTRODUCTION

In this project, we design and implement a Flappy Bird like video game built on the Lightweight Java Game Library 3 (LWJGL 3). Flappy Bird is a very popular competitive game on mostly found on Java platform, driving a lot of people crazy.

GAME SCENERIO

In this game, the player can control the vertical movement of bird (every pressing on the key makes the bird leap upward for a little bit, and the bird will fall freely without control). As soon as the game begins, tubes will keep appearing from the right side of the screen and moving leftwards. (so that it seems like the bird flying forward) and after 5 second background of the game changes from day light to evening and after 20 seconds it changes from evening to Midnight and so on as the player progresses through the game.

GOAL OF THE GAME

The goal of this game is to control the bird, dodging and passing the incoming tubes, as many as possible. While a secondary goal of the player can be described as to build a high score which will be based on the time he survives in the game. The game is endless until the bird eventually hit one of the tubes, ground, or ceiling.

Hierarchical

Description



Flappy Bird Window: It is the main window in which the game is being displayed which consists of two buffers front and back which are being switched continuously.



Background: There is a background present on the main window which is an image being displayed into the game using texture and shader concept. The background is present only half of the window which is being looped again and again.



Fading Background: There is a layer of greyish affect present on the main background which controls the lightning of the game and is switched automatically based on time from day to evening and to night.



Pipes: Pipes are being the loaded on the background in set of 2. The sizes are being varied randomly which makes up the distance between the pipes.



Bird: The bird is also a image which is being loaded with the help of shaders. The bird z value changes (it leans) forward and backward whether it is moving up or down. Another phenomenon of collision detection is also implemented which tells whether the bird and the pipe has collided or not so that we can start the game again.

Description of libraries

LWJGL

The Lightweight Java Game Library ( LWJGL ) is an open-source Java software library for video game developers that enables cross-platform access to popular native APIs useful in the development of graphics (OpenGL, Vulkan, GLFW), audio (OpenAL) and parallel computing (OpenCL) applications.This access is direct and high-performance, yet also wrapped in a type-safe and user-friendly layer, appropriate for the Java ecosystem.

LWJGL is an enabling technology and provides low-level access. It is not a framework and does not provide higher-level utilities than what the native libraries expose. As such, novice programmers are encouraged to try one of the frameworks or game engines that make use of LWJGL, before working directly with the library.

It provides direct access to a library known as GLFW.

GLFW

GLFW is a lightweight utility library for use with OpenGL. GLFW stands for Graphics Library Framework. It provides programmers with the ability to create and manage windows and OpenGL contexts, as well as handle joystick, keyboard and mouse input.

The API provides a thin, multi-platform abstraction layer, primarily for applications whose sole graphics output is through the OpenGL API. It is just as API like FreeGLUT which can provide access to Open GL which it is lightweight.

Description of methods and algorithms

Main class

  • @Override

      public void run() {}
    
      The method is from the Runnable class which is overridden. Its purpose is to keep start the game, initialize it and keep the game running on a background thread.
    

Shader class

  • public static void loadAll(){}

This method is used to load all of the 4 shaders used in our game background, pipe, bird, fading background. It is a static method which means it can be called without object creation.

  • public int getUniform (String name) {}

As we have used the phenomenon of caching the id of the shader program created in a HashMap. So that as a frame is loaded again and again so it might put stress on the CPU. This method will check whether the value is present in cache if not present put it.

Texture class

  • private int load(String path) {}

The main purpose of this texture class is to load images present in the game in the res folder and convert them to texture.

A texture is a 2D image (even 1D and 3D textures exist) used to add detail to an object. Because we can insert a lot of detail in a single image, we can give the illusion the object is extremely detailed without having to specify extra vertices.

This method converts the image into a Buffered Image, rearranges the ARGB values of it and then generates a texture, binding the data of the image onto it and returning the id of the texture created.

  • public void bind () {},public void unbind() {}

These two methods will bind or unbind the texture when needed in our program.

Vertex class

  • public VertexArray(float [] vertices, byte[] indices, float[] textureCoordinates) {}

This method is used to create a Vertex Array object for every object i.e(bird, pipe) in our game. A Vertex Array Object (VAO) is an object which contains one or more Vertex Buffer Objects and is designed to store the information for a complete rendered object.

Three kinds of buffer i.e. Vertex Buffer Object (VBO), Index Buffer Object (IBO), Texture Buffer Object (TBO) are used.A vertex buffer object (VBO) is an OpenGL feature that provides methods for uploading vertex data (position, normal vector, color, etc.) to the video device for non-immediate-mode rendering. This enables substantial performance gains of the game.

Matrix 4f

Another important class of our game is the math class which consists of all the matrix for translation, rotation and setting up the projection.

  • public static Matrix4f orthographic (float left, float right, float bottom, float top, float near, float far) {}

We have used orthographic 3d projection in our game as the projection matrix so consists of the equation required to generate the (4*4) matrix.

  • public static Matrix4f translate (Vector3f vector) {}

This method is used to translate the x, y and z axis whichever is specified from one place to another.

  • public static Matrix4f rotate (float angle) {}

This method will be used mainly to rotate the object which is mainly the bird in our game. We have rotated the bird only in z direction when the bird moves up or down, so we see a slight rotation in our object.

  • public Matrix4f multiply (Matrix4f matrix) {}

This method will multiply two matrices (the original matrix and the matrix provided) and then return a new Matrix which will be the result.

File Utils

  • public static String loadAsString(String file) {}

This method is used to read all data from a file put it in a StringBuilder and return it.

Algorithm of the game

The main class which is used to initialize the game is Level class and control the game. We have 3 main elements in our game:

  • Background

The background is only present on the half of the window which is being replicated on the other half. The background is continuously scrolling left and is being reloaded when 250per of the background is being scrolled. This gives the effect of bird moving.

  • Pipes

The pipes are set on the background as a set of 2 with some specified offset (5.0f). 10 set of pipes are present on the background at a time. While the distance between the pipe is a random variable which is being varied continuously. The pipe is reloaded again when the background is loaded.

  • Bird

The main logic of the bird is the bird is fixed at the center of the screen i.e. (x position remains static) while the y position of the bird changes continuously. When the space key is pressed which is used to make the bird flap the bird tilts towards upward position and there is a slight increment in the y position of the bird.

Collision detection is another phenomenon used in the game in Level class which matches the co ordinate of the pipe and bird and check if there is a collision or not. If there is a collision it moves the decreases the y position of the bird and eventually it falls. When the space key is pressed the game restarts again.

Help Manual

The main control used for the game is spacebar when spacebar is pressed the bird flaps and on collision when spacebar is pressed the game starts again.