本篇文章小编给大家分享一下用js编写简单的贪吃蛇小游戏代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
代码如下:
HTML 5 部分
js部分
tools.js
function getStyle(ele, styleObj) { for (const key in styleObj) { ele.style[key] = styleObj[key]; } } function getRandom(a, b) { return Math.floor(Math.random() * (b - a) + a +1) }
snake.js
function Snake() { this.scence = document.querySelector('#scence'); this.body = [ [0, 0, 'grey', null], [0, 1, 'grey', null], [0, 2, '#000', null] ]; this.dir = 'right'; this.lastdir = 'right'; this.; this.; this.scence_w = scence.offsetWidth; this.scence_h = scence.offsetHeight; } Snake.prototype.found = function () { for (let i = 0; i < this.body.length; i++) { if (this.body[i][3] == null) { this.body[i][3] = document.createElement('div'); } getStyle(this.body[i][3], { width: this.width + 'px', height: this.height + 'px', position: 'absolute', top: this.height * (this.body[i][0]) + 'px', left: this.width * (this.body[i][1]) + 'px', backgroundColor: this.body[i][2] }); this.scence.appendChild(this.body[i][3]); } } //运动函数 Snake.prototype.move = function () { var length = this.body.length; for (let i = 0; i < length - 1; i++) { this.body[i][0] = this.body[i + 1][0]; this.body[i][1] = this.body[i + 1][1]; } let snakehead = this.body[length - 1] switch (this.dir) { case 'right': snakehead[1] += 1; break; case 'left': snakehead[1] -= 1 break; case 'up': snakehead[0] -= 1 break; case 'down': snakehead[0] += 1 break; } this.lastdir = this.dir; snake.found(); } //计时运动 Snake.prototype.shift = function () { document.onkeydown = (e) => { e = e || window.event; let key = e.keyCode; switch (key) { case 39: if (this.lastdir == 'left') { this.dir = 'left' } else { this.dir = 'right' }; break; case 37: if (this.lastdir == 'right') { this.dir = 'right' } else { this.dir = 'left' }; break; case 38: if (this.lastdir == 'down') { this.dir = 'down' } else { this.dir = 'up' }; break; case 40: if (this.lastdir == 'up') { this.dir = 'up' } else { this.dir = 'down' }; break; } } } //游戏结束 Snake.prototype.over = function () { let top = this.body[this.body.length - 1][0]; let left = this.body[this.body.length - 1][1]; let width = this.scence_w / this.width - 1; let height = this.scence_w / this.height - 1; if (top < 0 || left < 0 || top > width || left > height) { clearInterval(timeid) alert('game over'); } for (let i = 0; i < this.body.length - 1; i++) { if (top == this.body[i][0] && left == this.body[i][1]) { clearInterval(timeid) alert('game over'); } } } let snake = new Snake(); snake.found(); snake.shift(); timeid = setInterval(function () { snake.move(); food.eat(); snake.over() }, 100)
food.js
function Food() { this.scence = document.querySelector('#scence'); this.; this.; this.body = [-1, -1, 'red', null]; this.scence_w = scence.offsetWidth; this.scence_h = scence.offsetHeight; } //食物生成 Food.prototype.crteate = function () { this.body[1] = getRandom(0, this.scence_w / this.width-1); this.body[0] = getRandom(0, this.scence_h / this.height-1); this.body[3] = document.createElement('div'); getStyle(this.body[3], { width: this.width + 'px', height: this.height + 'px', position: 'absolute', top: this.height * (this.body[0] ) + 'px', left: this.width * (this.body[1] ) + 'px', backgroundColor: this.body[2], borderRadius: ' 50%', }); this.scence.appendChild(this.body[3]); } //蛇吃到食物 Food.prototype.eat=function(){ // const new=[0, 0, 'grey', null] if(snake.body[snake.body.length-1][0]==this.body[0] && snake.body[snake.body.length-1][1]==this.body[1]){ this.scence.removeChild(this.body[3]); this.crteate(); snake.body.unshift([-1,-1,"grey",null]) } } let food = new Food(); food.crteate(); food.eat();
忍者必须死34399账号登录版 最新版v1.0.138v2.0.72
下载勇者秘境oppo版 安卓版v1.0.5
下载忍者必须死3一加版 最新版v1.0.138v2.0.72
下载绝世仙王官方正版 最新安卓版v1.0.49
下载Goat Simulator 3手机版 安卓版v1.0.8.2
Goat Simulator 3手机版是一个非常有趣的模拟游
Goat Simulator 3国际服 安卓版v1.0.8.2
Goat Simulator 3国际版是一个非常有趣的山羊模
烟花燃放模拟器中文版 2025最新版v1.0
烟花燃放模拟器是款仿真的烟花绽放模拟器类型单机小游戏,全方位
我的世界动漫世界 手机版v友y整合
我的世界动漫世界模组整合包是一款加入了动漫元素的素材整合包,
我的世界贝爷生存整合包 最新版v隔壁老王
我的世界MITE贝爷生存整合包是一款根据原版MC制作的魔改整