-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
124 lines (97 loc) · 3.04 KB
/
Copy pathscript.js
File metadata and controls
124 lines (97 loc) · 3.04 KB
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
const canvas = document.getElementById("board");
const context = canvas.getContext("2d");
const scoreElement = document.getElementById("score");
const restartBtn = document.getElementById("restart-btn");
const blockSize = 25;
const rows = 20;
const cols = 20;
let snakeX = blockSize * 5;
let snakeY = blockSize * 5;
let velocityX = 0;
let velocityY = 0;
let snakeBody = [];
let foodX;
let foodY;
let gameOver = false;
let score = 0;
let highScore = localStorage.getItem("snakeHighScore") || 0;
window.onload = function() {
document.getElementById("highScore").innerText = highScore;
placeFood();
document.addEventListener("keydown", changeDirection);
setInterval(update, 100);
}
function update() {
if (gameOver) {
return;
}
context.fillStyle = "black";
context.fillRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "red";
context.fillRect(foodX, foodY, blockSize, blockSize);
if (snakeX === foodX && snakeY === foodY) {
snakeBody.push([foodX, foodY]);
score += 10;
if (score > highScore) {
highScore = score;
localStorage.setItem("snakeHighScore", highScore);
}
scoreElement.innerHTML = `Score: ${score} | High Score: <span id="highScore">${highScore}</span>`;
placeFood();
}
for (let i = snakeBody.length - 1; i > 0; i--) {
snakeBody[i] = snakeBody[i - 1];
}
if (snakeBody.length) {
snakeBody[0] = [snakeX, snakeY];
}
context.fillStyle = "lime";
snakeX += velocityX * blockSize;
snakeY += velocityY * blockSize;
context.fillRect(snakeX, snakeY, blockSize, blockSize);
for (let i = 0; i < snakeBody.length; i++) {
context.fillRect(snakeBody[i][0], snakeBody[i][1], blockSize, blockSize);
}
if (snakeX < 0 || snakeX >= cols * blockSize || snakeY < 0 || snakeY >= rows * blockSize) {
gameOver = true;
}
for (let i = 0; i < snakeBody.length; i++) {
if (snakeX === snakeBody[i][0] && snakeY === snakeBody[i][1]) {
gameOver = true;
}
}
}
function changeDirection(e) {
if (e.code === "ArrowUp" && velocityY !== 1) {
velocityX = 0;
velocityY = -1;
}
else if (e.code === "ArrowDown" && velocityY !== -1) {
velocityX = 0;
velocityY = 1;
}
else if (e.code === "ArrowLeft" && velocityX !== 1) {
velocityX = -1;
velocityY = 0;
}
else if (e.code === "ArrowRight" && velocityX !== -1) {
velocityX = 1;
velocityY = 0;
}
}
function placeFood() {
foodX = Math.floor(Math.random() * cols) * blockSize;
foodY = Math.floor(Math.random() * rows) * blockSize;
}
function resetGame() {
snakeX = blockSize * 5;
snakeY = blockSize * 5;
velocityX = 0;
velocityY = 0;
snakeBody = [];
score = 0;
gameOver = false;
scoreElement.innerHTML = `Score: ${score} | High Score: <span id="highScore">${highScore}</span>`;
placeFood();
}
restartBtn.addEventListener("click", resetGame);