Remake your own version of Flappy bird using JAVA
Hey Everyone...Today's Post is for JAVA Programmers..Today, we are gonna see how to make a or how to remake a flappy bird game for free..I hope everyone have played Flappy Bird..SO, Lets Begin, Follow the steps given below
Step 1:
You must have a PC
Step 2:
Download Download Greenfoot in your PC, Greenfoot is the best application for beginners who all are learning java in game Development..Greenfoot allows you to create 2d games in simple JAVA coding..IF you dont have Greenfoot Click here to download it directly or use this link to go to the official Page
Step 3:
Click here to download the essential Images and Sounds
Step 4:
Create a New Project by clicking scenario > new Java Scenario
Step 5:
Now select the folder where your project and game have to save and type your file name
Step 6:
After creating your projects Upload all the images where you have downloaded for the link by clicking Actors > New Subclass > (Settings) Symbol > Import From library
Step 7:
Import all the sounds in your project where you have redirected to set the location of your folder
Example : My PC > Local Disk > Folder name > Project Name > Sounds
Step 8:
After doing all the above steps create various subclass and set images as per given in the below Image
Step 9:
OOOH now its time to code
Step 10: (Setting up The MyWorld Subclass)
Just copy and paste the code or understand and create your own creativity
public class MyWorld extends World
{
public MyWorld()
{
super(600, 400, 1);
addObject(new FlappyBird(), 100, 200);
addObject(new Pipe(), 300, 150);
addObject(new Pipe(), 600, 150);
addObject(new Score(), 300, 100);
}
}
Step 11:(Setting up Flappy Bird subclass)
This subclass code is gonna big because its main subclass
public class FlappyBird extends Actor
{
private double a = 1;
private int b = 200;
private boolean haspressed = false;
private boolean isalive = true;
private boolean isacross = false;
private boolean hasaddscore = false;
public FlappyBird()
{
GreenfootImage image = getImage();
image.scale(50,40);
}
public void act()
{
if(spacePressed())
{
a=-2;
}
a+=0.1;
b+=a;
setLocation(getX(), (int)(b));
if(isTouchPipe())
{
isalive = false;
}
if(isTouchPipe())
{
isalive = false;
}
if(!isalive)
{
getWorld().addObject(new GameOver(), 300, 200);
getWorld().removeObject(this);
}
if(!hasaddscore && isacross && isalive)
{
Greenfoot.playSound("score.mp3");
Score.add(1);
}
hasaddscore = isacross;
}
public boolean spacePressed()
{
boolean pressed = false;
if(Greenfoot.isKeyDown("Space"))
{
if(!haspressed)
{
Greenfoot.playSound("fly.mp3");
pressed = true;
}
haspressed = true;
}
else
{
haspressed = false;
}
return pressed;
}
public boolean isTouchPipe()
{
isacross = false;
for(Pipe pipe : getWorld().getObjects(Pipe.class))
{
if(Math.abs(pipe.getX() - getX()) < 69)
{
if(Math.abs(pipe.getY() + 30 - getY()) > 37)
{
Greenfoot.playSound("out.mp3");
isalive = false;
}
isacross = true;
}
}
return !isalive;
}
}
step 12:(Setting up GameOver subclass)
Every game as a gameover part which no one likes but its important
But suprisingly we are not going to touch gameover subclass because it dont have any property its just a image need to show after we lose the game..SO we can call this class from the main sub class
step 13:(Setting up Pipe subclass)
Our main villain in this game is our go green pipe..if flappy bird touch the pipe the game lose..
public class Pipe extends Actor
{
public Pipe()
{
GreenfootImage image = getImage();
image.scale(500,900);
}
public void act()
{
setLocation(getX()-1, getY());
if(getX() <= 1)
{
setLocation(getX() + 600, Greenfoot.getRandomNumber(250)+50);
}
}
}
Step 14:(Setting up score subclass)
After passing the pipes we need to increase the scores so this class helps us to increase the score each time when we go across the pipe
public class Score extends Actor
{
public static int score;
public Score()
{
score = 0;
}
public void act()
{
World myWorld = getWorld();
myWorld.showText(String.valueOf(score), 300, 100);
}
public static void add(int num)
{
score+=num;
}
}
Step 15:
Just enjoy your GamePlay
No comments