欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

如何基于javascript實(shí)現(xiàn)貪吃蛇游戲

這篇文章主要介紹了如何基于javascript實(shí)現(xiàn)貪吃蛇游戲,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè),成都做網(wǎng)站公司-創(chuàng)新互聯(lián)已向上千企業(yè)提供了,網(wǎng)站設(shè)計(jì),網(wǎng)站制作,網(wǎng)絡(luò)營(yíng)銷等服務(wù)!設(shè)計(jì)與技術(shù)結(jié)合,多年網(wǎng)站推廣經(jīng)驗(yàn),合理的價(jià)格為您打造企業(yè)品質(zhì)網(wǎng)站。

html代碼:

<div class="content">
  <div class="btn startBtn">       <!-- 開始按鈕 -->  
    <button type="button"></button>
  </div>
  <div class="btn stopBtn">        <!-- 暫停按鈕 -->
    <button type="button"></button>
  </div>
  <div id="snakeWrap"></div>     <!-- 主題內(nèi)容 -->
</div>

css代碼:

* {
  margin: 0;
  padding: 0;
}
body {
  background-color: #565F65;
  width: 100%;
  height: 10vh;
  overflow: hidden;
}

.content {
  width: 500px;
  height: 500px;
  position: absolute;
  top: 50%;
  left: 50%; 
  margin-top: -250px;
  margin-left: -250px;
  background-color: #565F65;
  border: 10px solid #E7E7E7;
  box-shadow: inset 0px 0px 5px 2px #000;
}
.btn {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  background-color: rgba(0, 0, 0, .3);  /*游戲未開始時(shí)和暫停時(shí)的遮罩*/
  z-index: 2;
}
.btn button {
  background: none;
  border: none;
  background-size: 100% 100%;
  cursor: pointer;
  outline: none;
  position: absolute;
  left: 50%;
  top: 50%;
}
.startBtn button{
  width: 170px;
  height: 80px;
  background-image: url(img/startbtn.png);
  margin-top: -40px;
  margin-left: -85px;
}
.stopBtn {
  display: none;
}
.stopBtn button{
  width: 70px;
  height: 70px;
  background-image: url(img/stopbtn.png);
  margin-top: -35px;
  margin-left: -35px;
}

#snakeWrap {
  width: 500px;
  height: 500px;
  position: relative;
}
.snakeHead { /*蛇頭樣式*/
  background-color: aqua;
  border-radius: 50%;
}
.snakeBody { /*蛇身樣式*/      
  background-color: navajowhite;
  border-radius: 50%;
}
.food {  /*食物樣式*/
  background-image: url(img/food.png);
  background-size: cover;
}

javascript 代碼:

var sw = 20,  //一個(gè)方塊的寬
   sh = 20,  //一個(gè)方塊的高
    tr = 25,  //行數(shù)
    td = 25;  //列數(shù)
var snake = null,  //蛇的實(shí)例
    food = null,  //食物的實(shí)例
    game = null;  //游戲的實(shí)例
function Square(x, y, classname) {
  this.x = x * sw;     //方塊實(shí)際的位置
  this.y = y * sh;     //方塊實(shí)際的位置
  this.class = classname;
  
  this.viewContent = document.createElement('div');  //方塊對(duì)應(yīng)的DOM元素
  this.viewContent.className = this.class;
  this.parent = document.getElementById('snakeWrap'); //方塊的父級(jí)
}

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 = [];   //存儲(chǔ)蛇身上的每一個(gè)方塊的位置,二維數(shù)組
  
  this.directionNum = {  //存儲(chǔ)蛇走的方向,用一個(gè)對(duì)象來表示
    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;    // 存儲(chǔ)蛇頭信息
  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的位置存起來
  
  //讓蛇頭蛇身形成鏈表關(guān)系
  snakeHead.last = null;
  snakeHead.next = snakeBody1;
  
  snakeBody1.last = snakeHead;
  snakeBody1.next = snakeBody2;
  
  snakeBody2.last = snakeBody1;
  snakeBody2.next = null;
  
  //給蛇添加一條屬性,用來表示蛇走的方向
  this.direction = this.directionNum.right; //默認(rèn)讓蛇往右走
  
}

//這個(gè)方法用來獲取蛇頭的下一個(gè)位置對(duì)應(yīng)的元素, 要根據(jù)元素做不同的事情
Snake.prototype.getNextPos = function() {
  var nextPos = [             //蛇頭要走的下一個(gè)點(diǎn)的坐標(biāo)
    this.head.x/sw + this.direction.x,
    this.head.y/sh + this.direction.y
  ];
  
  //下一個(gè)點(diǎn)是自己,代表撞到了自己,游戲結(jié)束
  var selfCollind = false;   //是否撞到自己
  this.pos.forEach(function(value) {
    if(value[0] == nextPos[0] && value[1] == nextPos[1]) {
      //如果數(shù)組中的兩個(gè)數(shù)據(jù)都相等,就說明下一個(gè)點(diǎn)在蛇身上里面能找到,代表撞到自己了
      selfCollind = true;
    }
  });
  if(selfCollind) {
    console.log('撞到自己了!');
    
    this.strategies.die.call(this);
    
    return;
  }
  
  //下一個(gè)點(diǎn)是墻,游戲結(jié)束
  if(nextPos[0] < 0 || nextPos[1] < 0 || nextPos[0] > td - 1 || nextPos[1] > tr - 1) {
    console.log('撞墻了!');
    
    this.strategies.die.call(this);
    
    return;
  }
  //下一個(gè)點(diǎn)是食物,吃
  if(food && food.pos[0] == nextPos[0] && food.pos[1] == nextPos[1]) {
    //如果這個(gè)條件成立說明現(xiàn)在蛇頭要走的下一個(gè)點(diǎn)是食物的那個(gè)點(diǎn)
    console.log('撞到食物了了!');
    this.strategies.eat.call(this);
    return;
  }
  //下一個(gè)點(diǎn)什么都不是,走
  this.strategies.move.call(this);
};

//處理碰撞后要做的事 
Snake.prototype.strategies = {
  move : function(format) {  //這個(gè)參數(shù)用于決定要不要?jiǎng)h除最后一個(gè)方塊(蛇尾), 當(dāng)傳了這個(gè)參數(shù)后就表示要做的事情是吃
    //創(chuàng)建新身體(在蛇頭位置)
    var newBody = new Square(this.head.x/sw, this.head.y/sh, 'snakeBody');
    //更新鏈表關(guān)系
    newBody.next = this.head.next;
    newBody.next.last = newBody;
    newBody.last = null;
    
    
    this.head.remove();  //舊舌頭從原來的位置刪除
    newBody.create();
    
    //創(chuàng)建一個(gè)新蛇頭(蛇頭下一個(gè)要走到的點(diǎn))
    var newHead = new Square(this.head.x/sw + this.direction.x, this.head.y/sh + this.direction.y, 'snakeHead')
    //更新鏈表關(guān)系
    newHead.next = newBody;
    newHead.last = null;
    newBody.last = newHead;
    newHead.viewContent.style.transform = 'rotate('+this.direction.rotate+'deg)';
    newHead.create();
    
    
    //蛇身上每一個(gè)方塊的坐標(biāo)也要更新
    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) {  //如何format 的值為 false, 表示需要?jiǎng)h除(除了吃之外的操作)
      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();

//創(chuàng)建食物
function createFood() {
  //食物小方塊的隨機(jī)坐標(biāo)
  var x = null;
  var y = null;
  
  var include = true;  //循環(huán)跳出的條件, true表示食物的坐標(biāo)在蛇身上(需要繼續(xù)循環(huán)),false表示食物坐標(biāo)不在蛇身上(不循環(huán)了)
  while(include) {
    x = Math.round(Math.random() * (td - 1)); //0-29
    y = Math.round(Math.random() * (tr - 1));
    
    snake.pos.forEach(function(value) {
      if(x != value[0] && y != value[1]) {
        //這個(gè)條件成立說明現(xiàn)在隨機(jī)出來的這個(gè)坐標(biāo),在蛇身上并沒有找到
        include = false;
      }
    });
    
  }
  //生成食物
  food = new Square(x, y, 'food');
  food.pos = [x,y]; //存儲(chǔ)一下生成食物的坐標(biāo),用于跟蛇頭要走的下一個(gè)點(diǎn)作對(duì)比
  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;
  this.speed = 200;
}
Game.prototype.init = function() {
  snake.init();
  snake.getNextPos();
  createFood();
  
  document.onkeydown = function(ev) {  //用戶按下方向鍵觸發(fā)事件
    if(ev.which == 37 && snake.direction != snake.directionNum.right) { 
      //用戶按下左鍵是,蛇不能是往右走
      snake.direction = snake.directionNum.left;
    }else if(ev.which == 38 && snake.direction != snake.directionNum.dowm) {
      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();
    
  }, this.speed);
}
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 puseBtn = document.querySelector('.stopBtn button')
snakeWrap.onclick = function() {
  game.pause();
  puseBtn.parentNode.style.display = 'block';
}

puseBtn.onclick =function() {
  game.start();
  puseBtn.parentNode.style.display = 'none';
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

新聞標(biāo)題:如何基于javascript實(shí)現(xiàn)貪吃蛇游戲
文章網(wǎng)址:http://aaarwkj.com/article42/ggpshc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、服務(wù)器托管、全網(wǎng)營(yíng)銷推廣、網(wǎng)站策劃微信公眾號(hào)、網(wǎng)站收錄

廣告

聲明:本網(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í)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)
国产亚洲美女在线视频视频| 午夜影院网站在线看黄| 18禁视频免费无遮挡| 国产日韩熟女中文字幕| 亚洲精品久久麻豆蜜桃| 9热在线视频精品这里只有| 日本韩国国语对白一区二区三区| 青青草网站在线观看视频| 欧美日韩精品偷拍一区二区| 视频一区中文字幕在线| 亚洲一区精品二人人爽久久| 国产亚洲男人av一区三区| 一区二区三区四区在线视频观看 | 国产av超爽剧情系列| 精品一区二区日韩在线| 在线成人免费日韩视频| 少妇高潮试看二十分钟| 日韩一区二区高清视频在线观看| 久久午夜视频在线观看| 伊在人亚洲香蕉精品区| 日韩高清不卡在线视频| 美女在线视频一区二区三区| 中文字幕免费不卡一区| 欧美精品激情在线不卡| 日韩中文字幕视频一区| 日韩精品一二三区乱码| 国产中文字幕精品在线| 国产成人av在线观看| 中文字幕乱码熟女人妻视频| 国产日韩精品一区二区在线 | 18禁黄久久久一区二区三区| 人人妻人人澡人人爽久久av| 国产精品伦一区二区视频| 午夜福利视频在线观看| 亚洲av精二区三区四区| 欧美成人午夜精品一区二区| 国产亚洲精品久久综合阿香| 欧美日韩亚洲国产三级| 日本一区二区手机在线| 青青草原天堂在线免费观看| 真实夫妻露脸爱视频九色网|