azkiatunnisa

Untitled

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