贪吃蛇

作者: 向北_f098 | 来源:发表于2017-10-16 20:54 被阅读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() {
// onkeydown
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 = "up";
        }
        else if (c == 39)
        {
            if (gGameBox.snake.direct == "left")
            {
                return ;
            }
            gGameBox.snake.direct = "right";
        }
        else if (c == 40)
        {
            if (gGameBox.snake.direct == "up")
            {
                return ;
            }
            gGameBox.snake.direct = "down";
        }
    }
},

start: function() {

    gGameBox.init(); // 游戏初始化
    
    gGameBox.food = new Food(); // 创建食物
    gGameBox.snake = new Snake(); // 创建蛇
    gGameBox.keyControl();
    gGameBox.timer = setInterval(function() {
        gGameBox.clear();
        gGameBox.snake.move();
        gGameBox.food.show();
    }, 500);

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

···
## 初始化
···init: function() {
var oTable = document.createElement("table");
for (var i = 0; i < gGameBox.rows; i++)
{
r oTr = document.createElement("tr");
var arr = [];
for (var j = 0; j < gGameBox.cols; j++) {
var oTd = document.createElement("td");
arr.push(oTd);
}
gGameBox.allTds.push(arr);

        oTable.appendChild(oTr);
    }
    document.body.appendChild(oTable);
}

};···

相关文章

  • 贪吃蛇巴士 for Mac(休闲街机游戏) v0.1

    贪吃蛇巴士(snakeybust)是将经典街机游戏“贪吃蛇”用现代图形来展现的休闲街机游戏。贪吃蛇巴士Mac版游戏...

  • 贪吃蛇背后的故事

    贪吃蛇介绍 贪吃蛇最先出现于1976年,是Gremlin平台推出的一款经典街机游戏Blockade。贪吃蛇(也叫做...

  • 贪吃蛇大结局

    童年的回忆,贪吃蛇大结局,看一下贪吃蛇最后变成了什么?

  • JS-进阶-Day2

    贪吃蛇案例:

  • 贪吃蛇

    js 贪吃蛇代码

  • 我的贪吃蛇

    最近一直在玩一个游戏――贪吃蛇大作战,为此,耗费我不少时间。 我给我的贪吃蛇命名:我的贪吃蛇wr,后面wr是我名字...

  • 萌萌小蛇蛇

    这绝对是一个会令你振奋的贪吃蛇游戏,支持多人在线的贪吃蛇游戏。好好想想,你是否已经腻歪了单纯的吃食物变长的贪吃蛇游...

  • 一只贪吃蛇的自白

    我是一只贪吃蛇,想必大家都玩儿过贪吃蛇的游戏,那里面的贪吃蛇吃的东西越多,身体就越长,最后把自己给弄死了。我跟他们...

  • 2017-06-03

    贪吃蛇啊, 贪吃蛇, 你贪吃的是你的食物, 我贪吃的是对世间的留恋, 美女, 金钱, 路上的风景……

  • Web前端------JS高级贪吃蛇游戏案例(主要内容:原型)

    贪吃蛇案例思想总结 贪吃蛇游戏主要包括:地图对象、蛇对象、食物对象、游戏对象(控制游戏执行、操控小蛇和食物对象),...

网友评论

      本文标题:贪吃蛇

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