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;
- //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));
- }
- //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( xVel > 0 && (x + diameter/2 + xVel) >= width ) xVel = -xVel;
- else if( xVel < 0 && (x - diameter/2 + xVel) <= 0 ) xVel = -xVel;
- if( yVel > 0 && (y + diameter/2 + yVel) >= height ) yVel = -yVel;
- else if( yVel < 0 && (y - diameter/2 + yVel) <= 0 ) yVel = -yVel;
- }
- //accessors
- public int getDiameter(){ return diameter; }
- public color getColor(){ return colour; }
- public int getX(){ return x; }
- public int getY(){ return y; }
- public int getXVel(){ return xVel; }
- public int getYVel(){ return yVel; }
- //mutators
- public void setDiameter(int d){ diameter = d; }
- public void setColor(color c){ colour = c; }
- public void setX(int theX){ x = theX; }
- public void setY(int theY){ y = theY; }
- public void setXVel(int xv){ xVel = xv; }
- public void setYVel(int yv){ yVel = yv; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement