Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Ball{
- //instance variables
- private int diameter;
- private color colour;
- private int x, y;
- private int xVel, yVel;
- //methods/abilities
- //draw the ball on the screen
- public void display(){
- fill( colour );
- ellipse(x, y, diameter, diameter);
- }
- //make the ball "move": change the x and y by their respective velocities
- public void move(){
- x += xVel;
- y += yVel;
- }
- //make the ball "bounce": make the xVel/yVel flipflop depending on wall hit
- public void bounce(){
- if( x >= width || x <= 0 ) xVel = -xVel;
- if( y >= height || y <= 0 ) yVel = -yVel;
- }
- //constructor - make a random ball
- public Ball(){
- x = width / 2;
- y = height / 2;
- diameter = (int)(Math.random() * 76 + 25);//25 - 100
- xVel = (int)(Math.random() * 11 - 5);//-5 - 5
- yVel = (int)(Math.random() * 11 - 5);//-5 - 5
- colour = color( (int)(Math.random() * 256), (int)(Math.random() * 256), (int)(Math.random() * 256));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement