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. /**
  6.  * Class yang mengatur bola Pong.
  7.  *
  8.  * @author Thomas Felix
  9.  * @version 19 Desember 2020
  10.  */
  11.  
  12. public class Ball
  13. {
  14.     public int x, y, width = 25, height = 25;
  15.    
  16.     public int motionX, motionY;
  17.  
  18.     public Random random;
  19.    
  20.     private Pong pong;
  21.    
  22.     public int amountOfHits;
  23.    
  24.     public Ball(Pong pong)
  25.     {
  26.         this.pong = pong;
  27.    
  28.         this.random = new Random();
  29.    
  30.         spawn();
  31.     }
  32.    
  33.     public void update(Paddle paddle1, Paddle paddle2)
  34.     {
  35.         int speed = 5;
  36.    
  37.         this.x += motionX * speed;
  38.         this.y += motionY * speed;
  39.    
  40.         if (this.y + height - motionY > pong.height || this.y + motionY < 0)
  41.         {
  42.             if (this.motionY < 0)
  43.             {
  44.                 this.y = 0;
  45.                 this.motionY = random.nextInt(4);
  46.    
  47.                 if (motionY == 0)
  48.                 {
  49.                     motionY = 1;
  50.                 }
  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.        
  78.         else if (checkCollision(paddle2) == 1)
  79.         {
  80.             this.motionX = -1 - (amountOfHits / 5);
  81.             this.motionY = -2 + random.nextInt(4);
  82.    
  83.             if (motionY == 0)
  84.             {
  85.                 motionY = 1;
  86.             }
  87.    
  88.             amountOfHits++;
  89.         }
  90.    
  91.         if (checkCollision(paddle1) == 2)
  92.         {
  93.             paddle2.score++;
  94.             spawn();
  95.         }
  96.        
  97.         else if (checkCollision(paddle2) == 2)
  98.         {
  99.             paddle1.score++;
  100.             spawn();
  101.         }
  102.     }
  103.    
  104.     public void spawn()
  105.     {
  106.         this.amountOfHits = 0;
  107.         this.x = pong.width / 2 - this.width / 2;
  108.         this.y = pong.height / 2 - this.height / 2;
  109.    
  110.         this.motionY = -2 + random.nextInt(4);
  111.    
  112.         if (motionY == 0)
  113.         {
  114.             motionY = 1;
  115.         }
  116.    
  117.         if (random.nextBoolean())
  118.         {
  119.             motionX = 1;
  120.         }
  121.        
  122.         else
  123.         {
  124.             motionX = -1;
  125.         }
  126.     }
  127.    
  128.     public int checkCollision(Paddle paddle)
  129.     {
  130.         if (this.x < paddle.x + paddle.width && this.x + width > paddle.x && this.y < paddle.y + paddle.height && this.y + height > paddle.y)
  131.         {
  132.             return 1; //bounce
  133.         }
  134.        
  135.         else if ((paddle.x > x && paddle.paddleNumber == 1) || (paddle.x < x - width && paddle.paddleNumber == 2))
  136.         {
  137.             return 2; //score
  138.         }
  139.    
  140.         return 0; //nothing
  141.     }
  142.    
  143.     public void render(Graphics g)
  144.     {
  145.         g.setColor(Color.WHITE);
  146.         g.fillOval(x, y, width, height);
  147.     }
  148. }
');