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