xxxxxxxxxx
68
let player;
let bricks;
let ball;
let BRICK_W, BRICK_H, BRICK_O;
let NUM_ROWS, NUM_COLS;
function setup() {
createCanvas(610, 400);
NUM_ROWS = 4;
NUM_COLS = 8;
BRICK_O = 10;
BRICK_W = 60;
BRICK_H = 20;
frameRate(10);
// create bricks
bricks = [];
let x = BRICK_W;
let y = BRICK_H+20;
for (let r = 0; r < NUM_ROWS; r++) {
for (let c = 0; c < NUM_COLS; c++) {
bricks.push(new Brick(createVector(x, y), BRICK_W, BRICK_H, 0, null));
x += BRICK_W+BRICK_O;
}
x = BRICK_W;
y += BRICK_H+BRICK_O;
}
// create player
player = new Player(createVector(width/2, height-BRICK_H-20), createVector(1, 1), BRICK_W, BRICK_H, 0, null);
// create ball
ball = new Ball(createVector(width/2, height-BRICK_H-20-20), 10);
}
function keyReleased() {
player.resetVel();
}
function draw() {
background(220);
fill(0);
if (keyIsDown(LEFT_ARROW))
player.move("left");
if (keyIsDown(RIGHT_ARROW))
player.move("right");
for (let i = 0; i < bricks.length; i++) {
if (bricks[i].checkCollision(ball)) {
bricks.splice(i, 1);
//ball.brickHit();
} else {
bricks[i].draw();
}
}
//bricks.forEach((elem) => elem.checkCollision(ball));
//bricks.forEach((elem) => elem.draw());
player.draw();
ball.move();
ball.bounce();
ball.draw();
//noLoop();
}