document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  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 = 20, height = 20;
  8.  
  9.     public int motionX, motionY;
  10.  
  11.     public Random random;
  12.  
  13.     private Pong pong;
  14.  
  15.     public int amountOfHits;
  16.  
  17.     public Ball(Pong pong)
  18.     {
  19.         this.pong = pong;
  20.  
  21.         this.random = new Random();
  22.  
  23.         spawn();
  24.     }
  25.  
  26.     public void update(Paddle paddle1, Paddle paddle2)
  27.     {
  28.         int speed = 8;
  29.  
  30.         this.x += motionX * speed;
  31.         this.y += motionY * speed;
  32.  
  33.         if (this.y + height - motionY > pong.height || this.y + motionY < 0)
  34.         {
  35.             if (this.motionY < 0)
  36.             {
  37.                 this.y = 0;
  38.                 this.motionY = random.nextInt(4);
  39.  
  40.                 if (motionY == 0)
  41.                 {
  42.                     motionY = 1;
  43.                 }
  44.             }
  45.             else
  46.             {
  47.                 this.motionY = -random.nextInt(4);
  48.                 this.y = pong.height - height;
  49.  
  50.                 if (motionY == 0)
  51.                 {
  52.                     motionY = -1;
  53.                 }
  54.             }
  55.         }
  56.  
  57.         if (checkCollision(paddle1) == 1)
  58.         {
  59.             this.motionX = 1 + (amountOfHits / 5);
  60.             this.motionY = -2 + random.nextInt(4);
  61.  
  62.             if (motionY == 0)
  63.             {
  64.                 motionY = 1;
  65.             }
  66.  
  67.             amountOfHits++;
  68.         }
  69.         else if (checkCollision(paddle2) == 1)
  70.         {
  71.             this.motionX = -1 - (amountOfHits / 5);
  72.             this.motionY = -2 + random.nextInt(4);
  73.  
  74.             if (motionY == 0)
  75.             {
  76.                 motionY = 1;
  77.             }
  78.  
  79.             amountOfHits++;
  80.         }
  81.  
  82.         if (checkCollision(paddle1) == 2)
  83.         {
  84.             paddle2.score++;
  85.             spawn();
  86.         }
  87.         else if (checkCollision(paddle2) == 2)
  88.         {
  89.             paddle1.score++;
  90.             spawn();
  91.         }
  92.     }
  93.  
  94.     public void spawn()
  95.     {
  96.         this.amountOfHits = 0;
  97.         this.x = pong.width / 2 - this.width / 2;
  98.         this.y = pong.height / 2 - this.height / 2;
  99.  
  100.         this.motionY = -2 + random.nextInt(4);
  101.  
  102.         if (motionY == 0)
  103.         {
  104.             motionY = 1;
  105.         }
  106.  
  107.         if (random.nextBoolean())
  108.         {
  109.             motionX = 1;
  110.         }
  111.         else
  112.         {
  113.             motionX = -1;
  114.         }
  115.     }
  116.  
  117.     public int checkCollision(Paddle paddle)
  118.     {
  119.         if (this.x < paddle.x + paddle.width && this.x + width > paddle.x && this.y < paddle.y + paddle.height && this.y + height > paddle.y)
  120.         {
  121.             return 1; //bounce
  122.         }
  123.         else if ((paddle.x > x && paddle.paddleNumber == 1) || (paddle.x < x - width && paddle.paddleNumber == 2))
  124.         {
  125.             return 2; //score
  126.         }
  127.  
  128.         return 0; //nothing
  129.     }
  130.  
  131.     public void render(Graphics g)
  132.     {
  133.         g.setColor(Color.BLUE);
  134.         g.fillOval(x, y, width, height);
  135.     }
  136.  
  137. }
');