Answered step by step
Verified Expert Solution
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 Height ;
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 this;
Renderer Random
renderer new Renderer;
rand new Random;
jframe
renderer
Title
DCO
size
mouseListener
keyListener
Resizable
Visible
jframe.addrenderer;
jframe.setTitleFlappy Bird";
jframe.setDefaultCloseOperationJFrameEXITONCLOSE;
jframe.setSizeWidth Height;
jframe.addMouseListenerthis;
jframe.addKeyListenerthis;
jframe.setResizablefalse;
jframe.setVisibletrue;
bird new RectangleWidth Height ;
columns new ArrayList;
addColumntrue;
addColumntrue;
addColumntrue;
addColumntrue;
timer.start;
public void addColumnboolean start
int space ;
int width ;
int height rand.nextInt;
if start
columns.addnew RectangleWidth width columns.size Height height width, height;
columns.addnew RectangleWidth width columnssize width, Height height space;
else
columns.addnew Rectanglecolumnsgetcolumnssizex Height height width, height;
columns.addnew Rectanglecolumnsgetcolumnssizex width, Height height space;
public void paintColumnGraphics g Rectangle column
gsetColorColorRED.darker;
gfillRectcolumnx column.y column.width, column.height;
public void jump
if gameOverFlappy
bird new RectangleWidth Height ;
columns.clear;
gravityMotion ;
score ;
addColumntrue;
addColumntrue;
addColumntrue;
addColumntrue;
gameOverFlappy false;
if started
started true;
else if gameOverFlappy
if gravityMotion
gravityMotion ;
gravityMotion ;
@Override
public void actionPerformedActionEvent e
int speed ;
ticks;
if started
for int i ; i columns.size; i
Rectangle column columns.geti;
column.x speed;
if ticks && gravityMotion
gravityMotion ;
for int i ; i columns.size; i
Rectangle column columns.geti;
if columnx column.width
columns.removecolumn;
if columny
addColumnfalse;
bird.y gravityMotion;
for Rectangle column : columns
if columny && bird.x bird.width column.x column.width && bird.x bird.width column.x column.width
score;
if columnintersectsbird
gameOverFlappy true;
if birdx column.x
bird.x column.x bird.width;
else
if columny
bird.y column.y bird.height;
else if birdy column.height
bird.y column.height;
if birdy Height bird.y
gameOverFlappy true;
if birdy gravityMotion Height
bird.y Height bird.height;
gameOverFlappy true;
renderer.repaint;
public void repaintGraphics g
gsetColorColordarkGray;
gfillRect Width, Height;
gsetColorColororange;
gfillRect Height Width, ;
gsetColorColormagenta;
gfillRect Height Width, ;
gsetColorColorWHITE;
gfillRectbirdx bird.y bird.width, bird.height;
for Rectangle column : columns
paintColumng column;
gsetColorColorgreen;
gsetFontnew FontArial;
if started
gdrawStringClick to activate your game!",
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started