Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package flappyBird; / / Color import java.awt.Color; / / Font import java.awt.Font; / / Graphics import java.awt.Graphics; / / Rectangle import java.awt.Rectangle; / / ActionEvent

package flappyBird;
//Color
import java.awt.Color;
//Font
import java.awt.Font;
//Graphics
import java.awt.Graphics;
//Rectangle
import java.awt.Rectangle;
//ActionEvent
import java.awt.event.ActionEvent;
//ActionListener
import java.awt.event.ActionListener;
//KeyEvent
import java.awt.event.KeyEvent;
//KeyListener
import java.awt.event.KeyListener;
//MouseEvent
import java.awt.event.MouseEvent;
//MouseListener
import java.awt.event.MouseListener;
//ArrayList
import java.util.ArrayList;
//Random
import java.util.Random;
//JFrame
import javax.swing.JFrame;
//Timer
import javax.swing.Timer;
public class FlappyBird implements ActionListener, MouseListener, KeyListener
{
//flappyBirdGame
public static FlappyBird flappyBirdGame;
// Width, Height
public final int Width =1400, Height =1000;
//renderer
public Renderer renderer;
//bird
public Rectangle bird;
//columns
public ArrayList columns;
// ticks, gravityMotion, score
public int ticks, gravityMotion, score;
// gameOverFlappy, started
public boolean gameOverFlappy, started;
// rand
public Random rand;
public FlappyBird()
{
// jframe... JFrame... Timer
JFrame jframe = new JFrame();
Timer timer = new Timer(20, this);
//Renderer, Random
renderer = new Renderer();
rand = new Random();
/* jframe
* renderer
* Title
* DCO
* size
* mouseListener
* keyListener
* Resizable
* Visible
*/
jframe.add(renderer);
jframe.setTitle("Flappy Bird");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(Width, Height);
jframe.addMouseListener(this);
jframe.addKeyListener(this);
jframe.setResizable(false);
jframe.setVisible(true);
bird = new Rectangle(Width /2-25, Height /2-20,35,35);
columns = new ArrayList();
addColumn(true);
addColumn(true);
addColumn(true);
addColumn(true);
timer.start();
}
public void addColumn(boolean start){
int space =350;
int width =150;
int height =50+ rand.nextInt(300);
if (start){
columns.add(new Rectangle(Width + width + columns.size()*325, Height - height -120, width, height));
columns.add(new Rectangle(Width + width +(columns.size()-1)*325,0, width, Height - height - space));
} else {
columns.add(new Rectangle(columns.get(columns.size()-1).x +700, Height - height -120, width, height));
columns.add(new Rectangle(columns.get(columns.size()-1).x,0, width, Height - height - space));
}
}
public void paintColumn(Graphics g, Rectangle column)
{
g.setColor(Color.RED.darker());
g.fillRect(column.x, column.y, column.width, column.height);
}
public void jump()
{
if (gameOverFlappy){
bird = new Rectangle(Width /2-25, Height /2-25,20,20);
columns.clear();
gravityMotion =0;
score =0;
addColumn(true);
addColumn(true);
addColumn(true);
addColumn(true);
gameOverFlappy = false;
} if (!started){
started = true;
}else if (!gameOverFlappy){
if (gravityMotion >0){
gravityMotion =0;
}
gravityMotion -=10;
}
}
@Override
public void actionPerformed(ActionEvent e){
int speed =10;
ticks++;
if (started){
for (int i =0; i < columns.size(); i++){
Rectangle column = columns.get(i);
column.x -= speed;
}
if (ticks %2==0 && gravityMotion <120){
gravityMotion +=2;
}
for (int i =0; i < columns.size(); i++){
Rectangle column = columns.get(i);
if (column.x + column.width <0){
columns.remove(column);
if (column.y ==0){
addColumn(false);
}
}
}
bird.y += gravityMotion;
for (Rectangle column : columns){
if (column.y ==0 && bird.x + bird.width /2> column.x + column.width /2-10 && bird.x + bird.width /2< column.x + column.width /2+10){
score++;
}
if (column.intersects(bird)){
gameOverFlappy = true;
if (bird.x <= column.x){
bird.x = column.x - bird.width;
} else {
if (column.y !=0){
bird.y = column.y - bird.height;
}
else if (bird.y < column.height){
bird.y = column.height;
}
}
}
}
if (bird.y > Height -120|| bird.y <0){
gameOverFlappy = true;
}
if (bird.y + gravityMotion >= Height -120){
bird.y = Height -120- bird.height;
gameOverFlappy = true;
}
}
renderer.repaint();
}
public void repaint(Graphics g){
g.setColor(Color.darkGray);
g.fillRect(0,0, Width, Height);
g.setColor(Color.orange);
g.fillRect(0, Height -120, Width, 120);
g.setColor(Color.magenta);
g.fillRect(0, Height -120, Width, 20);
g.setColor(Color.WHITE);
g.fillRect(bird.x, bird.y, bird.width, bird.height);
for (Rectangle column : columns){
paintColumn(g, column);
}
g.setColor(Color.green);
g.setFont(new Font("Arial",1,100));
if (!started){
g.drawString("Click to activate your game!", 100

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions