Flappy Bird - Java Code
Flappy Bird - Java Code
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics; The list of imported classes to be used
import java.awt.Graphics2D; for graphics, mouse and key
import java.awt.Rectangle; interactions, array, random int, timer
import java.awt.RenderingHints; and screen frame.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.Timer;
public FlappyBird() {
frame.add(renderer);
frame.setTitle("Flappy Bird");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.addMouseListener(this);
frame.addKeyListener(this);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
if (start) {
columns.add(new Rectangle(WIDTH + width + columns.size() * 300, HEIGHT - height - 120, width, height));
columns.add(new Rectangle(WIDTH + width + (columns.size() - 1) * 300, 0, width, HEIGHT - height - space));
} else {
columns.add(new Rectangle(columns.get(columns.size() - 1).x + 600, HEIGHT - height - 120, width, height));
columns.add(new Rectangle(columns.get(columns.size() - 1).x, 0, width, HEIGHT - height - space));
}
}
If start becomes true, green rectangles that are opposite of each other are
created, replicating the pipes in the original Flappy Bird game.
It will set the columns on the right side of the screen then, it will continue to
create it as the game goes. The height is set to random to create variation for
the bird’s obstacles.
public void paintColumn(Graphics g, Rectangle column) { This method gives the column its
g.setColor(Color.green.darker()); shape and color.
g.fillRect(column.x, column.y, column.width, column.height);
}
Public void jump() {
if (gameOver) {
bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
If gameOver is true, a new rectangle
columns.clear();
will be created. The columns will be
yMotion = 0;
cleared, the yMotion and score will
score = 0;
revert back to 0. The
columnSpeedMultiplier = 1;
columnSpeedMultiplier will go back
to 1.
for (int i = 0; i <= 4; i++) {
addColumn(true);
New columns will be called again and
}
the gameOver will be set to false.
gameOver = false;
}
if (!started) {
If started is false, asign it with true.
started = true;
And as the jump method is being
} else if (!gameOver) {
called, it will assign -8 to the yMotion
if (yMotion > 0) {
of the bird sprite.
yMotion = 0;
}
Else, if the gameOver is false set the
yMotion -= 8;
the yMotion to 0 is it’s greater than
}
0.
}
if (column.intersects(bird)) {
gameOver = true;
paintCloud(g);
g.setColor(Color.orange);
g.fillRect(0, HEIGHT - 120, WIDTH, 120);
g.setColor(Color.green);
g.fillRect(0, HEIGHT - 120, WIDTH, 20);
g2d.setColor(Color.yellow);
g.fillOval(bird.x, bird.y, bird.width, bird.height);
g2d.setColor(Color.black);
g.drawOval(bird.x, bird.y, bird.width, bird.height);
g.setColor(Color.white);
g.setFont(new Font("Arial", 1, 100));
if (!started) {
g.drawString("Click to start!", 150, HEIGHT / 2 - 50);
}
if (gameOver) {
g.drawString("Game Over!", 220, HEIGHT / 2 - 90); The drawString method shows the
g.drawString("Retry Again!", 220, HEIGHT / 2 - 0); texts and score on the screen of the
game.
g.setFont(new Font("Arial", 1, 50));
g.drawString("Score: " + score, WIDTH / 2 - 70, HEIGHT / 2 + 150);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
}
import java.awt.Graphics;
import javax.swing.JPanel;
}
}