Advertisement
lamaulfarid

Pong

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