MasMasDo

Untitled

Dec 14th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.03 KB | None | 0 0
  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.  
  12. import javax.swing.JFrame;
  13. import javax.swing.Timer;
  14.  
  15. /**
  16.  * Class Pong untuk membuat opsi permainan, bot,
  17.  * score, dan pengaturan dasar game.
  18.  *
  19.  * @author Naufaliando
  20.  * @version 14/12/2020
  21.  */
  22.  
  23. public class Pong implements ActionListener, KeyListener
  24. {
  25.     public static Pong pong;
  26.     public int width = 700, height = 700;
  27.     public Renderer renderer;
  28.     public Paddle player1;
  29.     public Paddle player2;
  30.     public Ball ball;
  31.     public boolean bot = false, selectingDifficulty;
  32.     public boolean w, s, up, down;
  33.     public int gameStatus = 0, scoreLimit = 7, playerWon; //0 = Menu, 1 = Paused, 2 = Playing, 3 = Over
  34.     public int botDifficulty, botMoves, botCooldown = 0;
  35.     public Random random;
  36.     public JFrame jframe;
  37.  
  38.     public Pong()
  39.     {
  40.         Timer timer = new Timer(20, this);
  41.         random = new Random();
  42.  
  43.         jframe = new JFrame("Pong");
  44.  
  45.         renderer = new Renderer();
  46.  
  47.         jframe.setSize(width + 15, height + 35);
  48.         jframe.setVisible(true);
  49.         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50.         jframe.add(renderer);
  51.         jframe.addKeyListener(this);
  52.  
  53.         timer.start();
  54.     }
  55.  
  56.     public void start()
  57.     {
  58.         gameStatus = 2;
  59.         player1 = new Paddle(this, 1);
  60.         player2 = new Paddle(this, 2);
  61.         ball = new Ball(this);
  62.     }
  63.  
  64.     public void update()
  65.     {
  66.         if (player1.score >= scoreLimit)
  67.         {
  68.             playerWon = 1;
  69.             gameStatus = 3;
  70.         }
  71.  
  72.         if (player2.score >= scoreLimit)
  73.         {
  74.             gameStatus = 3;
  75.             playerWon = 2;
  76.         }
  77.  
  78.         if (w)
  79.         {
  80.             player1.move(true);
  81.         }
  82.         if (s)
  83.         {
  84.             player1.move(false);
  85.         }
  86.  
  87.         if (!bot)
  88.         {
  89.             if (up)
  90.             {
  91.                 player2.move(true);
  92.             }
  93.             if (down)
  94.             {
  95.                 player2.move(false);
  96.             }
  97.         }
  98.         else
  99.         {
  100.             if (botCooldown > 0)
  101.             {
  102.                 botCooldown--;
  103.  
  104.                 if (botCooldown == 0)
  105.                 {
  106.                     botMoves = 0;
  107.                 }
  108.             }
  109.  
  110.             if (botMoves < 10)
  111.             {
  112.                 if (player2.y + player2.height / 2 < ball.y)
  113.                 {
  114.                     player2.move(false);
  115.                     botMoves++;
  116.                 }
  117.  
  118.                 if (player2.y + player2.height / 2 > ball.y)
  119.                 {
  120.                     player2.move(true);
  121.                     botMoves++;
  122.                 }
  123.  
  124.                 if (botDifficulty == 0)
  125.                 {
  126.                     botCooldown = 20;
  127.                 }
  128.                 if (botDifficulty == 1)
  129.                 {
  130.                     botCooldown = 15;
  131.                 }
  132.                 if (botDifficulty == 2)
  133.                 {
  134.                     botCooldown = 10;
  135.                 }
  136.             }
  137.         }
  138.  
  139.         ball.update(player1, player2);
  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. }
Advertisement
Add Comment
Please, Sign In to add comment