Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fBird;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import java.awt.Image;
- 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.GroupLayout;
- import javax.swing.ImageIcon;
- import javax.swing.JComponent;
- import static javax.swing.JFrame.EXIT_ON_CLOSE;
- import javax.swing.JLabel;
- import javax.swing.JFrame;
- import javax.swing.Timer;
- public class FBird implements ActionListener, MouseListener
- {
- public static FBird fBird;
- public final int WIDTH = 800;
- public final int HEIGHT = 800;
- public Renderer renderer;
- public Rectangle bird;
- public int ticks, yMotion, score, highScore =0;
- public ArrayList<Rectangle> columns;
- public Random rObj;
- public boolean gameOver, started;
- // public Image image;
- public FBird()
- {
- renderer = new Renderer();
- rObj = new Random();
- JFrame jframe = new JFrame();
- Timer timer = new Timer(20, this);
- jframe.add(renderer);
- jframe.setTitle("Flappy Bird");
- jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- jframe.setSize(WIDTH, HEIGHT);
- jframe.addMouseListener(this);
- jframe.setResizable(false);
- jframe.setResizable(true);
- jframe.setVisible(true);
- bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
- columns = new ArrayList<Rectangle>();
- addColumn(true);
- addColumn(true);
- addColumn(true);
- addColumn(true);
- timer.start();
- }
- public void jump()
- {
- if(gameOver)
- {
- bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
- columns.clear();
- yMotion = 0;
- score = 0;
- addColumn(true);
- addColumn(true);
- addColumn(true);
- addColumn(true);
- gameOver = false;
- }
- if(!started)
- {
- started = true;
- }
- else if(!gameOver)
- {
- if(yMotion >0)
- {
- yMotion = 0;
- }
- yMotion -= 10;
- }
- }
- 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;
- }
- 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);
- }
- }
- }
- if (ticks % 2 == 0 && yMotion < 15)
- {
- yMotion +=2;
- }
- bird.y += yMotion;
- 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(score > highScore)
- {
- highScore = score;
- }
- if(column.intersects(bird))
- {
- gameOver = 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)
- {
- gameOver = true;
- }
- if(bird.y + yMotion >= HEIGHT - 120)
- {
- bird.y = HEIGHT - 120 - bird.height;
- }
- }
- }
- renderer.repaint();
- }
- public void addColumn(boolean start)
- {
- int space = 300;
- int width = 100;
- int height = 50 + rObj.nextInt(300);
- 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));
- }
- }
- public void paintColumn(Graphics g, Rectangle column)
- {
- g.setColor(Color.green.darker());
- g.fillRect(column.x, column.y, column.width, column.height);
- }
- public void repaint(Graphics g)
- {
- g.setColor(Color.cyan);
- g.fillRect(0, 0, WIDTH, HEIGHT);
- g.setColor(Color.red);
- g.fillRect(bird.x, bird.y, bird.width, bird.height);
- g.setColor(Color.orange);
- g.fillRect(0, 680, WIDTH, 120);
- g.setColor(Color.green);
- g.fillRect(0, 680, WIDTH, 30);
- //g.drawImage(image,3,4,this);
- for(Rectangle column : columns)
- {
- paintColumn(g, column);
- }
- g.setColor(Color.white);
- g.setFont(new Font("Arial", 1, 100));
- if(!started)
- {
- g.drawString("Click to Start", 75, HEIGHT/2 - 50);
- }
- if(gameOver)
- {
- g.drawString("Game Over", 75, HEIGHT/2 - 50);
- }
- if(!gameOver && started)
- {
- g.drawString(String.valueOf(score), WIDTH/2 - 25, 100);
- g.setFont(new Font("Arial", 1, 30));
- g.drawString("High Score: " + String.valueOf(highScore), WIDTH - 230, 40);
- }
- }
- public static void main(String[] args)
- {
- fBird = new FBird();
- }
- public void mouseClicked(MouseEvent e)
- {
- jump();
- }
- public void mousePressed(MouseEvent e)
- {
- }
- public void mouseReleased(MouseEvent e)
- {
- }
- public void mouseEntered(MouseEvent e)
- {
- }
- public void mouseExited(MouseEvent e)
- {
- }
- public void keyTyped(KeyEvent e)
- {
- }
- public void keyPressed(KeyEvent e)
- {
- }
- }
- package fBird;
- import java.awt.Graphics;
- import javax.swing.JPanel;
- public class Renderer extends JPanel
- {
- private static final long serialVersionUID = 1L;
- protected void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- FBird.fBird.repaint(g);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement