Advertisement
fosterbl

Ball class Processing complete

Feb 5th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. class Ball{
  2.   //instance variables
  3.   private int diameter;
  4.   private color colour;
  5.   private int x, y;
  6.   private int xVel, yVel;
  7.  
  8.   //methods/abilities
  9.  
  10.   //draw the ball on the screen
  11.   public void display(){
  12.     fill( colour );
  13.     ellipse(x, y, diameter, diameter);
  14.   }
  15.  
  16.   //make the ball "move": change the x and y by their respective velocities
  17.   public void move(){
  18.     x += xVel;
  19.     y += yVel;
  20.   }
  21.  
  22.   //make the ball "bounce": make the xVel/yVel flipflop depending on wall hit
  23.   public void bounce(){
  24.     if( x >= width || x <= 0 ) xVel = -xVel;
  25.     if( y >= height || y <= 0 ) yVel = -yVel;
  26.   }
  27.  
  28.   //constructor - make a random ball
  29.   public Ball(){
  30.     x = width / 2;
  31.     y = height / 2;
  32.     diameter = (int)(Math.random() * 76 + 25);//25 - 100
  33.     xVel = (int)(Math.random() * 11 - 5);//-5 - 5
  34.     yVel = (int)(Math.random() * 11 - 5);//-5 - 5
  35.     colour = color( (int)(Math.random() * 256), (int)(Math.random() * 256), (int)(Math.random() * 256));
  36.   }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement