Tugas 7 : Membuat Game Pong

  Pada pertemuan kali ini saya dan teman-teman diberikan penugasan oleh pak Fajar Baskoro untuk mengimplementasikan pembuatan game yang berupa game pong.java, dimana game ini merupakan permainan video generasi pertama yang dirilis sebagai permainan arcade yang dioperasikan dengan koin yang dikembangkan oleh Atari Inc. Dalam pembuatan game ini, kita membuat package bernama pong, dimana dalam package tersebut terdapat 4 class yaitu : 

  • Class Pong : Class utama untuk game pong 
  • Class Paddle : Class untuk objek paddle yang memiliki fungsi diantaranya fungsi untuk merubah warna, ukuran dan lain-lain pada paddle 
  • Class Ball : Class untuk mengatur ukuran, bentuk, warna bola 
  • Class Renderer : Class untuk membuat tampilan dasar grafik pada game pong

Berikut adalah implementasi dari class-class yang telah dibuat : 


Dan ini adalah dokumentasi program saat dijalankan




   Terdapat pilihan bot-difficult nya dan maksimum score agar game tersebut dapat dimenangkan, untuk pilihan bot-difficult nya tinggal pencet shift kemudian terdapat 3 opsi : 1. Easy 2. Medium 3. Hard


Gambar Diatas ketika sedang dalam permainan


Dan ini ketika game sudah selesai

Untuk sourcecode class Pong.java : 

DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1.  
  2. /**
  3.  * Source code untuk class Pong.
  4.  *
  5.  * @author Pais
  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 ActionListenerKeyListener  
  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(20this);  
  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(this1);  
  53.            player2 = new Paddle(this2);  
  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(00, width, height);  
  129.            g.setRenderingHint(RenderingHints.KEY_ANTIALIASINGRenderingHints.VALUE_ANTIALIAS_ON);  
  130.            if (gameStatus == 0)  
  131.            {  
  132.                 g.setColor(Color.WHITE);  
  133.                 g.setFont(new Font("Arial"150));  
  134.                 g.drawString("PONG", width / 2 - 7550);  
  135.                 if (!selectingDifficulty)  
  136.                 {  
  137.                      g.setFont(new Font("Arial"130));  
  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"130));  
  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"150));  
  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 / 20, width / 2, height);  
  161.                 g.setStroke(new BasicStroke(2f));  
  162.                 g.drawOval(width / 2 - 150, height / 2 - 150300300);  
  163.                 g.setFont(new Font("Arial"150));  
  164.                 g.drawString(String.valueOf(player1.score), width / 2 - 9050);  
  165.                 g.drawString(String.valueOf(player2.score), width / 2 + 6550);  
  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"150));  
  174.                 g.drawString("PONG", width / 2 - 7550);  
  175.                 if (bot && playerWon == 2)  
  176.                 {  
  177.                      g.drawString("The Bot Wins!", width / 2 - 170200);  
  178.                 }  
  179.                 else  
  180.                 {  
  181.                      g.drawString("Player " + playerWon + " Wins!", width / 2 - 165200);  
  182.                 }  
  183.                 g.setFont(new Font("Arial"130));  
  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.  }

Untuk sourcecode class Paddle.java 

DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1.  
  2. /**
  3.  * Source code untuk class Paddle.
  4.  *
  5.  * @author Pais
  6.  * @version 16 Desember 2020
  7.  */
  8.  
  9.  package pong;  
  10.  import java.awt.Color;  
  11.  import java.awt.Graphics;  
  12.  public class Paddle  
  13.  {  
  14.       public int paddleNumber;  
  15.       public int x, y, width = 50, height = 250;  
  16.       public int score;  
  17.       public Paddle(Pong pong, int paddleNumber)  
  18.       {  
  19.            this.paddleNumber = paddleNumber;  
  20.            if (paddleNumber == 1)  
  21.            {  
  22.                 this.x = 0;  
  23.            }  
  24.            if (paddleNumber == 2)  
  25.            {  
  26.                 this.x = pong.width - width;  
  27.            }  
  28.            this.y = pong.height / 2 - this.height / 2;  
  29.       }  
  30.       public void render(Graphics g)  
  31.       {  
  32.            g.setColor(Color.WHITE);  
  33.            g.fillRect(x, y, width, height);  
  34.       }  
  35.       public void move(boolean up)  
  36.       {  
  37.            int speed = 15;  
  38.            if (up)  
  39.            {  
  40.                 if (- speed > 0)  
  41.                 {  
  42.                      y -= speed;  
  43.                 }  
  44.                 else  
  45.                 {  
  46.                      y = 0;  
  47.                 }  
  48.            }  
  49.            else  
  50.            {  
  51.                 if (+ height + speed < Pong.pong.height)  
  52.                 {  
  53.                      y += speed;  
  54.                 }  
  55.                 else  
  56.                 {  
  57.                      y = Pong.pong.height - height;  
  58.                 }  
  59.            }  
  60.       }  
  61.  }  

Untuk sourcecode class Ball.java : 

DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1.  
  2. /**
  3.  * Source code untuk class Ball.
  4.  *
  5.  * @author Pais
  6.  * @version 16 Desember 2020
  7.  */
  8.  
  9.  package pong;  
  10.  import java.awt.Color;  
  11.  import java.awt.Graphics;  
  12.  import java.util.Random;  
  13.  public class Ball  
  14.  {  
  15.       public int x, y, width = 25, height = 25;  
  16.       public int motionX, motionY;  
  17.       public Random random;  
  18.       private Pong pong;  
  19.       public int amountOfHits;  
  20.       public Ball(Pong pong)  
  21.       {  
  22.            this.pong = pong;  
  23.            this.random = new Random();  
  24.            spawn();  
  25.       }  
  26.       public void update(Paddle paddle1, Paddle paddle2)  
  27.       {  
  28.            int speed = 5;  
  29.            this.x += motionX * speed;  
  30.            this.y += motionY * speed;  
  31.            if (this.y + height - motionY > pong.height || this.y + motionY < 0)  
  32.            {  
  33.                 if (this.motionY < 0)  
  34.                 {  
  35.                      this.y = 0;  
  36.                      this.motionY = random.nextInt(4);  
  37.                      if (motionY == 0)  
  38.                      {  
  39.                           motionY = 1;  
  40.                      }  
  41.                 }  
  42.                 else  
  43.                 {  
  44.                      this.motionY = -random.nextInt(4);  
  45.                      this.y = pong.height - height;  
  46.                      if (motionY == 0)  
  47.                      {  
  48.                           motionY = -1;  
  49.                      }  
  50.                 }  
  51.            }  
  52.            if (checkCollision(paddle1) == 1)  
  53.            {  
  54.                 this.motionX = 1 + (amountOfHits / 5);  
  55.                 this.motionY = -2 + random.nextInt(4);  
  56.                 if (motionY == 0)  
  57.                 {  
  58.                      motionY = 1;  
  59.                 }  
  60.                 amountOfHits++;  
  61.            }  
  62.            else if (checkCollision(paddle2) == 1)  
  63.            {  
  64.                 this.motionX = -1 - (amountOfHits / 5);  
  65.                 this.motionY = -2 + random.nextInt(4);  
  66.                 if (motionY == 0)  
  67.                 {  
  68.                      motionY = 1;  
  69.                 }  
  70.                 amountOfHits++;  
  71.            }  
  72.            if (checkCollision(paddle1) == 2)  
  73.            {  
  74.                 paddle2.score++;  
  75.                 spawn();  
  76.            }  
  77.            else if (checkCollision(paddle2) == 2)  
  78.            {  
  79.                 paddle1.score++;  
  80.                 spawn();  
  81.            }  
  82.       }  
  83.       public void spawn()  
  84.       {  
  85.            this.amountOfHits = 0;  
  86.            this.x = pong.width / 2 - this.width / 2;  
  87.            this.y = pong.height / 2 - this.height / 2;  
  88.            this.motionY = -2 + random.nextInt(4);  
  89.            if (motionY == 0)  
  90.            {  
  91.                 motionY = 1;  
  92.            }  
  93.            if (random.nextBoolean())  
  94.            {  
  95.                 motionX = 1;  
  96.            }  
  97.            else  
  98.            {  
  99.                 motionX = -1;  
  100.            }  
  101.       }  
  102.       public int checkCollision(Paddle paddle)  
  103.       {  
  104.            if (this.x < paddle.x + paddle.width && this.x + width > paddle.x && this.y < paddle.y + paddle.height && this.y + height > paddle.y)  
  105.            {  
  106.                 return 1; //bounce  
  107.            }  
  108.            else if ((paddle.x > x && paddle.paddleNumber == 1) || (paddle.x < x - width && paddle.paddleNumber == 2))  
  109.            {  
  110.                 return 2; //score  
  111.            }  
  112.            return 0; //nothing  
  113.       }  
  114.       public void render(Graphics g)  
  115.       {  
  116.            g.setColor(Color.WHITE);  
  117.            g.fillOval(x, y, width, height);  
  118.       }  
  119.  }

Untuk sourcecode class Renderer.java : 

DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1.  
  2. /**
  3.  * Source code untuk class Renderer.
  4.  *
  5.  * @author Pais
  6.  * @version 16 Desember 2020
  7.  */
  8.  
  9.  package pong;  
  10.  import java.awt.Graphics;  j
  11.  import java.awt.Graphics2D;  
  12.  import javax.swing.JPanel;  
  13.  public class Renderer extends JPanel  
  14.  {  
  15.       private static final long serialVersionUID = 1L;  
  16.       @Override  
  17.       protected void paintComponent(Graphics g)  
  18.       {  
  19.            super.paintComponent(g);  
  20.            Pong.pong.render((Graphics2D) g);  
  21.       }  
  22.  }  


Sekian dari saya.Apabila ada kesalahan saya mohon maaf yang sebesar-besarnya.