I do not understand how to make multiple colored bricks into an array. Each row would have a different color and I only have one brick that appears and I am not sure how to get an 8x4 array of bricks.
I have no clue on how to go about doing this.
//Tracy Miles
//N220
//4.29.2019
var screenW = 800;
var screenH = 600;
var objects = [];
//setup for the canvas
function setup() {
createCanvas(screenW, screenH);
var paddle = new Paddle();
var ball = new Ball(paddle);
objects.push(paddle);
objects.push(ball);
objects.push(new Block(screenH/2, screenW/2, ball));
}
function draw() {
background(0);
for (var i = 0; i < objects.length; i++)
{
objects[i].update();
}
}
function Paddle() {
this.x = 0; this.y = screenH - 35;
this.width = 100; this.height = 20;
this.update = function() {
this.x = mouseX;
rect(this.x, this.y, this.width, this.height);
}
}
function Ball(paddle) {
this.paddle = paddle;
this.x = screenW/2; this.y = screenH - 40;
this.rad = 10;
this.speedX = 3; this.speedY = -3;
this.update = function() {
this.x += this.speedX;
this.y += this.speedY;
if (this.x < 0 || this.x > screenW) {
this.speedX *= -1;
}
if (this.y < 0 || this.y > screenH) {
this.speedY *= -1;
}
if (this.y > this.paddle.y && this.y < this.paddle.y + this.paddle.height) {
if (this.x > this.paddle.x && this.x < this.paddle.x + this.paddle.width) {
this.speedY *= -1;
}
}
rect(this.x, this.y, this.rad, this.rad, 90);
}
}
////////This is where the main problem arises.//////////////
function Block(x, y, ball) {
this.ball = ball;
this.x = x; this.y = x;
this.width= 100; this.height= 20;
this.broken = false;
this.update = function() {
if (!this.broken) {
rect(this.x, this.y, this.width, this.height);
if (this.ball.y > this.y && this.ball.y < this.y + this.height) {
if (this.ball.x > this.x && this.ball.x < this.x + this.width) {
this.broken = true;
ball.speedY *= -1;
}
}
}
}
}
I want to have an 8x4 array of blocks and each row being a different color.