Advertisement
raffi_pratama

Untitled

Jan 11th, 2021
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.46 KB | None | 0 0
  1. package com.flappybird.view;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.Image;
  8. import java.awt.Rectangle;
  9. import java.awt.Toolkit;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.awt.event.KeyAdapter;
  13. import java.awt.event.KeyEvent;
  14.  
  15. import javax.swing.JPanel;
  16. import javax.swing.Timer;
  17.  
  18. import com.flappybird.controller.Controller;
  19. import com.flappybird.model.Bird;
  20. import com.flappybird.model.Tube;
  21. import com.flappybird.model.TubeColumn;
  22. import com.flappybird.model.proxy.ProxyImage;
  23. /**
  24.  * Class utama yang berisi bagaimana game ini dijalankan.
  25.  */
  26. public class Game extends JPanel implements ActionListener {
  27.  
  28.  private static final long serialVersionUID = 1L;
  29.  private boolean isRunning = false;
  30.     private ProxyImage proxyImage;
  31.     private Image background;
  32.     private Bird bird;
  33.     private TubeColumn tubeColumn;
  34.     private int highScore = 0;
  35.    
  36.     /**
  37.      * Constructor
  38.      */
  39.     public Game() {
  40.  
  41.         proxyImage = new ProxyImage("/res/background1.png");
  42.         background = proxyImage.loadImage().getImage();
  43.         setFocusable(true);
  44.         setDoubleBuffered(false);
  45.         addKeyListener(new GameKeyAdapter());
  46.         Timer timer = new Timer(15, this);
  47.         timer.start();
  48.     }
  49.  
  50.     @Override
  51.     public void actionPerformed(ActionEvent e) {
  52.         Toolkit.getDefaultToolkit().sync();
  53.         if (isRunning) {
  54.             bird.tick();
  55.             tubeColumn.tick();
  56.             checkColision();
  57.         }
  58.  
  59.         repaint();
  60.     }
  61.  
  62.     @Override
  63.     public void paint(Graphics g) {
  64.         Graphics2D g2 = (Graphics2D) g;
  65.         g2.drawImage(background, 0, 0, null);
  66.         if (isRunning) {
  67.             this.bird.render(g2, this);
  68.             this.tubeColumn.render(g2, this);
  69.             g2.setColor(Color.black);
  70.             g.setFont(new Font("Arial", 1, 20));
  71.             g2.drawString("Your Score", Window.WIDTH/2-60, 20);
  72.             g2.drawString(""+ this.tubeColumn.getPoints(), Window.WIDTH/2-15, 40);
  73.         } else {
  74.             g2.setColor(Color.black);
  75.             g.setFont(new Font("Arial", 1, 70));
  76.             g2.drawString("FLAPPY BIRD", Window.WIDTH / 2 - 240, Window.HEIGHT / 2 - 50);
  77.             g.setFont(new Font("Arial", 1, 20));
  78.             g2.drawString("Press Space to Make the Bird Jump", Window.WIDTH / 2 - 175, Window.HEIGHT / 2 + 100);
  79.             g2.drawString("Press Enter to Start the Game", Window.WIDTH / 2 - 150, Window.HEIGHT / 2 + 125);
  80.             g2.drawString("High Score", Window.WIDTH/2-60, 20);
  81.             g2.drawString(""+ highScore, Window.WIDTH/2-20, 40);
  82.             g.setFont(new Font("Arial", 1, 5));
  83.         }
  84.  
  85.         g.dispose();
  86.     }
  87.    
  88.     /**
  89.      * Fungsi yang dijalankan ketika user ingin bermain kembali
  90.      */
  91.     private void restartGame() {
  92.         if (!isRunning) {
  93.             this.isRunning = true;
  94.             this.bird = new Bird(Window.WIDTH / 2, Window.HEIGHT / 2);
  95.             this.tubeColumn = new TubeColumn();
  96.         }
  97.     }
  98.    
  99.     /**
  100.      * Fungsi yang dijalankan ketika Bird terkena pipa/Game over
  101.      */
  102.     private void endGame() {
  103.         this.isRunning = false;
  104.         if (this.tubeColumn.getPoints() > highScore) {
  105.             highScore = this.tubeColumn.getPoints();
  106.         }
  107.         this.tubeColumn.setPoints(0);
  108.  
  109.     }
  110.    
  111.     /**
  112.      * Fungsi yang dijalankan apakah terjadi tabrakan antara Bird dan Tubes
  113.      */
  114.     private void checkColision() {
  115.         Rectangle rectBird = this.bird.getBounds();
  116.         Rectangle rectTube;
  117.  
  118.         for (int i = 0; i < this.tubeColumn.getTubes().size(); i++) {
  119.             Tube tempTube = this.tubeColumn.getTubes().get(i);
  120.             rectTube = tempTube.getBounds();
  121.             if (rectBird.intersects(rectTube)) {
  122.                 endGame();
  123.             }
  124.         }
  125.     }
  126.  
  127.     // Key
  128.     private class GameKeyAdapter extends KeyAdapter {
  129.  
  130.         private final Controller controller;
  131.  
  132.         public GameKeyAdapter() {
  133.             controller = new Controller();
  134.         }
  135.  
  136.         @Override
  137.         public void keyPressed(KeyEvent e) {
  138.             if (e.getKeyCode() == KeyEvent.VK_ENTER) {
  139.                 restartGame();
  140.             }
  141.         }
  142.  
  143.         @Override
  144.         public void keyReleased(KeyEvent e) {
  145.             if (isRunning) {
  146.                 controller.controllerReleased(bird, e);
  147.             }
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement