Hello everyone!!
i’m new in this of programming but i am trying to learn by making this sort of codes. I would like the ball and the wall1 interact each other making the ball bounce on the brick.
May somebody guide me, please?
thanks for your time
var wall1, wall2;
var ball;
function keyPressed(){
if(keyCode === UP_ARROW){ wall1.y = wall1.y - 80;}
if(keyCode === DOWN_ARROW){
wall1.y = wall1.y + 80;
}
}
function setup() {
createCanvas(650, 500);
wall1 = new Wall(20, 20, 20, 120);
wall2 = new Wall(width - 40, 20, 20, 120);
ball = new Ball(width/2, height/2, 20);
}
function draw() {
background(0);
let aSq = wall1.b * ball.h;
let aCr = PI * ball.r * ball.r;
let d = dist(ball.x, wall1.x, ball.r, wall1.x/2);
if(d < ball.r + wall1.x/2){
Ball.xspeed = Ball.xspeed * -1;
Ball.yspeed = Ball.yspeed * -1;
}
ball.moveBall();
ball.ball();
//wall1.moveSquare();
wall1.showSquare();
wall2.moveSquare();
wall2.showSquare();
}
class Ball{
constructor(x, y, r){
this.x = x;
this.y = y;
this.r = r;
this.xspeed = random(5, 10);
this.yspeed = random(5, 10);
}
ball(){
stroke(255);
strokeWeight(4);
noFill();
ellipse(this.x, this.y, this.r * 2);
}
moveBall(){
if(this.x >= width || this.x <= 0){
this.xspeed = this.xspeed * -1;
}
this.x = this.x + this.xspeed;
if(this.y >= height || this.y <= 0){
this.yspeed = this.yspeed * -1;
}
this.y = this.y + this.yspeed * -1;
}
}
class Wall{
constructor(x, y, b, h){
this.x = x;
this.y = y;
this.b = b;
this.h = h;
this.yspeed = random(5, 8);
}
showSquare(){
rect(this.x, this.y, this.b, this.h);
}
moveSquare(){
if(this.y >= height-this.h || this.y <= 0){
this.yspeed = this.yspeed * -1;
}
this.y = this.y + this.yspeed;
}
}