-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
175 lines (138 loc) · 3.81 KB
/
game.js
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
window.onload = function () {
let canvas = document.getElementById('screen');
let ctx = canvas.getContext('2d');
let score = document.getElementById('score');
let canvasWidth = canvas.width;
let canvasHeight = canvas.height;
let boxSize = 10;
let direction = 'right';
let food = createFood();
let snake = createSnake();
let blikFood = true;
let gameOver = false;
let time = 90;
let interval = setInterval(paint, time)
function restartGame() {
food = createFood()
snake = createSnake()
score.innerText = 0;
direction = 'right';
}
function paint() {
ctx.fillStyle = '#212121'
ctx.fillRect(0, 0, canvasWidth, canvasHeight)
ctx.strokeStyle = 'blue';
ctx.lineWidth = 5;
ctx.strokeRect(0, 0, canvasWidth, canvasHeight)
paintFood()
if(gameOver){
snake.forEach((cell, index) => {
if (index == 0) {
paintCell(cell, 'white')
} else {
paintCell(cell)
}
})
return
}
moveSnake(snake, direction)
snake.forEach((cell, index) => {
if (index == 0) {
paintCell(cell, 'white')
} else {
paintCell(cell)
}
})
}
function paintFood() {
if(blikFood) {
paintCell(food, 'red')
} else {
paintCell(food, 'white', 'white')
}
blikFood = !blikFood
}
function moveSnake(snake, direction) {
let snakeHeadX = snake[0].x;
let snakeHeadY = snake[0].y;
let tail = {};
if (direction == 'right') snakeHeadX++;
if (direction == 'left') snakeHeadX--;
if (direction == 'down') snakeHeadY++;
if (direction == 'up') snakeHeadY--;
if(hasCollision(snakeHeadX, snakeHeadY)) {
gameOver = true
restartGame();
return
}
if (snakeHeadX == food.x && snakeHeadY == food.y) {
tail.x = food.x;
tail.y = food.y;
score.innerText++;
/* time -= Math.round(time / 10000);
console.log(time)
clearInterval(interval)
setInterval(paint, time) */
food = createFood();
} else {
tail = snake.pop()
tail.x = snakeHeadX;
tail.y = snakeHeadY;
}
snake.unshift(tail)
}
function createSnake() {
let snake = [];
let snakeLength = 1;
for (let i = snakeLength; i >= 0; i--) {
snake.push({ x: i, y: 0 })
}
return snake
}
function hasCollision(snakeHeadX, snakeHeadY) {
hasCollisionToBody(snake, snakeHeadX, snakeHeadY)
if (
(snakeHeadX == -1) ||
(snakeHeadY == -1) ||
(snakeHeadX == (canvasWidth/boxSize)) ||
(snakeHeadY == (canvasHeight/boxSize)) ||
hasCollisionToBody(snake, snakeHeadX, snakeHeadY)
) {
return true
} else {
return false
}
}
function hasCollisionToBody(snake, nextX, nextY) {
let overlay = false;
snake.forEach((cell, index) => {
if(index == 0) return;
if((cell.x == nextX) && (cell.y == nextY)) {
overlay = true
}
})
return overlay
}
function createFood() {
return {
x: Math.round(Math.random() * (canvasWidth - boxSize) / boxSize),
y: Math.round(Math.random() * (canvasHeight - boxSize) / boxSize)
}
}
function paintCell({ x, y }, color = '#76ff03', borderColor = '#212121') {
let cellWidth = boxSize;
ctx.fillStyle = color
ctx.fillRect(x * cellWidth, y * cellWidth, cellWidth, cellWidth)
ctx.strokeStyle = borderColor
ctx.lineWidth = 1;
ctx.strokeRect(x * cellWidth, y * cellWidth, cellWidth, cellWidth)
}
document.addEventListener('keydown', (key) => {
gameOver = false;
if (key.code == 'Space') gameOver = true;
if (key.code == 'ArrowRight') direction = 'right';
if (key.code == 'ArrowLeft') direction = 'left';
if (key.code == 'ArrowDown') direction = 'down';
if (key.code == 'ArrowUp') direction = 'up';
})
}