這篇文章給大家分享的是有關(guān)如何使用js實(shí)現(xiàn)貪吃蛇小游戲的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
我們擁有10多年網(wǎng)頁(yè)設(shè)計(jì)和網(wǎng)站建設(shè)經(jīng)驗(yàn),從網(wǎng)站策劃到網(wǎng)站制作,我們的網(wǎng)頁(yè)設(shè)計(jì)師為您提供的解決方案。為企業(yè)提供成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、微信開(kāi)發(fā)、微信小程序定制開(kāi)發(fā)、手機(jī)網(wǎng)站制作設(shè)計(jì)、H5高端網(wǎng)站建設(shè)、等業(yè)務(wù)。無(wú)論您有什么樣的網(wǎng)站設(shè)計(jì)或者設(shè)計(jì)方案要求,我們都將富于創(chuàng)造性的提供專業(yè)設(shè)計(jì)服務(wù)并滿足您的需求。
具體內(nèi)容如下
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>貪吃蛇</title> <link rel="stylesheet" href="style.css" > </head> <body> <div id="game"> <div id="explain"> <div>操作WASD 空格開(kāi)始/暫停 R重新開(kāi)始</div> <div>當(dāng)前分?jǐn)?shù):<span id="grade">0</span>分</div> </div> <canvas id="snakegame" width="500" height="500"> </canvas> </div> <script type="text/javascript" src="main.js" charset="UTF-8"></script> </body> </html>
style.css
*{ padding: 0; margin: 0; } #game { width: 500px; margin: auto; } #explain { width: 500px; } #explain div{ width: 500px; height: 30px; } #snakegame { background: green; }
main.js
/** * el 掛載的元素 * attribute 貪吃蛇的屬性 */ class Game { constructor (el, attribute) { this.el = document.getElementById(el); // 獲取畫(huà)布的寬高 this.el.elW = parseInt(window.getComputedStyle(this.el).width); this.el.elH = parseInt(window.getComputedStyle(this.el).height); this.init(attribute); this.keyListening(); } // 初始化 init(attribute) { this.attribute = { color: "red", // 顏色 direction: "rigth", // 移動(dòng)方向 state: "pause", // 狀態(tài) run pause end grade: 0, // 分?jǐn)?shù) body: [{x: 20, y: 0}, {x: 0, y: 0}], // 貪吃蛇身體 wh: 20, // 矩形的寬高 speed: 200 // 速度 }; if (attribute) { this.newAttribute = attribute; Object.keys(attribute).forEach(key => { this.attribute[key] = attribute[key]; }); } this.food ={ x: 0, y: 0, color: 'red' } this.draw(); this.foodDraw(); } // 繪制貪吃蛇 draw() { let el = this.el; let { body, wh, color } = this.attribute; // 確定瀏覽器是否支持canvans元素 if (el.getContext) { let context = snakegame.getContext("2d"); context.fillStyle = color; body.forEach( key => { context.fillRect(key.x, key.y, wh, wh); }); } } // 隨機(jī)生成食物 foodDraw() { let el = this.el, wh = this.attribute.wh; this.food.x = Math.floor(Math.random()*(el.elW - wh)), this.food.y = Math.floor(Math.random()*(el.elH - wh)); while (this.isOverlap()) { this.food.x = Math.floor(Math.random()*(el.elW - wh)), this.food.y = Math.floor(Math.random()*(el.elH - wh)); } if (el.getContext) { let context = snakegame.getContext("2d"); context.fillStyle = this.food.color; context.fillRect(this.food.x, this.food.y, wh, wh); } } // 判斷食物是否與貪吃蛇的身體重疊 isOverlap() { let { wh } = this.attribute; let food = this.food; let flag = false; function isIn(key, x, y) { if (key.x <= x && key.x + wh >= x && key.y <= y && key.y + wh >= y) { return true; } else { return false; } } this.attribute.body.forEach(key => { // 食物的上下左右四個(gè)點(diǎn)一個(gè)點(diǎn)在貪吃蛇的身體內(nèi)就判斷為重疊 if (isIn(key, food.x, food.y) || isIn(key, food.x, food.y + wh) || isIn(key, food.x + wh, food.y) || isIn(key, food.x + wh, food.y + wh)) { flag = true; } }); return flag; } // 清除圖形 clear(x, y, width, height) { // 確定瀏覽器是否支持canvans元素 if (this.el.getContext) { let context = snakegame.getContext("2d"); context.clearRect(x, y, width, height); } } // 游戲狀態(tài)更新 updateState(state) { this.attribute.state = state; if (state === "run") { this.run(); } } // 游戲線程 run() { let { body, wh, speed} = this.attribute; let time = setInterval(() => { // 判斷游戲線程是否在運(yùn)行 if (this.attribute.state !== 'run') { clearInterval(time); } let obj = {}; switch(this.attribute.direction) { case 'left': obj['x'] = body[0].x - wh; obj['y'] = body[0].y; break; case 'rigth': obj['x'] = body[0].x + wh; obj['y'] = body[0].y; break; case 'up': obj['x'] = body[0].x; obj['y'] = body[0].y - wh; break; case 'down': obj['x'] = body[0].x; obj['y'] = body[0].y + wh; break; } body.unshift(obj); // 判斷是否吃到食物 if (this.isOverlap()) { this.clear(this.food.x, this.food.y, wh, wh); this.attribute.grade++; this.foodDraw(); this.draw(); } else { if (this.end()) { alert("游戲結(jié)束"); this.updateState('end'); clearInterval(time); } else { let item = body.pop(); this.clear(item.x, item.y, wh, wh); this.draw(); } } }, speed); } // 鍵盤(pán)事件監(jiān)聽(tīng) keyListening() { document.onkeydown = (event) => { let e = event || window.event || arguments.callee.caller.arguments[0]; if (e && e.keyCode === 87 && this.attribute.direction !== 'down') { // 按下W this.attribute.direction = 'up'; } if (e && e.keyCode === 65 && this.attribute.direction !== 'rigth') { // 按下A this.attribute.direction = 'left'; } if (e && e.keyCode === 68 && this.attribute.direction !== 'left') { // 按下D this.attribute.direction = 'rigth'; } if (e && e.keyCode === 83 && this.attribute.direction !== 'up') { // 按下W this.attribute.direction = 'down'; } if (e && e.keyCode === 32) { // 按下空格 let state; if (this.attribute.state === 'pause') { state = 'run'; } if (this.attribute.state === 'run') { state = 'pause'; } this.updateState(state); } if (e && e.keyCode === 82) { // 按下R鍵 this.reStart(); } } } // 是否死亡 end() { let body = [...this.attribute.body]; let obj = body.shift(); let flag = false; if (obj.x < 0 || obj.x >= this.el.elW || obj.y < 0 || obj.y >= this.el.elH) { flag = true; } body.forEach(key => { if (key.x === obj.x && key.y === obj.y) { flag = true; } }); return flag; } // 重新開(kāi)始 reStart() { // 清除整個(gè)畫(huà)布 this.clear(0, 0, this.el.elW, this.el.elH); // 重新開(kāi)始 this.init(this.newAttribute); } } let game = new Game("snakegame", {color: "yellow"}); let grade = document.getElementById("grade"); let oldGrade = game.attribute.grade; setInterval(() => { if (oldGrade !== game.attribute.grade) { oldGrade = game.attribute.grade; grade.innerText = game.attribute.grade; } })
截圖
感謝各位的閱讀!關(guān)于“如何使用js實(shí)現(xiàn)貪吃蛇小游戲”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
分享名稱:如何使用js實(shí)現(xiàn)貪吃蛇小游戲
文章來(lái)源:http://redsoil1982.com.cn/article8/iigsop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、電子商務(wù)、響應(yīng)式網(wǎng)站、軟件開(kāi)發(fā)、App開(kāi)發(fā)、搜索引擎優(yōu)化
聲明:本網(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)