nodeJS贪吃蛇

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;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.js

module.exports = {
    time : 150,
    startNum : 3
}
    原文作者:恃爱行凶
    原文地址: https://segmentfault.com/a/1190000018151058
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞