dewimardanic

Untitled

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