xxxxxxxxxx
92
var level1 = [
[false, false, false, false, false],
[false, false, false, false, false],
[true, false, true, false, true],
[false, false, false, false, false],
[false, false, false, false, false],
];
function setup() {
createCanvas(400, 400);
stroke(255, 0, 0);
}
function draw() {
var won = true;
for (var r = 0; r < level1.length; r++) {
for (var c = 0; c < level1[r].length; c++) {
if (level1[r][c] === true) {
won = false;
}
}
}
if (won === true) {
background(0, 0, 0);
fill(200, 0, 0);
strokeWeight(2);
stroke(200, 0, 0);
textAlign(CENTER);
textSize(40);
text("You Won!", 200, 100);
textSize(15);
strokeWeight(1);
text("Press 'space' to Reset level 1", 200, 200);
if(keyIsDown(32)){
won = false;
drawLevel1();
resetLevel1();
}
}else drawLevel1();
}
function mousePressed() {
let col = int(mouseX / 80);
let row = int(mouseY / 80);
flipLight(row, col);
if (isValid(row, col - 1) === true) {
flipLight(row, col - 1);
}
if (isValid(row, col + 1) === true) {
flipLight(row, col + 1);
}
if (isValid(row - 1, col) === true) {
flipLight(row - 1, col);
}
if (isValid(row + 1, col) === true) {
flipLight(row + 1, col);
}
}
function flipLight(r, c) {
level1[r][c] = !level1[r][c];
}
function isValid(r, c) {
if (r < 0 || r > 4) {
return false;
}
if (c < 0 || c > 4) {
return false;
}
return true;
}
function drawLevel1() {
for (var r = 0; r < level1.length; r++) {
for (var c = 0; c < level1[r].length; c++) {
let x = c * 80;
let y = r * 80;
if (level1[r][c] === false) fill(0);
else fill(0, 0, 200);
rectMode(CORNER);
rect(x, y, 80, 80);
}
}
}
function resetLevel1(){
level1[2][0] = true;
level1[2][2] = true;
level1[2][4] = true;
}