js实现贪吃蛇游戏(简易版)

这篇文章主要为大家详细介绍了js实现贪吃蛇游戏简易版,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了js实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下

直接开始

效果图:

项目结构:图片自己找的

1.html

    

最高分: 0

分数: 0

2.area.js

 function render(x, y, color, img) { ctx.beginPath(); ctx.fillStyle = color || 'red'; if (img) { ctx.drawImage(img, x * w, y * h, img.width, img.height); } else { ctx.fillRect(x * w, y * h, w, h); } ctx.closePath(); ctx.fill(); } function clear(x, y) { ctx.clearRect(x * w, y * h, w, h); }

3.config.js

 var exit = []; var parse = true; var view = document.getElementById('view'); var ctx = view.getContext('2d'); var width = 400, height = 400; var w = 20, h = 20; var maxX = (width / w) - 1, maxY = (height / h) - 1; var speed = 500; var scoreMax = 0; var imgsrcs = [ './img/snakeheadup.png-600', //图片自己找 './img/snakeheaddown.png-600', './img/snakeheadleft.png-600', './img/snakeheadright.png-600' ]; var imgs = []; var length = 4; for (var i = 0, len = imgsrcs.length; i 

4.food.js

 function Food() { this.init(); } Food.prototype = { init: function() { this.update(); }, update: function() { var food = this.makeCoordinate(); this.food = food; var foodX = food[0]; var foodY = food[1]; this.render(foodX, foodY); }, render: function(x, y) { render(x, y, 'blue'); }, makeCoordinate: function() { var x = this.random(0, maxX); var y = this.random(0, maxY); for (var i = 0; i 

5.init.js

 function init() { food = new Food(); snake = new Snake(); move = new Move(); score = new Score(); var timer = setInterval(function() { if (!parse) { snake.move(); } }, speed); } for (var j = 0; j 

6.move.js

 function Move() { } Move.prototype = { up: function(s) { snake.move('up'); }, down: function(s) { snake.move('down'); }, left: function(s) { snake.move('left'); }, right: function(s) { snake.move('right'); } } document.onkeydown = function(e) { var key = e.keyCode; if(!parse){ switch (key) { case 37: case 65: move.left(snake); break; case 38: case 87: move.up(snake); break; case 39: case 68: move.right(snake); break; case 40: case 83: move.down(snake); break; default: break; } } };

7.score.js

 var scoreDom = document.getElementById('score'); var scoreMaxDom = document.getElementById('scoreMax'); function Score() { this.score = 0; } Score.prototype = { scoreAdd: function() { this.score += 1; scoreDom.innerHTML = this.score; }, scoreClear: function() { this.score = 0; scoreDom.innerHTML = 0; }, highScore: function() { scoreMax = scoreMax > this.score ? scoreMax : this.score; scoreMaxDom.innerHTML = scoreMax; } }

8.snake.js

 function Snake() { this.head = [0, 5, imgs[1]]; // x,y坐标,头 this.body = []; // 二维数组,身体 this.snake = []; //完整的蛇 this.init(6); this.direction = 'down'; // 默认向下运动 } Snake.prototype = { init: function(len) { len = len ? len > 10 ? 10 : len : 5; var head = this.head = [0, len, imgs[1]]; var body = this.body; for (var i = 0; i  maxX || y <0 || y> maxY) { this.gameOver(); } var body = this.body; for (var i = 0; i 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是js实现贪吃蛇游戏(简易版)的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » JavaScript 教程