-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
167 lines (146 loc) · 5.39 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// JavaScript variables
let score = 0;
let gameStarted = false;
let gameOver = false;
let startTime = 0;
let totalTime = 0;
let stopwatchInterval;
let clickTimes = 0;
const infoSign = document.getElementById('info-sign');
const infoPopup = document.getElementById('info-popup');
const closeInfoPopup = document.getElementById('close-info-popup');
const mask = document.getElementById('mask');
const svg = document.getElementById('svg');
const toggleSwitch = document.getElementById('toggle-switch');
toggleSwitch.addEventListener('change', function() {
if (this.checked) {
svg.style.display = 'block';
} else {
svg.style.display = 'none';
}
});
document.addEventListener("mousemove", (event) => {
console.log("Mouse move");
var point = svg.createSVGPoint();
point.x = event.clientX;
point.y = event.clientY;
point = point.matrixTransform(svg.getScreenCTM().inverse());
mask.setAttribute('cx', point.x);
mask.setAttribute('cy', point.y);
console.log(point.x, point.y);
});
infoSign.addEventListener('click', () => {
infoPopup.style.display = 'block';
});
closeInfoPopup.addEventListener('click', () => {
infoPopup.style.display = 'none';
});
//
// Function to place Waldo in a random location
function placeWaldo() {
const waldo = document.getElementById('waldo');
const container = document.querySelector('.game-container');
const maxX = container.offsetWidth - waldo.offsetWidth;
const maxY = container.offsetHeight - waldo.offsetHeight;
const randomX = Math.floor(Math.random() * maxX);
const randomY = Math.floor(Math.random() * maxY);
waldo.style.left = randomX + 'px';
waldo.style.top = randomY + 'px';
}
// Function to update the score
function updateScore() {
const counter = document.getElementById('counter');
counter.textContent = `Score: ${score}`;
}
// Function to update the stopwatch
function updateStopwatch() {
const stopwatch = document.getElementById('stopwatch');
const currentTime = new Date().getTime();
const elapsedMilliseconds = currentTime - startTime;
const seconds = Math.floor(elapsedMilliseconds / 1000);
const minutes = Math.floor(seconds / 60);
const remainingSeconds = seconds % 60;
// const milliseconds = elapsedMilliseconds % 1000;
stopwatch.textContent = `${minutes}:${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}`;
}
const startButton = document.getElementById('start-button');
startButton.addEventListener('click', () => {
if (!gameStarted) {
gameStarted = true;
startTime = new Date().getTime();
stopwatchInterval = setInterval(updateStopwatch, 1000);
// Show Waldo when the game starts
const waldo = document.getElementById('waldo');
waldo.style.display = 'block';
// Make sure that the stopwatch and counter are visible
const counter = document.getElementById('counter');
counter.style.display = 'block';
const stopwatch = document.getElementById('stopwatch');
stopwatch.style.display = 'block';
// Hide the final results if there are any
}
else {
// Reset the stopwatch and score on subsequent clicks
clearInterval(stopwatchInterval);
startTime = new Date().getTime();
updateStopwatch();
}
if (gameOver) {
gameOver = false;
// Remove the game over message if it exists
const gameOverMessage = document.querySelector('.game-over-message');
if (gameOverMessage) {
gameOverMessage.remove();
}
}
score = 0;
totalTime = 0;
clickTimes = 0;
updateScore();
placeWaldo();
});
// Event listener for clicking on Waldo
const waldo = document.getElementById('waldo');
waldo.addEventListener('click', () => {
if (gameStarted) {
score++;
updateScore();
// const currentTime = new Date().getTime();
if (startTime > 0) {
// const elapsedTime = currentTime - startTime;
clickTimes++;
}
// startTime = currentTime;
updateStopwatch();
placeWaldo();
}
});
const stopButton = document.getElementById('stop-button');
stopButton.addEventListener('click', () => {
if (gameStarted & !gameOver) {
gameStarted = false;
gameOver = true;
// Hide Waldo, the counter, and the stopwatch
const waldo = document.getElementById('waldo');
waldo.style.display = 'none';
const counter = document.getElementById('counter');
counter.style.display = 'none';
const stopwatch = document.getElementById('stopwatch');
stopwatch.style.display = 'none';
// Calculate total elapsed time
totalTime += new Date().getTime() - startTime;
// Calculate score/time (average time between clicks)
const averageTime = clickTimes > 0 ? totalTime / clickTimes / 1000 : 0;
// Display the final score, total time, and score/time
const gameContainer = document.querySelector('.game-container');
const gameOverMessage = document.createElement('div');
gameOverMessage.className = 'game-over-message';
gameOverMessage.innerHTML = `
<h1>Game Over</h1>
<p>Final Score: ${score}</p>
<p>Total Time: ${(totalTime / 1000).toFixed(2)} seconds</p>
<p>${averageTime.toFixed(2)} seconds per click</p>
`;
gameContainer.appendChild(gameOverMessage);
}
});