-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.js
129 lines (112 loc) · 3.17 KB
/
snake.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
var frame = document.querySelector(".frame")
var size = frame.offsetWidth/20;
for(var i = 0; i < 20; i++){
var row = document.createElement("DIV");
row.style.width = frame.offsetWidth + "px";
row.style.height = size + "px";
row.style.display = "flex";
row.className = "rows";
frame.appendChild(row);
for(var j = 0; j < 20; j++){
var column = document.createElement("DIV");
column.style.width = size + "px";
column.style.height = row.offsetHeight + "px";
column.style.backgroundColor = "#52d952";
column.className = "columns";
row.appendChild(column);
}
}
var rows = frame.querySelectorAll(".rows");
var snake = [rows[Math.floor(Math.random() * 20)].children[Math.floor(Math.random() * 20)]];
snake[0].style.backgroundColor = "#0a0aa8";
var food = rows[Math.floor(Math.random() * 20)].children[Math.floor(Math.random() * 20)];
food.style.backgroundColor = "red";
function go(event) {
var row = snake[0].parentElement;
var index;
for(var i = 0; i < row.children.length; i++){
if(row.children[i] == snake[0]){
index = i;
break;
}
}
if (event.key == "ArrowUp") {
row = row.previousElementSibling;
direction(row.children[index]);
}
else if (event.key == "ArrowDown") {
if(row.nextElementSibling){
direction(row.nextElementSibling.children[index]);
}else {
window.alert("Game over!");
start();
}
}
else if (event.key == "ArrowLeft") {
direction(row.children[index-1]);
}
else if (event.key == "ArrowRight") {
direction(row.children[index+1]);
}
}
var score = document.getElementsByTagName("span")[0];
function move(newPos) {
if(snake[1] != newPos){
for(var i = snake.length-1; i > 0; i--){
snake[i] = snake[i-1];
}
snake[0] = newPos;
snake[0].style.backgroundColor = "#0a0aa8";
}
}
function direction(newHead) {
if(typeof newHead != 'undefined'){
var tail = snake[snake.length-1];
move(newHead);
checkCrossOver();
checkTail(tail);
}else {
window.alert("Game over!");
start();
}
}
function checkTail(part) {
if(eatFood()){
snake.push(part);
}else if(part == food){
part = food;
part.style.backgroundColor = "red";
}else if(!snake.includes(part)){
part.style.backgroundColor = "#52d952";
}
}
function eatFood() {
if (snake[0] == food) {
while(snake.includes(food)){
food = rows[Math.floor(Math.random() * 20)].children[Math.floor(Math.random() * 20)];
}
score.innerHTML = String(parseInt(score.innerHTML) + 10);
food.style.backgroundColor = "red";
return true;
}
}
function start() {
var columns = document.querySelectorAll(".columns");
for(var i = 0; i < columns.length; i++){
columns[i].style.backgroundColor = "#52d952";
}
snake = [rows[Math.floor(Math.random() * 20)].children[Math.floor(Math.random() * 20)]];
snake[0].style.backgroundColor = "#0a0aa8";
food = rows[Math.floor(Math.random() * 20)].children[Math.floor(Math.random() * 20)];
food.style.backgroundColor = "red";
score.innerHTML = "0";
}
function checkCrossOver() {
for(var i = 1; i < snake.length; i++){
if(snake[0] == snake[i]){
window.alert("Game over!");
start();
break;
}
}
}