這篇文章主要為大家展示了js如何實(shí)現(xiàn)簡(jiǎn)單貪吃蛇游戲,內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來(lái)看看吧。
成都創(chuàng)新互聯(lián)公司主要為客戶提供服務(wù)項(xiàng)目涵蓋了網(wǎng)頁(yè)視覺(jué)設(shè)計(jì)、VI標(biāo)志設(shè)計(jì)、全網(wǎng)整合營(yíng)銷推廣、網(wǎng)站程序開(kāi)發(fā)、HTML5響應(yīng)式成都網(wǎng)站建設(shè)、手機(jī)網(wǎng)站制作、微商城、網(wǎng)站托管及網(wǎng)頁(yè)維護(hù)、WEB系統(tǒng)開(kāi)發(fā)、域名注冊(cè)、國(guó)內(nèi)外服務(wù)器租用、視頻、平面設(shè)計(jì)、SEO優(yōu)化排名。設(shè)計(jì)、前端、后端三個(gè)建站步驟的完善服務(wù)體系。一人跟蹤測(cè)試的建站服務(wù)標(biāo)準(zhǔn)。已經(jīng)為成都軟裝設(shè)計(jì)行業(yè)客戶提供了網(wǎng)站營(yíng)銷服務(wù)。
上下左右鍵控制方向使貪吃蛇吃葡萄
吃5個(gè)葡萄,游戲結(jié)束時(shí)左上角為總得分。
運(yùn)行結(jié)果:
界面和css代碼這里就不加贅述了,主要貼js代碼(加了注釋):
var config = { width: 20, //一個(gè)格子的寬度 height: 20, //一個(gè)格子的高度 tr: 30, //行數(shù) td: 30 //列數(shù) } var snake = null, //Snake的實(shí)例 food = null, //Food的實(shí)例 game = null; //游戲的實(shí)例 //我們把蛇移動(dòng)的整個(gè)區(qū)域設(shè)置成一個(gè)具有30列30行的網(wǎng)格坐標(biāo) //方塊(格子)坐標(biāo)位置 /** 0,0 (0,0) 20,0 (1,0) 40,0 (2,0) */ function Square(x, y, className) { this.x = x*config.width; this.y = y*config.height; this.className = className; this.contentDom = document.createElement('div');//該位置的方塊對(duì)應(yīng)的DOM元素 this.contentDom.className = this.className; this.parent = document.getElementsByClassName("innerSnake")[0]; } Square.prototype.create = function() { //創(chuàng)建方塊并添加到頁(yè)面 this.contentDom.style.position = 'absolute'; this.contentDom.style.width = config.width + 'px'; this.contentDom.style.height = config.height + 'px'; this.contentDom.style.left = this.x + 'px'; this.contentDom.style.top = this.y + 'px'; this.parent.appendChild(this.contentDom); }; Square.prototype.remove = function() { //移除方塊 this.parent.removeChild(this.contentDom); }; //蛇 function Snake() { this.head = null; //蛇頭 this.tail = null; //蛇尾 this.pos = []; //二維數(shù)組,存儲(chǔ)蛇身上每個(gè)節(jié)點(diǎn)(方塊) this.directionKey = { //存儲(chǔ)蛇走的方向 left: { //往左走 x: -1, //橫坐標(biāo)減1,一個(gè)坐標(biāo)表示一個(gè)格子 y: 0, //縱坐標(biāo)不變 rotate: 90 }, right: { //往右走 x: 1, y: 0, rotate: -90 }, up: { //往上走 x: 0, y: -1, rotate: 180 }, down: { //往下走 x: 0, y: 1, rotate: 0 //蛇頭圖片方向,順時(shí)針為正 } } } Snake.prototype.init = function() { //初始化蛇 //蛇頭 var snakeHead = new Square(2,0,"head"); snakeHead.create(); //將蛇頭添加到界面 this.head = snakeHead; //存儲(chǔ)蛇頭信息 this.pos.push([2,0]); //存儲(chǔ)蛇頭坐標(biāo) //蛇的第1節(jié)身體 var snakeBody1 = new Square(1,0,"body"); snakeBody1.create(); //將蛇的第一節(jié)身體添加到界面 this.pos.push([1,0]); //蛇的尾巴 var snakeTail = new Square(0,0,"body"); snakeTail.create(); //將蛇尾添加到界面 this.tail = snakeTail; //存儲(chǔ)蛇尾信息 this.pos.push([0,0]); //形成鏈表關(guān)系 snakeHead.prev = null; //蛇頭的前面沒(méi)有元素,指向null snakeHead.next = snakeBody1; //蛇頭的后面有一節(jié)身體,其.next指針指向后面那節(jié)身體 snakeBody1.prev = snakeHead; //蛇的第一節(jié)身體,.prev指向前面的蛇頭snakeHead snakeBody1.next = snakeTail; //蛇的第一節(jié)身體,.next指向后面的身體,此時(shí)是蛇尾snakeTail snakeTail.prev = snakeBody1; //蛇尾,.prev指向前面的蛇身體snakeBody1 snakeTail.next = null; //蛇尾后面沒(méi)有元素,指向Null //初始蛇的走向,后面想改變蛇的走向即改變this.direction this.direction = this.directionKey.right; //默認(rèn)向右走 }; //獲取蛇頭下一個(gè)位置對(duì)應(yīng)的元素,根據(jù)元素做下一個(gè)動(dòng)作 Snake.prototype.getNextPos = function() { var nextPos = [ //獲取蛇頭走的下一個(gè)點(diǎn)的坐標(biāo) this.head.x / config.width + this.direction.x, this.head.y / config.height + this.direction.y ]; //判斷下一個(gè)點(diǎn)是自己or食物or圍墻or無(wú)障礙? var self = false; //設(shè)置下一個(gè)點(diǎn)是否是自己 this.pos.forEach(function(val) { //val即二位數(shù)組中的一個(gè)坐標(biāo) if(val.toString() === nextPos.toString()) { //下一個(gè)坐標(biāo)等于蛇全部身體的一個(gè),即下一個(gè)點(diǎn)是自己 self = true; } }); if(self) { // console.log('撞到自己了!'); this.collide.end.call(this); //game over return; } else if(nextPos[0] < 0 || nextPos[1] < 0 || nextPos[0] > config.td-1 || nextPos[1] > config.tr-1) { // console.log('撞到墻壁了!'); this.collide.end.call(this); //game over return; } else if (food && food.pos[0] === nextPos[0] && food.pos[1] === nextPos[1]) { console.log('撞到食物了!'); this.collide.eat.call(this); } else { // console.log('啥都沒(méi)遇到!'); this.collide.move.call(this, false); //注意:.call(this)重新設(shè)置this指向,使其指向當(dāng)前實(shí)例對(duì)象Snake } }; //處理碰撞后的事件 Snake.prototype.collide = { /* 碰到自己or墻壁,游戲結(jié)束end(); 碰到食物,eat(); 啥都沒(méi)遇到,move(); */ move: function(isEat) { //isEat 是否吃了食物,不是則刪除蛇尾 /* 掐頭去尾: create新蛇頭,remove舊蛇頭; create一個(gè)新身體,放在(替代)舊蛇頭的位置; remove蛇尾,蛇尾prev的元素變成新蛇尾 */ var x = this.head.x / config.width + this.direction.x, y = this.head.y / config.height + this.direction.y; //聲明一個(gè)新身體 var newBody = new Square(this.head.x/config.width, this.head.y/config.height, "body"); //更新鏈表關(guān)系 newBody.next = this.head.next; newBody.next.prev = newBody; newBody.prev = null; this.head.remove(); //刪除舊蛇頭 newBody.create(); //添加蛇身體,替代在舊蛇頭位置 //聲明一個(gè)新蛇頭(下一個(gè)走的點(diǎn)) var newHead = new Square(x, y, "head"); //更新鏈表關(guān)系 newHead.prev = null; newHead.next = newBody; newBody.prev = newHead; this.pos.unshift([x, y]); //更新蛇節(jié)點(diǎn)的坐標(biāo)this.pos this.head = newHead; //更新this.head的信息 newHead.contentDom.style.transform = `rotate(${this.direction.rotate}deg)` newHead.create(); //添加蛇頭 //刪除蛇尾:吃食物則不刪 if(!isEat) { //沒(méi)有吃食物,刪除蛇尾 this.tail.remove(); this.tail = this.tail.prev; this.pos.pop(); //更新蛇節(jié)點(diǎn)坐標(biāo) } // console.log(this.pos); //打印數(shù)組,驗(yàn)證 }, eat: function() { this.collide.move.call(this, true); //傳參true,表示此時(shí)為吃操作 food.remove(); //刪除被吃掉的食物 game.score ++; //記錄分?jǐn)?shù) createFood(); //此時(shí)再隨機(jī)產(chǎn)生一個(gè)食物 }, end: function() { console.log('end'); game.gameOver(); } } snake = new Snake(); //創(chuàng)建食物 function createFood() { var x = null, y = null; var include = true; //表示食物的位置是否在蛇身上 var random = function(max, min) { //產(chǎn)生一個(gè)隨機(jī)數(shù) return Math.floor(Math.random()*(max - min + 1)) }; while(include) { x = random(config.tr - 1, 0); y = random(config.td - 1, 0); snake.pos.forEach(function(val) { if(x != val[0] && y != val[1]) { include = false; } }); } //生成食物 food = new Square(x, y, "food"); food.pos = [x, y]; //記錄食物坐標(biāo) food.create(); } //游戲邏輯 function Game() { this.score = 0; //分?jǐn)?shù) this.timer = null; //計(jì)時(shí)器 } Game.prototype.init = function() { snake.init(); // snake.getNextPos(); //獲取下一個(gè)點(diǎn)坐標(biāo) createFood(); document.onkeydown = function(event) { if(event.which == 37 && snake.direction != snake.directionKey.right) { //鼠標(biāo)左鍵,蛇不能是正在往右走 snake.direction = snake.directionKey.left; } else if (event.which == 38 && snake.direction != snake.directionKey.down) { //鼠標(biāo)上鍵 snake.direction = snake.directionKey.up; } else if (event.which == 39 && snake.direction != snake.directionKey.left) { //鼠標(biāo)右鍵 snake.direction = snake.directionKey.right; } else if (event.which == 40 && snake.direction != snake.directionKey.up) { //鼠標(biāo)下鍵 snake.direction = snake.directionKey.down; } } this.start(); }; game = new Game(); //開(kāi)始游戲 Game.prototype.start = function() { this.timer = setInterval(function() { snake.getNextPos(); //獲取下一個(gè)坐標(biāo)點(diǎn),做下一步動(dòng)作 }, 200); }; //游戲結(jié)束 Game.prototype.gameOver = function() { console.log("gameOver"); clearInterval(this.timer); var gameOver = document.querySelector('.gameOver'); var gameScore = document.querySelector('.gameOver .score'); gameOver.style.display = 'block'; //顯示游戲結(jié)束界面 gameScore.innerHTML = `${this.score}`; //將分?jǐn)?shù)記入該界面 }; //開(kāi)啟游戲 function startGame() { var startBtn = document.querySelector('.btn button'); var snakeWrap = document.querySelector('.snakeWrap'); startBtn.onclick = function() { startBtn.parentNode.style.display = 'none'; //隱藏開(kāi)始游戲界面 snakeWrap.style.display = 'block'; //顯示進(jìn)入游戲的界面 game.init(); } } startGame();
主要用到鏈表數(shù)據(jù)結(jié)構(gòu)
以上就是關(guān)于js如何實(shí)現(xiàn)簡(jiǎn)單貪吃蛇游戲的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。
網(wǎng)站欄目:js如何實(shí)現(xiàn)簡(jiǎn)單貪吃蛇游戲
鏈接分享:http://chinadenli.net/article44/ppehhe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、動(dòng)態(tài)網(wǎng)站、服務(wù)器托管、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站營(yíng)銷、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)