trizehn

Paddle

Dec 16th, 2020
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class Paddle here.
  4.  *
  5.  * @author Daffa Tristan Firdaus
  6.  * @version 17 Desember 2020
  7.  */
  8. import java.awt.Color;
  9. import java.awt.Graphics;
  10.  
  11. public class Paddle
  12. {
  13.  
  14.     public int paddleNumber;
  15.  
  16.     public int x, y, width = 50, height = 250;
  17.  
  18.     public int score;
  19.  
  20.     public Paddle(Pong pong, int paddleNumber)
  21.     {
  22.         this.paddleNumber = paddleNumber;
  23.  
  24.         if (paddleNumber == 1)
  25.         {
  26.             this.x = 0;
  27.         }
  28.  
  29.         if (paddleNumber == 2)
  30.         {
  31.             this.x = pong.width - width;
  32.         }
  33.  
  34.         this.y = pong.height / 2 - this.height / 2;
  35.     }
  36.  
  37.     public void render(Graphics g)
  38.     {
  39.         g.setColor(Color.CYAN);
  40.         g.fillRect(x, y, width, height);
  41.     }
  42.  
  43.     public void move(boolean up)
  44.     {
  45.         int speed = 15;
  46.  
  47.         if (up)
  48.         {
  49.             if (y - speed > 0)
  50.             {
  51.                 y -= speed;
  52.             }
  53.             else
  54.             {
  55.                 y = 0;
  56.             }
  57.         }
  58.         else
  59.         {
  60.             if (y + height + speed < Pong.pong.height)
  61.             {
  62.                 y += speed;
  63.             }
  64.             else
  65.             {
  66.                 y = Pong.pong.height - height;
  67.             }
  68.         }
  69.     }
  70.  
  71. }
Add Comment
Please, Sign In to add comment