xxxxxxxxxx
135
let knob;
let doneButton;
let restartButton;
let checkCount = 0;
let targetCount;
let numbers = []; // List to store numbers loaded from file
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));
// Load numbers from the file
loadStrings("numbers.txt",
result => {
numbers = result.map(Number); // Convert strings to numbers
console.log("Numbers loaded successfully:", numbers);
},
error => console.error("Failed to load numbers:", error)
);
}
function setup() {
console.log("Setup function started");
createCanvas(windowWidth, windowHeight);
doneButton = createButton('Done Checking');
doneButton.position(windowWidth / 2 - 40, windowHeight / 2 + 200);
doneButton.mousePressed(doneChecking);
// Select a random number from the list
selectTargetCount();
}
function draw() {
background(255, 255, 255);
textSize(20);
textAlign(CENTER, CENTER);
// Draw the main header
text('Is the stove off?', windowWidth / 2, 50);
// Draw the subheader
textSize(12);
text(`Check the stove the \ncorrect nnumber of times to know if it is off`, windowWidth / 2, 80);
if (isGameOver) {
displayGameOverMessage();
} else {
// Draw the stove image
image(knob, windowWidth / 2 - 100, windowHeight / 2 - 100, 200, 200);
// Draw the check count below the stove image
textSize(12);
text(`Checks: ${checkCount}`, windowWidth / 2, windowHeight / 2 + 120);
}
}
function displayGameOverMessage() {
let lines = message.split('\n');
textSize(16);
for (let i = 0; i < lines.length; i++) {
text(lines[i], windowWidth / 2, windowHeight / 2 - 100 + i * 20);
}
}
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 = `Oof what a waste of time.\nYou should have only checked it ${targetCount} times.\nTry again?`;
} else if (checkCount != targetCount) {
message = `Incorrect!\nYou should have checked it ${targetCount} times.\nTry again?!`;
} else {
message = `Perfect!\nYou checked the stove the right amount of times`;
}
isGameOver = true;
// Hide the "Done Checking" button
doneButton.hide();
// Show the "Restart" button
restartButton = createButton('Try Again?');
restartButton.position(windowWidth / 2 -20, windowHeight / 2 + 200);
restartButton.mousePressed(restartGame);
}
}
function restartGame() {
console.log("Restarting game");
checkCount = 0;
isGameOver = false;
message = '';
selectTargetCount();
// Show the "Done Checking" button
doneButton.show();
// Remove the "Restart" button
restartButton.remove();
}
function selectTargetCount() {
if (numbers.length > 0) {
targetCount = int(random(numbers));
console.log(`New target number of checks: ${targetCount}`);
} else {
console.error("Numbers list is empty. Make sure the file is loaded correctly.");
}
}