document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /**
  2.  * untuk mengganti warna ukuran dan pergerakan bola dll
  3.  *
  4.  * @author (Aristya Vika)
  5.  * @version (16.12)
  6.  */
  7. import java.awt.Color;
  8. import java.awt.Graphics;
  9. import java.util.Random;
  10.  
  11. public class Ball
  12. {
  13.  
  14.     public int x, y, width = 20, height = 20;
  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.             else
  53.             {
  54.                 this.motionY = -random.nextInt(4);
  55.                 this.y = pong.height - height;
  56.  
  57.                 if (motionY == 0)
  58.                 {
  59.                     motionY = -1;
  60.                 }
  61.             }
  62.         }
  63.  
  64.         if (checkCollision(paddle1) == 1)
  65.         {
  66.             this.motionX = 1 + (amountOfHits / 5);
  67.             this.motionY = -2 + random.nextInt(4);
  68.  
  69.             if (motionY == 0)
  70.             {
  71.                 motionY = 1;
  72.             }
  73.  
  74.             amountOfHits++;
  75.         }
  76.         else if (checkCollision(paddle2) == 1)
  77.         {
  78.             this.motionX = -1 - (amountOfHits / 5);
  79.             this.motionY = -2 + random.nextInt(4);
  80.  
  81.             if (motionY == 0)
  82.             {
  83.                 motionY = 1;
  84.             }
  85.  
  86.             amountOfHits++;
  87.         }
  88.  
  89.         if (checkCollision(paddle1) == 2)
  90.         {
  91.             paddle2.score++;
  92.             spawn();
  93.         }
  94.         else if (checkCollision(paddle2) == 2)
  95.         {
  96.             paddle1.score++;
  97.             spawn();
  98.         }
  99.     }
  100.  
  101.     public void spawn()
  102.     {
  103.         this.amountOfHits = 0;
  104.         this.x = pong.width / 2 - this.width / 2;
  105.         this.y = pong.height / 2 - this.height / 2;
  106.  
  107.         this.motionY = -2 + random.nextInt(4);
  108.  
  109.         if (motionY == 0)
  110.         {
  111.             motionY = 1;
  112.         }
  113.  
  114.         if (random.nextBoolean())
  115.         {
  116.             motionX = 1;
  117.         }
  118.         else
  119.         {
  120.             motionX = -1;
  121.         }
  122.     }
  123.  
  124.     public int checkCollision(Paddle paddle)
  125.     {
  126.         if (this.x < paddle.x + paddle.width && this.x + width > paddle.x && this.y < paddle.y + paddle.height && this.y + height > paddle.y)
  127.         {
  128.             return 1; //bounce
  129.         }
  130.         else if ((paddle.x > x && paddle.paddleNumber == 1) || (paddle.x < x - width && paddle.paddleNumber == 2))
  131.         {
  132.             return 2; //score
  133.         }
  134.  
  135.         return 0; //nothing
  136.     }
  137.  
  138.     public void render(Graphics g)
  139.     {
  140.         g.setColor(Color.BLUE);
  141.         g.fillOval(x, y, width, height);
  142.     }
  143.  
  144. }
');