xxxxxxxxxx
75
class Bars {
constructor() {
this.r1yValue = 0;
this.r2yValue = 0;
}
inputCheck(){
function keyPressed() {
if (keyCode === 83) {
this.r1yValue += 20;
} else if (keyCode === 87) {
this.r1yValue += -20;
}
if (keyCode === 76) {
this.r2yValue += 20;
} else if (keyCode === 79) {
this.r2yValue += -20;
}
}
}
draw(){
rect(0, this.r1yValue, 50, 50)
rect(350, this.r2yValue, 50, 50)
}
}
class BouncingBall {
constructor() {
this.xPos = width / 2;
this.yPos = random(100, 300);
this.xSpeed = 4;
this.ySpeed = 7;
}
move() {
// Moves the Ball
this.xPos += this.xSpeed;
this.yPos += this.ySpeed;
}
collisionCheck() {
// Check for Left and Right Wall
if (this.xPos <= 15 || this.xPos >= width - 15) {
this.xSpeed = -this.xSpeed;
}
// Check for Ceiling and Floor
if (this.yPos <= 15 || this.yPos >= height - 15) {
this.ySpeed = -this.ySpeed;
}
}
draw() {
circle(this.xPos, this.yPos, 30);
}
}
let myBouncingBall;
function setup() {
createCanvas(400, 400);
myBouncingBall = new BouncingBall();
myBars = new Bars();
}
let value = 0;
function draw() {
background(220);
fill(value);
myBars.inputCheck();
myBars.draw();
myBouncingBall.move();
myBouncingBall.collisionCheck();
myBouncingBall.draw();
}