圆月山庄资源网 Design By www.vgjia.com

本文实例为大家分享了JS实现简单贪吃蛇游戏的具体代码,供大家参考,具体内容如下

1、使用语言 HTML+CSS+JavaScript
2、使用工具 visual studio code
3、GitHub项目地址:贪吃蛇
4、项目运行截图:

JS实现简单贪吃蛇小游戏

5、项目功能分析:主要使用数组来存储蛇的位置,地图和蛇都是一个二维数组,对于有蛇的地方,地图的CSS就会发生改变,同时添加了添加了一个音乐播放按钮,通过CSS动画实现旋转。

6、项目代码:(项目代码有详细的注释)

snake.html

<!--
 * @Author: CSU_XZY
 * @Date: 2020-10-13 22:06:51
 * @LastEditors: CSU_XZY
 * @LastEditTime: 2020-10-18 17:08:54
 * @FilePath: \第二天\贪吃蛇\snake.html
 * @Description: just to play
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>eatSnake</title>
<head>
 <style type="text/css">
 *{
 margin:0;
 padding:0;
 border: 0px;
 }
 body{
 background: #444;
 }
 table{
 border-collapse:collapse;
 overflow: hidden;
 border:1px solid #ddd;
 margin:10px auto 10px auto;
 }
 td{
 display: table-cell;
 width:20px;
 height:20px;
 background: #fff;
 border:1px #eeeeee solid;
 }
 .snake{
 background: #3388ee;
 }
 .notsnake{
 background: #fff;
 }
 .food{
 background: #33aa33;
 }
 .snake_head{
 background: #dd4444;
 }
 .notice{
 width:450px;
 text-align: center;
 margin:50px auto;
 color:#fff;
 font-size: 14px;
 }
 </style>
</head>
<body>
<p class="notice">提示:空格键控制开始/ 暂停,方向键控制蛇的移动方向,F5刷新</p>
<script type="text/javascript" src="/UploadFiles/2021-04-02/snake.js">

snake.js

/*
 * @Author: CSU_XZY
 * @Date: 2020-10-17 12:38:26
 * @LastEditors: CSU_XZY
 * @LastEditTime: 2020-10-18 22:40:24
 * @FilePath: \第二天\snake\snake.js
 * @Description: just to play
 */
window.onload = function(){
 var snake = function(width,height,snakeId,speed){
  this.width = width || 10;
  this.height = height || 10;
  this.snakeId = snakeId || "snake";
  this.goX = 0;
  this.goY = 0;
  this.speed = this.oldSpeed = speed || 10;
  this.Grid = []; //存放td的二维数组
  this.snakeGrid = []; //存放蛇的数组
  this.foodGrid = [];
  this.snakeTimer = null;
  this.derectkey = 39;
  this.stop = true;
  this.init();
  document.getElementById("myAudio").play();
  box.style.animationPlayState = 'running'
 }

 snake.prototype = {
  //创建二维数组
  multiArray : function(m , n)
  {
   var array = new Array(m); //长
   for(let i = 0; i < m; i++)
   {
    array[i] = new Array(n); //宽
   }
   return array;
  },
  //函数修正this
  bind : function(fn,context)
  {
   return function(){
 return fn.apply(context,arguments);
 }
  },
  //移动的主函数
  move:function(){
 var _this = this;
 if(_this.snakeTimer){clearInterval(_this.snakeTimer);}
 _this.snakeTimer = setInterval(_this.bind(_this.main,_this),Math.floor(3000/this.speed));
  },
  //重来
  reset()
  {
   this.goX = 0;
   this.goY = 0;
   this.speed = this.oldSpeed;
   this.derectkey = 39;
   this.stop = true;
   this.init();
  },
  //确定键盘事件
  keyDown : function(e)
  { 
   var e = e || window.event;
   var keycode = e"table");
   var tbody = document.createElement("tbody");
   for(let i = 0; i < this.width; i++)
   {
    var tr = document.createElement("tr");
    for(let j = 0; j < this.height; j++)
    {
     var td = document.createElement("td");
     this.Grid[i][j] = tr.appendChild(td);
    }
    tbody.appendChild(tr);
   }
   table.appendChild(tbody);
   table.id = this.snakeId;
   document.body.appendChild(table);
  },
  //产生随机点
  randomPoint : function(initX,initY,endX,endY)
  {
   var p = []; //用来存放产生的随机点的数组
   var initX = initX || 0;
   var initY = initY || 0;
   var endX = endX || this.width;
   var endY = endY || this.height;
   p[0] = Math.floor(Math.random()*(endX - initX)) + Math.floor(initX);
   p[1] = Math.floor(Math.random()*(endY - initY)) + Math.floor(initY)
   return p;
  },
  //初始化食物的位置
  initFood : function()
  {
   this.foodGrid = this.randomPoint();

   if(this.isInSnake(this.foodGrid))
   {
    this.initFood();
    return false;
   }
   this.Grid[this.foodGrid[0]][this.foodGrid[1]].className = "food";
  },
  //判断点是否在蛇身上
  isInSnake : function(point,pos)
  {
   var snakeGrid = this.snakeGrid;
   if(point instanceof Array)
   {
    let n = snakeGrid.length;
    for(let i = pos || 0; i < n; i++)
    {
     if(point[0] == snakeGrid[i][0] && point[1] == snakeGrid[i][1])
     return true;
    }
   }
   return false;
  },
  //给蛇涂颜色
  paintSnake : function(){
   var snakeGrid = this.snakeGrid;
   for(let i = 0; i < snakeGrid.length; i++)
   {
    this.Grid[snakeGrid[i][0]][snakeGrid[i][1]].className = "snake_body";
   }
  },
  //初始化蛇的位置
  initSnake : function()
  {
   this.snakeGrid = [];

   this.snakeGrid.push([1,3]);
   this.snakeGrid.push([1,2]);
   this.snakeGrid.push([1,1]);

   this.paintSnake();

   this.Grid[this.snakeGrid[0][0]][this.snakeGrid[0][1]].className = "snake_head";
   this.Grid[this.snakeGrid[this.snakeGrid.length-1][0]][this.snakeGrid[this.snakeGrid.length-1][1]].className = "snake_tail";
  },
  //判断蛇是否撞墙
  isInWall : function(point){
   if(point instanceof Array){
    if(point[0] < 0 || point[0] > this.width1 - 1 || point[1] < 0 || point[1] > this.height - 1)
    return true;
   } 
   return false;
  },
  //初始化条件
  
  //控制函数运行的主函数
  main : function(){
   var snakeGrid = this.snakeGrid;
   var temp = snakeGrid[snakeGrid.length-1],
    isEnd = false;
    headX = snakeGrid[0][0];
    headY = snakeGrid[0][1];
    msg = "";
   switch(this.derectkey)
   {
    case 37:
  if(this.goY!=1){this.goY=-1;this.goX=0} //防止控制蛇往相反反方向走
  break;
 case 38:
  if(this.goX!=1){this.goX=-1;this.goY=0}
  break;
 case 39:
  if(this.goY!=-1){this.goY=1;this.goX=0}
  break;
 case 40:
  if(this.goX!=-1){this.goX=1;this.goY=0}
   }
   headX += this.goX;
   headY += this.goY;

   if(headX == this.foodGrid[0] && headY == this.foodGrid[1])
   {
    this.snakeGrid.unshift(this.foodGrid);
    this.initFood();
    if(this.snakeGrid.length>4){ //控制蛇加速
  if(this.snakeGrid.length==5){
  this.speed += 5;
  }
  else if(this.snakeGrid.length==10){
  this.speed += 3;
  }
  else if(this.snakeGrid.length==20){
  this.speed += 3;
  }
  else if(this.snakeGrid.length==30){
  this.speed += 3;
  }
  this.move();
 }
   }
   else
   {
    for(var i=this.snakeGrid.length-1;i>0;i--){
  this.snakeGrid[i] = this.snakeGrid[i-1] ;
 }
    this.snakeGrid[0] = [headX,headY]; 
    if(this.isInSnake(this.snakeGrid[0],1)){
  isEnd=true;
  msg = "哈皮,吃到自己啦!!";
 }
 //判断是否撞墙
 else if(this.isInWall(this.snakeGrid[0])){
  isEnd =true;
  msg = "撒比伟哥,撞墙了!!";
    }
    if(isEnd)
    {
     if(this.snakeTimer)
     clearInterval(this.snakeTimer);
     var score;
     let len = this.snakeGrid.length;
     if(len <= 5)
     score = len-3;
     else if(len>5 && len<=10)
     {
      score = 2 + 2*(len-5)
     }
     else if(len>10 && len <= 20)
     score = 12 + 3*(len-10);
     else
     score = 27 + 5*(len - 15);
     if(confirm(msg+"你的分数是:"+score+"! 是否重新开始?")){
  this.reset();
  }
  return false;
    }
    this.Grid[temp[0]][temp[1]].className = "notSnake";
   }
   this.paintSnake();
   this.Grid[headX][headY].className = "snake_head";
   this.Grid[this.snakeGrid[this.snakeGrid.length-1][0]][this.snakeGrid[this.snakeGrid.length-1][1]].className = "snake_tail";
  },
  init : function(){
   var _this = this;
   snake_id = document.getElementById(_this.snakeId)||0 ;
 if(snake_id){
 document.body.removeChild(snake_id);
 }
   _this.Grid = _this.multiArray(_this.width,_this.height);
   _this.creatMap();
   _this.initSnake();
   _this.initFood();
   document.onkeydown = _this.bind(_this.keyDown,_this);
  }
 }
 new snake(20,20,"snake",10);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
js,贪吃蛇

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。