/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.r2ELz0-S0Fm/rev.3354
*
* authors:
* Benjamin Y
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
// Pressing Control-R will render this sketch.
int paddleX = 200;
int paddleY = 350;
int ballX = 200;
int ballY = 330;
int ballDX = 3;
int ballDY = -3;
int ballR = 4;
int health = 5;
int score = 0;
int[][][] blocks = [[
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]
],[
[0,0,0,0,0,0,0,0,0,0],
[0,1,0,1,0,0,1,0,1,0],
[1,1,1,1,1,1,1,1,1,1],
[1,0,1,0,1,1,0,1,0,1],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,1,0,0,1,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]
],[
[0,0,0,0,0,0,0,0,0,0],
[2,2,2,2,2,2,2,2,2,2],
[1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]
],[
[0,0,0,0,0,0,0,0,0,0],
[2,2,2,2,2,2,2,2,2,2],
[1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1],
[3,3,3,3,3,3,3,3,3,3],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]
],[
[0,0,0,0,0,0,0,0,0,0],
[2,2,2,2,2,2,2,2,2,2],
[1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1],
[3,3,3,3,3,3,3,3,3,3],
[3,3,3,3,3,3,3,3,3,3],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]
],[
[0,0,0,0,0,0,0,0,0,0],
[3,3,3,3,2,2,3,3,3,3],
[1,1,1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1,1,1],
[3,3,3,3,3,3,3,3,3,3],
[3,3,3,3,3,3,3,3,3,3],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0]
]
];
boolean isCleared = false;
boolean isNotHitting = true;
int level = 0;
int maxlevel = 6;
int lastBrick = [-1,-1];
int seconds = 0;
textFont(createFont("Arial"));
void setup() { // this is run once.
// set the background color
background(0,0,204);
// canvas size (Variable aren't evaluated. Integers only, please.)
size(400, 400);
// smooth edges
smooth();
// limit the number of frames per second
frameRate(30);
// set the width of the line.
strokeWeight(3);
}
String format(float i) {
i -= 60;
i -= 82;
if (i < -0.0001) {
i += 60;
}
if (i < 10) {
return '0'+i;
} else {
return i;
}
}
void draw() { // this is run repeatedly.
background(0,0,204);
fill(0);
rect(paddleX-18,paddleY-2,36,4);
fill(102,102,255);
ellipse(ballX,ballY,ballR*2,ballR*2);
fill(255);
text("health: "+health+"/5",300,20);
text("score: "+score,12,20);
text("level: "+(level+1),100,20);
//text(floor((seconds)/30/60)+":"+format(((seconds)/30)%60),220,20);
text(floor(seconds/30),220,20);
text("Breakdown",150,20);
rect(80, 10, 10, 10);
int row = 0;
int column = 0;
isCleared = true;
isNotHitting = true;
for (int[] i: blocks[level]) {
for (int j: i) {
if (j == 1) {
fill(128,51,0);
rect(40*column,40*row,40,40);
if (ballX > 40*column && ballX < 40*(column+1) && ballY > 40*row && ballY < 40*(row+1) && lastBrick != [row,column]) {
blocks[level][row][column] = 0;
if (abs(40*(column+0.5)-ballX) >= abs(40*(row+0.5)-ballY)) {
ballDX = -ballDX;
} else {
ballDY = -ballDY;
}
score += 15;
isNotHitting = false;
} else {
isCleared = false;
}
}
if (j == 2) {
fill(255,128,0);
rect(40*column,40*row,40,40);
if (ballX > 40*column && ballX < 40*(column+1) && ballY > 40*row && ballY < 40*(row+1) && lastBrick != [row,column]) {
blocks[level][row][column] = 1;
if (abs(40*(column+0.5)-ballX) >= abs(40*(row+0.5)-ballY)) {
ballDX = -ballDX;
} else {
ballDY = -ballDY;
}
score += 30;
isNotHitting = false;
} else {
isCleared = false;
}
}
if (j == 3) {
fill(255,204,128);
rect(40*column,40*row,40,40);
if (ballX > 40*column && ballX < 40*(column+1) && ballY > 40*row && ballY < 40*(row+1) && lastBrick != [row,column]) {
blocks[level][row][column] = 2;
if (abs(40*(column+0.5)-ballX) >= abs(40*(row+0.5)-ballY)) {
ballDX = -ballDX;
} else {
ballDY = -ballDY;
}
score += 55;
isNotHitting = false;
} else {
isCleared = false;
}
}
if (j == 9) {
fill(255,255,0);
ellipse(40*column+20,40*row+20,20,20);
if (ballX > 40*column && ballX < 40*(column+1) && ballY > 40*row && ballY < 40*(row+1) && lastBrick != [row,column]) {
level += 1;
score += health * 48;
health = 5;
if (level > maxlevel) {
text("You Win!",200,200);
return;
}
}
}
column += 1;
}
row += 1;
column = 0;
}
if (isNotHitting) {
lastBrick = [-1,-1]
}
ballX += ballDX;
// ballY += ballDY+random(-4,5);
ballY += ballDY;
if (ballX < 0 || ballX > width) {
ballDX = -ballDX;
}
if (ballY < 0) {
ballDY = -ballDY;
}
if (ballY > height) {
health -= 1;
ballY = 330;
ballX = paddleX;
ballDX = 0;
ballDY = 0;
if (!mousePressed) {
ballDX = 3;
ballDY = -3;
}
}
if (ballX > paddleX-15 && ballX < paddleX+15 && ballY > paddleY-2 && ballY < paddleY+50) {
ballDY = -ballDY;
ballDX = (ballX-paddleX)/3;
ballY -= 10;
}
if ((mouseX > 80 && mouseX < 90) && (mouseY > 10 && mouseY < 20)) {
fill(255, 204, 102);
rect(0, 20, 400, 380);
textFont(createFont("Monotype Corsiva", 16));
fill(0);
text(" Help ALT break through the wall! In every level you have a\n ball to destroy the wall. You have five balls and try to break\n the wall in the least time possible. You score extra points if\n you have balls left. Have fun! \n",30, 50);
textFont(createFont("Arial"));
}
if (health == 0) {
text("You lose",200,200);
return;
}
if (isCleared) {
level += 1;
score += health * 48;
health = 5;
ballY = 330;
ballX = paddleX;
ballDX = 3;
ballDY = -3;
if (level > maxlevel) {
text("You Win!",200,200);
return;
}
}
paddleX = mouseX;
seconds += 1;
}