Flappy Bird Java Game
Using Greenfoot Java Programming
How to Play
Enter your player name
Press the screen to start the game
Use Space Button to make the bird fly in the air and go through the pipes
When the enemy appears on the screen, use S Button to shoot the enemy
Keep playing until you reach level 2, and the boss level. It will speed up and get harder as you go!!
Your player name and the score will be recorded on the scoreboard text file
Code Breakdown
Space Button- Make the Flappy Bird jump up in the air, in order to go through the pipes and avoid colliding with it. The code makes the bird accelerate in y-direction when space bar is pressed.
if(Greenfoot.isKeyDown("space"))
{
vAcc = speed;
}
'S' Buttton- Shoots a fire that when it hits the enemy bird, the enemy bird disappears. The shootTimer allows the bird to only shoot one bullet at a time.
if (Greenfoot.isKeyDown("s") && shootTimer.millisElapsed()>shotVal)
{
getWorld().addObject(new bullet(), getX(), getY());
shootTimer.mark();
}
Losing when there are no lives left
The bird starts with 3 lives in the beginning and every time the bird hits the obstacles, it loses a life.
If there is no life left for the bird, the game is over and the score is recorded.
if (b.lives == 0)
{
try{
BufferedWriter writer = new BufferedWriter(new FileWriter("scores.txt", true));
writer.write(b.name + "-" + b.timer.millisElapsed() + "\n");
writer.close();
}
catch(IOException e)
{
System.out.println("Something went horribly wrong. Best of luck y'all:)");
}
//Remove the bird from the screen
getWorld().removeObject(b);
Greenfoot.setWorld(new LoseWorld());
}
//if the bird collides with the pipe, life go down by one and plays the sound
b.lives--;
Greenfoot.playSound("chirp.mp3");
st.mark();
Skills I learned through Making the Video Game
Skill 1
Use of File IO to keep track of the player and score in the text file.
Skill 2
Coding an ending screen that has the ability to restart the game.
Skill 3
Understanding the object's motion, intersections, timers, counters, and detecting collisions and including as part of the game.
Skill 4
Commenting and indenting the code.
Level 1
If the player hits 15 sec, take to the next level
Set boolean complete to true since the actor made it to 10 sec in level1
if (gameTimer.millisElapsed()>15000)
{
complete=true;
gameTimer.mark();
}
Level 2
If the player hits 25 sec, take the player to the boss level
if (level2Timer.millisElapsed()>25000)
{
boss=true;
level2Timer.mark();
}
In level2, the game goes by faster and harder.
This is done by changing the speed that the background image move faster behind the game.
If the user is in level2 or boss level, change the background(speeds up)
if (MyWorld.complete==true || MyWorld.boss==true)
{
setImage("level2.png");
setLocation(getX()+speed+1, getY());
}
Level 1 Sprite
Level 2 Sprite
Enemy Sprite
Collision Detection
Checks if the bird collides with enemy
Enemy eHit = (Enemy)getOneIntersectingObject(Enemy.class);
If bird collides with the enemy, play sound and game over
if (eHit != null)
{
Greenfoot.playSound("chirp.mp3");
Greenfoot.setWorld(new LoseWorld());
}
Checks if the bird collides with pipes on top and bottom
PipeDown pd = (PipeDown)getOneIntersectingObject(PipeDown.class);
PipeUp pu = (PipeUp)getOneIntersectingObject(PipeUp.class);
ReadFile
First, we intialize the Arraylist by;
ArrayList scores = new ArrayList();
Calculates the highest score on the text file and prints out the high score attained by the player.
Spilts the string into the player name(string) and the score(integer).