码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript—面向对象 贪吃蛇_2 游戏对象

时间:2019-05-09 20:00:42      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:int   dde   break   color   foo   als   nbsp   his   case   

游戏对象

function Game(map) {
    this.map = map;
    this.food = new Food(this.map)
    this.snake = new Snake(this.map)
}

Game.prototype.go = function () {
    let food=this.food
    let snake=this.snake;
    food.show()
    snake.show()
    let go_time = setInterval(function () {
        console.log(snake,food)

        if (snake.gameif){
            clearInterval(go_time)
        }
        document.addEventListener(‘keydown‘, function (e) {
            //结束游戏。game over
            // up:38 down:40 left:37 reight:39
            switch (e.keyCode) {
                case 37: {
                    //禁止反方向
                    if (snake.direction==‘right‘){
                        break;
                    }
                    snake.direction=‘left‘
                    break;
                }
                case 38: {
                    if (snake.direction==‘down‘){
                        break;
                    }
                    snake.direction=‘up‘
                    break;
                }
                case 39: {
                    if (snake.direction==‘left‘){
                        break;
                    }
                    snake.direction=‘right‘
                    break;
                }
                case 40: {
                    if (snake.direction==‘up‘){
                        break;
                    }
                    snake.direction=‘down‘
                    break;
                }
            }
        }, false)

        snake.udlr()
        snake.show()
        snake.feed(food)

    }, 100)


}

 

JavaScript—面向对象 贪吃蛇_2 游戏对象

标签:int   dde   break   color   foo   als   nbsp   his   case   

原文地址:https://www.cnblogs.com/ruogu/p/10840391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!