Dwinanda

Pong (Ball)

Dec 21st, 2020
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1.  
  2. /**
  3.  * Class yang mengatur bola Pong.
  4.  *
  5.  * @author Dwinanda Bagoes Ansori
  6.  * @version 22 Desember 2020
  7.  */
  8. import java.awt.Color;
  9. import java.awt.Graphics;
  10. import java.util.Random;
  11.  
  12. public class Ball
  13. {
  14.     public int x, y, width = 25, height = 25;
  15.     public int motionX, motionY;
  16.     public Random random;
  17.     private Pong pong;
  18.     public int amountOfHits;
  19.  
  20.     public Ball(Pong pong)
  21.     {
  22.         this.pong = pong;
  23.         this.random = new Random();
  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 &&
  121.            this.y < paddle.y + paddle.height && this.y + height > paddle.y)
  122.     {
  123.         return 1; // bounce
  124.     }
  125.     else if ((paddle.x > x && paddle.paddleNumber == 1) ||
  126.                (paddle.x < x - width && paddle.paddleNumber == 2))
  127.     {
  128.         return 2; // score
  129.     }
  130.  
  131.     return 0; // nothing
  132.     }
  133.  
  134.     public void render(Graphics g)
  135.     {
  136.     g.setColor(Color.WHITE);
  137.     g.fillOval(x, y, width, height);
  138.     }
  139.  
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment