Dwinanda

Pong (Pong)

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