-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
205 lines (193 loc) · 15.5 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="Vanilla JavaScript Snake coding project">
<meta name="keywords" content="snake, JS, vanilla, Javascript, game, web game">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="icon" type="image/png" href="./resources/images/favicon-16x16.png">
<title>Vanilla JavaScript Snake</title>
</head>
<body class="body touch" data-control="Space">
<header class="header">
<h1 class="title">
JS Snake
</h1>
<button type="button" class="toggle-about link">About</button>
</header>
<main class="main">
<section id="about-section" class="about-section closed ">
<h2 class="about-title">Vanilla JavaScript Snake game on HTML5 canvas</h2>
<div class="about">
<p class="about-p">
I coded this game to practice my JavaScript skills. <br>My goal was to implement some functionality from
nostalgic
Nokia 'Snake' game.
</p>
<h3 class="about-ul-title">Present functionality:</h3>
<p class="attention">Attention: page will reload when resizing window and you will loose your game.</p>
<ul class="about-ul">
<li>Move, eat, grow(after finished digesting), die on collision.</li>
<li>Track score, record, length, speed.</li>
<li>Food: mice and rabbits. Rabbits show up and disappear randomly.</li>
<li>Fenced and free world modes.</li>
<li>Settings (sound, grid visibility, fence, saving).</li>
<li>Save to local storage.</li>
<li>Control with keyboard (arrows or VIM (h,j,k,l) ) and touch controls for mobile.</li>
</ul>
<h3 class="about-ul-title">Attribution:</h3>
<ul class="about-ul">
<li>
<a class="about-attr-link" target="_blank" href="https://pixabay.com/vectors/bunny-cartoon-rabbit-2025642/" class="attribution">Rabbit image by OpenClipart-Vectors from pixabay.com</a>
</li>
<li>
<a class="about-attr-link" target="_blank" href="https://pixabay.com/vectors/snake-reptile-lizard-nature-145409/" class="attribution">Snake image by OpenClipart-Vectors from pixabay.com</a>
</li>
<li>
<a class="about-attr-link" target="_blank" href="https://freesound.org/people/peterattarian/sounds/330998/">Background music by peterattarian from freesounds.org</a>
</li>
</ul>
</div>
<footer class="footer">
<a href="https://github.com/codevivi/JS-snake">Check it on
<img src="resources/images/GitHub-Mark-Light-64px.png" aria-hidden="true" target="_blank" alt="GitHub"> GitHub</a>
<p class="license">Vilma J. ©
<span id="year"></span>
</p>
</footer>
</section>
<div id="wrapper" class="wrapper-for-canvas">
<div class="touch-controls">
<div class="touch touch-up dir" data-control="ArrowUp" id="touch-up">↑</div>
<div class="touch-sides-wrapper">
<div class="touch touch-left dir" data-control="ArrowLeft" id="touch-left">←</div>
<div class="touch touch-right dir" data-control="ArrowRight" id="touch-right">→</div>
</div>
<div class="touch touch-down dir" data-control="ArrowDown" id="touch-down">↓</div>
</div>
<div class="display">
<button title="Settings" type="button" aria-label="open settings" class="settings-button settings-button--open" id="open-settings">
<svg width="24" height="24" aria-hidden="true" fill="none" xmlns="http://www.w3.org/2000/svg">
<path
d="M12.005 15.86c-2.12 0-3.86-1.72-3.86-3.86 0-2.12 1.72-3.86 3.86-3.86 2.12 0 3.86 1.72 3.86 3.86 0 2.12-1.74 3.86-3.86 3.86Zm0-6.3A2.45 2.45 0 0 0 9.565 12c0 1.34 1.1 2.44 2.44 2.44 1.34 0 2.44-1.1 2.44-2.44 0-1.34-1.1-2.44-2.44-2.44Zm0 11.64h-.1c-.7 0-1.3-.08-1.34-.08a.721.721 0 0 1-.62-.6l-.2-1.44c-.44-.14-.86-.32-1.26-.54l-1.14.86c-.26.2-.6.18-.86 0-.02-.02-.5-.4-1-.88l-.08-.08c-.5-.5-.86-.98-.88-1-.2-.26-.2-.6 0-.86l.88-1.16c-.22-.4-.38-.84-.52-1.28l-1.4-.2a.7.7 0 0 1-.6-.62c0-.02-.08-.64-.08-1.34v-.1c0-.7.08-1.3.08-1.34.04-.32.28-.56.6-.62l1.44-.2c.14-.44.32-.86.54-1.26l-.86-1.14c-.2-.26-.18-.6 0-.86.02-.02.4-.5.88-1l.08-.08c.5-.5.98-.86 1-.88.26-.2.6-.2.86 0l1.16.88c.4-.22.84-.38 1.28-.52l.2-1.4a.7.7 0 0 1 .62-.6c.02 0 .64-.08 1.34-.08h.1c.7 0 1.3.08 1.34.08.32.04.56.28.62.6l.2 1.44c.44.14.86.32 1.26.54l1.14-.86c.26-.2.6-.18.86 0 .02.02.5.4 1 .88l.08.08c.5.5.86.98.88 1 .2.26.2.6 0 .86l-.88 1.16c.22.4.38.84.52 1.28l1.4.2c.32.04.56.3.6.62 0 .02.08.64.08 1.34v.1c0 .7-.08 1.3-.08 1.34-.04.32-.28.56-.6.62l-1.44.2c-.14.44-.32.86-.54 1.26l.86 1.14c.2.26.18.6 0 .86-.02.02-.4.5-.88 1l-.08.08c-.5.5-.98.86-1 .88-.26.2-.6.2-.86 0l-1.16-.88c-.4.22-.84.38-1.28.52l-.2 1.4a.7.7 0 0 1-.62.6c-.04 0-.64.08-1.34.08Zm-.72-1.44c.18.02.4.02.62.02h.1c.22 0 .44 0 .62-.02l.18-1.32c.04-.3.26-.52.54-.6.62-.14 1.22-.38 1.76-.72.16-.1.36-.12.54-.08.1.02.2.08.28.14l1.06.8c.14-.12.3-.26.46-.42l.08-.08c.16-.16.3-.32.42-.46l-.8-1.06a.729.729 0 0 1-.04-.8c.34-.52.58-1.1.72-1.7.06-.3.3-.54.6-.58l1.34-.18c.02-.18.02-.4.02-.62v-.1c0-.22 0-.44-.02-.62l-1.32-.18a.704.704 0 0 1-.58-.54c-.14-.62-.38-1.2-.7-1.74-.04-.04-.06-.1-.08-.16a.706.706 0 0 1 .1-.66l.82-1.08c-.12-.14-.26-.3-.42-.46l-.08-.08c-.16-.16-.32-.3-.46-.42l-1.06.8a.72.72 0 0 1-.84 0c-.52-.32-1.1-.56-1.7-.72-.28-.06-.5-.3-.54-.6l-.18-1.34c-.18-.02-.4-.02-.62-.02h-.1c-.22 0-.44 0-.62.02l-.18 1.32a.68.68 0 0 1-.6.6c-.6.14-1.18.38-1.7.7-.16.1-.36.12-.54.08-.1-.02-.2-.08-.28-.14l-1.04-.8c-.14.12-.3.26-.46.42l-.08.08c-.16.16-.3.32-.42.46l.8 1.06c.18.24.18.54.04.8-.02.02-.02.04-.04.06-.32.52-.56 1.1-.7 1.7-.06.28-.3.5-.6.54l-1.34.18c-.02.18-.02.4-.02.62v.1c0 .22 0 .44.02.62l1.32.18c.32.04.56.28.6.6v.02c.14.6.38 1.16.7 1.68.1.18.14.4.06.6a.75.75 0 0 1-.14.24l-.78 1.04c.12.14.26.3.42.46l.08.08c.16.16.32.3.46.42l1.06-.8c.26-.2.64-.18.88.02.52.32 1.08.54 1.66.68.28.06.5.3.54.6l.18 1.36Z"
fill="#939393" />
</svg>
</button>
<p class="score" title="Score" aria-label="score">
<span class="display-text">score</span>
<svg class="display-svg" width="24" height="24" aria-hidden="true" fill="none" xmlns="http://www.w3.org/2000/svg">
<path
d="M4.005 19.2c-.18 0-.36-.04-.52-.14-.48-.28-.62-.9-.34-1.38l2.44-4.1c.18-.28.48-.48.82-.48.34-.02.66.14.86.42l1.04 1.44 2.2-3.76c.16-.28.48-.48.8-.5.34-.02.66.14.86.4l1.92 2.58 5.06-8.4c.28-.48.9-.62 1.38-.34.48.28.62.9.34 1.38L15.045 16c-.18.28-.48.46-.8.48-.34.02-.66-.14-.86-.4l-1.92-2.56-2.22 3.78c-.18.3-.48.48-.82.5a.984.984 0 0 1-.86-.42l-1.04-1.44-1.66 2.78c-.18.3-.52.48-.86.48Z"
fill="#939393" />
</svg>
<span class="display-value" id="score-display">600</span>
</p>
<p title="Record" class="record" aria-label="record">
<span class="display-text">record</span>
<svg class="display-svg" width="24" height="24" aria-hidden="true" fill="none" xmlns="http://www.w3.org/2000/svg">
<path
d="M19.005 6.02c0-2.12-3.52-3.22-7-3.22s-7 1.1-7 3.22v12.06c0 2.06 3.52 3.14 7 3.14s7-1.08 7-3.14V6.02Zm-1.6 6.06c0 .44-1.9 1.54-5.4 1.54-3.5 0-5.4-1.08-5.4-1.54V8.14c1.4.72 3.38 1.1 5.4 1.1 2.02 0 4-.38 5.4-1.1v3.94Zm-5.4-7.68c3.56 0 5.4 1.14 5.4 1.62 0 .48-1.84 1.62-5.4 1.62-3.56 0-5.4-1.14-5.4-1.62 0-.48 1.84-1.62 5.4-1.62Zm0 15.2c-3.5 0-5.4-1.08-5.4-1.54v-3.92c1.4.7 3.38 1.06 5.4 1.06 2.02 0 4-.36 5.4-1.06v3.92c0 .46-1.9 1.54-5.4 1.54Zm3.4-8.22v-.98c0-.38.32-.7.7-.7.38 0 .7.32.7.7v.98c0 .38-.32.7-.7.7-.38 0-.7-.3-.7-.7Z"
fill="#939393" />
</svg>
<span class="display-value" id="record-display">2000</span>
</p>
<p title="Length" class="length">
<span class="display-text">length</span>
<svg class="display-svg" width="24" height="24" aria-hidden="true" fill="none" xmlns="http://www.w3.org/2000/svg">
<path
d="M9.785 10.94h-5.46c-.62 0-1.12-.5-1.12-1.12v-4.3c0-.62.5-1.12 1.12-1.12h5.46c.62 0 1.12.5 1.12 1.12v4.3c0 .62-.5 1.12-1.12 1.12Zm-4.98-1.6h4.5V6h-4.5v3.34Zm14.88 1.6h-5.46c-.62 0-1.12-.5-1.12-1.12v-4.3c0-.62.5-1.12 1.12-1.12h5.46c.62 0 1.12.5 1.12 1.12v4.3c0 .62-.5 1.12-1.12 1.12Zm-4.98-1.6h4.5V6h-4.5v3.34ZM9.785 19.6h-5.46c-.62 0-1.12-.5-1.12-1.12v-4.3c0-.62.5-1.12 1.12-1.12h5.46c.62 0 1.12.5 1.12 1.12v4.3c0 .62-.5 1.12-1.12 1.12ZM4.805 18h4.5v-3.34h-4.5V18Zm14.88 1.6h-5.46c-.62 0-1.12-.5-1.12-1.12v-4.3c0-.62.5-1.12 1.12-1.12h5.46c.62 0 1.12.5 1.12 1.12v4.3c0 .62-.5 1.12-1.12 1.12Zm-4.98-1.6h4.5v-3.34h-4.5V18Z"
fill="#939393" />
</svg>
<span class="display-value" id="length-display">2000</span>
</p>
<p title="Speed" class="speed" aria-label="speed">
<span class="display-text">speed</span>
<svg class="display-svg" width="24" height="24" aria-hidden="true" fill="none" xmlns="http://www.w3.org/2000/svg">
<path
d="m17.365 7.78-3.88 8.36c-.34.86-.94 1.36-1.6 1.36-.54 0-1-.28-1.26-.72-.28-.52-.26-1.16.08-1.7l5.08-8.14c.26-.4.78-.54 1.2-.32.42.22.58.72.38 1.16Zm1.36 2.42c-.3-.32-.8-.34-1.14-.04-.32.3-.34.8-.04 1.14a7.826 7.826 0 0 1 2.06 5.3c0 .44.36.8.8.8.44 0 .8-.36.8-.8 0-2.38-.88-4.66-2.48-6.4Zm-6.08-1.32c.44.04.82-.28.86-.72a.788.788 0 0 0-.72-.86c-.26-.02-.52-.04-.78-.04-5.08 0-9.2 4.2-9.2 9.36 0 .44.36.8.8.8.44 0 .8-.36.8-.8 0-4.28 3.4-7.76 7.6-7.76.22-.02.44 0 .64.02Z"
fill="#939393" />
</svg>
<span class="display-value" id="speed-display">2000</span>
</p>
</div>
<canvas id="canvas-main-id" class="canvas-main">Your browser doesn't support HTML5 canvas element.</canvas>
<canvas class="canvas-bg" id="canvas-bg-id"> Your browser doesn't support HTML5 canvas element.</canvas>
<div class="over-canvas" id="over-canvas-id">
<div class="screen" id="screen-id">
<p class="screen-msg" class="touch" data-control="Enter" id="screen-msg-id">Press ENTER to start</p>
</div>
<div class="settings " id="settings-id">
<button type="button" aria-label="close settings" class="settings-button settings-button--close" id="close-settings"><svg width="24" height="24" fill="none" aria-hidden="true" xmlns="http://www.w3.org/2000/svg">
<path
d="M16.945 15.66c.36.36.36.92 0 1.28-.18.18-.4.26-.64.26s-.46-.08-.64-.26l-3.66-3.66-3.66 3.66c-.18.18-.4.26-.64.26s-.46-.08-.64-.26a.894.894 0 0 1 0-1.28l3.66-3.66-3.66-3.66a.894.894 0 0 1 0-1.28c.36-.36.92-.36 1.28 0l3.66 3.66 3.66-3.66c.36-.36.92-.36 1.28 0s.36.92 0 1.28L13.285 12l3.66 3.66Z"
fill="#fff" />
</svg></button>
<div class="settings-wrapper">
<h2 class="settings-title">Settings</h2>
<div class="settings-buttons">
<div class="button-field">
<p class="button-label">Sound</p>
<button type="button" class="toggle-button" id="sound-on-off"><span class="toggler" id="sound-toggler"></span></button>
</div>
<div class="button-field">
<p class="button-label">Grid</p>
<button type="button" class="toggle-button" id="grid-on-off"><span class="toggler" id="grid-toggler"></span></button>
</div>
<div class="button-field">
<p class="button-label">Fence</p>
<button type="button" class="toggle-button" id="border-on-off"><span class="toggler " id="border-toggler"></span></button>
</div>
<div class="button-field">
<p class="button-label">Use local storage</p>
<button type="button" class="toggle-button" id="save-on-off"><span class="toggler " id="save-toggler"></span></button>
</div>
<div class="button-field">
<p class="button-label">Speed</p>
<select class="select" id="set-speed">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- these images will be use only by js -->
<img class="hidden" id="rabbit_img" aria-hidden="true" src="resources/images/rabbit-greyscale.png" alt="rabbit" width="50" height="50" />
<img class="hidden" id="mouse_img" aria-hidden="true" src="resources/images/mouse.png" alt="mouse" width="50" height="50" />
<script>
const bodyEl = document.querySelector('.body');
const toggleAboutBtn = document.querySelector('.toggle-about');
const yearEl = document.getElementById('year');
const aboutSection = document.getElementById('about-section');
const currentYear = new Date().getUTCFullYear();
yearEl.textContent = currentYear;
toggleAboutBtn.addEventListener('click', toggleAbout);
function toggleAbout() {
if (aboutSection.classList.contains('open')) {
aboutSection.classList.add('closing');
toggleAboutBtn.textContent = 'About'
setTimeout(() => {
aboutSection.classList.remove('closing');
aboutSection.classList.remove('open');
bodyEl.classList.remove('no-scroll');
}, 1000);
} else {
aboutSection.classList.add('open');
bodyEl.classList.add('no-scroll');
toggleAboutBtn.textContent = 'Back to game'
}
}
</script>
<script src="./main.js"> </script>
</body>
</html>