bouncy_ball_game
//David Vargas dvargas
var vVelocity =1; //direction and speed of ball
var hVelocity =5;
var x= 100; // x position of ball
var y= 100; // y position of ball
var diam = 35; //diameter of circle
var points=0; // number of points
var title = true; //title screen text variable
function setup() {
createCanvas(200, 200);
background(220);
}
function draw() {
background(220);
fill(150,160,240);
ellipse(x,y,diam);
textSize(15);
text(points,width/2, 20);
x += hVelocity;
y += vVelocity;
// this makes ball bounce when it hits vertical walls
if(x> width-diam / 2){
hVelocity= random(-5,-1);
} else if (x< diam/ 2 ) {
hVelocity = random(1,5);
}
// this makes ball bounce when it hits horizontal walls
if (y > width -diam/2){
vVelocity= random(-5,-1);
} else if (y < diam/2){
vVelocity = random(1,5);
}
if(points>4){ //this creates the "you win" screen
background(100,250,100,50);
textSize(20);
fill(255);
text('You Win',width/3 ,height/2);
noLoop();
} else if(points<-4){ //this creates the "you lose" screen
background(240,100,100,50);
textSize(20);
fill(255);
text('You Lose',width/3, height/2);
noLoop();
}
if(title){ // displays tile screen before mouse is pressed
textSize(25);
text('Click the Ball',width/6,height/2)
}
print('points='+points);
}
function mousePressed() {
if(dist(mouseX,mouseY,x,y)< diam/2){ //this changes circle direction after being pressed
points+= 1
vVelocity=random(-5,5)
hVelocity=random(-5,5)
} else (points+=-1)
title= false // title screen dissappears once mouse is pressed
}