thomasfelix

PBO 9

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