Advertisement
Ramdan51-062

Ball

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