-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
81 lines (74 loc) · 2.54 KB
/
index.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
#!/usr/bin/env node
"use strict";
const Canvas = require("drawille-canvas");
const blessed = require('blessed');
const screen = blessed.screen({
fastCSR: true
});
let c;
let flush;
let canvas = new Canvas();
c = canvas.getContext('2d');
if (typeof document !== 'undefined') {
document.body.appendChild(canvas);
flush = function() {};
}
else {
flush = function() {
console.log(c.toString());
};
}
const snake = {
body: [
{x:10,y:10},
{x:11,y:10},
{x:12,y:10},
{x:13,y:10},
{x:14,y:10},
],
direction: "left",
};
screen.key("w",(ch,key)=>snake.direction!="down"?snake.direction="up":0);
screen.key("s",(ch,key)=>snake.direction!="up"?snake.direction="down":0);
screen.key("a",(ch,key)=>snake.direction!="right"?snake.direction="left":0);
screen.key("d",(ch,key)=>snake.direction!="left"?snake.direction="right":0);
screen.key(["esc","C-c"],()=> process.exit(0));
let fruits = [
{x:100,y:100}
];
let i = 0;
function move(){
const eaten = fruits.filter(f => f.x-snake.body[0].x==0&&f.y-snake.body[0].y == 0).length > 0?0:1;
fruits = fruits.filter(f => f.x-snake.body[0].x!=0||f.y-snake.body[0].y != 0);
const collisionMap = snake.body.reduce((acc,curr)=>{
acc[JSON.stringify(curr)] = acc[JSON.stringify(curr)]!=undefined?acc[JSON.stringify(curr)]+1:1;
return acc;
},{});
if(Object.values(collisionMap).filter(v => v > 1).length > 0) process.exit(0);
switch(snake.direction){
case "up":
snake.body = [{x:snake.body[0].x,y:(snake.body[0].y-1+canvas.height)%canvas.height},...snake.body.slice(0,snake.body.length-eaten)];
break
case "down":
snake.body = [{x:snake.body[0].x,y:(snake.body[0].y+1+canvas.height)%canvas.height},...snake.body.slice(0,snake.body.length-eaten)];
break
case "left":
snake.body = [{x:(snake.body[0].x-1+canvas.width)%canvas.width,y:snake.body[0].y},...snake.body.slice(0,snake.body.length-eaten)];
break
case "right":
snake.body = [{x:(snake.body[0].x+1+canvas.width)%canvas.width,y:snake.body[0].y},...snake.body.slice(0,snake.body.length-eaten)];
break
}
if(i%100 == 0){
fruits.push({x:Math.round(Math.random()*canvas.width),y:Math.round(Math.random()*canvas.height)})
}
i++;
}
function draw() {
c.clearRect(0, 0, canvas.width, canvas.height);
snake.body.forEach(e => c.strokeRect(e.x,e.y, 0, 0))
fruits.forEach(f => c.strokeRect(f.x,f.y, 0, 0))
flush();
}
setInterval(draw, 1000/15);
setInterval(move, 1000/30);