Advertisement
taufiq123

Untitled

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