Advertisement
Dr_U

10_PONGClass

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