run.js (須要本身先用npm下載keypress這個包 用 npm install keypress --save 下載)shell
若是想在xshell上玩須要把空格和方塊改下 (在windows的控制檯用的話一個方塊是兩個字符,在xshell上不知道爲啥是一個字符,還須要修改下xshell上文本顯示的樣式顯示纔會正常)npm
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;i<snakes.length-1;i++) { snakes[i].top = snakes[i+1].top snakes[i].left = snakes[i+1].left } var first = snakes.length-1 snakes[first].top = snakes[first].top+stepY; snakes[first].left = snakes[first].left+stepX; if(!isLive(snakes[first].top,snakes[first].left)) { live = false; console.log("遊戲結束") } else { eatFood(snakes[first].top,snakes[first].left); var snakes1 = [].concat(snakes); snakes1.push(food); DawnCanvas(snakes1) setTimeout(function(){ SnakeMove() },time); } } } function isLive(top,left){ if(top>24||top<0||left>24||left<0) { return false; } else { for(var i=0;i<snakes.length-1;i++) { if(top==snakes[i].top&&left==snakes[i].left) { return false; break; } } return true; } } function eatFood(top,left){ //是否吃到食物 if(top==food.top&&left==food.left) { score++; snakes.push({top:top,left:left}) createFood(); } } function createFood(){ //建立食物 isCreate = true do { food={ top:Math.round(Math.random()*24), left:Math.round(Math.random()*24) } for(var i=0;i<snakes.length;i++) { if(food.top==snakes[i].top&&food.left==snakes[i].left) { isCreate = false; break; } else { isCreate = true; } } } while(!isCreate) } function DawnSnake(startNum) //初始化蛇 { for(let i=0;i<startNum;i++) { var item = { top:0, left:i } snakes.push(item); } } function DawnCanvas(items1) //繪圖遊戲界面 { var isDawn = false; var canvas = ''; canvas += '┍┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┑\n'; for(let i=0;i<25;i++) { canvas += '│ '; for(let j=0;j<25;j++) { isDawn = false; for(let item of items1) { if(item.top == i && item.left == j) { canvas += '■'; isDawn = true; } if(isDawn) { break; } } if(!isDawn) { canvas += ' '; } } canvas+='│ \n'; } canvas += '┕┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┚\n'; process.stdout.write('\033[2J'); //清空控制檯 console.log(canvas) if(!live) { console.log('按任意鍵開始遊戲....') } else { console.log("得分:",score) } } const Rand = function(Min,Max){ var Range = Max - Min; var Rand = Math.random(); var num = Min + Math.round(Rand * Range); //四捨五入 return num; }
config.jscanvas
module.exports = { time : 150, startNum : 3 }