您好,登錄后才能下訂單哦!
本文實例為大家分享了JS實現(xiàn)貪吃蛇游戲的具體代碼,供大家參考,具體內容如下
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"> <link rel="stylesheet" href="./css/index.css" rel="external nofollow" > <title>貪吃蛇小游戲</title> </head> <body> <div class="content"> <div class="btn startBtn"><button></button></div> <div class="btn pauseBtn"><button></button></div> <div id="snakeWrap"> </div> </div> <script src="./js/index.js"></script> </body> </html>
css部分:
.content{ width: 640px; height: 640px; margin: 50px auto; position: relative; } .btn{ width: 100%; height: 100%; position: absolute; left: 0; top: 0; background-color: rgba(0,0,0,0); z-index: 1; } .btn button{ background: none; border:none; background-size: 100% 100%; cursor: pointer; outline: none; position: absolute; left: 50%; top: 50%; } .startBtn button{ width: 200px; height: 150px; background-image: url('../images/btn1.gif'); margin-left: -100px; margin-top: -75px; } .pauseBtn{ display: none; } .pauseBtn button{ width: 70px; height: 70px; background-image: url('../images/btn4.png'); margin-left: -35px; margin-top: -35px; } #snakeWrap{ position: relative; width: 600px; height: 600px; background: #FCE4EC; border: 20px solid #F8BBD0; } .snakeHead{ background-image: url('../images/snake.png'); background-size: cover; } .snakeBody{ background-color: #9CCC65; border-radius: 50%; } .food{ background: url('../images/food2.png') no-repeat; background-size: 100% 100%; }
js部分:
var sw = 20, // 一個方塊的寬度 sh = 20, // 高度 tr = 30, // 行數(shù) td = 30; // 列數(shù) var snake = null, // 蛇的實例 food = null, // 食物的實例 game = null; // 游戲實例 // 方塊構造函數(shù) function Square(x,y,classname) { this.x = x*sw; this.y = y*sh; this.class = classname; this.viewContent = document.createElement('div'); this.viewContent.className = this.class; this.parent = document.getElementById('snakeWrap'); } Square.prototype.create = function(){ // 創(chuàng)建方塊dom this.viewContent.style.position='absolute'; this.viewContent.style.width = sw+'px'; this.viewContent.style.height = sh + 'px'; this.viewContent.style.left = this.x +'px'; this.viewContent.style.top = this.y + 'px'; this.parent.appendChild(this.viewContent) } Square.prototype.remove = function() { this.parent.removeChild(this.viewContent); } // 蛇 function Snake () { this.head = null; //蛇頭 this.tail = null; // 蛇尾 this.pos = []; // 存儲蛇身上的每一個方塊的位置 this.directionNum = { // 存儲蛇走的方向,用一個對象來表示 left:{ x:-1, y:0, rotate:180 // 蛇頭旋轉角度 }, right:{ x:1, y:0, rotate:0 }, up:{ x:0, y:-1, rotate:-90 }, down:{ x:0, y:1, rotate:90 } } } Snake.prototype.init = function() { // 創(chuàng)建蛇頭 var snakeHead = new Square(2,0,'snakeHead'); snakeHead.create(); this.head = snakeHead; // 存儲蛇頭信息 this.pos.push([2,0]) // 存儲蛇頭位置 // 創(chuàng)建蛇身體 var snakeBody1 = new Square(1,0,'snakeBody'); snakeBody1.create(); this.pos.push([1,0]) // 存儲蛇身1位置 var snakeBody2 = new Square(0,0,'snakeBody'); snakeBody2.create(); this.tail = snakeBody2; // 存儲蛇尾信息 this.pos.push([0,0]) // 存儲蛇身1位置 // 形成鏈表關系 snakeHead.last = null; snakeHead.next = snakeBody1; snakeBody1.last = snakeHead; snakeBody1.next = snakeBody2; snakeBody2.last = snakeBody1; snakeBody2.next = null; // 給蛇添加一個屬性,用來表示蛇走的方向 this.direction = this.directionNum.right; // 默認往右走 } // 獲取蛇頭的下一個位置對應的元素,要根據元素做不同的事情 Snake.prototype.getNextPos = function() { var nextPos = [ this.head.x/sw+this.direction.x, this.head.y/sh+this.direction.y ] // 下個點是自己,游戲結束 var selfCollied = false; //是否撞到了自己 this.pos.forEach(function(value) { if(value[0]==nextPos[0] && value[1]==nextPos[1]){ selfCollied = true; } }); if(selfCollied){ console.log('撞到了自己'); this.strategies.die.call(this); return; } // 下個點是圍墻,游戲結束 if(nextPos[0]<0 || nextPos[1]<0 || nextPos[0]>td-1 || nextPos[1]>tr-1){ console.log('撞到了墻'); this.strategies.die.call(this); return; } // 下個點是食物,吃 if(food && food.pos[0]==nextPos[0] && food.pos[1]==nextPos[1]){ // 如果這個條件成立說明現(xiàn)在蛇頭要走的下一個點是食物的那個點; console.log('吃到食物了'); this.strategies.eat.call(this); return; } // 下個點什么都不是,走 this.strategies.move.call(this); }; // 處理碰撞后要做的事 Snake.prototype.strategies = { move:function(format) { // 該參數(shù)用于決定是否刪除蛇尾 // 創(chuàng)建新身體,在舊蛇頭的位置 var newBody = new Square(this.head.x/sw,this.head.y/sh,'snakeBody'); // 更新鏈表的關系 newBody.next = this.head.next; newBody.next.last = newBody; newBody.last = null; this.head.remove(); // 把舊蛇頭從原來的位置刪除 newBody.create(); // 創(chuàng)建蛇頭:蛇頭下一個點 var newHead = new Square(this.head.x/sw+this.direction.x,this.head.y/sh+this.direction.y,'snakeHead'); // 更新鏈表的關系 newHead.next = newBody; newHead.last = null; newBody.last = newHead; newHead.viewContent.style.transform = 'rotate('+this.direction.rotate+'deg)'; newHead.create(); // 更新蛇身上每一個方塊的坐標 this.pos.splice(0,0,[this.head.x/sw+this.direction.x,this.head.y/sh+this.direction.y]); this.head = newHead; //更新this.head if(!format){ // false: 需要刪除(處理吃之外的操作) this.tail.remove(); this.tail = this.tail.last; this.pos.pop(); } }, eat:function(){ this.strategies.move.call(this,true); createFood(); game.score++; }, die:function(){ game.over(); } } snake = new Snake(); // snake.init(); // 創(chuàng)建食物 function createFood(){ // 食物的隨機坐標 var x = null; var y = null; var include = true; // 循環(huán)跳出的條件,true表示食物坐標在蛇身上,false:表示不在 while(include){ x = Math.round(Math.random()*(td-1)); y = Math.round(Math.random()*(tr-1)); snake.pos.forEach(function(value){ if(x!=value[0] && y!=value[1]){ // 坐標不在蛇身上 include = false; } }) // 生成食物 food = new Square(x,y,'food'); food.pos = [x,y]; // 存儲食物的坐標,用于跟蛇頭下一個走的點作對比 var foodDom = document.querySelector('.food'); if(foodDom){ foodDom.style.left = x*sw +'px'; foodDom.style.top = y*sh +'px'; }else{ food.create(); } } } // 創(chuàng)建游戲邏輯 function Game(){ this.timer = null; this.score = 0; } Game.prototype.init = function(){ snake.init(); createFood(); document.onkeydown = function(ev) { // 用戶按下左鍵, 蛇不能是正在往右走的 if(ev.which == 37 && snake.direction != snake.directionNum.right){ snake.direction = snake.directionNum.left; }else if(ev.which == 38 && snake.direction != snake.directionNum.down){ snake.direction = snake.directionNum.up; }else if(ev.which == 39 && snake.direction != snake.directionNum.left){ snake.direction = snake.directionNum.right; }else if(ev.which == 40 && snake.direction != snake.directionNum.up){ snake.direction = snake.directionNum.down; } } this.start(); } Game.prototype.start = function(){ // 開始游戲 this.timer = setInterval(function(){ snake.getNextPos(); },200); } Game.prototype.pause = function() { clearInterval(this.timer); } Game.prototype.over = function() { clearInterval(this.timer); alert('你的得分為:'+ this.score); // 游戲回到最初始的狀態(tài) var snakeWrap = document.getElementById('snakeWrap'); snakeWrap.innerHTML = ''; snake = new Snake(); game = new Game(); var startBtnWrap = document.querySelector('.startBtn'); startBtnWrap.style.display = 'block'; } // 開啟游戲 game = new Game(); var startBtn = document.querySelector('.startBtn button'); startBtn.onclick = function(){ startBtn.parentNode.style.display = 'none'; game.init(); } // 暫停游戲 var snakeWrap = document.getElementById('snakeWrap'); var pauseBtn = document.querySelector('.pauseBtn button'); snakeWrap.onclick = function() { game.pause(); pauseBtn.parentNode.style.display='block'; } pauseBtn.onclick = function() { game.start(); pauseBtn.parentNode.style.display='none'; }
游戲截圖:
更多關于Js游戲的精彩文章,請查看專題: 《JavaScript經典游戲 玩不?!?/p>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。