Assignment 3: A Bouncing Ball
- jficco2
- Sep 15, 2020
- 1 min read
//These will keep track of positions
var xPos;
var yPos;
var xSpeed;
var ySpeed;
var ballsize;
var g;
var r;
var b;
function setup() {
createCanvas(400, 400);
//Setting my initial position
xPos=200;
yPos=200;
xSpeed=5;
ySpeed=5;
g=0;
r=1;
b=2;
}
function draw() {
background(220);
//Conditional
//If the ball touches right hand side of window
if(xPos>400-25){
//Then move ball to the left
//console.log("WE DID IT")
xSpeed=-xSpeed;
g= random(0,255);
r= random(255,0)
}
if(xPos<0 + 25){
xSpeed=-xSpeed
g= random(0,255);
r= random (1,255);
b= random (2,255);
}
//Conditional
if(yPos>400-25){
ySpeed=-ySpeed
g=random(0,255);
r=random(255,0)
}
if(yPos<0 + 25){
g=random(0,255);
r=random(1,255);
b=random(2,255);
}
/*if((xPos > 400) || (xPos < 0)){
xSpeed = -xSpeed;
}*/
/*if (xPos > 400){
xSpeed=-xSpeed
}
else if (xPos < 0){
xSpeed=-xSpeed
}*/
//move the ball
xPos=xPos+xSpeed;
//Drawing my ball
fill (r, g, b)
ellipse(xPos, yPos, 50, 50)
}
Comments