JavaScript—面向对象 贪吃蛇_2 游戏对象
Posted ruogu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript—面向对象 贪吃蛇_2 游戏对象相关的知识,希望对你有一定的参考价值。
游戏对象
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 游戏对象的主要内容,如果未能解决你的问题,请参考以下文章