-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.html
More file actions
151 lines (128 loc) · 3.72 KB
/
snake.html
File metadata and controls
151 lines (128 loc) · 3.72 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport",
content="width=device-width, initial-scale=1.0">
<title>test</title>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<canvas id="board"></canvas>
</body>
<style>
/* Write CSS Here */
body {
text-align: center;
}
.geeks {
font-size: 40px;
font-weight: bold;
color: green;
}
</style>
<script>
let blockSize = 25;
let total_row = 17; //total row number
let total_col = 17; //total column number
let board;
let context;
let snakeX = blockSize * 5;
let snakeY = blockSize * 5;
// Set the total number of rows and columns
let speedX = 0; //speed of snake in x coordinate.
let speedY = 0; //speed of snake in Y coordinate.
let snakeBody = [];
let foodX;
let foodY;
let gameOver = false;
window.onload = function () {
// Set board height and width
board = document.getElementById("board");
board.height = total_row * blockSize;
board.width = total_col * blockSize;
context = board.getContext("2d");
placeFood();
document.addEventListener("keyup", changeDirection); //for movements
// Set snake speed
setInterval(update, 1000 / 10);
}
function update() {
if (gameOver) {
return;
}
// Background of a Game
context.fillStyle = "green";
context.fillRect(0, 0, board.width, board.height);
// Set food color and position
context.fillStyle = "yellow";
context.fillRect(foodX, foodY, blockSize, blockSize);
if (snakeX == foodX && snakeY == foodY) {
snakeBody.push([foodX, foodY]);
placeFood();
}
// body of snake will grow
for (let i = snakeBody.length - 1; i > 0; i--) {
// it will store previous part of snake to the current part
snakeBody[i] = snakeBody[i - 1];
}
if (snakeBody.length) {
snakeBody[0] = [snakeX, snakeY];
}
context.fillStyle = "white";
snakeX += speedX * blockSize; //updating Snake position in X coordinate.
snakeY += speedY * blockSize; //updating Snake position in Y coordinate.
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 > total_col * blockSize
|| snakeY < 0
|| snakeY > total_row * blockSize) {
// Out of bound condition
gameOver = true;
alert("Game Over");
}
for (let i = 0; i < snakeBody.length; i++) {
if (snakeX == snakeBody[i][0] && snakeY == snakeBody[i][1]) {
// Snake eats own body
gameOver = true;
alert("Game Over");
}
}
}
// Movement of the Snake - We are using addEventListener
function changeDirection(e) {
if (e.code == "ArrowUp" && speedY != 1) {
// If up arrow key pressed with this condition...
// snake will not move in the opposite direction
speedX = 0;
speedY = -1;
}
else if (e.code == "ArrowDown" && speedY != -1) {
//If down arrow key pressed
speedX = 0;
speedY = 1;
}
else if (e.code == "ArrowLeft" && speedX != 1) {
//If left arrow key pressed
speedX = -1;
speedY = 0;
}
else if (e.code == "ArrowRight" && speedX != -1) {
//If Right arrow key pressed
speedX = 1;
speedY = 0;
}
}
// Randomly place food
function placeFood() {
// in x coordinates.
foodX = Math.floor(Math.random() * total_col) * blockSize;
//in y coordinates.
foodY = Math.floor(Math.random() * total_row) * blockSize;
}
</script>
</html>