xxxxxxxxxx
96
/*
*** BOUNCING BALL ***
Idea taken from The Coding Train
https://youtu.be/LO3Awjn_gyU
Made by Laura Pisa
My web: www.sites.google.com/view/frogrammergames
*/
let circleX,circleY;
let x,y;
let r,g,b;
let r1=1,g1=1,b1=1;
let rad=25;
let lastX=0,lastY=0,newX=0,newY=0;
function setup() {
createCanvas(windowWidth, windowHeight);
r=random(255);
g=random(255);
b=random(255);
r1=random(3);
g1=random(3);
b1=random(3);
x=random(3,6);
y=random(3,6);
circleX=random(rad,width);
circleY=random(rad,height);
}
function draw() {
if(mouseIsPressed){
if(mouseX!=newX || mouseY!=newY){
lastX=newX;
lastY=newY;
newX=mouseX;
newY=mouseY;
fill(r,g,b);
circle(mouseX,mouseY,rad*2);
}
background(0,0,0,10);
}
else{
noStroke();
circleX+=x;
circleY+=y;
if(r>255 || r<0 ){r1*=-1;}
if(g>255 || g<0 ){g1*=-1;}
if(b>255 || b<0 ){b1*=-1;}
r+=r1;
g+=g1;
b+=b1;
if(circleX>width-rad || circleX<rad){
x*=-1;
}
if(circleY>height-rad || circleY<rad){
y*=-1;
}
fill(r,g,b);
circle(circleX,circleY,rad*2);
background(0,0,0,10);
}
}
function mouseReleased(){
x=newX-lastX;
y=newY-lastY;
circleX=mouseX;
circleY=mouseY;
}
function mousePressed(){
background(0);
}