document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.awt.Color;  
  2. import java.awt.Graphics;  
  3. import java.util.Random;
  4. /**
  5.  * Class ini berfungsi sebagai bola.
  6.  * Class ini akan mengatur warna, ukuran, kecepatan, dan pergerakan bola
  7.  *
  8.  * @author Mtuhia Qurrota Akyun
  9.  * 18 Desember 2020
  10.  */  
  11. public class Ball  
  12. {  
  13.     public int x, y, width = 30, height = 30;  
  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.                 this.y = 0;  
  33.                 this.motionY = random.nextInt(4);  
  34.                 if (motionY == 0)
  35.                     motionY = 1;  
  36.             }  
  37.             else {  
  38.                  this.motionY = -random.nextInt(4);  
  39.                  this.y = pong.height - height;  
  40.                  if (motionY == 0)
  41.                     motionY = -1;  
  42.             }  
  43.         }  
  44.         if (checkCollision(paddle1) == 1){  
  45.             this.motionX = 1 + (amountOfHits / 5);  
  46.             this.motionY = -2 + random.nextInt(4);  
  47.             if (motionY == 0)
  48.                 motionY = 1;  
  49.             amountOfHits++;  
  50.         }  
  51.         else if (checkCollision(paddle2) == 1){  
  52.             this.motionX = -1 - (amountOfHits / 5);  
  53.             this.motionY = -2 + random.nextInt(4);  
  54.             if (motionY == 0)  
  55.                  motionY = 1;  
  56.             amountOfHits++;  
  57.         }  
  58.         if (checkCollision(paddle1) == 2){  
  59.             paddle2.score++;  
  60.             spawn();  
  61.         }  
  62.         else if (checkCollision(paddle2) == 2){  
  63.             paddle1.score++;  
  64.             spawn();  
  65.         }  
  66.     }  
  67.     public void spawn()  
  68.     {  
  69.         this.amountOfHits = 0;  
  70.         this.x = pong.width / 2 - this.width / 2;  
  71.         this.y = pong.height / 2 - this.height / 2;  
  72.         this.motionY = -2 + random.nextInt(4);  
  73.         if (motionY == 0)  
  74.             motionY = 1;  
  75.         if (random.nextBoolean())  
  76.             motionX = 1;  
  77.         else  
  78.             motionX = -1;  
  79.     }  
  80.     public int checkCollision(Paddle paddle)  
  81.     {  
  82.         if (this.x < paddle.x + paddle.width && this.x + width > paddle.x && this.y < paddle.y + paddle.height && this.y + height > paddle.y)  
  83.             return 1; //bounce  
  84.         else if ((paddle.x > x && paddle.paddleNumber == 1) || (paddle.x < x - width && paddle.paddleNumber == 2))  
  85.             return 2; //score  
  86.         return 0; //nothing  
  87.     }  
  88.     public void render(Graphics g)  
  89.     {  
  90.         g.setColor(Color.YELLOW);  
  91.         g.fillOval(x, y, width, height);  
  92.     }    
  93. }  
');