document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * Class Ball berfungsi untuk mengatur semua yang berkaitan dengan bola
  3.  *
  4.  * @author Johnivan Aldo Sudiono
  5.  * @version 19 Desemer 2020
  6.  */
  7.  
  8. import java.awt.Color;
  9. import java.awt.Graphics;
  10. import java.util.Random;
  11.  
  12. public class Ball
  13. {
  14.  
  15.     public int x, y, width = 25, height = 25;
  16.  
  17.     public int motionX, motionY;
  18.  
  19.     public Random random;
  20.  
  21.     private Pong pong;
  22.  
  23.     public int amountOfHits;
  24.  
  25.     public Ball(Pong pong)
  26.     {
  27.         this.pong = pong;
  28.  
  29.         this.random = new Random();
  30.  
  31.         spawn();
  32.     }
  33.  
  34.     public void update(Paddle Paddle1, Paddle Paddle2)
  35.     {
  36.         int speed = 5;
  37.  
  38.         this.x += motionX * speed;
  39.         this.y += motionY * speed;
  40.  
  41.         if (this.y + height - motionY > pong.height || this.y + motionY < 0)
  42.         {
  43.             if (this.motionY < 0)
  44.             {
  45.                 this.y = 0;
  46.                 this.motionY = random.nextInt(4);
  47.  
  48.                 if (motionY == 0)
  49.                 {
  50.                     motionY = 1;
  51.                 }
  52.             }
  53.             else
  54.             {
  55.                 this.motionY = -random.nextInt(4);
  56.                 this.y = pong.height - height;
  57.  
  58.                 if (motionY == 0)
  59.                 {
  60.                     motionY = -1;
  61.                 }
  62.             }
  63.         }
  64.  
  65.         if (checkCollision(Paddle1) == 1)
  66.         {
  67.             this.motionX = 1 + (amountOfHits / 5);
  68.             this.motionY = -2 + random.nextInt(4);
  69.  
  70.             if (motionY == 0)
  71.             {
  72.                 motionY = 1;
  73.             }
  74.  
  75.             amountOfHits++;
  76.         }
  77.         else if (checkCollision(Paddle2) == 1)
  78.         {
  79.             this.motionX = -1 - (amountOfHits / 5);
  80.             this.motionY = -2 + random.nextInt(4);
  81.  
  82.             if (motionY == 0)
  83.             {
  84.                 motionY = 1;
  85.             }
  86.  
  87.             amountOfHits++;
  88.         }
  89.  
  90.         if (checkCollision(Paddle1) == 2)
  91.         {
  92.             Paddle2.score++;
  93.             spawn();
  94.         }
  95.         else if (checkCollision(Paddle2) == 2)
  96.         {
  97.             Paddle1.score++;
  98.             spawn();
  99.         }
  100.     }
  101.  
  102.     public void spawn()
  103.     {
  104.         this.amountOfHits = 0;
  105.         this.x = pong.width / 2 - this.width / 2;
  106.         this.y = pong.height / 2 - this.height / 2;
  107.  
  108.         this.motionY = -2 + random.nextInt(4);
  109.  
  110.         if (motionY == 0)
  111.         {
  112.             motionY = 1;
  113.         }
  114.  
  115.         if (random.nextBoolean())
  116.         {
  117.             motionX = 1;
  118.         }
  119.         else
  120.         {
  121.             motionX = -1;
  122.         }
  123.     }
  124.  
  125.     public int checkCollision(Paddle paddle)
  126.     {
  127.         if (this.x < paddle.x + paddle.width && this.x + width > paddle.x && this.y < paddle.y + paddle.height && this.y + height > paddle.y)
  128.         {
  129.             return 1; //bounce
  130.         }
  131.         else if ((paddle.x > x && paddle.paddleNumber == 1) || (paddle.x < x - width && paddle.paddleNumber == 2))
  132.         {
  133.             return 2; //score
  134.         }
  135.  
  136.         return 0; //nothing
  137.     }
  138.  
  139.     public void render(Graphics g)
  140.     {
  141.         g.setColor(Color.WHITE);
  142.         g.fillOval(x, y, width, height);
  143.     }
  144.  
  145. }
');