xxxxxxxxxx
154
let img; // Variable to store the loaded image
let compHighlight; // Variable storing highlighted computer image
let showDorm = false; // Flag to track whether to show the image or not
let mainFont;
let numFont;
let showCompHighlight = false;
let showMenu = false; // Flag to track if the menu should be shown
let computerFont;
let computerBackground;
let snake;
let fruit;
function preload() {
img = loadImage('dormroomp.jpg'); // Load the image
mainFont = loadFont('MisterGrape.otf');
numFont = loadFont("crashingNumber.ttf");
computerFont=loadFont("computerFont.otf")
compHighlight = loadImage('comphighlight.jpg'); // Load the computer highlight image
compback = loadImage("computerbackground.jpeg");
}
function setup() {
createCanvas(800, 450);
snake = new Snake(20); // Initialize snake with grid size of 20
fruit = new Fruit(800, 450, 20); // Initialize fruit
}
function draw() {
background(250,249,240);
if (showMenu) {
drawMenu(); // If the menu is active, draw the menu
} else if (showDorm) {
if (showCompHighlight) {
image(compHighlight, 0, 0, width, height); // Display the hover image if the flag is true
} else {
image(img, 0, 0, width, height); // Display the main image
}
checkHoverOverComputer(); // Check if hovering over the computer
} else {
drawDoors(); // Draw the doors if the image is not being displayed
}
}
function drawDoors() {
//Main page text
textFont(mainFont);
textSize(80);
strokeWeight(15);
textAlign(CENTER);
text("Linus's Dorm experience",width/2,100);
rectMode(CENTER);
// Coordinates and dimensions for the doors
let middleDoorX = width - 400;
let doorY = height / 1.5;
let doorW = 150;
let doorH = 250;
// Middle Door (Interaction on hover)
if (mouseX > middleDoorX - doorW / 2 && mouseX < middleDoorX + doorW / 2 &&
mouseY > doorY - doorH / 2 && mouseY < doorY + doorH / 2) {
strokeWeight(5); // Thicker border on hover
} else {
strokeWeight(1); // Default stroke weight
}
// Draw the middle door
fill(20, 180, 190);
rect(middleDoorX, doorY, doorW, doorH);
fill(250);
rect(middleDoorX, height / 1.75, 50, 35);
circle(middleDoorX + 50, doorY + 15, 15);
fill(0);
textAlign(CENTER);
textFont(numFont);
textSize(20);
text('705', middleDoorX, (height / 1.75) + 5);
// Left and Right doors (Not interactive)
strokeWeight(1);
fill(20, 180, 190);
rect(width - 700, doorY, doorW, doorH);
rect(width - 100, doorY, doorW, doorH);
fill(250);
rect(width - 700, height / 1.75, 50, 35);
rect(width - 100, height / 1.75, 50, 35);
circle(width - 650, doorY + 15, 15);
circle(width - 50, doorY + 15, 15);
fill(0);
text('704', width - 700, (height / 1.75) + 5);
text('706', width - 100, (height / 1.75) + 5);
}
function drawMenu() {
background(50, 50, 50); // Change the background color for the menu
image(compback,0,0,width,height)
textFont(computerFont);
textSize(50);
fill(255);
textAlign(CENTER);
textSize(30);
text("Press 1: Open Drum Machine", width / 2, 150);
text("Press 2: Play Snake", width / 2, 250);
text("Press ESC to go back", width / 2, 350);
}
function mousePressed() {
let middleDoorX = width - 400;
let doorY = height / 1.5;
let doorW = 150;
let doorH = 250;
// Check if the mouse is within the middle door to display the main room image
if (!showDorm && mouseX > middleDoorX - doorW / 2 && mouseX < middleDoorX + doorW / 2 &&
mouseY > doorY - doorH / 2 && mouseY < doorY + doorH / 2) {
showDorm = true; // Set flag to true to show the dorm room image
showCompHighlight = false; // Reset highlight to ensure no immediate transition to computer menu
return; // Exit the function after entering the dorm room
}
// Check if the user clicks on the computer only after dorm room is shown
if (showDorm && !showMenu && showCompHighlight) {
showMenu = true; // Show the menu when the user clicks on the computer
}
}
function keyPressed() {
if (keyCode === ESCAPE) {
if (showMenu) {
showMenu = false; // Close the menu if it is open
} else if (showDorm) {
showDorm = false; // Return to the previous scene (doors)
}
}
}
// Function to check if the mouse is hovering over the computer area
function checkHoverOverComputer() {
let computerX = 344; // X coordinate of the computer
let computerY = 225; // Y coordinate of the computer
let computerW = 130; // Width of the computer area
let computerH = 93; // Height of the computer area
// Check if mouse is over the computer only when the user is in the dorm room and menu is not showing
if (showDorm && !showMenu && mouseX > computerX && mouseX < computerX + computerW &&
mouseY > computerY && mouseY < computerY + computerH) {
showCompHighlight = true; // Show the hover image
} else {
showCompHighlight = false; // Revert back to the main image
}
}