Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ball b_ball;
- Ball y_ball;
- Ball r_ball;
- void setup() {
- size(800, 800);
- background(0);
- b_ball = new Ball(50, 400, 100,2,2,color(0, 0,255));
- y_ball = new Ball(300, 200, 200,1,1,color(255,255, 0));
- r_ball = new Ball(700, 600, 100,3,3,color(255,0,0));
- }
- void draw() {
- background(0);
- b_ball.run();
- r_ball.run();
- y_ball.run();
- }
- class Ball {
- int x, y, diameter,radius, x_speed, y_speed, ball_color;
- Ball(int x_, int y_, int diameter_,int x_speed_, int y_speed_, int ball_color_) {
- x = x_;
- y = y_;
- diameter = diameter_;
- x_speed = x_speed_;
- y_speed = y_speed_;
- ball_color = ball_color_;
- }//close Ball constructor
- void run() {
- display();
- move();
- hit();
- }
- void display() {
- stroke(0);
- fill(ball_color);
- ellipse(x, y, diameter, diameter);
- }
- void move() {
- radius = diameter/2;
- if (x>=0&&y>=0) {
- x+=x_speed;
- y+=y_speed;
- if (x > width-radius || x < radius) {
- x_speed*=-1;
- }
- if (y > height-radius|| y < radius) {
- y_speed*=-1;
- }
- }
- }
- void hit(){
- if(collision()==true){
- r_ball.ball_color = 125;
- y_ball.ball_color = 125;
- b_ball.ball_color = 125;
- }
- else{
- r_ball.ball_color = color(255,0,0);
- y_ball.ball_color = color(255,255,0);
- b_ball.ball_color = color(0,0,255);
- }
- }
- boolean collision(){
- float dist_b_y,dist_y_r,dist_b_r;
- float dist_a,dist_b,dist_c;
- dist_b_y = (b_ball.x-y_ball.x)*(b_ball.x-y_ball.x)+(b_ball.y-y_ball.y)*(b_ball.y-y_ball.y);
- dist_a = sqrt(dist_b_y);
- dist_y_r = (y_ball.x-r_ball.x)*(y_ball.x-r_ball.x)+(y_ball.y-r_ball.y)*(y_ball.y-r_ball.y);
- dist_b = sqrt(dist_y_r);
- dist_b_r = (b_ball.x-r_ball.x)*(b_ball.x-r_ball.x)+(b_ball.y-r_ball.y)*(b_ball.y-r_ball.y);
- dist_c = sqrt(dist_b_r);
- if(dist_a-b_ball.radius<y_ball.radius){return true;}
- else if(dist_b-y_ball.radius<r_ball.radius){return true;}
- else if(dist_c-r_ball.radius<b_ball.radius){return true;}
- return false;
- }
- }//close Ball class
Advertisement
Add Comment
Please, Sign In to add comment