Advertisement
kuchuz

PBO-C 8 : Ball

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