欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

原生js實(shí)現(xiàn)貪吃蛇游戲

 更新時(shí)間:2020年10月26日 11:46:36   作者:leisure-ZL  
這篇文章主要為大家詳細(xì)介紹了原生js實(shí)現(xiàn)貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

原生JavaScript實(shí)現(xiàn)貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下

代碼:

<script>
 var timer = null;
 var oMain = document.getElementById("main");
 
 function Map(atom,xnum,ynum){//地圖,設(shè)置單位大小,及根據(jù)單位大小創(chuàng)建地圖
  this.atom = atom;
  this.xnum = xnum;
  this.ynum = ynum;
  
  this.create = function(){
  
  this.canvas = document.createElement("div");
  this.canvas.style.cssText = "position: relative;top: 40px;border: 1px solid red;background: #F1F1F1;"
  this.canvas.style.width = this.atom * this.xnum + "px";//畫(huà)布寬
  this.canvas.style.height = this.atom * this.ynum + "px";//畫(huà)布高
  main.appendChild(this.canvas);
  }
 }
 
 function Food(map){//食物
  this.width = map.atom;
  this.height = map.atom;
  //實(shí)現(xiàn)隨機(jī)背景色
  this.bgColor = "rgb("+Math.floor(Math.random()*200)+","+Math.floor(Math.random()*200)+","+Math.floor(Math.random()*200)+")";
 
  this.x = Math.floor(Math.random()*map.xnum);
  this.y = Math.floor(Math.random()*map.ynum);
  
  this.flag = document.createElement('div');
  this.flag.style.width = this.width + 'px';
  this.flag.style.height = this.height + 'px';
  this.flag.style.backgroundColor = this.bgColor;
  this.flag.style.borderRadius = '50%';
  this.flag.style.position = "absolute";
  this.flag.style.left = this.x * this.width + 'px';
  this.flag.style.top = this.y * this.height + 'px';
  
  map.canvas.appendChild(this.flag);
 }
 
 //重新開(kāi)始
 function restart(map,snake){
  for(var i=0; i<snake.body.length; i++){
  map.canvas.removeChild(snake.body[i].flag);
  }
  snake.body = [
  {x : 2,y : 0},//蛇頭
  {x : 1,y : 0},//蛇身
  {x : 0,y : 0}//蛇尾
  ]
  snake.direction = "right";
  snake.display();
  
  map.canvas.removeChild(food.flag);
  food = new Food(map);
 }
 
 function Snake(map){
  this.width = map.atom;
  this.height = map.atom;
  
  this.direction = "right";
  
  this.body = [
  {x : 2,y : 0},
  {x : 1,y : 0},
  {x : 0,y : 0}
  ]
  
  this.display = function(){
  for(var i=0; i<this.body.length; i++){
   if(this.body[i].x != null){
   var s = document.createElement('div');
   //將節(jié)點(diǎn)保存
   this.body[i].flag = s;
   
   //設(shè)置樣式
   s.style.width = this.width + 'px';
   s.style.height = this.height + 'px';
   s.style.backgroundColor = "rgb("+Math.floor(Math.random()*200)+","+Math.floor(Math.random()*200)+","+Math.floor(Math.random()*200)+")";
   s.style.borderRadius = '50%';
   
   //設(shè)置位置
   s.style.position = "absolute";
   s.style.left = this.body[i].x * this.width + 'px';
   s.style.top = this.body[i].y * this.height + 'px';
   
   //添加到地圖
   map.canvas.appendChild(s);
   }
  }
  }
  
  this.run = function(){
  for(var i=this.body.length-1; i>0; i--){
   this.body[i].x = this.body[i-1].x;
   this.body[i].y = this.body[i-1].y
   
  }
  switch(this.direction){
   case "left":this.body[0].x -= 1;break;
   case "right":this.body[0].x += 1;break;
   case "up":this.body[0].y -= 1;break;
   case "down":this.body[0].y += 1;break;
  }
  //吃食物
  if(this.body[0].x == food.x && this.body[0].y == food.y){
   this.body.push({x : null,y : null,flag : null});
   
   map.canvas.removeChild(food.flag);
   food = new Food(map);
  }
  
  //判斷游戲結(jié)束
  if(this.body[0].x<0 || this.body[0].x>map.xnum-1 || this.body[0].y<0 || this.body[0].y>map.ynum-1){
   clearInterval(timer);
   alert("GAME OVER!");
   restart(map,this);
   return false;
  }
  for(var i=4; i<this.body.length; i++){
   if(this.body[0].x == this.body[i].x && this.body[0].y == this.body[i].y){
   clearInterval(timer);
   alert("GAME OVER!");
   restart(map,this);
   return false;
   }
  }
  
  //刪除原來(lái)
  for(var i=0; i<this.body.length; i++){
   if(this.body[i].flag != null){
   map.canvas.removeChild(this.body[i].flag);
   }
  }
  this.display();
  }
 }

 
 //創(chuàng)建地圖對(duì)象
 var map = new Map(20,40,20);
 map.create();
 
 //創(chuàng)建食物對(duì)象
 var food = new Food(map);
 
 //創(chuàng)建蛇對(duì)象
 var snake = new Snake(map);
 snake.display();
 
 
 //加上鍵盤(pán)事件(改變蛇頭方向)
 window.onkeydown = function(e){
  event = e || window.event;
  
  switch(event.keyCode){
  case 38:
   if(snake.direction != "down")//禁止掉頭
   snake.direction = "up";
  break;
  case 40:
   if(snake.direction != "up")
   snake.direction = "down";
  break;
  case 37:
   if(snake.direction != "right")
   snake.direction = "left";
  break;
  case 39:
   if(snake.direction != "left")
   snake.direction = "right";
  break;
  }
 }
 
 var speed = 100;
 
 function start(){
  clearInterval(timer);
  timer = setInterval(function(){
  snake.run();
  document.getElementById("score").innerHTML = snake.body.length-3;
  start();
  },speed)
 }
 
 //難度
 document.getElementById("1").onclick = function(){
  speed = 100;
 }
 
 document.getElementById("2").onclick = function(){
  speed = 50;
 }
 
 document.getElementById("3").onclick = function(){
  speed = 20;
 }
 
 document.getElementById("begin").onclick = function(){
  start();
 }
 
 document.getElementById("pause").onclick = function(){
  
  clearInterval(timer);
 }
</script>

代碼僅有js部分,完整代碼可以上我的github免費(fèi)下載

更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:

C++經(jīng)典小游戲匯總

python經(jīng)典小游戲匯總

python俄羅斯方塊游戲集合

JavaScript經(jīng)典游戲 玩不停

javascript經(jīng)典小游戲匯總

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • JavaScript實(shí)現(xiàn)換膚效果(換背景)

    JavaScript實(shí)現(xiàn)換膚效果(換背景)

    這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)換膚效果,即換背景功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • javascript實(shí)現(xiàn)無(wú)縫上下滾動(dòng)特效

    javascript實(shí)現(xiàn)無(wú)縫上下滾動(dòng)特效

    這篇文章主要介紹了javascript實(shí)現(xiàn)無(wú)縫上下滾動(dòng)特效的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • 教大家輕松制作Bootstrap漂亮表格(table)

    教大家輕松制作Bootstrap漂亮表格(table)

    這篇文章主要為大家詳細(xì)介紹了Bootstrap輕松制作漂亮表格table的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • JavaScript使用Base64編碼和Blob對(duì)象加密圖像url地址

    JavaScript使用Base64編碼和Blob對(duì)象加密圖像url地址

    有時(shí)候會(huì)看到一些網(wǎng)站的圖片src中是blob:http://example.com/7c672acb-375c-4a26-9af9-99cb4c77f04d,這樣的圖片加載怎么實(shí)現(xiàn)呢?本文講解在瀏覽器中JavaScript使用解析Base64編碼和Blob對(duì)象技術(shù)來(lái)實(shí)現(xiàn),下面是實(shí)現(xiàn)的步驟和相應(yīng)的示例代碼,
    2023-12-12
  • 利用JS、CSS實(shí)現(xiàn)列表自動(dòng)滑動(dòng)滾動(dòng)效果實(shí)例

    利用JS、CSS實(shí)現(xiàn)列表自動(dòng)滑動(dòng)滾動(dòng)效果實(shí)例

    這篇文章主要給大家介紹了關(guān)于利用JS、CSS實(shí)現(xiàn)列表自動(dòng)滑動(dòng)滾動(dòng)效果的相關(guān)資料,本人在項(xiàng)目中遇到這樣的需求,親自來(lái)做的,通過(guò)html頁(yè)面配合js,css來(lái)做出的列表自動(dòng)滑動(dòng),需要的朋友可以參考下
    2024-07-07
  • JavaScript實(shí)現(xiàn)ArrayBuffer到Base64的轉(zhuǎn)換

    JavaScript實(shí)現(xiàn)ArrayBuffer到Base64的轉(zhuǎn)換

    本文探討了在 JavaScript 中將 ArrayBuffer 轉(zhuǎn)換為 Base64 字符串時(shí)遇到的棧溢出問(wèn)題,并提供了幾種實(shí)用的解決方案,我們將通過(guò)生動(dòng)的比喻來(lái)解釋相關(guān)概念,比較不同方法的性能和兼容性,最終提供一個(gè)平衡而實(shí)用的方法,需要的朋友可以參考下
    2024-10-10
  • ES6中l(wèi)et 和 const 的新特性

    ES6中l(wèi)et 和 const 的新特性

    這篇文章主要介紹了ES6中l(wèi)et 和 const 的新特性,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • 兩種簡(jiǎn)單的跨域方法(jsonp、php)

    兩種簡(jiǎn)單的跨域方法(jsonp、php)

    這篇文章主要為大家詳細(xì)介紹了兩種簡(jiǎn)單的跨域方法,使用jsonp和php實(shí)現(xiàn)跨域,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • js LZ77算法的實(shí)現(xiàn)代碼

    js LZ77算法的實(shí)現(xiàn)代碼

    JS操作二進(jìn)制很麻煩,而且一直沒(méi)有一個(gè)好的無(wú)損壓縮工具來(lái)實(shí)現(xiàn)純文本的壓縮。
    2010-04-04
  • JavaScript——DOM操作——Window.document對(duì)象詳解

    JavaScript——DOM操作——Window.document對(duì)象詳解

    下面小編就為大家?guī)?lái)一篇JavaScript——DOM操作——Window.document對(duì)象詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-07-07

最新評(píng)論