trizehn

Ball

Dec 16th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class Ball here.
  4.  *
  5.  * @author Daffa Tristan Firdaus
  6.  * @version 17 Desember 2020
  7.  */
  8.  
  9. import java.awt.Color;
  10. import java.awt.Graphics;
  11. import java.util.Random;
  12.  
  13. public class Ball
  14. {
  15.  
  16.     public int x, y, width = 25, height = 25;
  17.  
  18.     public int motionX, motionY;
  19.  
  20.     public Random random;
  21.  
  22.     private Pong pong;
  23.  
  24.     public int amountOfHits;
  25.  
  26.     public Ball(Pong pong)
  27.     {
  28.         this.pong = pong;
  29.  
  30.         this.random = new Random();
  31.  
  32.         spawn();
  33.     }
  34.  
  35.     public void update(Paddle paddle1, Paddle paddle2)
  36.     {
  37.         int speed = 5;
  38.  
  39.         this.x += motionX * speed;
  40.         this.y += motionY * speed;
  41.  
  42.         if (this.y + height - motionY > pong.height || this.y + motionY < 0)
  43.         {
  44.             if (this.motionY < 0)
  45.             {
  46.                 this.y = 0;
  47.                 this.motionY = random.nextInt(4);
  48.  
  49.                 if (motionY == 0)
  50.                 {
  51.                     motionY = 1;
  52.                 }
  53.             }
  54.             else
  55.             {
  56.                 this.motionY = -random.nextInt(4);
  57.                 this.y = pong.height - height;
  58.  
  59.                 if (motionY == 0)
  60.                 {
  61.                     motionY = -1;
  62.                 }
  63.             }
  64.         }
  65.  
  66.         if (checkCollision(paddle1) == 1)
  67.         {
  68.             this.motionX = 1 + (amountOfHits / 5);
  69.             this.motionY = -2 + random.nextInt(4);
  70.  
  71.             if (motionY == 0)
  72.             {
  73.                 motionY = 1;
  74.             }
  75.  
  76.             amountOfHits++;
  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.         else if (checkCollision(paddle2) == 2)
  97.         {
  98.             paddle1.score++;
  99.             spawn();
  100.         }
  101.     }
  102.  
  103.     public void spawn()
  104.     {
  105.         this.amountOfHits = 0;
  106.         this.x = pong.width / 2 - this.width / 2;
  107.         this.y = pong.height / 2 - this.height / 2;
  108.  
  109.         this.motionY = -2 + random.nextInt(4);
  110.  
  111.         if (motionY == 0)
  112.         {
  113.             motionY = 1;
  114.         }
  115.  
  116.         if (random.nextBoolean())
  117.         {
  118.             motionX = 1;
  119.         }
  120.         else
  121.         {
  122.             motionX = -1;
  123.         }
  124.     }
  125.  
  126.     public int checkCollision(Paddle paddle)
  127.     {
  128.         if (this.x < paddle.x + paddle.width && this.x + width > paddle.x && this.y < paddle.y + paddle.height && this.y + height > paddle.y)
  129.         {
  130.             return 1; //bounce
  131.         }
  132.         else if ((paddle.x > x && paddle.paddleNumber == 1) || (paddle.x < x - width && paddle.paddleNumber == 2))
  133.         {
  134.             return 2; //score
  135.         }
  136.  
  137.         return 0; //nothing
  138.     }
  139.  
  140.     public void render(Graphics g)
  141.     {
  142.         g.setColor(Color.WHITE);
  143.         g.fillOval(x, y, width, height);
  144.     }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment