-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseSnake.html
182 lines (156 loc) · 4.81 KB
/
baseSnake.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
<!DOCTYPE html>
<html>
<head>
<title>Snake Game</title>
</head>
<body>
<div id="score">0</div>
<canvas id="snakeboard" width="600" height="600"></canvas>
<style>
#back{
width: 4rem;
height: 3rem;
color: white;
border: 2px solid black;
background-color: black;
font-size: 1.4rem;
}
#snakeboard {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#score {
font-size: 6rem;
}
</style>
</body>
<script>
const board_border = 'black';
const board_background = "white";
const snake_col = 'red';
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 changing_direction = false;
let food_x;
let food_y;
let dx = 10;
let dy = 0;
const snakeboard = document.getElementById("snakeboard");
const snakeboard_ctx = snakeboard.getContext("2d");
main();
gen_food();
document.addEventListener("DOMContentLoaded", function () {
pTag = document.querySelector("div");
newVal = document.createElement("p");
newVal.innerHTML = '';
pTag.appendChild(newVal);
});
document.addEventListener("keydown", change_direction);
function main() {
if (has_game_ended()) return;
changing_direction = false;
setTimeout(function onTick() {
clear_board();
drawFood();
move_snake();
drawSnake();
main();
}, 100)
}
function clear_board() {
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 drawFood() {
snakeboard_ctx.fillStyle = 'yellow';
snakeboard_ctx.strokestyle = 'darkgreen';
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 has_game_ended() {
for (let i = 4; i < snake.length; i++) {
if (snake[i].x === snake[0].x && snake[i].y === snake[0].y) return true
}
const hitLeftWall = snake[0].x < 0;
const hitRightWall = snake[0].x > snakeboard.width - 10;
const hitToptWall = snake[0].y < 0;
const hitBottomWall = snake[0].y > snakeboard.height - 10;
return hitLeftWall || hitRightWall || hitToptWall || hitBottomWall
}
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_direction(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 === UP_KEY && !goingDown) {
dx = 0;
dy = -10;
}
if (keyPressed === RIGHT_KEY && !goingLeft) {
dx = 10;
dy = 0;
}
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();
}
}
</script>
<form class="" id="f1" action="" method="post">
<input id='back' type="submit" name="" value="Back" onclick="document.getElementById('f1').action='feed.php'">
</form>
</html>