-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.html
223 lines (213 loc) · 7.33 KB
/
snake.html
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Chess Snake Dual Game</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet" />
<link rel="stylesheet" href="snake.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.3/p5.js"></script>
</head>
<body style="background-color: #212121">
<div class="container-fluid pb-3">
<!-- <h2 id="game-title" class="text-white text-center"/> -->
<div class="row justify-content-center mb-5 pt-n1">
<!-- Square -->
<div
id="leftComponent"
class="col-lg-5 my-5"
style="width: 660px; height: 660px">
<div class="card shadow">
<div
class="card-header text-white"
style="
background-color: #1a1a1a;
border-bottom: 1px solid #727272;
">
<h5 class="mb-0">Snake game on the identified square topology</h5>
</div>
<div class="card-body">
<div id="game"></div>
</div>
<div class="card-footer" style="border-top: 1px solid #727272">
<div
class="d-flex justify-content-between align-items-center text-white">
<span id="score1"> </span>
<span id="highestScore1"> </span>
</div>
</div>
</div>
</div>
<!-- 3D topology -->
<div class="col-lg-5 my-5" style="height: 660px; width: 660px">
<div class="card shadow">
<div
class="card-header text-white"
style="
background-color: #1a1a1a;
border-bottom: 1px solid #727272;
">
<h5 id="title-surface" class="mb-0" />
</div>
<div class="card-body">
<div id="surface"></div>
</div>
<div class="card-footer" style="border-top: 1px solid #727272">
<div
class="d-flex justify-content-between align-items-center text-white">
<span id="score2"> </span>
<span id="highestScore2"> </span>
</div>
</div>
</div>
</div>
</div>
<!-- Footer bar -->
<footer
class="text-light py-2 fixed-bottom footer-shadow"
style="background-color: #292929">
<div class="container text-center">
<p class="mb-0 custom-footer-text">
© 2024 Dipartimento di Matematica, Felice Casorati
</p>
</div>
</footer>
</div>
<!-- Script to display the title of the game based on the topology -->
<script>
document.addEventListener("DOMContentLoaded", function () {
const topology = sessionStorage.getItem("topology");
const titleElementSurface = document.getElementById("title-surface");
if (topology) {
titleElementSurface.textContent = `Snake game on ${topology} surface`;
} else {
titleElementSurface.textContent = "Snake game on unknown topology";
}
});
</script>
<!-- Script to display the score and highest score on both components -->
<script>
document.addEventListener("DOMContentLoaded", function () {
setInterval(function () {
// Extract useful data
const currentScore = sessionStorage.getItem("currentScore") || 1;
let topology = sessionStorage.getItem("topology");
let highestScore =
localStorage.getItem("highestScore_" + topology) || 1;
// Display the current score on both components
document.getElementById("score1").textContent =
"Score: " + currentScore;
document.getElementById("score2").textContent =
"Score: " + currentScore;
// Display the highest score on both components
document.getElementById("highestScore1").textContent =
"Highest score on " + topology + " is : " + highestScore;
document.getElementById("highestScore2").textContent =
"Highest score on " + topology + " is : " + highestScore;
// Update the final score in the modal
document.getElementById("finalScoreDeath").textContent = currentScore;
document.getElementById("highestScoreDeath").textContent =
highestScore;
document.getElementById("finalScoreWin").textContent = currentScore;
document.getElementById("highestScoreWin").textContent = highestScore;
}, 50); // have to update at least every 1000/framerate ms
});
</script>
<!-- Function to toggle components -->
<script>
document.addEventListener("keydown", function (event) {
if (event.code === "KeyQ") {
const leftComponent = document.getElementById("leftComponent");
if (leftComponent.style.display === "none") {
leftComponent.style.display = "block";
} else {
leftComponent.style.display = "none";
}
}
});
</script>
<!-- Death Modal -->
<div
class="modal fade"
id="deathModal"
data-bs-backdrop="static"
data-bs-keyboard="false"
tabindex="-1"
aria-labelledby="deathModalLabel"
aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content text-center bg-dark text-white">
<!-- Modal header -->
<div
class="modal-header text-center align-items-center justify-content-center">
<h5 class="modal-title" id="deathModalLabel" style="color: #ff5370">
Game Over!
</h5>
</div>
<!-- Modal body -->
<div class="modal-body row align-items-center">
<p class="my-3">Your final score: <span id="finalScoreDeath" /></p>
<p>Your highest score: <span id="highestScoreDeath" /></p>
</div>
<!-- Modal footer -->
<div class="modal-footer align-items-center justify-content-center">
<button
type="button"
class="btn btn-primary"
onclick="clearSessionAndRestart()">
Restart Game
</button>
</div>
</div>
</div>
</div>
<!-- Win Modal -->
<div
class="modal fade"
id="winModal"
data-bs-backdrop="static"
data-bs-keyboard="false"
tabindex="-1"
aria-labelledby="winModalLabel"
aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content text-center bg-dark text-white">
<!-- Modal header -->
<div
class="modal-header text-center align-items-center justify-content-center">
<h5 class="modal-title" id="winModalLabel" style="color: #c3e88d">
You win!
</h5>
</div>
<!-- Modal body -->
<div class="modal-body row align-items-center">
<p class="my-3">Your final score: <span id="finalScoreWin" /></p>
<p>Your highest score: <span id="highestScoreWin" /></p>
</div>
<!-- Modal footer -->
<div class="modal-footer align-items-center justify-content-center">
<button
type="button"
class="btn btn-primary"
onclick="clearSessionAndRestart()">
Restart Game
</button>
</div>
</div>
</div>
</div>
<!-- Function for the button -->
<script>
function clearSessionAndRestart() {
sessionStorage.clear(); // Clear session storage
window.location.href = "setup.html"; // Redirect to the setup page
}
</script>
<script type="module" src="init.js"></script>
<script src="snake.js"></script>
<script src="keys.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>