美文网首页
JS的贪吃蛇

JS的贪吃蛇

作者: Issac357 | 来源:发表于2017-10-14 15:57 被阅读0次

在开始的时候我们需要做一个游戏引擎,用来实现蛇体运行的平台和蛇,和果子,以及开始游戏,结束游戏。
游戏引擎

var gGameBox={
  rows:20,//行数
cols:20,//列数
allTds: [], // 存储所有的td元素对象

    food: null, // 食物对象

    snake: null, // 蛇对象
    
    timer: null, // 计时器
    clear:function(){
        for (var i = 0;i<gGameBox.allTds.length ; i++)
        {
            for (var j = 0 ;j<gGameBox.allTds[i].length ; j++)
            {
                gGameBox.allTds[i][j].className = "";
            }
        }
    },
keyControl:function(){
        window.onkeydown=function(e){
            var c =e.keyCode
            
            if (c==37)
            {
                if (gGameBox.snake.direct == "right")
                {
                    return;
                }
                gGameBox.snake.direct = "left";
            }
            else if (c==38)
            {
                if (gGameBox.snake.direct == "down")
                {
                    return;
                }
                gGameBox.snake.direct = "top";
            }
            else if (c==39)
            {
                if (gGameBox.snake.direct == "left")
                {
                    return;
                }
                gGameBox.snake.direct = "right";
            }
            else if (c==40)
            {
                if (gGameBox.snake.direct == "top")
                {
                    return;
                }
                gGameBox.snake.direct = "down";
            }
        }
    },

    // 方法: 游戏开始
    start: function() {

        gGameBox.init(); // 游戏初始化
        
        gGameBox.food = new Food(); // 创建食物
        gGameBox.snake = new Snake(); // 创建蛇

        gGameBox.keyControl();
        // 启动游戏时,定时移动蛇
    gGameBox.timer = setInterval(function() {

            // 1. 清空棋盘
            gGameBox.clear();
            
            // 2. 蛇移动
            gGameBox.snake.move();

            // 3. 显示食物
            gGameBox.food.show();

        }, 1000);

        //gGameBox.snake.fresh();
    },

        // 初始化
    init: function() {
        // 场景布置好, 用表格来做
        var oTable = document.createElement("table");
    
        for (var i = 0; i < gGameBox.rows; i++)
        {
            // 创建tr
            var oTr = document.createElement("tr"); 

            // 每一行,定义1个空数组
            var arr = [];

            for (var j = 0; j < gGameBox.cols; j++) {
                // 创建td
                var oTd = document.createElement("td"); 

                oTr.appendChild(oTd);

                // 将td放到空数组中
                arr.push(oTd);
            }
            // 将当前行所有的td,压入到 allTds 属性中
            gGameBox.allTds.push(arr);

            oTable.appendChild(oTr);
        }

        // 添加到body
        document.body.appendChild(oTable);
    }

}

food的实现

function Food() {

    // 坐标
    this.x = 0;
    this.y = 0;

    // 一开始就随机位置
    this.change();
}

// 方法1: 出现在环境中
Food.prototype.show = function() {
    gGameBox.allTds[this.y][this.x].className = "food";
}

// 方法2: 改变位置, 随机的
Food.prototype.change = function() {
    this.x = parseInt(Math.random() * gGameBox.cols);
    this.y = parseInt(Math.random() * gGameBox.rows);

    this.show();
}

蛇体的实现

function Snake() {

    // 存储蛇 所有节点坐标, 同时也存储了蛇的长度  this.arr.length
    //  默认蛇头  this.arr[0]  节点
    this.arr = [
        {x: 5, y: 1},
        {x: 4, y: 1},
        {x: 3, y: 1},
        {x: 2, y: 1},
        {x: 1, y: 1}
    ];

    // 当前移动方向:   left, right, down, up
    this.direct = "down";

    // 创建完就刷新到页面上
    this.fresh();
}



// 方法1: 更新到页面上  fresh 刷新  
Snake.prototype.fresh = function() {
    // 给所有蛇节点,都添加样式
    for (var i = 0; i < this.arr.length; i++)
    {
        // this.arr[i] 是蛇节点对象
        var x = this.arr[i].x;
        var y = this.arr[i].y;

        gGameBox.allTds[y][x].className = "snake"
    }
}

// 方法2: 移动
Snake.prototype.move = function() {

    // 蛇头坐标
    var x = this.arr[0].x;
    var y = this.arr[0].y;

    // 思路: 根据当前蛇的方向,来分情况处理
    if (this.direct == "right")
    {
        //   4      3       2     1      0
        // (1,1) (2,1) (3,1) (4,1) (5,1)
        // (2,1) (3,1) (4,1) (5,1) (6,1)
        //       (2,1) (3,1) (4,1) (5,1) (6,1)
        //  在 蛇头 增加新点 (6,1), 删除蛇尾
        x++;

    }
    else if (this.direct == "left")
    {
        x--
    }
    else if (this.direct == "top")
    {
        y--;
    }
    else if (this.direct == "down")
    {
        y++;
    }

    /*if (x<0)
    {
        //x=gGameBox.cols-1
            this.direct ="down"
        
    }*/
    if (x<0||y<0||x>gGameBox.rows||y>gGameBox.cols)
    {
        alert("gg");
        clearInterval(gGameBox.timer);
        return;
    }

    if (x==gGameBox.food.x && y== gGameBox.food.y)
    {
        this.arr.unshift({x: x, y: y});
        gGameBox.food.change();
        this.fresh();

        return;
    }

    // 在蛇头增加新点
    this.arr.unshift({x: x, y: y});

    // 删除蛇尾
    this.arr.pop();

    // 将新蛇刷新到页面上
    this.fresh();
}

相关文章

  • 贪吃蛇

    js 贪吃蛇代码

  • JS高级学习:贪吃蛇案例

    键盘按下事件 案例:贪吃蛇 Food.js Snake.js Game.js

  • 贪吃蛇练习(html 、JS)

    用JS完成一个 贪吃蛇,在HTML中写写内容,CSS中写样式,JS中写动作,基本可以实现贪吃蛇这个简单的操作。

  • 前端大神作品推荐

    js开发实现简单贪吃蛇游戏(20行代码)

  • 不小心用js重做了一遍贪吃蛇

    贪吃蛇游戏想必没人会感到陌生,这个游戏的js版本在网上也是一搜一大把,今天我要介绍的仍然是如何用js做一个贪吃蛇游...

  • JS的贪吃蛇

    在开始的时候我们需要做一个游戏引擎,用来实现蛇体运行的平台和蛇,和果子,以及开始游戏,结束游戏。游戏引擎 food...

  • JS编写的贪吃蛇Dome

    最近在家玩了贪吃蛇大作战,突发兴致下就想到用JS来写一个贪吃蛇的简单Dome,这里并没有用canvas来编写,有空...

  • 贪吃蛇

    贪吃蛇效果: 键盘的w、s、a、d分别来控制蛇移动方向:上、下、左、右js代码:

  • WEB 八

    JS 面向对象编程 利用JS面向对象编程写一个贪吃蛇小游戏 思路:地图->蛇->让蛇运动->用键盘控制蛇运动->食...

  • js版贪吃蛇

    这家伙想通了其实超级简单 原理,主要就是绘制食物和蛇咯,食物出现的位置需要检测(不出现在蛇身上),至于蛇(蛇身存为...

网友评论

      本文标题:JS的贪吃蛇

      本文链接:https://www.haomeiwen.com/subject/lxktuxtx.html