PVector pos;
PVector delta;
int paddle;
int nrows;
int ncols;
float bwidth;
float bheight;
boolean[][] bricks;
void setup() {
size(320, 240);
smooth();
colorMode(HSB);
pos = new PVector(width / 2, height / 2);
delta = new PVector(-2, -2);
paddle = 20;
nrows = 5;
ncols = 5;
bwidth = width / ncols;
bheight = 15;
bricks = new boolean[nrows][ncols];
reset();
}
void draw() {
background(#FFFFFF);
if (pos.x < 0 || pos.x > width) {
delta.x = -delta.x;
}
if (pos.y < 0) {
delta.y = -delta.y;
}
if (pos.y > height) {
if (pos.x > mouseX - paddle && pos.x < mouseX + paddle) {
delta.y = -delta.y;
}
}
for (int r = 0; r < nrows; r++) {
for (int c = 0; c < ncols; c++) {
if (bricks[r][c]) {
if (pos.x > bwidth * c && pos.x < bwidth * (c + 1) &&
pos.y > bheight * r && pos.y < bheight * (r + 1)) {
bricks[r][c] = false;
delta.y = -delta.y;
}
}
}
}
stroke(#000000);
strokeWeight(10);
line(mouseX - paddle, height, mouseX + paddle, height);
noStroke();
fill(#000000);
ellipse(pos.x, pos.y, 20, 20);
stroke(#FFFFFF);
strokeWeight(1);
for (int r = 0; r < nrows; r++) {
fill(color(255.0 / nrows * r, 255, 255));
for (int c = 0; c < ncols; c++) {
if (bricks[r][c]) {
rect(bwidth * c, bheight * r, bwidth, bheight);
}
}
}
pos.x += delta.x;
pos.y += delta.y;
}
void keyPressed() {
if(key == 'r' || key == 'R') {
reset();
}
}
void reset() {
pos.x = width / 2;
pos.y = height / 2;
delta.x = -2;
delta.y = -2;
for (int r = 0; r < nrows; r++) {
for (int c = 0; c < ncols; c++) {
bricks[r][c] = true;
}
}
}