run.js (需要自己先用npm下载keypress这个包 用 npm install keypress --save 下载)
如果想在xshell上玩需要把空格和方块改下 (在windows的控制台用的话一个方块是两个字符,在xshell上不知道为啥是一个字符,还需要修改下xshell上文本显示的样式显示才会正常)
const config = require('./config');const keypress = require('keypress');var live = false;var snakes = [];var food = {};var score = 0;var keyDown = 'right';var stepX = 1;var stepY = 0;const startNum = config.startNum;const time = config.time;DawnCanvas([{top:0,left:0},{top:10,left:0}])keypress(process.stdin); //监听键盘输入process.stdin.on('keypress', function (ch, key) { //console.log('got "keypress"', key); if (key && key.ctrl && key.name == 'c') { process.abort(); } if(key) { if(!live) { start(); } else { //console.log(keyDown) if(key.name == "up" || key.name == "w") { if(keyDown != 'down') { keyDown = 'up'; stepX = 0; stepY = -1; } } else if(key.name == "down" || key.name == "s") { if(keyDown != 'up') { keyDown = 'down'; stepX = 0; stepY = 1; } } else if(key.name == "left" || key.name == "a") { if(keyDown != 'right') { keyDown = 'left'; stepX = -1; stepY = 0; } } else if(key.name == "right" || key.name == "d") { if(keyDown != 'left') { keyDown = 'right'; stepX = 1; stepY = 0; } } } }});process.stdin.setRawMode(true);process.stdin.resume();function start(){ snakes = []; stepX = 1; stepY = 0; keyDown = 'right'; live = true; score = 0; DawnSnake(startNum); createFood(); SnakeMove();}function SnakeMove() { if(live){ for(var i=0;i24||top<0||left>24||left<0) { return false; } else { for(var i=0;i
config.js
module.exports = { time : 150, startNum : 3}