javascript實(shí)現(xiàn)貪吃蛇游戲(娛樂(lè)版)
本文實(shí)例為大家分享了javascript實(shí)現(xiàn)貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下
一共三個(gè)對(duì)象map,snake,food,代表的含義如名字。snake和food其實(shí)就是數(shù)組,表示位置,map來(lái)畫(huà)圖、判斷得分、失敗等等,直接上代碼,可直接運(yùn)行。
<!doctype html><html><body> <canvas style='background:Black'></canvas> <h1>Score:</h1> <h2 id='score'>0</h2> <script> //地圖 function Map() { this.field = document.getElementById('map').getContext('2d'); //畫(huà)布 this.draw = function (something) { //畫(huà)蛇或者食物 this.field.fillStyle = something.color; var position; for (position in something.positions) { this.field.fillRect(something.positions[position][0], something.positions[position][1], 20, 20); } } this.clear = function () { //清除畫(huà)布 this.field.clearRect(0, 0, 400, 400); } this.judge = function (snake, food) { //判斷狀態(tài)(得分、失敗、普通) var snakeHeadX = snake.positions[0][0]; var snakeHeadY = snake.positions[0][1]; var foodX = food.positions[0][0]; var foodY = food.positions[0][1]; if ((snakeHeadX == foodX) && (snakeHeadY == foodY)) { //吃食物 snake.positions.unshift([foodX, foodY]); food.positions[0] = [Math.floor(Math.random() * 20) * 20, Math.floor(Math.random() * 20) * 20]; this.clear(); this.draw(food); this.draw(snake); var score = document.getElementById(’score’); score.innerHTML = (Number(score.innerHTML)+1).toString(); } else if ((snakeHeadX+20 > 400) || (snakeHeadX < 0) || (snakeHeadY+20 > 400) || (snakeHeadY < 0)) { alert(’GIME OVER!’); //撞墻 } else { this.clear(); this.draw(food); this.draw(snake); } } } //蛇 function Snake() { this.positions = [[40 + 20, 40], [40, 40], [40 - 20, 40]]; //蛇的軀干 this.color = 'Yellow'; this.direction = [1,0]; //蛇頭方向 this.move = function () { //移動(dòng) this.positions.unshift([this.positions[0][0] + this.direction[0] * 20, this.positions[0][1] + this.direction[1] * 20]); this.positions.pop(); } this.obeyOrders = function (snake = this) { //等待鍵盤(pán)上下左右 document.onkeydown = function (event) { var e = event || window.event || arguments.callee.caller.arguments[0]; var order = e.keyCode; console.log(snake.direction); switch (order) { case 37: snake.direction[0] = -1; snake.direction[1] = 0; break; case 38: snake.direction[1] = -1; snake.direction[0] = 0; break; case 39: snake.direction[0] = 1; snake.direction[1] = 0; break; case 40: snake.direction[1] = 1; snake.direction[0] = 0; break; default: ; } }; } } //食物 function Food() { this.positions = [[Math.floor(Math.random() * 20) * 20, Math.floor(Math.random() * 20) * 20]]; //隨機(jī)位置 this.color = ’Red’; } //開(kāi)始執(zhí)行 (function () { var map = new Map(); var snake = new Snake(); var food = new Food(); map.draw(snake); map.draw(food); snake.obeyOrders(); var t=0; var interval = setInterval(function () { //每0.5s走一步 map.judge(snake, food); snake.move(); }, 500); })() </script></body></html>
效果如圖
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 前端html+css實(shí)現(xiàn)動(dòng)態(tài)生日快樂(lè)代碼2. 關(guān)于html嵌入xml數(shù)據(jù)島如何穿過(guò)樹(shù)形結(jié)構(gòu)關(guān)系的問(wèn)題3. XML 非法字符(轉(zhuǎn)義字符)4. 初試WAP之wml+ASP查詢5. WMLScript的語(yǔ)法基礎(chǔ)6. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)7. 不要在HTML中濫用div8. el-input無(wú)法輸入的問(wèn)題和表單驗(yàn)證失敗問(wèn)題解決9. XML入門的常見(jiàn)問(wèn)題(三)10. JSP取得在WEB.XML中定義的參數(shù)
