xxxxxxxxxx
285
let help = "Press 'f' (possibly twice) to toggle fullscreen";
let tryAgainMessage = false;
let gameState = "start";
let startImg, p1Img, p2Img, p3Img, endImg, tryImg;
let wrongSound;
// variables to read from Arduino
let digit;
let button;
let locked;
// variables to send to Arduino
let open = false;
let codes = [0, 0, 0];
let buttonState = false; // Tracks button state
let buttonPressCount = -2;
let correctCode = [
[1, 6, 4], // Corresponding to p1Img
[3, 1, 9], // Corresponding to p2Img
[7, 5, 9], // Corresponding to p3Img
];
let chooseCode = [];
function preload() {
// Load sounds
wrongSound = loadSound("wrong.mp3");
// Load images
startImg = loadImage("start.png");
instImg = loadImage("inst.png");
p1Img = loadImage("1.png");
p2Img = loadImage("2.png");
p3Img = loadImage("3.png");
endImg = loadImage("end.png");
tryImg = loadImage("try.png");
// Add images to the array
chooseCode.push(p1Img);
chooseCode.push(p2Img);
chooseCode.push(p3Img);
}
function setup() {
buttonPressCount = -2;
createCanvas(windowWidth, windowHeight);
restart();
print(help);
}
function draw() {
// Display different images based on the game state
if (gameState === "start") {
image(startImg, 0, 0, width, height);
} else if (gameState === "instruction") {
// Display the instruction image
image(instImg, 0, 0, width, height);
} else if (gameState === "play") {
// Display the randomly chosen image
image(chooseCode[randomIndex], 0, 0, width, height);
displayCode(); // Call the function to display the code
} else if (gameState === "end") {
image(endImg, 0, 0, width, height);
}
textSize(20);
textAlign(LEFT);
if (!serialActive) {
// text("Press C to select Serial Port", 20, 30);
} else {
// text("Connected", 20, 30);
// Print the current values
// text("digit = " + str(digit), 20, 50);
// text("button = " + button, 20, 70);
// text("locked = " + locked, 20, 90);
// print(locked);
// Display the code
let codeDisplay = "Code: ";
for (let i = 0; i < codes.length; i++) {
if (buttonPressCount === i) {
codeDisplay += digit; // Show the currently entered digit at the specific index
} else {
codeDisplay += codes[i]; // Show the existing code at other indexes
}
if (i < codes.length - 1) {
codeDisplay += " - ";
}
}
// text(codeDisplay, 20, 120);
// Display the button press count
// text("Button Press Count: " + buttonPressCount, 20, 140);
}
if (tryAgainMessage) {
displayTryAgainMessage();
}
}
function readSerial(data) {
////////////////////////////////////
// READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
let fromArduino = split(trim(data), ",");
if (fromArduino.length === 3) {
digit = int(fromArduino[0]);
button = str(fromArduino[1]);
locked = str(fromArduino[2]);
if (gameState === "start" && button.trim().toLowerCase() === "false") {
// gameState will change to "instruction" first
gameState = "instruction";
}
if (gameState === "instruction") {
if (buttonPressCount === -1 && button.trim().toLowerCase() === "false") {
// Move to "play" state if buttonPressCount is -1 and button is released
gameState = "play";
}
if (button.trim().toLowerCase() === "true" && buttonState) {
// Button released, increment count only during instruction
buttonPressCount++;
if (buttonPressCount <= 3) {
codes[buttonPressCount - 1] = digit;
}
buttonState = false; // Reset button state
} else if (button.trim().toLowerCase() === "false") {
// Button pressed during instruction
buttonState = true; // Button pressed
}
}
if (gameState === "play") {
if (button.trim().toLowerCase() === "true" && buttonState) {
// Button released, increment count
buttonPressCount++;
// Enter the digit
if (buttonPressCount <= 3) {
codes[buttonPressCount - 1] = digit;
}
buttonState = false; // Reset button state
} else if (button.trim().toLowerCase() === "false") {
buttonState = true; // Button pressed
}
}
// If the game is ended, press the button to restart the entire game
if (gameState === "end" && button.trim().toLowerCase() === "false") {
restart();
}
// After entering 3 codes
if (buttonPressCount === 3) {
if (
JSON.stringify(codes) === JSON.stringify(correctCode[randomIndex])
) {
locked = "false";
open = "true";
gameState = "end";
tryAgainMessage = false;
} else {
locked = "true";
tryAgain();
wrongSound.play();
}
}
}
}
//////////////////////////////////
// SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = open + "\n";
writeSerial(sendToArduino);
}
//========================================================================================
// Functions to trigger "Full Screen" mode, and set up the SERIAL
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function keyTyped() {
if (key === "f") {
toggleFullscreen();
}
if (key == "c") {
// important to have in order to start the serial connection!!
setUpSerial();
}
}
function toggleFullscreen() {
let fs = fullscreen();
fullscreen(!fs);
}
//========================================================================================
// Function to generate a random index for selecting an image
function randomImage() {
randomIndex = floor(random(chooseCode.length)); // Generate random index
}
//========================================================================================
// Function to make another attempt to enter the code
function tryAgain() {
codes = [0, 0, 0];
buttonPressCount = 0;
tryAgainMessage = true;
}
// Function to restart the entire game
function restart() {
gameState = "start";
locked = "true"; // Codes match, set locked to true
open = "false"; // Change open to a string false
codes = [0, 0, 0];
buttonPressCount = -2;
randomImage(); // This selects a new random image
tryAgainMessage = false;
}
//========================================================================================
// Function to display the code entries on the screen
function displayCode() {
textSize(180);
textAlign(CENTER, CENTER);
let centerX = (20 * width) / 32;
let centerY = (25 * height) / 32;
let digitWidth = textWidth("0"); // Width of a digit
let digitSpacing = 50; // Define the spacing between digits
for (let i = 0; i < codes.length; i++) {
if (buttonPressCount === i) {
noFill();
stroke("#BA1615"); // Red stroke color
strokeWeight(8); // Adjust the stroke weight
rect(
centerX + i * (digitWidth + digitSpacing),
centerY - textSize() / 2,
textSize(),
textSize(),
10 // Corner radius for rounded corners
);
noStroke();
fill("#0858A9"); // Reset text fill color
text(
digit,
centerX + i * (digitWidth + digitSpacing) + textSize() / 2,
centerY
);
} else {
if (codes[i] !== 0) {
fill("#BA1615"); // Change fill to red if the digit is entered
} else {
fill("#0858A9"); // Use default fill color if the digit is not entered
}
text(
codes[i],
centerX + i * (digitWidth + digitSpacing) + textSize() / 2,
centerY
);
}
}
}
// Function to display the "Try Again" message
function displayTryAgainMessage() {
// Display the tryImg with a scaled size
let scaleFactor = 0.8; // Adjust the scale factor as needed
let scaledWidth = tryImg.width * scaleFactor;
let scaledHeight = tryImg.height * scaleFactor;
image(
tryImg,
(20 * width) / 32,
(15 * height) / 32 - scaledHeight / 2,
scaledWidth,
scaledHeight
);
}