Advertisement
Guest User

Untitled

a guest
May 25th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.37 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.awt.Rectangle;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.KeyListener;
  9. import java.awt.event.MouseEvent;
  10. import java.awt.event.MouseListener;
  11. import java.util.ArrayList;
  12. import java.util.Random;
  13.  
  14. import javax.swing.JFrame;
  15. import javax.swing.Timer;
  16.  
  17. public class FlappyBird implements ActionListener, MouseListener, KeyListener
  18. {
  19.  
  20.     public static FlappyBird flappyBird;
  21.  
  22.     public final int WIDTH = 800, HEIGHT = 800;
  23.  
  24.     public Renderer renderer;
  25.  
  26.     public Rectangle bird;
  27.  
  28.     public ArrayList<Rectangle> columns;
  29.  
  30.     public int ticks, yMotion, score;
  31.  
  32.     public boolean gameOver, started;
  33.  
  34.     public Random rand;
  35.  
  36.     public FlappyBird()
  37.     {
  38.         JFrame jframe = new JFrame();
  39.         Timer timer = new Timer(20, this);
  40.  
  41.         renderer = new Renderer();
  42.         rand = new Random();
  43.  
  44.         jframe.add(renderer);
  45.         jframe.setTitle("Flappy Bird");
  46.         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47.         jframe.setSize(WIDTH, HEIGHT);
  48.         jframe.addMouseListener(this);
  49.         jframe.addKeyListener(this);
  50.         jframe.setResizable(false);
  51.         jframe.setVisible(true);
  52.  
  53.         bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
  54.         columns = new ArrayList<Rectangle>();
  55.  
  56.         addColumn(true);
  57.         addColumn(true);
  58.         addColumn(true);
  59.         addColumn(true);
  60.  
  61.         timer.start();
  62.     }
  63.  
  64.     public void addColumn(boolean start)
  65.     {
  66.         int space = 300;
  67.         int width = 100;
  68.         int height = 50 + rand.nextInt(300);
  69.  
  70.         if (start)
  71.         {
  72.             columns.add(new Rectangle(WIDTH + width + columns.size() * 300, HEIGHT - height - 120, width, height));
  73.             columns.add(new Rectangle(WIDTH + width + (columns.size() - 1) * 300, 0, width, HEIGHT - height - space));
  74.         }
  75.         else
  76.         {
  77.             columns.add(new Rectangle(columns.get(columns.size() - 1).x + 600, HEIGHT - height - 120, width, height));
  78.             columns.add(new Rectangle(columns.get(columns.size() - 1).x, 0, width, HEIGHT - height - space));
  79.         }
  80.     }
  81.  
  82.     public void paintColumn(Graphics g, Rectangle column)
  83.     {
  84.         g.setColor(Color.green.darker());
  85.         g.fillRect(column.x, column.y, column.width, column.height);
  86.     }
  87.  
  88.     public void jump()
  89.     {
  90.         if (gameOver)
  91.         {
  92.             bird = new Rectangle(WIDTH / 2 - 10, HEIGHT / 2 - 10, 20, 20);
  93.             columns.clear();
  94.             yMotion = 0;
  95.             score = 0;
  96.  
  97.             addColumn(true);
  98.             addColumn(true);
  99.             addColumn(true);
  100.             addColumn(true);
  101.  
  102.             gameOver = false;
  103.         }
  104.  
  105.         if (!started)
  106.         {
  107.             started = true;
  108.         }
  109.         else if (!gameOver)
  110.         {
  111.             if (yMotion > 0)
  112.             {
  113.                 yMotion = 0;
  114.             }
  115.  
  116.             yMotion -= 10;
  117.         }
  118.     }
  119.  
  120.     @Override
  121.     public void actionPerformed(ActionEvent e)
  122.     {
  123.         int speed = 10;
  124.  
  125.         ticks++;
  126.  
  127.         if (started)
  128.         {
  129.             for (int i = 0; i < columns.size(); i++)
  130.             {
  131.                 Rectangle column = columns.get(i);
  132.  
  133.                 column.x -= speed;
  134.             }
  135.  
  136.             if (ticks % 2 == 0 && yMotion < 15)
  137.             {
  138.                 yMotion += 2;
  139.             }
  140.  
  141.             for (int i = 0; i < columns.size(); i++)
  142.             {
  143.                 Rectangle column = columns.get(i);
  144.  
  145.                 if (column.x + column.width < 0)
  146.                 {
  147.                     columns.remove(column);
  148.  
  149.                     if (column.y == 0)
  150.                     {
  151.                         addColumn(false);
  152.                     }
  153.                 }
  154.             }
  155.  
  156.             bird.y += yMotion;
  157.  
  158.             for (Rectangle column : columns)
  159.             {
  160.                 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)
  161.                 {
  162.                     score++;
  163.                 }
  164.  
  165.                 if (column.intersects(bird))
  166.                 {
  167.                     gameOver = true;
  168.  
  169.                     if (bird.x <= column.x)
  170.                     {
  171.                         bird.x = column.x - bird.width;
  172.  
  173.                     }
  174.                     else
  175.                     {
  176.                         if (column.y != 0)
  177.                         {
  178.                             bird.y = column.y - bird.height;
  179.                         }
  180.                         else if (bird.y < column.height)
  181.                         {
  182.                             bird.y = column.height;
  183.                         }
  184.                     }
  185.                 }
  186.             }
  187.  
  188.             if (bird.y > HEIGHT - 120 || bird.y < 0)
  189.             {
  190.                 gameOver = true;
  191.             }
  192.  
  193.             if (bird.y + yMotion >= HEIGHT - 120)
  194.             {
  195.                 bird.y = HEIGHT - 120 - bird.height;
  196.                 gameOver = true;
  197.             }
  198.         }
  199.  
  200.         renderer.repaint();
  201.     }
  202.  
  203.     public void repaint(Graphics g)
  204.     {
  205.         g.setColor(Color.cyan);
  206.         g.fillRect(0, 0, WIDTH, HEIGHT);
  207.  
  208.         g.setColor(Color.orange);
  209.         g.fillRect(0, HEIGHT - 120, WIDTH, 120);
  210.  
  211.         g.setColor(Color.green);
  212.         g.fillRect(0, HEIGHT - 120, WIDTH, 20);
  213.  
  214.         g.setColor(Color.red);
  215.         g.fillRect(bird.x, bird.y, bird.width, bird.height);
  216.  
  217.         for (Rectangle column : columns)
  218.         {
  219.             paintColumn(g, column);
  220.         }
  221.  
  222.         g.setColor(Color.white);
  223.         g.setFont(new Font("Arial", 1, 100));
  224.  
  225.         if (!started)
  226.         {
  227.             g.drawString("Click to start!", 75, HEIGHT / 2 - 50);
  228.         }
  229.  
  230.         if (gameOver)
  231.         {
  232.             g.drawString("Game Over!", 100, HEIGHT / 2 - 50);
  233.         }
  234.  
  235.         if (!gameOver && started)
  236.         {
  237.             g.drawString(String.valueOf(score), WIDTH / 2 - 25, 100);
  238.         }
  239.     }
  240.  
  241.     public static void main(String[] args)
  242.     {
  243.         flappyBird = new FlappyBird();
  244.     }
  245.  
  246.     @Override
  247.     public void mouseClicked(MouseEvent e)
  248.     {
  249.         jump();
  250.     }
  251.  
  252.     @Override
  253.     public void keyReleased(KeyEvent e)
  254.     {
  255.         if (e.getKeyCode() == KeyEvent.VK_SPACE)
  256.         {
  257.             jump();
  258.         }
  259.     }
  260.    
  261.     @Override
  262.     public void mousePressed(MouseEvent e)
  263.     {
  264.     }
  265.  
  266.     @Override
  267.     public void mouseReleased(MouseEvent e)
  268.     {
  269.     }
  270.  
  271.     @Override
  272.     public void mouseEntered(MouseEvent e)
  273.     {
  274.     }
  275.  
  276.     @Override
  277.     public void mouseExited(MouseEvent e)
  278.     {
  279.     }
  280.  
  281.     @Override
  282.     public void keyTyped(KeyEvent e)
  283.     {
  284.  
  285.     }
  286.  
  287.     @Override
  288.     public void keyPressed(KeyEvent e)
  289.     {
  290.  
  291.     }
  292.  
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement