canvas實現的貪吃蛇遊戲程式碼例項
分享一段程式碼例項,它實現了貪吃車遊戲效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> .map { cursor: none; border: 1px solid #000; } </style> </head> <body> <canvas id="map" class="map" width="800" height="800"></canvas> <script> var config = { //網格密度 MeshDensity: 50 } var Bamboo = function(x, y, ishead) { this.x = x; this.y = y; this.ishead = ishead; return this; } var DirectionKey = []; DirectionKey[37] = "left"; DirectionKey[38] = "up"; DirectionKey[39] = "right"; DirectionKey[40] = "down"; var loaded = function() { var canvas = document.getElementById("map"); var context = canvas.getContext("2d"); var memorydata = { //身體 snakebody: [], //移動方向 moveDirection: "right", //敵人 enemy: { x: 0, y: 0 }, //分數 score: 0, //是否死亡 isdied: false } //初始化資料 var cellsize = canvas.width / config.MeshDensity; //構建蛇身 for (var i = 3; i >= 0; i--) { var bamboo = new Bamboo(i, config.MeshDensity - 1, false); if (i == 3) { bamboo.ishead = true; } memorydata.snakebody.push(bamboo); } memorydata.enemy.x = Math.floor(Math.random() * config.MeshDensity); memorydata.enemy.y = Math.floor(Math.random() * config.MeshDensity); //蛇移動 function snakeMove() { var oldhead = memorydata.snakebody[0]; oldhead.ishead = false; var newhead = new Bamboo(); newhead.ishead = true; if (canEat()) { newhead.x = memorydata.enemy.x; newhead.y = memorydata.enemy.y; memorydata.enemy.x = Math.floor(Math.random() * config.MeshDensity); memorydata.enemy.y = Math.floor(Math.random() * config.MeshDensity); memorydata.score++; } else { switch (memorydata.moveDirection) { case "up": newhead.y = oldhead.y - 1; newhead.x = oldhead.x; break; case "right": newhead.y = oldhead.y; newhead.x = oldhead.x + 1; break; case "down": newhead.y = oldhead.y + 1; newhead.x = oldhead.x; break; case "left": newhead.y = oldhead.y; newhead.x = oldhead.x - 1; break; } if (newhead.x < 0) { newhead.x = config.MeshDensity - 1; } if (newhead.x > config.MeshDensity - 1) { newhead.x = 0; } if (newhead.y < 0) { newhead.y = config.MeshDensity - 1; } if (newhead.y > config.MeshDensity - 1) { newhead.y = 0; } memorydata.snakebody.splice(memorydata.snakebody.length - 1, 1); } memorydata.snakebody.unshift(newhead); } //判斷蛇是否能吃掉目標 function canEat() { var head = memorydata.snakebody[0]; var result = false; switch (memorydata.moveDirection) { case "up": if (head.y == memorydata.enemy.y + 1 && head.x == memorydata.enemy.x) { result = true; } break; case "right": if (head.y == memorydata.enemy.y && head.x == memorydata.enemy.x - 1) { result = true; } break; case "down": if (head.y == memorydata.enemy.y - 1 && head.x == memorydata.enemy.x) { result = true; } break; case "left": if (head.y == memorydata.enemy.y && head.x == memorydata.enemy.x + 1) { result = true; } break; } return result; } //死亡判斷 function isDead() { var head = memorydata.snakebody[0]; var result = false; for (var i = 1; i < memorydata.snakebody.length; i++) { var bamboo = memorydata.snakebody<i>; if (bamboo) { if (head.x == bamboo.x && head.y == bamboo.y) { result = true; break; } } } return result; } //繪製蛇身 function drawSnakeBody() { for (var i = 0; i < memorydata.snakebody.length; i++) { var bamboo = memorydata.snakebody<i>; if (bamboo) { var x = bamboo.x * cellsize; var y = bamboo.y * cellsize; if (bamboo.ishead) { context.fillStyle = "blue"; } else { context.fillStyle = "green"; } context.save(); context.fillRect(x, y, cellsize, cellsize); } } context.restore(); } //繪製敵人 function drawEnemy() { var x = memorydata.enemy.x * cellsize; var y = memorydata.enemy.y * cellsize; context.fillStyle = "red"; context.save(); context.fillRect(x, y, cellsize, cellsize); context.restore(); } //繪製分數文字 function drawTextScore() { context.fillStyle = "cyan"; context.save(); context.font = "italic 30px sans-serif"; context.textBaseline = 'top'; context.fillText(memorydata.score, 0, 0); context.restore(); } //繪製遊戲結束文字 function drawTextFailed() { if (memorydata.isdied) { context.fillStyle = "#000"; context.save(); context.font = "normal 30px sans-serif"; context.textBaseline = 'top'; var text = "YOU DIED"; context.fillText(text, config.MeshDensity / 2 * cellsize - text.length * cellsize / 2, config.MeshDensity / 2 * cellsize); context.restore(); } } //重新整理幀 (function drawFrame() { window.requestAnimationFrame(drawFrame, canvas); context.clearRect(0, 0, canvas.width, canvas.height); drawEnemy(); drawSnakeBody(); drawTextScore(); drawTextFailed(); }()); //更新資料 var worldTime = setInterval(function() { snakeMove(); if (isDead()) { clearInterval(worldTime); memorydata.isdied = true; } }, 200); //轉向控制 function turnCtl(event) { var event = event || window.event; if (!event.keyCode || event.keyCode == 0) { event.keyCode = event.which; } var direction = DirectionKey[event.keyCode]; if (direction) { switch (memorydata.moveDirection) { case "up": if (direction == "down") { return false; } break; case "right": if (direction == "left") { return false; } break; case "down": if (direction == "up") { return false; } break; case "left": if (direction == "right") { return false; } break; } memorydata.moveDirection = DirectionKey[event.keyCode]; } } window.addEventListener("keydown", turnCtl, false); } window.addEventListener("load", loaded, false); </script> </body> </html> </i></i>
相關文章
- canvas貪吃蛇遊戲程式碼例項Canvas遊戲
- js貪吃蛇遊戲程式碼例項JS遊戲
- javascript貪吃蛇小遊戲程式碼例項JavaScript遊戲
- js實現的移動端貪吃蛇遊戲程式碼例項JS遊戲
- OpenGL實現貪吃蛇程式碼
- jQuery 實現貪吃蛇遊戲jQuery遊戲
- Java實現貪吃蛇Java
- js開發實現簡單貪吃蛇遊戲(20行程式碼)JS遊戲行程
- python實現貪吃蛇Python
- Ubuntu下C語言實現貪吃蛇遊戲UbuntuC語言遊戲
- Python:遊戲:貪吃蛇Python遊戲
- (完整原始碼)貪吃蛇小遊戲——HTML+CSS+JavaScript實現原始碼遊戲HTMLCSSJavaScript
- 100行Python程式碼實現貪吃蛇小遊戲(超詳細)Python遊戲
- 一個貪吃蛇小遊戲(17行程式碼)遊戲行程
- 純JS實現貪吃蛇遊戲 —— 可能是全網程式碼最優雅的實現。JS遊戲
- 貪吃蛇c原始碼原始碼
- Shell寫的貪吃蛇遊戲(轉)遊戲
- 貪吃蛇源程式 (轉)
- C語言實現桌面貪吃蛇C語言
- Python實現貪吃蛇大作戰Python
- WebGL實現HTML5的3D貪吃蛇遊戲WebHTML3D遊戲
- 貪吃蛇jsJS
- 04 貪吃蛇
- [譯] RxJS 遊戲之貪吃蛇JS遊戲
- c/c++實現簡單的貪吃蛇視覺化遊戲C++視覺化遊戲
- canvas實現的刮刮樂程式碼例項Canvas
- C語言貪吃蛇原始碼C語言原始碼
- [原生JS][程式導向]貪吃蛇JS
- canvas實現的美猴王程式碼例項Canvas
- 無法執行的貪吃蛇遊戲程式碼,求大神幫忙改動!遊戲
- canvas繪製魚吃豆效果程式碼例項Canvas
- 利用python實現簡易版的貪吃蛇遊戲(面向python小白)Python遊戲
- 100行JS實現HTML5的3D貪吃蛇遊戲JSHTML3D遊戲
- canvas小遊戲程式碼例項Canvas遊戲
- h5+js實現單機版貪吃蛇H5JS
- html畫布製作貪吃蛇小遊戲HTML遊戲
- H5遊戲開發:貪吃蛇H5遊戲開發
- canvas實現的驗證碼效果程式碼例項Canvas