javascript实现贪吃蛇经典游戏(2)

window.addEventListener('load', function() { (function() { //改变计时器内this指向 var that; function Game(map) { // var options = { x: 20, y: 20, width: 30, height: 30, color: 'green' }; this.food = new Food(); this.snake = new Snake(); this.map = map; that = this; } //cwen渲染 Game.prototype.start = function() { // 1.把食物和蛇渲染到页面 this.food.render(this.map); this.snake.render(this.map); // 2.游戏逻辑编写 //让蛇动起来 //判断地图边界 // runSnake(); //todo判断玩法(两种模式,原理一样) goInput(); //通过键盘控制蛇头方向 //! keydown(); //蛇头碰到食物时处理 //在snake.js中判断 } function goInput() { var it = prompt('try:\n 经典玩法请按1\n 无敌玩法请输入(博主最帅)\n') if (it == 1) { runSnake(); keydown(); } else if (it == '博主最帅') { runSnake1(); keydown1(); } else { alert('you input could not be found!!!'); goInput(); } } //让蛇动起来 function runSnake() { var timeId = setInterval(function() { // var a = mark; that.snake.move(that.food, that.map); that.snake.render(that.map); //判断地图边界 var maxX = (that.map.offsetWidth) / that.snake.width; var maxY = (that.map.offsetHeight) / that.snake.height; var headX = that.snake.kont[0].x; var headY = that.snake.kont[0].y; if (headX < 0 || headX >= maxX) { alert('Game Over ' + '得分为 ' + that.snake.mark); clearInterval(timeId); } else if (headY < 0 || headY >= maxY) { alert('Game Over ' + '成绩为 ' + that.snake.mark); clearInterval(timeId); } }, 150) } //无敌版本蛇运动 function runSnake1() { var timeId1 = setInterval(function() { that.snake.move(that.food, that.map); that.snake.render(that.map); //判断地图边界 var maxX = (that.map.offsetWidth - that.snake.width) / that.snake.width; var maxY = (that.map.offsetHeight - that.snake.height) / that.snake.height; var headX = that.snake.kont[0].x; var headY = that.snake.kont[0].y; if (headX < 0) { that.snake.kont[0].x = (that.map.offsetWidth - that.snake.width) / that.snake.width; } else if (headX > maxX) { that.snake.kont[0].x = 0; } else if (headY < 0) { that.snake.kont[0].y = (that.map.offsetHeight - that.snake.height) / that.snake.height; } else if (headY > maxY) { that.snake.kont[0].y = 0; } }, 50) } //通过键盘控制蛇头方向 function keydown() { document.addEventListener('keydown', function(e) { //通过事件对象判断按了哪个键 37left,38top,39right,40bottom // console.log(e.keyCode); //其在走的同时按下反方向无用 if (e.keyCode == 37 && that.snake.direction != 'right') { that.snake.direction = 'left'; } else if (e.keyCode == 38 && that.snake.direction != 'bottom') { that.snake.direction = 'top'; } else if (e.keyCode == 39 && that.snake.direction != 'left') { that.snake.direction = 'right'; } else if (e.keyCode == 40 && that.snake.direction != 'top') { that.snake.direction = 'bottom'; } }); } function keydown1() { document.addEventListener('keydown', function(e) { //通过事件对象判断按了哪个键 37left,38top,39right,40bottom // console.log(e.keyCode); //无敌版本四面八方任你行 if (e.keyCode == 37) { that.snake.direction = 'left'; } else if (e.keyCode == 38) { that.snake.direction = 'top'; } else if (e.keyCode == 39) { that.snake.direction = 'right'; } else if (e.keyCode == 40) { that.snake.direction = 'bottom'; } }); } //把Game开放 window.Game = Game; })() })

5)main开启游戏

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/02893c60847b5e012bbc4a866a80de95.html