Advertisement
mnaufaldillah

Ball Tugas 9

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