Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Naksh-Rathore authored Feb 15, 2025
0 parents commit cf27327
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 0 deletions.
Binary file added images/person.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stop & Go</title>
<link rel="stylesheet" href="style.css">
</head>
<body>


<h1>Stop & Go</h1>

<div id="container">
<hr>

<img src="./images/person.png" alt="person">

<div id="trafficLight">
<button id="redLight" disabled></button><br><br>
<button id="greenLight" disabled></button>
</div>
</div>

<script src="script.js"></script>
</body>
</html>
77 changes: 77 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const redLight = document.getElementById("redLight");
const greenLight = document.getElementById("greenLight");
const player = document.querySelector("img");

const greenLightSound = new Audio("./sounds/start.mp3");
const redLightSound = new Audio("./sounds/stop.mp3");

const finishLine = document.querySelector("hr");

redLight.style.backgroundColor = "rgba(89, 87, 87, 0.746)";

let yOffset = 0;
let isRedLight = true;
let playing = true;

function changeLight() {
if (!isRedLight) {
redLightSound.currentTime = 0;
redLightSound.play();

redLight.style.backgroundColor = "red";
greenLight.style.backgroundColor = "rgba(89, 87, 87, 0.746)";
isRedLight = true;
}

else {
greenLightSound.currentTime = 0;
greenLightSound.play();

greenLight.style.backgroundColor = "green";
redLight.style.backgroundColor = "rgba(89, 87, 87, 0.746)";
isRedLight = false;
}

setTimeout(changeLight, Math.floor(Math.random() * 3001) + 1000);
}

document.addEventListener("keydown", event => {
event.preventDefault();

if (event.repeat) return;

if (!playing) return;

if (isRedLight) {
playing = false;
yOffset = 0;
window.alert("You lost!");

redLight.style.backgroundColor = "rgba(89, 87, 87, 0.746)";
greenLight.style.backgroundColor = "green";
}

const playerRect = player.getBoundingClientRect();
const finishLineRect = finishLine.getBoundingClientRect();

if (playerRect.bottom <= finishLineRect.top) {
playing = false;

redLight.style.backgroundColor = "rgba(89, 87, 87, 0.746)";
greenLight.style.backgroundColor = "rgba(89, 87, 87, 0.746)";

window.alert("You won!");
}

if (event.key === "ArrowUp") {
yOffset -= 10;
}

else if (event.key === "ArrowDown") {
yOffset += 10;
}

player.style.transform = `translateY(${yOffset}px)`;
});

changeLight();
Binary file added sounds/start.mp3
Binary file not shown.
Binary file added sounds/stop.mp3
Binary file not shown.
46 changes: 46 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
body {
font-family: sans-serif;
text-align: center;
position: relative;
}

#timeDisplay {
transform: translateY(75px);
}

hr {
transform: translateY(75px);
}

img {
position: absolute;
width: 100px;
top: 590px;
left: 300px;
transition: transform 0.25s ease;
}

#trafficLight {
background-color: rgb(255, 238, 0);
transform: translateY(250px);
border: 2px solid grey;
padding: 20px;
width: 10%;
margin-top: -32px;
margin-left: 575px;
}

button {
border: solid 1px;
width: 75px;
height: 75px;
border-radius: 50px;
background-color: rgba(89, 87, 87, 0.746);
transition: background-color 0.2s ease;
}

hr {
height: 5px;
background-color: orange;
border: none;
}

0 comments on commit cf27327

Please sign in to comment.