document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * Class Pong ini adalah class utama dari game ini
  3.  *
  4.  * @author Muhammad Bagus Istighfar
  5.  * @version 16 Desember 2020
  6.  */
  7. import java.awt.BasicStroke;
  8. import java.awt.Color;
  9. import java.awt.Font;
  10. import java.awt.Graphics2D;
  11. import java.awt.RenderingHints;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import java.awt.event.KeyEvent;
  15. import java.awt.event.KeyListener;
  16. import java.util.Random;
  17. import javax.swing.JFrame;
  18. import javax.swing.Timer;
  19.  
  20. public class Pong implements ActionListener, KeyListener
  21. {
  22.  
  23.     public static Pong pong;
  24.  
  25.     public int width = 700, height = 700;
  26.  
  27.     public Renderer renderer;
  28.  
  29.     public Paddle player1;
  30.  
  31.     public Paddle player2;
  32.  
  33.     public Ball ball;
  34.  
  35.     public boolean bot = false, selectingDifficulty;
  36.  
  37.     public boolean w, s, up, down;
  38.  
  39.     public int gameStatus = 0, scoreLimit = 7, playerWon; //0 = Menu, 1 = Paused, 2 = Playing, 3 = Over
  40.  
  41.     public int botDifficulty, botMoves, botCooldown = 0;
  42.  
  43.     public Random random;
  44.  
  45.     public JFrame jframe;
  46.  
  47.     public Pong()
  48.     {
  49.         Timer timer = new Timer(20, this);
  50.         random = new Random();
  51.  
  52.         jframe = new JFrame("Pong");
  53.  
  54.         renderer = new Renderer();
  55.  
  56.         jframe.setSize(width + 15, height + 35);
  57.         jframe.setVisible(true);
  58.         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  59.         jframe.add(renderer);
  60.         jframe.addKeyListener(this);
  61.  
  62.         timer.start();
  63.     }
  64.  
  65.     public void start()
  66.     {
  67.         gameStatus = 2;
  68.         player1 = new Paddle(this, 1);
  69.         player2 = new Paddle(this, 2);
  70.         ball = new Ball(this);
  71.     }
  72.  
  73.     public void update()
  74.     {
  75.         if (player1.score >= scoreLimit)
  76.         {
  77.             playerWon = 1;
  78.             gameStatus = 3;
  79.         }
  80.  
  81.         if (player2.score >= scoreLimit)
  82.         {
  83.             gameStatus = 3;
  84.             playerWon = 2;
  85.         }
  86.  
  87.         if (w)
  88.         {
  89.             player1.move(true);
  90.         }
  91.         if (s)
  92.         {
  93.             player1.move(false);
  94.         }
  95.  
  96.         if (!bot)
  97.         {
  98.             if (up)
  99.             {
  100.                 player2.move(true);
  101.             }
  102.             if (down)
  103.             {
  104.                 player2.move(false);
  105.             }
  106.         }
  107.         else
  108.         {
  109.             if (botCooldown > 0)
  110.             {
  111.                 botCooldown--;
  112.  
  113.                 if (botCooldown == 0)
  114.                 {
  115.                     botMoves = 0;
  116.                 }
  117.             }
  118.  
  119.             if (botMoves < 10)
  120.             {
  121.                 if (player2.y + player2.height / 2 < ball.y)
  122.                 {
  123.                     player2.move(false);
  124.                     botMoves++;
  125.                 }
  126.  
  127.                 if (player2.y + player2.height / 2 > ball.y)
  128.                 {
  129.                     player2.move(true);
  130.                     botMoves++;
  131.                 }
  132.  
  133.                 if (botDifficulty == 0)
  134.                 {
  135.                     botCooldown = 20;
  136.                 }
  137.                 if (botDifficulty == 1)
  138.                 {
  139.                     botCooldown = 15;
  140.                 }
  141.                 if (botDifficulty == 2)
  142.                 {
  143.                     botCooldown = 10;
  144.                 }
  145.             }
  146.         }
  147.  
  148.         ball.update(player1, player2);
  149.     }
  150.  
  151.     public void render(Graphics2D g)
  152.     {
  153.         g.setColor(Color.BLACK);
  154.         g.fillRect(0, 0, width, height);
  155.         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  156.  
  157.         if (gameStatus == 0)
  158.         {
  159.             g.setColor(Color.WHITE);
  160.             g.setFont(new Font("Comic Sans MS", 1, 50));
  161.  
  162.             g.drawString("PONG", width / 2 - 75, 50);
  163.  
  164.             if (!selectingDifficulty)
  165.             {
  166.                 g.setFont(new Font("Comic Sans MS", 1, 30));
  167.  
  168.                 g.drawString("Press Space to Play", width / 2 - 150, height / 2 - 25);
  169.                 g.drawString("Press Shift to Play with Bot", width / 2 - 200, height / 2 + 25);
  170.                 g.drawString("<< Score Limit: " + scoreLimit + " >>", width / 2 - 150, height / 2 + 75);
  171.             }
  172.         }
  173.  
  174.         if (selectingDifficulty)
  175.         {
  176.             String string = botDifficulty == 0 ? "Easy" : (botDifficulty == 1 ? "Medium" : "Hard");
  177.  
  178.             g.setFont(new Font("Comic Sans MS", 1, 30));
  179.  
  180.             g.drawString("<< Bot Difficulty: " + string + " >>", width / 2 - 180, height / 2 - 25);
  181.             g.drawString("Press Space to Play", width / 2 - 150, height / 2 + 25);
  182.         }
  183.  
  184.         if (gameStatus == 1)
  185.         {
  186.             g.setColor(Color.WHITE);
  187.             g.setFont(new Font("Comic Sans MS", 1, 50));
  188.             g.drawString("PAUSED", width / 2 - 103, height / 2 - 25);
  189.         }
  190.  
  191.         if (gameStatus == 1 || gameStatus == 2)
  192.         {
  193.             g.setColor(Color.WHITE);
  194.  
  195.             g.setStroke(new BasicStroke(5f));
  196.  
  197.             g.drawLine(width / 2, 0, width / 2, height);
  198.  
  199.             g.setStroke(new BasicStroke(2f));
  200.  
  201.             g.drawOval(width / 2 - 150, height / 2 - 150, 300, 300);
  202.  
  203.             g.setFont(new Font("Comic Sans MS", 1, 50));
  204.  
  205.             g.drawString(String.valueOf(player1.score), width / 2 - 90, 50);
  206.             g.drawString(String.valueOf(player2.score), width / 2 + 65, 50);
  207.  
  208.             player1.render(g);
  209.             player2.render(g);
  210.             ball.render(g);
  211.         }
  212.  
  213.         if (gameStatus == 3)
  214.         {
  215.             g.setColor(Color.WHITE);
  216.             g.setFont(new Font("Comic Sans MS", 1, 50));
  217.  
  218.             g.drawString("PONG", width / 2 - 75, 50);
  219.  
  220.             if (bot && playerWon == 2)
  221.             {
  222.                 g.drawString("The Bot Wins!", width / 2 - 170, 200);
  223.             }
  224.             else
  225.             {
  226.                 g.drawString("Player " + playerWon + " Wins!", width / 2 - 165, 200);
  227.             }
  228.  
  229.             g.setFont(new Font("Comic Sans MS", 1, 30));
  230.  
  231.             g.drawString("Press Space to Play Again", width / 2 - 185, height / 2 - 25);
  232.             g.drawString("Press ESC for Menu", width / 2 - 140, height / 2 + 25);
  233.         }
  234.     }
  235.  
  236.     @Override
  237.     public void actionPerformed(ActionEvent e)
  238.     {
  239.         if (gameStatus == 2)
  240.         {
  241.             update();
  242.         }
  243.  
  244.         renderer.repaint();
  245.     }
  246.  
  247.     public static void main(String[] args)
  248.     {
  249.         pong = new Pong();
  250.     }
  251.  
  252.     @Override
  253.     public void keyPressed(KeyEvent e)
  254.     {
  255.         int id = e.getKeyCode();
  256.  
  257.         if (id == KeyEvent.VK_W)
  258.         {
  259.             w = true;
  260.         }
  261.         else if (id == KeyEvent.VK_S)
  262.         {
  263.             s = true;
  264.         }
  265.         else if (id == KeyEvent.VK_UP)
  266.         {
  267.             up = true;
  268.         }
  269.         else if (id == KeyEvent.VK_DOWN)
  270.         {
  271.             down = true;
  272.         }
  273.         else if (id == KeyEvent.VK_RIGHT)
  274.         {
  275.             if (selectingDifficulty)
  276.             {
  277.                 if (botDifficulty < 2)
  278.                 {
  279.                     botDifficulty++;
  280.                 }
  281.                 else
  282.                 {
  283.                     botDifficulty = 0;
  284.                 }
  285.             }
  286.             else if (gameStatus == 0)
  287.             {
  288.                 scoreLimit++;
  289.             }
  290.         }
  291.         else if (id == KeyEvent.VK_LEFT)
  292.         {
  293.             if (selectingDifficulty)
  294.             {
  295.                 if (botDifficulty > 0)
  296.                 {
  297.                     botDifficulty--;
  298.                 }
  299.                 else
  300.                 {
  301.                     botDifficulty = 2;
  302.                 }
  303.             }
  304.             else if (gameStatus == 0 && scoreLimit > 1)
  305.             {
  306.                 scoreLimit--;
  307.             }
  308.         }
  309.         else if (id == KeyEvent.VK_ESCAPE && (gameStatus == 2 || gameStatus == 3))
  310.         {
  311.             gameStatus = 0;
  312.         }
  313.         else if (id == KeyEvent.VK_SHIFT && gameStatus == 0)
  314.         {
  315.             bot = true;
  316.             selectingDifficulty = true;
  317.         }
  318.         else if (id == KeyEvent.VK_SPACE)
  319.         {
  320.             if (gameStatus == 0 || gameStatus == 3)
  321.             {
  322.                 if (!selectingDifficulty)
  323.                 {
  324.                     bot = false;
  325.                 }
  326.                 else
  327.                 {
  328.                     selectingDifficulty = false;
  329.                 }
  330.  
  331.                 start();
  332.             }
  333.             else if (gameStatus == 1)
  334.             {
  335.                 gameStatus = 2;
  336.             }
  337.             else if (gameStatus == 2)
  338.             {
  339.                 gameStatus = 1;
  340.             }
  341.         }
  342.     }
  343.  
  344.     @Override
  345.     public void keyReleased(KeyEvent e)
  346.     {
  347.         int id = e.getKeyCode();
  348.  
  349.         if (id == KeyEvent.VK_W)
  350.         {
  351.             w = false;
  352.         }
  353.         else if (id == KeyEvent.VK_S)
  354.         {
  355.             s = false;
  356.         }
  357.         else if (id == KeyEvent.VK_UP)
  358.         {
  359.             up = false;
  360.         }
  361.         else if (id == KeyEvent.VK_DOWN)
  362.         {
  363.             down = false;
  364.         }
  365.     }
  366.  
  367.     @Override
  368.     public void keyTyped(KeyEvent e)
  369.     {
  370.  
  371.     }
  372. }
');