kuchuz

PBO-C 8 : Pong

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