Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.flappybird.view;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.Rectangle;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- import com.flappybird.controller.Controller;
- import com.flappybird.model.Bird;
- import com.flappybird.model.Tube;
- import com.flappybird.model.TubeColumn;
- import com.flappybird.model.proxy.ProxyImage;
- /**
- * Class utama yang berisi bagaimana game ini dijalankan.
- */
- public class Game extends JPanel implements ActionListener {
- private static final long serialVersionUID = 1L;
- private boolean isRunning = false;
- private ProxyImage proxyImage;
- private Image background;
- private Bird bird;
- private TubeColumn tubeColumn;
- private int highScore = 0;
- /**
- * Constructor
- */
- public Game() {
- proxyImage = new ProxyImage("/res/background1.png");
- background = proxyImage.loadImage().getImage();
- setFocusable(true);
- setDoubleBuffered(false);
- addKeyListener(new GameKeyAdapter());
- Timer timer = new Timer(15, this);
- timer.start();
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- Toolkit.getDefaultToolkit().sync();
- if (isRunning) {
- bird.tick();
- tubeColumn.tick();
- checkColision();
- }
- repaint();
- }
- @Override
- public void paint(Graphics g) {
- Graphics2D g2 = (Graphics2D) g;
- g2.drawImage(background, 0, 0, null);
- if (isRunning) {
- this.bird.render(g2, this);
- this.tubeColumn.render(g2, this);
- g2.setColor(Color.black);
- g.setFont(new Font("Arial", 1, 20));
- g2.drawString("Your Score", Window.WIDTH/2-60, 20);
- g2.drawString(""+ this.tubeColumn.getPoints(), Window.WIDTH/2-15, 40);
- } else {
- g2.setColor(Color.black);
- g.setFont(new Font("Arial", 1, 70));
- g2.drawString("FLAPPY BIRD", Window.WIDTH / 2 - 240, Window.HEIGHT / 2 - 50);
- g.setFont(new Font("Arial", 1, 20));
- g2.drawString("Press Space to Make the Bird Jump", Window.WIDTH / 2 - 175, Window.HEIGHT / 2 + 100);
- g2.drawString("Press Enter to Start the Game", Window.WIDTH / 2 - 150, Window.HEIGHT / 2 + 125);
- g2.drawString("High Score", Window.WIDTH/2-60, 20);
- g2.drawString(""+ highScore, Window.WIDTH/2-20, 40);
- g.setFont(new Font("Arial", 1, 5));
- }
- g.dispose();
- }
- /**
- * Fungsi yang dijalankan ketika user ingin bermain kembali
- */
- private void restartGame() {
- if (!isRunning) {
- this.isRunning = true;
- this.bird = new Bird(Window.WIDTH / 2, Window.HEIGHT / 2);
- this.tubeColumn = new TubeColumn();
- }
- }
- /**
- * Fungsi yang dijalankan ketika Bird terkena pipa/Game over
- */
- private void endGame() {
- this.isRunning = false;
- if (this.tubeColumn.getPoints() > highScore) {
- highScore = this.tubeColumn.getPoints();
- }
- this.tubeColumn.setPoints(0);
- }
- /**
- * Fungsi yang dijalankan apakah terjadi tabrakan antara Bird dan Tubes
- */
- private void checkColision() {
- Rectangle rectBird = this.bird.getBounds();
- Rectangle rectTube;
- for (int i = 0; i < this.tubeColumn.getTubes().size(); i++) {
- Tube tempTube = this.tubeColumn.getTubes().get(i);
- rectTube = tempTube.getBounds();
- if (rectBird.intersects(rectTube)) {
- endGame();
- }
- }
- }
- // Key
- private class GameKeyAdapter extends KeyAdapter {
- private final Controller controller;
- public GameKeyAdapter() {
- controller = new Controller();
- }
- @Override
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_ENTER) {
- restartGame();
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {
- if (isRunning) {
- controller.controllerReleased(bird, e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement