xxxxxxxxxx
93
let lampcenterx, lampcentery, lampw, lamph;
let switchtop, switchmiddle, switchbottom, switchx, switchw, switchh;
let state = "Off"; // Declare for lamp color
let switchState = "top"; // Declare switch position
function setup() {
createCanvas(500, 700);
lamph = 200;
lampw = 200;
lampcenterx = width / 2;
lampcentery = lamph / 2 + 200;
switchtop = 430; // top position (switch)
switchmiddle = 490; // middle position (switch)
switchbottom = 550; // bottom position (switch)
switchx = lampcenterx - 20;
switchw = 40;
switchh = 40;
}
function draw() {
background(50);
// Lamp wire
noStroke();
fill(20, 240, 160);
rect(lampcenterx - 3, 0, 6, 200);
// Lamp
strokeWeight(5);
stroke(20, 240, 160);
fill(160, 160, 160, 150);
arc(lampcenterx, lampcentery, lampw, lamph, PI, 0);
// Switch Base
stroke(0);
strokeWeight(2);
fill(100);
rect(lampcenterx - 20, 430, 40, 160, 10);
// Switch button
stroke(0);
strokeWeight(2);
fill(255, 0, 0);
// Draw red switch button with if statement
if (switchState == "top") {
rect(switchx, switchtop, switchw, switchh, 10); // top
} else if (switchState == "middle") {
rect(switchx, switchmiddle, switchw, switchh, 10); // middle
} else if (switchState == "bottom") {
rect(switchx, switchbottom, switchw, switchh, 10); //bottom position
}
// Lamp light based on state
noStroke();
if (state == "Off") {
fill(0, 0, 0, 200); // no light
} else if (state == "Warm") {
fill(255, 197, 148, 150); // Warm light
} else if (state == "White") {
fill(212, 235, 255, 150); // White light
}
arc(lampcenterx, lampcentery, lampw, lamph, PI, 0); // Lamp light
if (state != "Off") {
quad(150, 300, 350, 300, 490, 700, 10, 700); // Light beam area
}
}
function mousePressed() {
// Check if mouse is inside the switch base
if (mouseX > switchx && mouseX < switchx + switchw && mouseY > 430 && mouseY < 590) {
// Cycle through the switch positions
if (switchState == "top") {
switchState = "middle"; // Move to the middle
} else if (switchState == "middle") {
switchState = "bottom"; // Move to the bottom
} else if (switchState == "bottom") {
switchState = "top"; // Move back to the top
}
// Change lamp color based on state
if (state == "Off") {
state = "Warm"; // Warm light
} else if (state == "Warm") {
state = "White"; // White light
} else {
state = "Off"; // Turn off
}
}
}