xxxxxxxxxx
227
let imgs = {};
let sounds = {};
let punched = false;
let state = "start";
function preload() {
backImg = loadImage("assets/back.png");
font = loadFont("assets/Sigmar-Regular.ttf");
imgs.blast = { img: loadImage("assets/blast.png"), xoff: 0, yoff: 0 };
imgs.bomb = { img: loadImage("assets/bomb.png"), xoff: 0, yoff: 0 };
imgs.hammer = { img: loadImage("assets/hammer.png"), xoff: 0, yoff: 0 };
imgs.hole = { img: loadImage("assets/hole.png"), xoff: 0, yoff: 30 };
imgs.mole = { img: loadImage("assets/mole.png"), xoff: 0, yoff: 0 };
sounds.bomb = loadSound("sounds/Bomb hit.mp3");
sounds.back = loadSound("sounds/Game main theme.mp3");
sounds.gameover = loadSound("sounds/game-over.mp3");
sounds.whak = loadSound("sounds/Whacking a mole.mp3");
}
function setup() {
createCanvas(600, 600);
imageMode(CENTER);
textFont(font);
game = new Game();
textAlign(CENTER, CENTER);
}
function draw() {
image(backImg, width / 2, height / 2, width, height);
switch (state) {
case "start":
sounds.back.stop();
textSize(68);
fill(255);
text("WhACK!\na MOLE!", width / 2, height / 2 - 120);
textSize(16);
text("press anywhere to start!", width / 2, height - 30);
textSize(26);
let img = [imgs.hole, imgs.mole, imgs.bomb];
image(
img[floor(frameCount / 60) % 3].img,
width / 2 + img[floor(frameCount / 60) % 3].xoff,
height / 2 + 150 + img[floor(frameCount / 60) % 3].yoff
);
if (mouseIsPressed) {
mouseIsPressed = false;
sounds.whak.play();
state = "game";
}
break;
case "game":
game.show();
if (!sounds.back.isPlaying()) sounds.back.play();
break;
}
if (mouseX != 0 && mouseY != 0) {
push();
translate(mouseX, mouseY + 10);
if (punched) {
rotate(-PI / 2);
}
scale(map(game.holes.length, 4, 20, 1, 0.25));
image(imgs.hammer.img, 0, 0, 150, 150);
pop();
}
}
function mousePressed() {
if (state == "gameOver") {
state = "start";
game = new Game();
mouseIsPressed = false;
loop();
}
}
function gameOver() {
sounds.gameover.play();
setTimeout(() => {
sounds.back.stop();
image(imgs.blast.img, mouseX, mouseY, 250, 250);
background(20, 100);
textSize(64);
text("Game Over", width / 2, height / 2);
textSize(16);
text("click anywhere to restart!", width / 2, height - 50);
textSize(46);
text(game.score, width / 2, 35);
state = "gameOver";
}, 100);
noLoop();
}
class Game {
constructor() {
this.x = 10;
this.y = height / 2 - 80;
this.w = width - 20;
this.h = height / 2 + 70;
this.holesNum = 4;
this.holes = [];
this.difficulty = 60;
this.score = 0;
this.timer = 4800;
}
show() {
//timer
if (this.timer > 4800) this.timer = 4800;
this.timer -= 1.5;
fill(20, 100);
rect(10, 5, width - 20, 10);
fill(255);
rect(10, 5, map(this.timer, 0, 4800, 0, width - 20), 10);
if (this.timer < 0) {
mouseX = width / 2;
mouseY = height / 2;
gameOver();
}
//score
fill(255);
textSize(46);
if (punched) textSize(54);
text(this.score, width / 2, 35);
if (this.holesNum != this.holes.length) {
this.holes = this.findHolePositions(1);
}
for (let i = 0; i < this.holes.length; i++) {
push();
translate(this.holes[i].x, this.holes[i].y);
scale(this.holes[i].d / 250);
let img;
switch (this.holes[i].type) {
case "hole":
img = imgs.hole;
//nothing
break;
case "mole":
img = imgs.mole;
break;
case "bomb":
img = imgs.bomb;
break;
}
if (this.holes[i].type == "mole" || this.holes[i].type == "bomb") {
//check mouse click on mole
if (mouseIsPressed) {
if (
dist(mouseX, mouseY, this.holes[i].x, this.holes[i].y) <
this.holes[i].d
) {
mouseIsPressed = false;
punched = true;
sounds.whak.play();
setTimeout(() => {
punched = false;
}, 200);
if (this.holes[i].type == "mole") {
this.holes[i].type = "hole";
this.score += 1;
this.timer += 30;
} else {
sounds.bomb.play();
gameOver();
}
}
}
}
image(img.img, img.xoff, img.yoff);
pop();
}
if (this.difficulty - this.score < 20) {
this.difficulty += 30;
this.holesNum += 1;
}
if (frameCount % (this.difficulty - this.score) == 0) {
let hole = random(this.holes);
if (hole.type == "mole") {
hole.type = "hole";
} else {
hole.type = "mole";
}
if (random(1) < 0.1) {
hole.type = "bomb";
setTimeout(() => {
hole.type = "hole";
}, 1000);
}
}
}
findHolePositions(n, d = 200) {
let arr = [];
for (let i = 0; i < this.holesNum; i++) {
let x = random(this.x + d / 2, this.x + this.w - d / 2);
let y = random(this.y + d / 2, this.y + this.h - d / 2);
arr.push({ x: x, y: y, d: d, type: "hole" });
}
//no hole should overlap
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length; j++) {
if (i != j) {
let d_ = dist(arr[i].x, arr[i].y, arr[j].x, arr[j].y);
if (d_ < d) {
n += 1;
if (n > 50) {
n = 0;
d *= 0.9;
return this.findHolePositions(n, d);
}
return this.findHolePositions(n, d);
}
}
}
}
return arr;
}
}