xxxxxxxxxx
120
// y=100
// direction = 1;
let gravity = 1.5;
class Ball{
constructor(){
this.x = 0;
this.y= 100;
this.d = 30;
// constant velocity of the ball
this.v = 0.001;
this.xg = 0;
this.yg = 0;
}
draw() {
// stroke(0, 255, 255);
ellipse(this.x, this.y, this.d);
}
update() {
this.draw();
this.y += this.yg;
// ensures that ball doesnt go past screen
if (this.y + this.d + this.yg <= 0) {
this.yg += gravity;
}
}
}
class Platform{
constructor({x,y}){
this.x = x;
this.y= y;
this.w = 30;
this.h = 30;
this.s = 1;
}
draw(){
push();
rectMode(CENTER);
rotateX(1.45);
rect(this.x,this.y,this.w,this.h);
pop();
}
move(){
this.y = this.y + this.s
this.s = this.s + 0.0001
}
}
let ball;
let plat;
let scrollOffset = 0;
function setup(){
createCanvas(710, 400, WEBGL);
ball = new Ball();
plat = [
(new Platform({x:0,y:250})),
(new Platform({x:0,y:200})),
(new Platform({x:50,y:100})),
(new Platform({x:0,y:0})),
(new Platform({x:-50,y:-100})),
(new Platform({x:0,y:-200})),
];
}
function draw(){
background(255);
ball.update();
plat.forEach((plat) => {
plat.draw();
plat.move();
});
// // stops ball at x = 450 to create an illusion of scrolling
// if (ball.y < 450) {
// ball.y += ball.yg;
// ball.yg += ball.v;
// plat.forEach((plat) => {
// //moves the ground to the left to create the illusion of scrolling
// plat.y -= 10;
// scrollOffset -= 5;
// });
plat.forEach((plat) => {
if (
ball.y + ball.d <= plat.y &&
ball.y + ball.d + ball.yg >= plat.y &&
ball.x + ball.d >= plat.x &&
ball.x <= plat.x + plat.w
) {
ball.yg = 0;
}
});
}
function keyPressed() {
if (keyCode === UP_ARROW) {
ball.yg -= 20;
}
if (keyCode === LEFT_ARROW) {
ball.x -= 20;
}
if (keyCode === RIGHT_ARROW) {
ball.x += 20;
}
}