游戏对象
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) }