mbah_bejo

Ball

Dec 10th, 2020 (edited)
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. /** Class ini berfungsi sebagai bola pada game ini.
  2.  * mengatur kecepatan, warna, ukuran, kecepatan saat
  3.  * memantul, serta pergerakan bola
  4.  *
  5.  * Author thomasdwi.a
  6.  * Version 20201210
  7.  */
  8.  
  9. package pong;
  10.  
  11. import java.awt.*;
  12. import java.util.Random;
  13.  
  14. public class Ball {
  15.     public int x,y,lebar = 40, panjang =40;
  16.     public int motionX, motionY;
  17.     public Random random;
  18.     private Pong pong;
  19.     public int amountOfHits;
  20.  
  21.     /**
  22.      * contruktor untuk membuat bola saat
  23.      * game mau dimulai
  24.      * @param pong permintaan pong membuat bola
  25.      */
  26.     public Ball(Pong pong){
  27.         this.pong = pong;
  28.         this.random = new Random();
  29.         spawn();
  30.     }
  31.  
  32.     public void update(Paddle paddle1, Paddle paddle2){
  33.         int speed = 8;
  34.         this.x += motionX * speed;
  35.         this.y += motionY * speed;
  36.  
  37.         if(this.y + panjang  - motionY  > pong.panjang || this.y + motionY < 0){
  38.             if (this.motionY <0){
  39.                 this.y = 0;
  40.                 this.motionY = random.nextInt(5);
  41.  
  42.                 if(motionY == 0) motionY = 1;
  43.  
  44.             }
  45.             else {
  46.                 this.motionY = -random.nextInt(5);
  47.                 this.y = pong.panjang - panjang;
  48.                 if (motionY == 0) motionY = -1;
  49.             }
  50.         }
  51.         if(checkCollision(paddle1) == 1){
  52.             this.motionX = 1 + (amountOfHits/5);
  53.             this.motionY = -2 + random.nextInt(5);
  54.             if(motionY == 0) motionY =1;
  55.  
  56.             amountOfHits++;
  57.         }
  58.         else if(checkCollision(paddle2) == 1){
  59.             this.motionX = -1 - (amountOfHits/5);
  60.             this.motionY = -2 + random.nextInt(5);
  61.             if(motionY == 0) motionY = 1;
  62.  
  63.             amountOfHits++;
  64.         }
  65.  
  66.         if (checkCollision(paddle1) == 2){
  67.             paddle2.score++;
  68.             spawn();
  69.         }
  70.         else if (checkCollision(paddle2) == 2){
  71.             paddle1.score++;
  72.             spawn();
  73.         }
  74.     }
  75.  
  76.     /**
  77.      * method yang membuat bola dispawn saat memulai game
  78.      * atau sehabis mencetak skor
  79.      */
  80.     public void spawn(){
  81.         this.amountOfHits =0;
  82.         this.x = pong.lebar/2 - this.lebar/2;
  83.         this.y = pong.panjang/2 - this.panjang/2;
  84.  
  85.         this.motionY = -2 + random.nextInt(5);
  86.  
  87.         if(motionY==0) motionY =1;
  88.         if(random.nextBoolean()) motionX =1;
  89.         else motionX = -1;
  90.     }
  91.  
  92.     public  int checkCollision(Paddle paddle){
  93.         if (this.x < paddle.x + paddle.lebar && this.x + lebar > paddle.x
  94.                 && this.y < paddle.y + paddle.panjang && this.y + panjang > paddle.y) return 1; // memantul
  95.         else if ((paddle.x > x && paddle.paddleNumber ==1)
  96.                 || (paddle.x < x - lebar && paddle.paddleNumber ==2)) return 2; //skor
  97.         return  0;//nothing
  98.     }
  99.  
  100.     /**
  101.      *  Method untuk membuat tampilan bola
  102.      * @param g
  103.      */
  104.     public void  render(Graphics g){
  105.         g.setColor(Color.YELLOW );
  106.         g.fillOval(x,y,lebar,panjang);
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment