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.  * Class Pong ini adalah class utama pada program ini
  15.  *
  16.  * author Hanifa Fauziah
  17.  * version 1.0
  18.  */
  19. public class Pong implements ActionListener, KeyListener{
  20.     public static Pong pong;
  21.     public int width = 700, height = 700;
  22.     public Renderer renderer;
  23.     public Paddle player1;
  24.     public Paddle player2;
  25.     public Ball ball;
  26.  
  27.     public boolean bot = false, selectingDifficulty;
  28.     public boolean w, s, up, down;
  29.     public int gameStatus = 0, scoreLimit = 7, playerWon;
  30.     //0 = Menu, 1 = Paused, 2 = Playing, 3 = Over
  31.  
  32.     public int botDifficulty, botMoves, botCooldown = 0;
  33.  
  34.     public Random random;
  35.  
  36.     public JFrame jframe;
  37.  
  38.     public Pong(){
  39.         Timer timer = new Timer(20, this);
  40.         random = new Random();
  41.  
  42.         jframe = new JFrame("Game Pong");
  43.  
  44.         renderer = new Renderer();
  45.  
  46.         jframe.setSize(width + 15, height + 35);
  47.         jframe.setVisible(true);
  48.         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  49.         jframe.add(renderer);
  50.         jframe.addKeyListener(this);
  51.  
  52.         timer.start();
  53.     }
  54.  
  55.     public void start(){
  56.         gameStatus = 2;
  57.         player1 = new Paddle(this, 1);
  58.         player2 = new Paddle(this, 2);
  59.         ball = new Ball(this);
  60.     }
  61.  
  62.     public void update(){
  63.         if (player1.score >= scoreLimit){
  64.             playerWon = 1;
  65.             gameStatus = 3;
  66.         }
  67.  
  68.         if (player2.score >= scoreLimit){
  69.             gameStatus = 3;
  70.             playerWon = 2;
  71.         }
  72.  
  73.         if (w){
  74.             player1.move(true);
  75.         }
  76.         if (s){
  77.             player1.move(false);
  78.         }
  79.  
  80.         if (!bot){
  81.             if (up) {
  82.                 player2.move(true);
  83.             }
  84.             if (down){
  85.                 player2.move(false);
  86.             }
  87.         }
  88.         else{
  89.             if (botCooldown > 0){
  90.                 botCooldown--;
  91.                 if (botCooldown == 0){
  92.                     botMoves = 0;
  93.                 }
  94.             }
  95.  
  96.             if (botMoves < 10){
  97.                 if (player2.y + player2.height / 2 < ball.y){
  98.                     player2.move(false);
  99.                     botMoves++;
  100.                 }
  101.  
  102.                 if (player2.y + player2.height / 2 > ball.y){
  103.                     player2.move(true);
  104.                     botMoves++;
  105.                 }
  106.  
  107.                 if (botDifficulty == 0){
  108.                     botCooldown = 20;
  109.                 }
  110.                 if (botDifficulty == 1){
  111.                     botCooldown = 15;
  112.                 }
  113.                 if (botDifficulty == 2){
  114.                     botCooldown = 10;
  115.                 }
  116.             }
  117.         }
  118.         ball.update(player1, player2);
  119.     }
  120.  
  121.     public void render(Graphics2D g){
  122.         g.setColor(Color.BLACK);
  123.         g.fillRect(0, 0, width, height);
  124.         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  125.  
  126.         if (gameStatus == 0){
  127.             g.setColor(Color.WHITE);
  128.             g.setFont(new Font("Arial", 1, 50));
  129.  
  130.             g.drawString("PONG", width / 2 - 75, 50);
  131.  
  132.             if (!selectingDifficulty){
  133.                 g.setFont(new Font("Arial", 1, 30));
  134.                 g.drawString("Press Space to Play", width / 2 - 150, height / 2 - 25);
  135.                 g.drawString("Press Shift to Play with Bot", width / 2 - 200, height / 2 + 25);
  136.                 g.drawString("<< Score Limit: " + scoreLimit + " >>", width / 2 - 150, height / 2 + 75);
  137.             }
  138.         }
  139.  
  140.         if (selectingDifficulty){
  141.             String string = botDifficulty == 0 ? "Easy" : (botDifficulty == 1 ? "Medium" : "Hard");
  142.  
  143.             g.setFont(new Font("Arial", 1, 30));
  144.  
  145.             g.drawString("<< Bot Difficulty: " + string + " >>", width / 2 - 180, height / 2 - 25);
  146.             g.drawString("Press Space to Play", width / 2 - 150, height / 2 + 25);
  147.         }
  148.  
  149.         if (gameStatus == 1){
  150.             g.setColor(Color.WHITE);
  151.             g.setFont(new Font("Arial", 1, 50));
  152.             g.drawString("PAUSED", width / 2 - 103, height / 2 - 25);
  153.         }
  154.  
  155.         if (gameStatus == 1 || gameStatus == 2){
  156.             g.setColor(Color.WHITE);
  157.             g.setStroke(new BasicStroke(5f));
  158.             g.drawLine(width / 2, 0, width / 2, height);
  159.             g.setStroke(new BasicStroke(2f));
  160.             g.drawOval(width / 2 - 150, height / 2 - 150, 300, 300);
  161.             g.setFont(new Font("Arial", 1, 50));
  162.  
  163.             g.drawString(String.valueOf(player1.score), width / 2 - 90, 50);
  164.             g.drawString(String.valueOf(player2.score), width / 2 + 65, 50);
  165.  
  166.             player1.render(g);
  167.             player2.render(g);
  168.             ball.render(g);
  169.         }
  170.  
  171.         if (gameStatus == 3){
  172.             g.setColor(Color.WHITE);
  173.             g.setFont(new Font("Arial", 1, 50));
  174.  
  175.             g.drawString("PONG", width / 2 - 75, 50);
  176.  
  177.             if (bot && playerWon == 2){
  178.                 g.drawString("The Bot Wins!", width / 2 - 170, 200);
  179.             }
  180.             else{
  181.                 g.drawString("Player " + playerWon + " Wins!", width / 2 - 165, 200);
  182.             }
  183.  
  184.             g.setFont(new Font("Arial", 1, 30));
  185.  
  186.             g.drawString("Press Space to Play Again", width / 2 - 185, height / 2 - 25);
  187.             g.drawString("Press ESC for Menu", width / 2 - 140, height / 2 + 25);
  188.         }
  189.     }
  190.  
  191.     @Override
  192.     public void actionPerformed(ActionEvent e){
  193.         if (gameStatus == 2){
  194.             update();
  195.         }
  196.  
  197.         renderer.repaint();
  198.     }
  199.  
  200.     public static void main(String[] args){
  201.         pong = new Pong();
  202.     }
  203.  
  204.     @Override
  205.     public void keyPressed(KeyEvent e){
  206.         int id = e.getKeyCode();
  207.  
  208.         if (id == KeyEvent.VK_W){
  209.             w = true;
  210.         }
  211.         else if (id == KeyEvent.VK_S){
  212.             s = true;
  213.         }
  214.         else if (id == KeyEvent.VK_UP){
  215.             up = true;
  216.         }
  217.         else if (id == KeyEvent.VK_DOWN){
  218.             down = true;
  219.         }
  220.         else if (id == KeyEvent.VK_RIGHT){
  221.             if (selectingDifficulty){
  222.                 if (botDifficulty < 2){
  223.                     botDifficulty++;
  224.                 }
  225.                 else{
  226.                     botDifficulty = 0;
  227.                 }
  228.             }
  229.             else if (gameStatus == 0){
  230.                 scoreLimit++;
  231.             }
  232.         }
  233.         else if (id == KeyEvent.VK_LEFT){
  234.             if (selectingDifficulty){
  235.                 if (botDifficulty > 0){
  236.                     botDifficulty--;
  237.                 }
  238.                 else{
  239.                     botDifficulty = 2;
  240.                 }
  241.             }
  242.             else if (gameStatus == 0 && scoreLimit > 1){
  243.                 scoreLimit--;
  244.             }
  245.         }
  246.         else if (id == KeyEvent.VK_ESCAPE && (gameStatus == 2 || gameStatus == 3)){
  247.             gameStatus = 0;
  248.         }
  249.         else if (id == KeyEvent.VK_SHIFT && gameStatus == 0){
  250.             bot = true;
  251.             selectingDifficulty = true;
  252.         }
  253.         else if (id == KeyEvent.VK_SPACE){
  254.             if (gameStatus == 0 || gameStatus == 3){
  255.                 if (!selectingDifficulty){
  256.                     bot = false;
  257.                 }
  258.                 else{
  259.                     selectingDifficulty = false;
  260.                 }
  261.  
  262.                 start();
  263.             }
  264.             else if (gameStatus == 1){
  265.                 gameStatus = 2;
  266.             }
  267.             else if (gameStatus == 2){
  268.                 gameStatus = 1;
  269.             }
  270.         }
  271.     }
  272.  
  273.     @Override
  274.     public void keyReleased(KeyEvent e){
  275.         int id = e.getKeyCode();
  276.  
  277.         if (id == KeyEvent.VK_W){
  278.             w = false;
  279.         }
  280.         else if (id == KeyEvent.VK_S){
  281.             s = false;
  282.         }
  283.         else if (id == KeyEvent.VK_UP){
  284.             up = false;
  285.         }
  286.         else if (id == KeyEvent.VK_DOWN){
  287.             down = false;
  288.         }
  289.     }
  290.  
  291.     @Override
  292.     public void keyTyped(KeyEvent e){}
  293. }
');