-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
148 lines (139 loc) · 3.51 KB
/
main.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
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "#FF0000";
var snakes = new Array();
var snakeNodeNew;
ctx.fillRect(20 + 10 * 20, 20, 19, 20);
ctx.fillRect(20 + 20 * 20, 200, 19, 20);
ctx.fillRect(20 + 10 * 20, 300, 19, 20);
for (let i = 0; i < 10; i++) {
if (i == 9) { ctx.fillStyle = "#00FF00" };
ctx.fillRect(20 + i * 20, 0, 19, 20);
snakes.push({
x: 10 + i * 20,
y: 0,
w: 19,
h: 19
})
}
document.getElementById("codes").innerText = "Hello Snake Man !";
var myVar;
function myStartFunction() {
myVar = setInterval(mainFunc, 500);
console.log("Start...")
}
function mainFunc() {
if (snakes.slice(-2, -1)[0].y != snakes.slice(-1)[0].y) {
if (snakes.slice(-2, -1)[0].y < snakes.slice(-1)[0].y) {
snakeNodeNew = {
x: snakes.slice(-1)[0].x,
y: snakes.slice(-1)[0].y + snakes.slice(-1)[0].h,
w: 19,
h: 19
}
} else {
snakeNodeNew = {
x: snakes.slice(-1)[0].x,
y: snakes.slice(-1)[0].y - snakes.slice(-1)[0].h,
w: 19,
h: 19
}
}
} else {
if (snakes.slice(-2, -1)[0].x < snakes.slice(-1)[0].x) {
snakeNodeNew = {
x: snakes.slice(-1)[0].x + snakes.slice(-1)[0].w,
y: snakes.slice(-1)[0].y,
w: 19,
h: 19
}
} else {
snakeNodeNew = {
x: snakes.slice(-1)[0].x - snakes.slice(-1)[0].w,
y: snakes.slice(-1)[0].y,
w: 19,
h: 19
}
}
}
if (!snakes.map((item) => {
return JSON.stringify(item)
}).includes(JSON.stringify(snakeNodeNew))) {
var imgData = ctx.getImageData(snakeNodeNew.x, snakeNodeNew.y, snakeNodeNew.w, snakeNodeNew.h);
if (imgData.data[0] === 255) snakes.push(snakeNodeNew);
else {
snakes.shift();
snakes.push(snakeNodeNew);
}
}
ctx.clearRect(0, 0, 500, 500);
for (let snake of snakes) {
if (snake === snakes.slice(-1)[0]) {
ctx.fillStyle = "#FF0000";
ctx.fillRect(snake.x, snake.y, 18, 18);
} else {
ctx.fillStyle = "#00FF00";
ctx.fillRect(snake.x, snake.y, 18, 18);
}
}
}
function myStopFunction() {
clearInterval(myVar);
console.log("Stop...")
}
document.addEventListener("keydown", (e) => {
switch (e.which) {
case 38:
snakeNodeNew = {
x: snakes.slice(-1)[0].x,
y: snakes.slice(-1)[0].y - snakes.slice(-1)[0].h,
w: 19,
h: 19
}
break;
case 39:
snakeNodeNew = {
x: snakes.slice(-1)[0].x + snakes.slice(-1)[0].w,
y: snakes.slice(-1)[0].y,
w: 19,
h: 19
}
break;
case 37:
snakeNodeNew = {
x: snakes.slice(-1)[0].x - snakes.slice(-1)[0].w,
y: snakes.slice(-1)[0].y,
w: 19,
h: 19
}
break;
case 40:
snakeNodeNew = {
x: snakes.slice(-1)[0].x,
y: snakes.slice(-1)[0].y + snakes.slice(-1)[0].h,
w: 19,
h: 19
}
break;
case 32:
myStopFunction()
break;
}
if (!snakes.map((item) => {
return JSON.stringify(item)
}).includes(JSON.stringify(snakeNodeNew))) {
snakes.shift();
snakes.push(snakeNodeNew);
}
document.getElementById("codes").innerText = "Hello Snake Man ! keycode:" + e.which.toString();
ctx.clearRect(0, 0, 500, 500);
for (let snake of snakes) {
if (snake === snakes.slice(-1)[0]) {
ctx.fillStyle = "#FF0000";
ctx.fillRect(snake.x, snake.y, 18, 18);
} else {
ctx.fillStyle = "#00FF00";
ctx.fillRect(snake.x, snake.y, 18, 18);
}
}
})