-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
150 lines (121 loc) Β· 4.61 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
let gameStarted = false;
let slayTime = 0;
let roundEnded = false;
let setTimeoutIds = []; // Array to store timeout IDs
// Audio Files
const getSound = new Audio('get.mp3');
const setSound = new Audio('set.mp3');
const slaySound = new Audio('slay.mp3');
// Buttons
const player1Btn = document.getElementById('player1-btn');
const player2Btn = document.getElementById('player2-btn');
const startBtn = document.getElementById('start-btn');
// Characters
const player1 = document.getElementById('player1');
const player2 = document.getElementById('player2');
// Message & Winner Elements
const messageEl = document.getElementById('message');
const winnerEl = document.getElementById('winner');
startBtn.textContent = 'Start Game';
// Event Listeners for Mobile Buttons
player1Btn.addEventListener('click', () => {
if (!roundEnded) handleInput(1);
});
player2Btn.addEventListener('click', () => {
if (!roundEnded) handleInput(2);
});
// Event Listeners for Keypress (PC Support)
document.addEventListener('keydown', (event) => {
if (roundEnded) return;
if (event.key === 'a' || event.key === 'A') handleInput(1);
if (event.key === 'l' || event.key === 'L') handleInput(2);
});
// Event Listener for Start/Play Again Button
startBtn.addEventListener('click', () => {
startGame();
startBtn.style.display = 'none';
});
function stopAllAudio() {
getSound.pause();
getSound.currentTime = 0;
setSound.pause();
setSound.currentTime = 0;
slaySound.pause();
slaySound.currentTime = 0;
// Clear all pending timeouts
setTimeoutIds.forEach(id => clearTimeout(id));
setTimeoutIds = [];
}
function startGame() {
if (gameStarted) return;
// Reset all states
roundEnded = false;
gameStarted = true;
slayTime = 0;
// Reset characters to initial state
player1.textContent = 'πββοΈ';
player2.textContent = 'πββοΈ';
// Start the sequence with proper timing
messageEl.textContent = "Get...";
getSound.play().catch(() => console.log("Browser blocked autoplay"));
// Set after 1 second
const setTimeoutId = setTimeout(() => {
messageEl.textContent = "Set...";
setSound.play();
}, 1000);
setTimeoutIds.push(setTimeoutId);
// Random delay for SLAY (between 2-4 seconds after "Set")
const randomDelay = Math.random() * 2000 + 2000;
const slayTimeoutId = setTimeout(() => {
messageEl.textContent = "SLAY!";
slaySound.play();
slayTime = performance.now();
console.log("SLAY triggered!");
}, 2000 + randomDelay); // 2000 = waiting for Get and Set
setTimeoutIds.push(slayTimeoutId);
}
function handleInput(player) {
if (!gameStarted || roundEnded) return;
if (slayTime === 0) {
// Player pressed too early
roundEnded = true;
stopAllAudio(); // Stop all audio when someone presses early
const loser = player;
const winner = player === 1 ? 2 : 1;
// Update emojis
document.getElementById(`player${loser}`).textContent = 'π';
document.getElementById(`player${winner}`).textContent = 'π
';
messageEl.textContent = `Player ${loser} pressed too early! Player ${winner} WINS!`;
gameStarted = false;
startBtn.textContent = 'Play Again';
startBtn.style.display = 'block';
return;
}
// Set roundEnded immediately to prevent multiple inputs
roundEnded = true;
const reactionTime = performance.now() - slayTime;
console.log(`Player ${player} reacted in ${reactionTime.toFixed(2)}ms`);
declareWinner(player, reactionTime);
}
function declareWinner(player, reactionTime) {
const winner = player === 1 ? "Player 1" : "Player 2";
// Get player elements
const winnerEl = document.getElementById(`player${player}`);
const loserEl = document.getElementById(`player${player === 1 ? '2' : '1'}`);
// Update emojis
winnerEl.textContent = 'π
';
loserEl.textContent = 'π';
// Show winner message
messageEl.textContent = `${winner} SLAYS! Reaction Time: ${reactionTime.toFixed(2)}ms`;
// Reset game state and show Play Again button
gameStarted = false;
startBtn.textContent = 'Play Again';
startBtn.style.display = 'block';
// Reset emojis after a delay
setTimeout(() => {
if (!gameStarted) {
winnerEl.textContent = 'πββοΈ';
loserEl.textContent = 'πββοΈ';
}
}, 3000);
}