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