Wednesday, April 28, 2021

Bouncing Ball Animation

Bouncing Ball Animation


 

Codes:

//Declaring the balls
var ball1, ball2, ball3, ball4;

//Creating the balls
ball1 = createSprite(50,50,20,20);
ball2 = createSprite(350,50,20,20);
ball3 = createSprite(50,350,20,20);
ball4 = createSprite(350,350,20,20);

//Setting the colors
ball1.shapeColor = "Blue";
ball2.shapeColor = "Green";
ball3.shapeColor = "Red";
ball4.shapeColor = "Purple";

//Assigning velocities to the balls

ball1.velocityX = +8;
ball1.velocityY = +6;

ball2.velocityX = -8;
ball2.velocityY = +6;

ball3.velocityX = +8;
ball3.velocityY = -6;

ball4.velocityX = -8;
ball4.velocityY = -6;

//Creating the edges
createEdgeSprites();


function draw() {
//Clear the screen
background("Yellow");

ball1.bounceOff(edges);
ball2.bounceOff(edges);
ball3.bounceOff(edges);
ball4.bounceOff(edges);

//Make ball bounce with each other
ball1.bounce(ball2);
ball1.bounce(ball3);
ball1.bounce(ball4);
ball2.bounce(ball3);
ball2.bounce(ball4);
ball3.bounce(ball4);

if(ball1.isTouching(ball2)||ball1.isTouching(ball3)||ball1.isTouching(ball4)
||ball2.isTouching(ball3)||ball2.isTouching(ball4)||ball3.isTouching(ball4)){
  
  playSound("sound://category_tap/game_bubble_pop_click.mp3")
}




drawSprites(); 
}


Video:-





Previous Post
Next Post

post written by:

0 Comments: