xxxxxxxxxx
209
let knob;
let checkButton;
let doneButton;
let restartButton;
let checkCount = 0;
let targetCount;
let numbers = []; // List to store numbers loaded from file
let isGameOver = false;
let message = '';
let timerStart = false;
let startTime;
let knobX, knobY, knobSize;
let isKnobVisible = true; // Track the visibility of the knob
let isTryAgainVisible = false; // Track the visibility of the "Try Again" button
function preload() {
console.log("Preload function started");
knob = loadImage("images/knob.png");
// 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);
checkButton = createButton(`Check the stove`);
checkButton.mousePressed(checkStove);
doneButton = createButton('I checked it enough');
doneButton.mousePressed(doneChecking);
// Initial button positions
positionButtons();
// Select a random number from the list
selectTargetCount();
// Set knob position and size
knobX = windowWidth / 2 - 100;
knobY = windowHeight / 2 - 100;
knobSize = 200;
}
function draw() {
background(255, 255, 255);
textSize(20);
textAlign(CENTER, CENTER);
if (!isGameOver) {
// Draw the main header
text('Is the stove off?', windowWidth / 2, 50);
// Draw the subheader
textSize(12);
text('Check if the stove is off the right number of times.\nWhen you think you have it, hit "I checked it enough".', windowWidth / 2, 100);
}
if (isGameOver) {
displayGameOverMessage();
} else {
// Draw the stove image if visible
if (isKnobVisible) {
image(knob, knobX, knobY, knobSize, knobSize);
}
// Draw the check count below the stove image
text(`You've checked the stove ${checkCount} times`, windowWidth / 2, windowHeight / 2 + 120);
}
// if (timerStart) {
// displayTimer();
// }
}
function positionButtons() {
const buttonWidth = 150;
const buttonMargin = 10;
const totalWidth = buttonWidth * 2 + buttonMargin;
const xPosition = (windowWidth - totalWidth) / 2;
const yPosition = windowHeight / 2 + 200;
checkButton.position(xPosition, yPosition);
doneButton.position(xPosition + buttonWidth + buttonMargin, yPosition);
}
function displayGameOverMessage() {
let lines = message.split('\n');
textSize(12);
for (let i = 0; i < lines.length; i++) {
text(lines[i], windowWidth / 2, windowHeight / 2 - 100 + i * 20);
}
// Show the "Try Again" button
if (!restartButton) {
restartButton = createButton('Try Again');
restartButton.position(windowWidth / 2 - 40, windowHeight / 2 + 100);
restartButton.mousePressed(restartGame);
isTryAgainVisible = true; // Mark that the "Try Again" button is visible
}
}
function checkStove() {
if (!isGameOver) {
checkCount++;
checkButton.html(`Check Stove`);
console.log("Check stove, count:", checkCount);
if (!timerStart) {
timerStart = true;
startTime = millis();
}
}
}
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 = `Not enough...\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 buttons
doneButton.hide();
checkButton.hide();
// Hide the knob
isKnobVisible = false;
}
}
function restartGame() {
console.log("Restarting game");
checkCount = 0;
isGameOver = false;
message = '';
timerStart = false;
startTime = 0;
selectTargetCount();
// Show the buttons
doneButton.show();
checkButton.show();
// Reset the check button text
checkButton.html(`Check Stove`);
// Remove the "Try Again" button
if (restartButton) {
restartButton.remove();
restartButton = null;
isTryAgainVisible = false; // Mark that the "Try Again" button is not visible
}
// Show the knob
isKnobVisible = true;
// Reposition buttons
positionButtons();
}
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.");
}
}
function mousePressed() {
console.log("Mouse pressed. isGameOver:", isGameOver, "isKnobVisible:", isKnobVisible, "isTryAgainVisible:", isTryAgainVisible); // Debugging statement
if (!isGameOver && isKnobVisible && !isTryAgainVisible) { // Check if the game is not over, knob is visible, and "Try Again" is not visible
let knobCenterX = knobX + knobSize / 2;
let knobCenterY = knobY + knobSize / 2;
console.log("Mouse position:", mouseX, mouseY); // Debugging statement
console.log("Knob center position:", knobCenterX, knobCenterY); // Debugging statement
console.log("Distance from knob center:", dist(mouseX, mouseY, knobCenterX, knobCenterY)); // Debugging statement
if (dist(mouseX, mouseY, knobCenterX, knobCenterY) < knobSize / 2) {
alert('Looks like the stove is off');
}
}
}
function displayTimer() {
let elapsedTime = millis() - startTime;
let totalSeconds = floor(elapsedTime / 1000);
let minutes = floor(totalSeconds / 60);
let seconds = totalSeconds % 60;
fill(0);
textSize(14);
text(nf(minutes, 2) + ':' + nf(seconds, 2), windowWidth / 2, 150);
}