Advertisement
Dr_U

10_BallClass

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