Advertisement
Guest User

Untitled

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