-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
258 lines (210 loc) · 6.52 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Snake game</title>
</head>
<body>
<div id="score">0</div>
<canvas id="snakeboard" width="400" height="400"></canvas>
<div id="controls" style="text-align:center;width:480px;">
<button id = "moveup">UP</button><br><br>
<button id ="moveleft">LEFT</button>
<button id ="moveright">RIGHT</button><br><br>
<button id ="movedown">DOWN</button>
</div>
<style>
#snakeboard {
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, 0%);
}
#score {
height: 20%;
padding: 5px;
width: 50%;
margin: auto;
margin-top: 0%;
margin-bottom: 2vh;
border-radius: 30px;
color: white;
background-color: #000;
text-align: center;
font-size: 140px;
border-style: none;
}
#controls{
margin: auto;
margin-top: 2vh;
}
button{
cursor: pointer;
margin: 5px;
font-size: 1.2rem;
height: 2.5rem;
border: none;
border-radius: 10px;
color: #fff;
background-color: black;
outline: none;
box-shadow: 0 0.3rem rgba(121, 121, 121, 0.70);
}
button:hover{
filter: brightness(150%);
}
button:active{
transform: translate(0,0.3rem);
box-shadow: 0 0.1rem rgba(225, 225, 225, 0.7);
}
</style>
</body>
<script>
const board_border = 'black';
const board_background = 'white';
const snake_col = 'lightblue';
const snake_border = 'darkblue';
let snake = [
{x:200,y:200},
{x:190,y:200},
{x:180,y:200},
{x:170,y:200},
{x:160, y:200}
]
let score = 0;
let food_x;
let food_y;
let changing_direction = false;
let dx = 10;
let dy = 0;
const snakeboard = document.getElementById("snakeboard");
const snakeboard_ctx = snakeboard.getContext("2d");
main();
gen_food();
document.addEventListener("keydown", change_directionk);
document.addEventListener("mousedown", change_directionm);
function main() {
if (game_has_ended()) return;
changing_direction = false;
setTimeout(function start() {
clearCanvas();
draw_food();
move_snake();
drawSnake();
main();
}, 100)
}
function clearCanvas() {
snakeboard_ctx.fillStyle = board_background;
snakeboard_ctx.strokestyle = board_border;
snakeboard_ctx.fillRect(0, 0, snakeboard.width, snakeboard.height);
snakeboard_ctx.strokeRect(0, 0, snakeboard.width, snakeboard.height);
}
function drawSnake(){
snake.forEach(drawSnakePart);
}
function draw_food(){
snakeboard_ctx.fillStyle = "red";
snakeboard_ctx.strokestyle = 'black';
snakeboard_ctx.fillRect(food_x, food_y, 10, 10);
snakeboard_ctx.strokeRect(food_x, food_y, 10, 10);
}
function drawSnakePart(snakePart) {
snakeboard_ctx.fillStyle = snake_col;
snakeboard_ctx.strokestyle = snake_border;
snakeboard_ctx.fillRect(snakePart.x, snakePart.y, 10, 10);
snakeboard_ctx.strokeRect(snakePart.x, snakePart.y, 10, 10);
}
function game_has_ended(){
for(let i =4; i<snake.length; i++){
const has_collided = snake[i].x === snake[0].x && snake[i].y === snake[0].y;
if(has_collided) return true;
}
const hitLeftWall = snake[0].x < 0;
const hitRightWall = snake[0].x > snakeboard.width - 10;
const hitTopWall = snake[0].y < 0;
const hitBottomWall = snake[0].y >snakeboard.width - 10;
return hitBottomWall || hitTopWall || hitLeftWall || hitRightWall
}
function random_food(min, max){
return Math.round((Math.random() * (max - min) + min)/ 10)*10;
}
function gen_food(){
food_x = random_food(0, snakeboard.width - 10);
food_y = random_food(0 , snakeboard.height - 10);
snake.forEach(function has_snake_eaten_food(part) {
const has_eaten = part.x == food_x && part.y == food_y;
if (has_eaten) gen_food();
})
}
function change_directionk(event){
const LEFT_KEY = 37;
const RIGHT_KEY = 39;
const UP_KEY = 38;
const DOWN_KEY = 40;
if (changing_direction) return;
changing_direction = true;
const keyPressed = event.keyCode;
const goingUp = dy === -10;
const goingDown = dy === 10;
const goingRight = dx === 10;
const goingLeft = dx === -10;
if(keyPressed === LEFT_KEY && !goingRight)
{
dx = -10;
dy = 0;
}
if(keyPressed === RIGHT_KEY && !goingLeft)
{
dx = 10;
dy = 0;
}
if(keyPressed === UP_KEY && !goingDown)
{
dx = 0;
dy = -10;
}
if(keyPressed === DOWN_KEY && !goingUp)
{
dx = 0;
dy = 10;
}
}
function move_snake() {
const head = {x: snake[0].x + dx, y: snake[0].y + dy};
snake.unshift(head);
const has_eaten_food = snake[0].x === food_x && snake[0].y === food_y;
if(has_eaten_food){
score += 10;
document.getElementById('score').innerHTML = score;
gen_food();
} else{
snake.pop();
}
}
function change_directionm(){
const goingUp = dy === -10;
const goingDown = dy === 10;
const goingRight = dx === 10;
const goingLeft = dx === -10;
if (changing_direction) return;
changing_direction = true;
document.getElementById("moveup").addEventListener("click", function moveup(){
if(!goingDown) {dy = -10;
dx = 0;}
})
document.getElementById("movedown").addEventListener("click", function movedown(){
if(!goingUp) {dy = 10;
dx = 0;}
})
document.getElementById("moveleft").addEventListener("click", function moveleft(){
if(!goingRight){ dy = 0;
dx = -10;}
})
document.getElementById("moveright").addEventListener("click", function moveright(){
if(!goingLeft) {dy = 0;
dx = 10;}
})
}
</script>
</html>