xxxxxxxxxx
83
let yPos = 200; let yVelo = 0;
let xPos = 200; let xVelo = 0;
jump = 10; side = 15
function setup() {
createCanvas(400, 400);
frameRate(30)
}
function draw() {
background(220);
circle(xPos,yPos,20);
/*if(keyIsDown(65)){
if(xVelo < -maxX){
xVelo += -2;
}
}*/
xPos += xVelo;
yPos -= yVelo; //Falling
if(yVelo > -10){
yVelo -= 0.5;
}
if(yPos >= 400){ //Collision w/ bottom of the screen
if(yVelo > 0.5 || yVelo < -0.5){
yVelo /= -1.5
}else{
yVelo = 0
yPos = 400
}
}
if(xVelo > 0){ //Horizontal deceleration
xVelo -= 1;
}
if(xVelo < 0){
xVelo += 1;
}
if(xPos >= 399){ //Collision w/ right of the screen
if(xVelo > 0.5 || xVelo < -0.5){
xVelo /= -1.5
}else{
xVelo = 0
xPos = 397
}
}
if(xPos <= 0){ //Collision w/ left of the screen
if(xVelo > 0.5 || xVelo < -0.5){
xVelo /= -1.5
}else{
xVelo = 0
xPos = 3
}
}
if(yPos <= 0){ //Collision w/ top of the screen
if(yVelo > 0.5 || yVelo < -0.5){
yVelo /= -1.5
}else{
yVelo = 10
yPos = 5
}
}
}
function keyPressed(){
switch(keyCode){
case 87: //Jumping (W)
yVelo = jump
break
case 65: //Left (A)
xVelo = -side
break
case 68: //Right (D)
xVelo = side
break
case 83: //Down (S)
yVelo = -jump
break
}
}