document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  *  Final Project PBO
  3.  *  Class About
  4.  *  
  5.  *  @author Muhammad Naufal Arieffaza
  6.  *  @version 12 Januari 2021
  7.  */
  8.  
  9. package brickout;
  10.  
  11. import java.awt.Color;
  12. import java.awt.Font;
  13. import java.awt.Graphics;
  14. import java.awt.Graphics2D;
  15. import java.awt.Rectangle;
  16. import java.awt.event.ActionEvent;
  17. import java.awt.event.ActionListener;
  18. import java.awt.event.KeyEvent;
  19. import java.awt.event.KeyListener;
  20. import java.util.Random;
  21. import javax.swing.JPanel;
  22. import javax.swing.Timer;
  23.  
  24. public class GamePlay extends JPanel implements KeyListener, ActionListener {  //KeyListener detecting arroy key and ActionListener for moving the ball
  25.  
  26.     private boolean play = false;
  27.     public int score = 0;
  28.  
  29.     private int totalBricks = 40;
  30.  
  31.     private Timer timer;
  32.     private int delay = 9;
  33.  
  34.     private int playerX = 300;
  35.  
  36.     private int ballPosX = 290;
  37.     private int ballPosY = 350;
  38.     private int ballDirX = getRandomNumberForX();
  39.     private int ballDirY = getRandomNumberForY();
  40.  
  41.     private MapGenerator mapPlay;
  42.  
  43.     public GamePlay() {
  44.         mapPlay = new MapGenerator(4, 10);
  45.  
  46.         addKeyListener(this);
  47.         setFocusable(true);
  48.         setFocusTraversalKeysEnabled(false);
  49.         timer = new Timer(delay, this);
  50.         timer.start();
  51.     }
  52.  
  53.     @Override
  54.     public void paint(Graphics graphics) {
  55.         //background
  56.         graphics.setColor(Color.black);
  57.         graphics.fillRect(1, 1, 692, 592);
  58.  
  59.         //drawing map of bricks
  60.         mapPlay.draw((Graphics2D) graphics, Color.yellow);
  61.  
  62.         //border
  63.         graphics.setColor(Color.black);
  64.         graphics.fillRect(0, 0, 3, 592);
  65.         graphics.fillRect(0, 0, 692, 3);
  66.         graphics.fillRect(691, 1, 3, 592);
  67.  
  68.         //score
  69.         graphics.setColor(Color.white);
  70.         graphics.setFont(new Font("serif", Font.BOLD, 22));
  71.         graphics.drawString("Score: " + score + "/200", 490, 30);
  72.  
  73.         //paddle
  74.         graphics.setColor(Color.white);
  75.         graphics.fillRect(playerX, 550, 100, 8);
  76.  
  77.         if (play == false) {
  78.             //game start message
  79.             graphics.setColor(Color.YELLOW);
  80.             graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 25));
  81.             graphics.drawString("Press Enter/Left/Right Arrow to start the game!", 90, 350);
  82.  
  83.             //ball hiding
  84.             graphics.setColor(Color.black);
  85.             graphics.fillOval(ballPosX, ballPosY, 20, 20);
  86.         } else {
  87.             //ball showing
  88.             graphics.setColor(Color.white);
  89.             graphics.fillOval(ballPosX, ballPosY, 20, 20);
  90.         }
  91.  
  92.         if (score >= 50 && score < 100) {
  93.             //ball color & size change
  94.             graphics.setColor(Color.yellow);
  95.             graphics.fillOval(ballPosX, ballPosY, 21, 21);
  96.         } else if (score >= 100 && score < 150) {
  97.             //ball
  98.             graphics.setColor(Color.orange);
  99.             graphics.fillOval(ballPosX, ballPosY, 22, 22);
  100.         } else if (score >= 150) {
  101.             //ball
  102.             graphics.setColor(Color.red);
  103.             graphics.fillOval(ballPosX, ballPosY, 23, 23);
  104.         }
  105.  
  106.         if (totalBricks <= 0) {
  107.             play = false;
  108.             ballDirX = 0;
  109.             ballDirY = 0;
  110.  
  111.             //hiding the ball after game over
  112.             graphics.setColor(Color.black);
  113.             graphics.fillOval(ballPosX, ballPosY, 23, 23);
  114.  
  115.             graphics.setColor(Color.RED);
  116.             graphics.setFont(new Font("serif", Font.BOLD, 30));
  117.             graphics.drawString("Victory! Score: " + score, 200, 300);
  118.  
  119.             graphics.setColor(Color.white);
  120.             graphics.setFont(new Font("serif", Font.BOLD, 20));
  121.             graphics.drawString("Press Enter to Restart..", 230, 330);
  122.  
  123.             //above score hiding
  124.             graphics.setColor(Color.black);
  125.             graphics.setFont(new Font("serif", Font.BOLD, 22));
  126.             graphics.drawString("Score: " + score + "/200", 490, 30);
  127.  
  128.             //hide remains bricks
  129.             mapPlay.draw((Graphics2D) graphics, Color.BLACK);
  130.  
  131.             //paddle
  132.             graphics.setColor(Color.black);
  133.             graphics.fillRect(playerX, 550, 100, 8);
  134.  
  135.             //game start message
  136.             graphics.setColor(Color.BLACK);
  137.             graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 25));
  138.             graphics.drawString("Press Enter/Left/Right Arrow to start the game!", 90, 350);
  139.         }
  140.  
  141.         if (ballPosY > 570) { // if ball fall in down
  142.             play = false;
  143.             ballDirX = 0;
  144.             ballDirY = 0;
  145.  
  146.             //hiding the ball after game over
  147.             graphics.setColor(Color.black);
  148.             graphics.fillOval(ballPosX, ballPosY, 23, 23);
  149.  
  150.             graphics.setColor(Color.RED);
  151.             graphics.setFont(new Font("serif", Font.BOLD, 30));
  152.             graphics.drawString("Game over! Score: " + score, 200, 300);
  153.  
  154.             graphics.setColor(Color.white);
  155.             graphics.setFont(new Font("serif", Font.BOLD, 20));
  156.             graphics.drawString("Press Enter to Restart..", 230, 330);
  157.  
  158.             //above score hiding
  159.             graphics.setColor(Color.black);
  160.             graphics.setFont(new Font("serif", Font.BOLD, 22));
  161.             graphics.drawString("Score: " + score + "/200", 490, 30);
  162.  
  163.             //hide remains bricks
  164.             mapPlay.draw((Graphics2D) graphics, Color.BLACK);
  165.  
  166.             //paddle
  167.             graphics.setColor(Color.black);
  168.             graphics.fillRect(playerX, 550, 100, 8);
  169.  
  170.             //game start message
  171.             graphics.setColor(Color.BLACK);
  172.             graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 25));
  173.             graphics.drawString("Press Enter/Left/Right Arrow to start the game!", 90, 350);
  174.         }
  175.         graphics.dispose();
  176.     }
  177.  
  178.     @Override
  179.     public void keyPressed(KeyEvent ke) {
  180.         if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {
  181.             if (playerX >= 600) {
  182.                 playerX = 600;
  183.             } else {
  184.                 moveRight();
  185.             }
  186.         }
  187.         if (ke.getKeyCode() == KeyEvent.VK_LEFT) {
  188.             if (playerX < 10) {
  189.                 playerX = 10;
  190.             } else {
  191.                 moveLeft();
  192.             }
  193.         }
  194.         if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
  195.             if (!play) {
  196.                 play = true;
  197.                 playerX = 310;
  198.                 ballPosX = 290;
  199.                 ballPosY = 350;
  200.                 ballDirX = getRandomNumberForX();
  201.                 ballDirY = getRandomNumberForY();
  202.                 totalBricks = 40;
  203.  
  204.                 mapPlay = new MapGenerator(4, 10);
  205.                 score = 0;
  206.  
  207.                 repaint();
  208.             }
  209.         }
  210.     }
  211.  
  212.     public void moveRight() {
  213.         play = true;
  214.         playerX += 20;
  215.     }
  216.  
  217.     public void moveLeft() {
  218.         play = true;
  219.         playerX -= 20;
  220.     }
  221.  
  222.     @Override
  223.     public void actionPerformed(ActionEvent ae) {
  224.         timer.start();
  225.  
  226.         if (play) {
  227.             if (new Rectangle(ballPosX, ballPosY, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8))) {
  228.                 ballDirY = -ballDirY;
  229.             }
  230.  
  231.             A:
  232.             for (int i = 0; i < mapPlay.map.length; i++) {
  233.                 for (int j = 0; j < mapPlay.map[0].length; j++) {
  234.                     if (mapPlay.map[i][j] > 0) {
  235.                         int brickX = j * mapPlay.brickWidth + 80;
  236.                         int brickY = i * mapPlay.brickHeight + 50;
  237.                         int brickWidth = mapPlay.brickWidth;
  238.                         int brickHeight = mapPlay.brickHeight;
  239.  
  240.                         Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight);
  241.                         Rectangle ballRect = new Rectangle(ballPosX, ballPosY, 20, 20);
  242.                         Rectangle brickRect = rect;
  243.  
  244.                         if (ballRect.intersects(brickRect)) {
  245.                             mapPlay.setBrickValue(0, i, j);
  246.                             totalBricks--;
  247.                             score += 5;
  248.  
  249.                             if (ballPosX + 19 <= brickRect.x || ballPosX + 1 >= brickRect.x + brickRect.width) {
  250.                                 ballDirX = -ballDirX;
  251.                             } else {
  252.                                 ballDirY = -ballDirY;
  253.                             }
  254.                             break A;
  255.                         }
  256.                     }
  257.                 }
  258.             }
  259.  
  260.             ballPosX += ballDirX;
  261.             ballPosY += ballDirY;
  262.  
  263.             if (ballPosX < 0) {  //for left
  264.                 ballDirX = -ballDirX;
  265.             }
  266.             if (ballPosY < 0) { //for top
  267.                 ballDirY = -ballDirY;
  268.             }
  269.             if (ballPosX > 670) { //for right
  270.                 ballDirX = -ballDirX;
  271.             }
  272.         }
  273.         repaint();
  274.     }
  275.  
  276.     @Override
  277.     public void keyTyped(KeyEvent ke) {
  278.  
  279.     }
  280.  
  281.     @Override
  282.     public void keyReleased(KeyEvent ke) {
  283.  
  284.     }
  285.  
  286.     public int getRandomNumberForY() {
  287.         Random random = new Random();
  288.         int max = -1;
  289.         int min = -5;
  290.         int randomNumber = min + random.nextInt(max - min + 1);
  291.         return randomNumber;
  292.     }
  293.  
  294.     public int getRandomNumberForX() {
  295.         Random random = new Random();
  296.         int max = -1;
  297.         int min = -3;
  298.         int randomNumber = min + random.nextInt(max - min + 1);
  299.         return randomNumber;
  300.     }
  301. }
');