xxxxxxxxxx
102
let knob;
let doneButton;
let restartButton;
let checkCount = 0;
let targetCount;
let numbers = [3, 5, 7, 10]; // Example list of numbers
let isGameOver = false;
let message = '';
function preload() {
console.log("Preload function started");
knob = loadImage("images/knob.png",
() => console.log("Image loaded successfully"),
(event) => console.error("Failed to load image", event));
}
function setup() {
console.log("Setup function started");
createCanvas(windowWidth, windowHeight);
doneButton = createButton('Done Checking');
doneButton.position(windowWidth / 2 - 110, windowHeight / 2 + 200);
doneButton.mousePressed(doneChecking);
restartButton = createButton('Restart');
restartButton.position(windowWidth / 2 + 10, windowHeight / 2 + 200);
restartButton.mousePressed(restartGame);
// Select a random number from the list
selectTargetCount();
}
function draw() {
background(255, 255, 255);
textSize(32);
textAlign(CENTER, CENTER);
// Draw the main header
text('Is the stove off?', windowWidth / 2, 50);
// Draw the subheader
textSize(16);
text('Check the stove the correct amount of times to know if it is off', windowWidth / 2, 80);
if (isGameOver) {
text(message, windowWidth / 2, windowHeight / 2 - 100);
} else {
// Draw the stove image
image(knob, windowWidth / 2 - 100, windowHeight / 2 - 100, 200, 200);
// Draw the check count below the stove image
textSize(16);
text(`Checks: ${checkCount}`, windowWidth / 2, windowHeight / 2 + 120);
}
}
function mousePressed() {
if (!isGameOver) {
// Check if the mouse is within the stove image area
let knobX = windowWidth / 2 - 100;
let knobY = windowHeight / 2 - 100;
let knobWidth = 200;
let knobHeight = 200;
if (mouseX > knobX && mouseX < knobX + knobWidth && mouseY > knobY && mouseY < knobY + knobHeight) {
checkStove();
}
}
}
function checkStove() {
if (!isGameOver) {
checkCount++;
console.log("Check stove, count:", checkCount);
}
}
function doneChecking() {
if (!isGameOver) {
console.log(`User's check count: ${checkCount}`);
console.log(`Target check count: ${targetCount}`);
if (checkCount != targetCount) {
message = `You should have checked it ${targetCount} times. Play again?`;
} else {
message = `Perfect! You checked the stove the right amount of times`;
}
isGameOver = true;
}
}
function restartGame() {
console.log("Restarting game");
checkCount = 0;
isGameOver = false;
message = '';
selectTargetCount();
}
function selectTargetCount() {
targetCount = int(random(numbers));
console.log(`New target number of checks: ${targetCount}`);
}