资讯专栏INFORMATION COLUMN

回归原生,使用javascript编写小游戏 --- 贪食蛇

AlienZHOU / 3002人阅读

摘要:将应用抽象成一个对象。地图使用一个二维数组作为结构。生成食物的范围。蛇碰到墙壁计算出穿过墙的范围蛇碰到自己的身体蛇吃到食物,长度加一并生成新的食物监听键盘事件。对上下左右移动做出反应。

需求分析

生成地图。

将应用抽象成一个对象。

地图使用一个二维数组作为结构。

生成食物。

生成食物的范围。

食物不能和身体生成位置重合。

生成蛇,开始移动。

蛇碰到墙壁,计算出穿过墙的范围.

蛇碰到自己的身体,Game Over .

蛇吃到食物,长度加一,并生成新的食物

监听键盘事件。

对上下左右移动做出反应。

代码实现(也可以查看 github ) html (index.html)



    
    
    
    Document
    


    
css (./style/index.css)
* {
    margin: 0;
    padding: 0;
}

.wrap{
    display: flex;
    justify-content: center;
    align-content: center;
}

#container{
    width: 100%;
    text-align: center;
}

#container div{
    /* width: 20px;
    height: 20px; */
    float: left;
    border: 1px solid #000;
}
js(./js/index.js)
    (function(self){

    function GreedySnake(gridXN,gridYN){
        // 地图
        this.gridXN = gridXN >= 10 ? gridXN : 10; 
        this.gridYN = gridYN >= 10 ? gridYN : 10;
        this.gridSize = 20; //每个格子的固定大小
        this.map = []; //保存地图中dom对象的二维数组
        this.container = document.getElementById("container");
        

        // 食物属性
        this.food = null;
        this.foodX = null;
        this.foodY = null;

        // 蛇
        this.snake = [];//将蛇抽象成一个二维列表,列为身体长度,排为坐标数组 
        this.snakeLen = null ; //默认蛇长为3
        // 初始蛇头坐标
        this.snakeHead = []

        // 蛇的移动方向
        this.direction = "top"

        // 速度
        this.speed = 1 ;

        this.timer = null
    }

    GreedySnake.prototype.init = function(){
        this.initMap();
        this.createSnake();
        this.createFood();
        this.snakeMove()
        this.listenKeyBoardEvents()
    }

    GreedySnake.prototype.restart = function(gridXN,gridYN){
    
        // 地图
        this.gridXN = null
        this.gridYN = null
        this.gridSize = null
        this.map = null
        this.container = null
        
        // 食物属性
        this.food = null;
        this.foodX = null;
        this.foodY = null;

        // 蛇
        this.snake = null;
        this.snakeLen = null; 
        // 初始蛇头坐标
        this.snakeHead = null;

        // 蛇的移动方向
        this.direction = null;

        // 速度
        this.speed = null;

    }


    // 初始化地图
    GreedySnake.prototype.initMap = function(){
        var gridYN = this.gridYN,
            gridXN = this.gridXN,
            w = gridXN * this.gridSize + gridXN + 1 ,
            h = gridYN * this.gridSize + gridYN + 1;
        
        this.container.style.width = w + "px";
        this.container.style.height = h  + "px";

        for(let y = 0 ; y < gridXN ; y++){
            this.map[y] = [] //初始化二维数组
            for(let x = 0 ; x < gridYN ; x++){
                this.createGrid(x,y)
            }
        }   
    }
    // 创建每一个格子,x轴格子索引,y轴格子索引
    GreedySnake.prototype.createGrid = function(idxX,idxY){
        // 当idxY > 0 时,对应的grid的向左移动1px,将格子之间的线重合;
        // 当idxX > 0 时,对应的grid的向上移动1px,将格子之间的线重合;
        let grid = document.createElement("div");
        grid.style.width = this.gridSize + "px";
        grid.style.height = this.gridSize + "px";
        this.map[idxY][idxX] = grid
        if(idxX > 0 ){
            grid.style.marginLeft = "-1px";
        }
        if(idxY > 0 ){
            grid.style.marginTop = "-1px";
        }
        this.container.appendChild(grid);
    }

    // 创建食物:原理就是根据在范围内的格子数,生成两个随机数,作为元素的坐标
    GreedySnake.prototype.createFood = function(){
        var gridYN = this.gridYN,
            gridXN = this.gridXN,
            temp = null ,
            flag = true;
        this.foodX = Math.floor(Math.random() * gridXN); //缓存
        this.foodY = Math.floor(Math.random() * gridYN);

        for(var i = 0 ; i= 0; i--){
            this.snake.push([this.gridYN - 1 - i ,this.gridXN - 1 - 3])
            this.map[this.gridYN - 1 - i][this.gridXN - 1 - 3].style.backgroundColor = "blue"
        }
        // 初始蛇头坐标
        this.snakeHead = this.snake[0]
        // console.log(this.snake)
    }
    
    // 开始移动,移动后蛇头坐标 +-1 
    GreedySnake.prototype.snakeMove = function(){
        let _this = this,
            sH = this.snakeHead,
            y = null,
            x = null,
            tempH = null,
            last = null,
            alive = true

        function common(){
            
            _this.map[tempH[0]][tempH[1]].style.backgroundColor = "blue"
            _this.snake.unshift(tempH);
            _this.snakeHead = tempH;
            // 检测蛇头是否和蛇的身体相撞
            for(var i = 1 ; i < _this.snake.length;i++){
                if(_this.snakeHead[0] == _this.snake[i][0] && _this.snakeHead[1] ==  _this.snake[i][2]){
                    alert("GAME OVER");
                    alive = false
                    return false
                }
            }
            // 当蛇吃到食物后再重新创建食物
            if(sH[0] === _this.foodY && sH[1] === _this.foodX){
                _this.createFood()
                return false
            }
            last = _this.snake.pop();
            _this.map[last[0]][last[1]].style.backgroundColor = ""
        }

        switch(this.direction){
            case "top":
                y = sH[0] //缓存
                tempH = [--y , sH[1] ];
                if(tempH[0] < 0){
                    tempH = [this.gridYN - 1 , sH[1]]
                }
                common();
                break;
            case "bottom":
                y = sH[0]
                tempH = [++y , sH[1] ];
                // 边界判断
                if(tempH[0] > this.gridYN - 1){
                    tempH = [ 0 , sH[1]]
                }
                common()
                break;
            case "left":
                x = sH[1]
                tempH = [sH[0] , --x ];
                if(tempH[1] < 0){
                    tempH = [ sH[0] , this.gridXN - 1]
                }
                common()
                break;
            case "right":
                x = sH[1]
                tempH = [sH[0] , ++x ];
                if(tempH[1] > this.gridXN - 1){
                    tempH = [  sH[0] ,  0 ]
                }
                common()
                break;
        }

        alive && setTimeout(function(){
            _this.snakeMove()
        },500 / this.speed)
    }

    // 注册键盘事件
    GreedySnake.prototype.listenKeyBoardEvents = function(){
        let _this = this
        self.onkeyup = function(e){
            let dir = _this.direction
            switch(e.keyCode){
                case 37:
                    if(dir != "right"){
                       _this.direction = "left";
                    }
                    break;
                case 38:
                    if(dir != "bottom"){
                        _this.direction = "top";
                    }
                    break;
                case 39:
                    if(dir != "left"){
                        _this.direction = "right";
                    }
                    break;
                case 40:
                    if(dir != "top"){
                        _this.direction = "bottom";
                    }
                    break;
            }
        }
    }

    GreedySnake.prototype.print = function(){
        return this.map
    }
    
    // 参数含义:x轴,y轴上的格子数量
    var greedySnake = new GreedySnake(20,20);
    greedySnake.init()
})(window)

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/94456.html

相关文章

  • python完成简单的贪吃蛇游戏附编号

      此篇文章主要是详细介绍了python完成简单的贪吃蛇小游戏附编号,文章内容紧扣主题进行详尽的基本介绍,具有很强的参考意义,需用的朋友可以学习一下  序言:  不知道有没有同学们和我一样,最开始触碰程序编程的动机就是为了做一个游戏打?  接下来要跟大家分享是指一个pygame所写的贪食蛇手机游戏:  贪食蛇这一个手机游戏在编程设计里的熟客,由于:  简易,最基本游戏情节你只需要蛇和食物2个就可以...

    89542767 评论0 收藏0
  • 给你的网页游戏添加游戏手柄支持

    摘要:自从买了手柄后一直想试试给自己写的小游戏增加手柄支持。表示该对象所表示的手柄是否还保持连接。。连接事件浏览器提供了两个手柄相关的事件。。完笔者给自己的贪食蛇小游戏增加了手柄摇杆控制蛇头方向功能之前笔者还写过俄罗斯方块之类的,代码找不到了 自从买了 Switch 手柄后一直想试试给自己写的小游戏增加手柄支持。今天终于抽出时间搞了一把。以下是笔记 ;) navigator.getGamep...

    stdying 评论0 收藏0
  • 曾探:爱JavaScript再多,它也只是生活的一部分

    摘要:拿足球比赛的例子来说,我们的目标只是进球,下底传中这种模式仅仅是达到进球目标的一种手段。但在这种动态解释型语言中,给对象动态添加职责是再简单不过的事情。这就造成了语言的装饰者模式不再关注于给对象动态添加职责,而是关注于给函数动态添加职责。 非商业转载请注明作译者、出处,并保留本文的原始链接:http://www.ituring.com.cn/article/199456 曾探...

    cyqian 评论0 收藏0
  • JavaScript 就要统治世界了?

    摘要:欢迎使用中文文档架构概览是网易项目团队开发的一个基于进行开发的应用层框架,提供了一个轻量级的容器来编写简单可维护的。 JavaScript 可以……嘛,不就是操作一下 DOM,可以让元素飞来飞去吗JavaScript 是……不就是用 jQuery 让网页动起来,顶多就是再用用 Ajax 和后端进行一下数据交换吗JavaScript 是一门……最讨厌和鄙视这种弱类型不需要编译的脚本语言...

    AbnerMing 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<