lamaulfarid

Ball

Dec 15th, 2020
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.63 KB | None | 0 0
  1.  
  2. /**
  3.  * Source code untuk class Ball.
  4.  *
  5.  * @author Ahmad Lamaul Farid
  6.  * @version 16 Desember 2020
  7.  */
  8.  
  9.  package pong;  
  10.  import java.awt.Color;  
  11.  import java.awt.Graphics;  
  12.  import java.util.Random;  
  13.  public class Ball  
  14.  {  
  15.       public int x, y, width = 25, height = 25;  
  16.       public int motionX, motionY;  
  17.       public Random random;  
  18.       private Pong pong;  
  19.       public int amountOfHits;  
  20.       public Ball(Pong pong)  
  21.       {  
  22.            this.pong = pong;  
  23.            this.random = new Random();  
  24.            spawn();  
  25.       }  
  26.       public void update(Paddle paddle1, Paddle paddle2)  
  27.       {  
  28.            int speed = 5;  
  29.            this.x += motionX * speed;  
  30.            this.y += motionY * speed;  
  31.            if (this.y + height - motionY > pong.height || this.y + motionY < 0)  
  32.            {  
  33.                 if (this.motionY < 0)  
  34.                 {  
  35.                      this.y = 0;  
  36.                      this.motionY = random.nextInt(4);  
  37.                      if (motionY == 0)  
  38.                      {  
  39.                           motionY = 1;  
  40.                      }  
  41.                 }  
  42.                 else  
  43.                 {  
  44.                      this.motionY = -random.nextInt(4);  
  45.                      this.y = pong.height - height;  
  46.                      if (motionY == 0)  
  47.                      {  
  48.                           motionY = -1;  
  49.                      }  
  50.                 }  
  51.            }  
  52.            if (checkCollision(paddle1) == 1)  
  53.            {  
  54.                 this.motionX = 1 + (amountOfHits / 5);  
  55.                 this.motionY = -2 + random.nextInt(4);  
  56.                 if (motionY == 0)  
  57.                 {  
  58.                      motionY = 1;  
  59.                 }  
  60.                 amountOfHits++;  
  61.            }  
  62.            else if (checkCollision(paddle2) == 1)  
  63.            {  
  64.                 this.motionX = -1 - (amountOfHits / 5);  
  65.                 this.motionY = -2 + random.nextInt(4);  
  66.                 if (motionY == 0)  
  67.                 {  
  68.                      motionY = 1;  
  69.                 }  
  70.                 amountOfHits++;  
  71.            }  
  72.            if (checkCollision(paddle1) == 2)  
  73.            {  
  74.                 paddle2.score++;  
  75.                 spawn();  
  76.            }  
  77.            else if (checkCollision(paddle2) == 2)  
  78.            {  
  79.                 paddle1.score++;  
  80.                 spawn();  
  81.            }  
  82.       }  
  83.       public void spawn()  
  84.       {  
  85.            this.amountOfHits = 0;  
  86.            this.x = pong.width / 2 - this.width / 2;  
  87.            this.y = pong.height / 2 - this.height / 2;  
  88.            this.motionY = -2 + random.nextInt(4);  
  89.            if (motionY == 0)  
  90.            {  
  91.                 motionY = 1;  
  92.            }  
  93.            if (random.nextBoolean())  
  94.            {  
  95.                 motionX = 1;  
  96.            }  
  97.            else  
  98.            {  
  99.                 motionX = -1;  
  100.            }  
  101.       }  
  102.       public int checkCollision(Paddle paddle)  
  103.       {  
  104.            if (this.x < paddle.x + paddle.width && this.x + width > paddle.x && this.y < paddle.y + paddle.height && this.y + height > paddle.y)  
  105.            {  
  106.                 return 1; //bounce  
  107.            }  
  108.            else if ((paddle.x > x && paddle.paddleNumber == 1) || (paddle.x < x - width && paddle.paddleNumber == 2))  
  109.            {  
  110.                 return 2; //score  
  111.            }  
  112.            return 0; //nothing  
  113.       }  
  114.       public void render(Graphics g)  
  115.       {  
  116.            g.setColor(Color.WHITE);  
  117.            g.fillOval(x, y, width, height);  
  118.       }  
  119.  }
Add Comment
Please, Sign In to add comment