document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.awt.BasicStroke;  
  2. import java.awt.Color;  
  3. import java.awt.Font;  
  4. import java.awt.Graphics2D;  
  5. import java.awt.RenderingHints;  
  6. import java.awt.event.ActionEvent;  
  7. import java.awt.event.ActionListener;  
  8. import java.awt.event.KeyEvent;  
  9. import java.awt.event.KeyListener;  
  10. import java.util.Random;  
  11. import javax.swing.JFrame;  
  12. import javax.swing.Timer;
  13. /**
  14.  *
  15.  * @author Muthia Qurrota Akyun
  16.  * 18 Desember 2020
  17.  */  
  18. public class Pong implements ActionListener, KeyListener  
  19. {  
  20.     public static Pong pong;  
  21.     public static final Color DARK_GREEN = new Color(0,153,0); //DARK GREEN
  22.     public int width = 700, height = 700;  
  23.     public Renderer renderer;  
  24.     public Paddle player1, player2;  
  25.     public Ball ball;  
  26.     public boolean bot = false, selectingDifficulty;  
  27.     public boolean w, s, up, down;  
  28.     public int gameStatus = 0, scoreLimit = 5, playerWon; //0 = Menu, 1 = Paused, 2 = Playing, 3 = Over  
  29.     public int botDifficulty, botMoves, botCooldown = 0;  
  30.     public Random random;  
  31.     public JFrame jframe;  
  32.     public Pong()  
  33.     {  
  34.         Timer timer = new Timer(20, this);  
  35.         random = new Random();  
  36.         jframe = new JFrame("Pong");  
  37.         renderer = new Renderer();  
  38.         jframe.setSize(width + 15, height + 35);  
  39.         jframe.setVisible(true);  
  40.         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  41.         jframe.add(renderer);  
  42.         jframe.addKeyListener(this);  
  43.         timer.start();  
  44.     }  
  45.     public void start()  
  46.     {  
  47.         gameStatus = 2;  
  48.         player1 = new Paddle(this, 1);  
  49.         player2 = new Paddle(this, 2);  
  50.         ball = new Ball(this);  
  51.     }  
  52.     public void update()  
  53.     {  
  54.         if (player1.score >= scoreLimit){  
  55.             playerWon = 1;  
  56.             gameStatus = 3;  
  57.         }  
  58.         if (player2.score >= scoreLimit){  
  59.             gameStatus = 3;  
  60.             playerWon = 2;  
  61.         }  
  62.         if (w)  
  63.             player1.move(true);  
  64.         if (s)  
  65.             player1.move(false);    
  66.         if (!bot){  
  67.             if (up)  
  68.                 player2.move(true);  
  69.             if (down)  
  70.                 player2.move(false);  
  71.         }  
  72.         else{  
  73.             if (botCooldown > 0) {  
  74.                 botCooldown--;  
  75.                 if (botCooldown == 0)  
  76.                 {  
  77.                     botMoves = 0;  
  78.                 }  
  79.             }  
  80.             if (botMoves < 10){  
  81.                 if (player2.y + player2.height / 2 < ball.y) {  
  82.                     player2.move(false);  
  83.                     botMoves++;  
  84.                 }  
  85.                 if (player2.y + player2.height / 2 > ball.y) {  
  86.                     player2.move(true);  
  87.                     botMoves++;  
  88.                 }  
  89.                 if (botDifficulty == 0)  
  90.                     botCooldown = 20;  
  91.                 if (botDifficulty == 1)  
  92.                     botCooldown = 15;  
  93.                 if (botDifficulty == 2)  
  94.                     botCooldown = 10;  
  95.             }  
  96.            }  
  97.            ball.update(player1, player2);  
  98.       }  
  99.       public void render(Graphics2D g)  
  100.       {  
  101.            g.setColor(DARK_GREEN);  
  102.            g.fillRect(0, 0, width, height);  
  103.            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
  104.            if (gameStatus == 0) {  
  105.                 g.setColor(Color.WHITE);  
  106.                 g.setFont(new Font("Calibri", 1, 50));  
  107.                 g.drawString("PONG", width / 2 - 75, 50);  
  108.                 if (!selectingDifficulty){  
  109.                      g.setFont(new Font("Calibri", 1, 30));  
  110.                      g.drawString("Press Space to Play", width / 2 - 150, height / 2 - 25);  
  111.                      g.drawString("Press Shift to Play with Bot", width / 2 - 200, height / 2 + 25);  
  112.                      g.drawString("<< Score Limit: " + scoreLimit + " >>", width / 2 - 150, height / 2 + 75);  
  113.                 }  
  114.            }  
  115.            if (selectingDifficulty){  
  116.                 String string = botDifficulty == 0 ? "Easy" : (botDifficulty == 1 ? "Medium" : "Hard");  
  117.                 g.setFont(new Font("Calibri", 1, 30));  
  118.                 g.drawString("<< Bot Difficulty: " + string + " >>", width / 2 - 180, height / 2 - 25);  
  119.                 g.drawString("Press Space to Play", width / 2 - 150, height / 2 + 25);  
  120.            }  
  121.            if (gameStatus == 1){  
  122.                 g.setColor(Color.WHITE);  
  123.                 g.setFont(new Font("Calibri", 1, 50));  
  124.                 g.drawString("PAUSED", width / 2 - 103, height / 2 - 25);  
  125.            }  
  126.            if (gameStatus == 1 || gameStatus == 2){  
  127.                 g.setColor(Color.WHITE);  
  128.                 g.setStroke(new BasicStroke(5f));  
  129.                 g.drawLine(width / 2, 0, width / 2, height);  
  130.                 g.setStroke(new BasicStroke(2f));  
  131.                 g.drawOval(width / 2 - 150, height / 2 - 150, 300, 300);  
  132.                 g.setFont(new Font("Arial", 1, 50));  
  133.                 g.drawString(String.valueOf(player1.score), width / 2 - 90, 50);  
  134.                 g.drawString(String.valueOf(player2.score), width / 2 + 65, 50);  
  135.                 player1.render(g);  
  136.                 player2.render(g);  
  137.                 ball.render(g);  
  138.            }  
  139.            if (gameStatus == 3){  
  140.                 g.setFont(new Font("Calibri", 1, 50));  
  141.                 g.drawString("PONG", width / 2 - 75, 50);  
  142.                 if (bot && playerWon == 2){
  143.                      g.setColor(Color.BLACK);  
  144.                      g.drawString("The Bot Wins!", width / 2 - 170, 200);  
  145.                 }
  146.                 else {
  147.                      g.setColor(Color.YELLOW);  
  148.                      g.drawString("Player " + playerWon + " Wins!", width / 2 - 165, 200);
  149.                 }
  150.                 g.setFont(new Font("Calibri", 1, 30));  
  151.                 g.drawString("Press Space to Play Again", width / 2 - 185, height / 2 - 25);  
  152.                 g.drawString("Press ESC for Menu", width / 2 - 140, height / 2 + 25);  
  153.            }  
  154.       }  
  155.       @Override  
  156.       public void actionPerformed(ActionEvent e)  
  157.       {  
  158.            if (gameStatus == 2)  
  159.                 update();  
  160.            renderer.repaint();  
  161.       }  
  162.       public static void main(String[] args)  
  163.       {  
  164.            pong = new Pong();  
  165.       }  
  166.       @Override  
  167.       public void keyPressed(KeyEvent e)  
  168.       {  
  169.            int id = e.getKeyCode();  
  170.            if (id == KeyEvent.VK_W)  
  171.                 w = true;  
  172.            else if (id == KeyEvent.VK_S)  
  173.                 s = true;  
  174.            else if (id == KeyEvent.VK_UP)  
  175.                 up = true;  
  176.            else if (id == KeyEvent.VK_DOWN)  
  177.                 down = true;  
  178.            else if (id == KeyEvent.VK_RIGHT) {  
  179.                 if (selectingDifficulty) {  
  180.                      if (botDifficulty < 2)  
  181.                           botDifficulty++;  
  182.                      else  
  183.                           botDifficulty = 0;  
  184.                 }  
  185.                 else if (gameStatus == 0)  
  186.                     scoreLimit++;  
  187.            }  
  188.            else if (id == KeyEvent.VK_LEFT) {  
  189.                 if (selectingDifficulty) {  
  190.                      if (botDifficulty > 0)  
  191.                           botDifficulty--;    
  192.                      else  
  193.                           botDifficulty = 2;  
  194.                 }  
  195.                 else if (gameStatus == 0 && scoreLimit > 1)  
  196.                     scoreLimit--;  
  197.            }  
  198.            else if (id == KeyEvent.VK_ESCAPE && (gameStatus == 2 || gameStatus == 3))  
  199.                 gameStatus = 0;  
  200.            else if (id == KeyEvent.VK_SHIFT && gameStatus == 0) {  
  201.                 bot = true;  
  202.                 selectingDifficulty = true;  
  203.            }  
  204.            else if (id == KeyEvent.VK_SPACE) {  
  205.                 if (gameStatus == 0 || gameStatus == 3) {  
  206.                      if (!selectingDifficulty)  
  207.                           bot = false;  
  208.                      else  
  209.                           selectingDifficulty = false;  
  210.                      start();  
  211.                 }  
  212.                 else if (gameStatus == 1)  
  213.                      gameStatus = 2;  
  214.                 else if (gameStatus == 2)  
  215.                      gameStatus = 1;  
  216.            }  
  217.       }  
  218.       @Override  
  219.       public void keyReleased(KeyEvent e)  
  220.       {  
  221.            int id = e.getKeyCode();  
  222.            if (id == KeyEvent.VK_W)
  223.                 w = false;  
  224.            else if (id == KeyEvent.VK_S)  
  225.                 s = false;  
  226.            else if (id == KeyEvent.VK_UP)  
  227.                 up = false;  
  228.            else if (id == KeyEvent.VK_DOWN)  
  229.                 down = false;  
  230.       }  
  231.       @Override  
  232.       public void keyTyped(KeyEvent e)  
  233.       {  
  234.       }  
  235.  }  
');