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.  * Write a description of class Ball here.
  6.  *
  7.  * @author Fiodhy Ardito Narawangsa
  8.  * @version Desember 2020
  9.  */
  10. public class Ball
  11. {
  12.     public int x, y, width = 25, height = 25;
  13.     public int motionX, motionY;
  14.     public Random random;
  15.     private Pong pong;
  16.     public int amountOfHits;
  17.  
  18.     public Ball(Pong pong)
  19.     {
  20.         this.pong = pong;
  21.  
  22.         this.random = new Random();
  23.  
  24.         spawn();
  25.     }
  26.  
  27.     public void update(Paddle paddle1, Paddle paddle2)
  28.     {
  29.         int speed = 5;
  30.  
  31.         this.x += motionX * speed;
  32.         this.y += motionY * speed;
  33.  
  34.         if (this.y + height - motionY > pong.height || this.y + motionY < 0)
  35.         {
  36.             if (this.motionY < 0)
  37.             {
  38.                 this.y = 0;
  39.                 this.motionY = random.nextInt(4);
  40.  
  41.                 if (motionY == 0)
  42.                 {
  43.                     motionY = 1;
  44.                 }
  45.             }
  46.             else
  47.             {
  48.                 this.motionY = -random.nextInt(4);
  49.                 this.y = pong.height - height;
  50.  
  51.                 if (motionY == 0)
  52.                 {
  53.                     motionY = -1;
  54.                 }
  55.             }
  56.         }
  57.  
  58.         if (checkCollision(paddle1) == 1)
  59.         {
  60.             this.motionX = 1 + (amountOfHits / 5);
  61.             this.motionY = -2 + random.nextInt(4);
  62.  
  63.             if (motionY == 0)
  64.             {
  65.                 motionY = 1;
  66.             }
  67.  
  68.             amountOfHits++;
  69.         }
  70.         else if (checkCollision(paddle2) == 1)
  71.         {
  72.             this.motionX = -1 - (amountOfHits / 5);
  73.             this.motionY = -2 + random.nextInt(4);
  74.  
  75.             if (motionY == 0)
  76.             {
  77.                 motionY = 1;
  78.             }
  79.  
  80.             amountOfHits++;
  81.         }
  82.  
  83.         if (checkCollision(paddle1) == 2)
  84.         {
  85.             paddle2.score++;
  86.             spawn();
  87.         }
  88.         else if (checkCollision(paddle2) == 2)
  89.         {
  90.             paddle1.score++;
  91.             spawn();
  92.         }
  93.     }
  94.  
  95.     public void spawn()
  96.     {
  97.         this.amountOfHits = 0;
  98.         this.x = pong.width / 2 - this.width / 2;
  99.         this.y = pong.height / 2 - this.height / 2;
  100.  
  101.         this.motionY = -2 + random.nextInt(4);
  102.  
  103.         if (motionY == 0)
  104.         {
  105.             motionY = 1;
  106.         }
  107.  
  108.         if (random.nextBoolean())
  109.         {
  110.             motionX = 1;
  111.         }
  112.         else
  113.         {
  114.             motionX = -1;
  115.         }
  116.     }
  117.  
  118.     public int checkCollision(Paddle paddle)
  119.     {
  120.         if (this.x < paddle.x + paddle.width && this.x + width > paddle.x && this.y < paddle.y + paddle.height && this.y + height > paddle.y)
  121.         {
  122.             return 1; //bounce
  123.         }
  124.         else if ((paddle.x > x && paddle.paddleNumber == 1) || (paddle.x < x - width && paddle.paddleNumber == 2))
  125.         {
  126.             return 2; //score
  127.         }
  128.  
  129.         return 0; //nothing
  130.     }
  131.  
  132.     public void render(Graphics g)
  133.     {
  134.         g.setColor(Color.WHITE);
  135.         g.fillOval(x, y, width, height);
  136.     }
  137. }
  138.