js实现贪吃蛇游戏含注释

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

两个小时完成的,有点简陋。

直接看效果。打开调试面板,在resource面板,新建snippet

粘贴以下代码,右键snippet,run。

clearInterval(timer);
document.body.innerHTML = "";

//每秒移动多少格
let speed = 10;
let speedUpMul = 3;

//是否能穿墙
let isThroughTheWall = true;

//行数
let row = 40;
let headColor = 'red';
let bodyColor = 'green';
let foodColor = 'yellow';
let borderColor = 'grey';

// 游戏全局变量
let hasFood = false;
//游戏状态
let gamestaus = 'start';
let hasAccelerate = false;

let mainContainer = document.createElement("div");
mainContainer.style.width = 20 * row + 1 + "px";
mainContainer.style.height = 20 * row + 1 + "px";
mainContainer.style.margin = "0 auto";
mainContainer.style.position = "relative";
mainContainer.style.border = "1px solid " + borderColor;

document.body.appendChild(mainContainer);

for(let i = 0;i<row;i++){
 let marginTop = 20 * i + "px";
 for(let j = 0;j<row;j++){
 let marginLeft = j * 20 + "px";
 let tempDiv = document.createElement('div');
 tempDiv.style.width = "19px";
 tempDiv.style.height = "19px";
 tempDiv.style.marginTop = marginTop;
 tempDiv.style.marginLeft = marginLeft;
 tempDiv.style.border = "0.5px solid " + borderColor;
 tempDiv.style.position = "absolute";
 tempDiv.id = j + "div" + i;
 mainContainer.appendChild(tempDiv);
 }
}

class Cell{
 constructor(x, y, color){
 if(isThroughTheWall){
  if(x < 0) x = row-1;
  if(x > row - 1) x = 0;
  if(y < 0) y = row - 1;
  if(y > row - 1) y = 0;
 }else{
  if(x < 0 || y < 0|| x > row - 1 || y > row - 1){
  clearInterval(timer);
  alert('游戏结束');
  return;
  }
 }
 this.x = x;
 this.y = y;
 this.color = color;
 let tempDiv = document.getElementById(x + 'div' + y);
 if(tempDiv) tempDiv.style.backgroundColor = color;
 }
}

snake = {
 head : {},
 body : [],
 dire : 1
}

let headx = Math.floor(Math.random() * 14) + 3;
let heady = Math.floor(Math.random() * 14) + 3;
snake.head = new Cell(headx, heady, headColor);

//上右下左
let direction = [1, 2, 3, 4]

snake.dire = direction[Math.floor(Math.random() * 4)];

if(snake.dire == 1){
 snake.body.push(new Cell(snake.head.x, snake.head.y+1, bodyColor));
 snake.body.push(new Cell(snake.head.x, snake.head.y+2, bodyColor));
 snake.body.push(new Cell(snake.head.x, snake.head.y+3, bodyColor));
}

if(snake.dire == 2){
 snake.body.push(new Cell(snake.head.x-1, snake.head.y, bodyColor));
 snake.body.push(new Cell(snake.head.x-2, snake.head.y, bodyColor));
 snake.body.push(new Cell(snake.head.x-3, snake.head.y, bodyColor));
}

if(snake.dire == 3){
 snake.body.push(new Cell(snake.head.x, snake.head.y-1, bodyColor));
 snake.body.push(new Cell(snake.head.x, snake.head.y-2, bodyColor));
 snake.body.push(new Cell(snake.head.x, snake.head.y-3, bodyColor));
}

if(snake.dire == 4){
 snake.body.push(new Cell(snake.head.x+1, snake.head.y, bodyColor));
 snake.body.push(new Cell(snake.head.x+2, snake.head.y, bodyColor));
 snake.body.push(new Cell(snake.head.x+3, snake.head.y, bodyColor));
}

function game(){
 if(gamestaus == 'pause'){
 return;
 }
 if(gamestaus == 'gameover'){
 clearInterval(timer);
 alert('游戏结束');
 return;
 }
 initFood();
 let snakeHeadX = snake.head.x;
 let snakeHeadY = snake.head.y;
 let color = '';
 if(snake.dire == 1){
 let tempDiv = document.getElementById(snakeHeadX + 'div' + (snakeHeadY-1));
 if(tempDiv) color = tempDiv.style.backgroundColor;
 snake.head = new Cell(snakeHeadX, snakeHeadY - 1, headColor);
 }
 if(snake.dire == 2){
 let tempDiv = document.getElementById((snakeHeadX + 1) + 'div' + snakeHeadY);
 if(tempDiv) color = tempDiv.style.backgroundColor;
 snake.head = new Cell(snakeHeadX + 1, snakeHeadY, headColor);
 }
 if(snake.dire == 3){
 let tempDiv = document.getElementById(snakeHeadX + 'div' + (snakeHeadY+1));
 if(tempDiv) color = tempDiv.style.backgroundColor;
 snake.head = new Cell(snakeHeadX, snakeHeadY + 1, headColor);
 }
 if(snake.dire == 4){
 let tempDiv = document.getElementById((snakeHeadX - 1) + 'div' + snakeHeadY);
 if(tempDiv) color = tempDiv.style.backgroundColor;
 snake.head = new Cell(snakeHeadX - 1, snakeHeadY, headColor);
 }
 snake.body.unshift(new Cell(snakeHeadX, snakeHeadY, bodyColor));
 if(color && color == foodColor){
 hasFood = false;
 initFood();
 }else if(color && color == bodyColor){
 gamestaus = 'gameover';
 }else{
 let lastBody = snake.body.pop();
 new Cell(lastBody.x, lastBody.y, '');
 }
}
var timer = setInterval(game, 10 / speed * 100)

/**
 * 初始化食物
 */
function initFood(){
 while(!hasFood){
 let x = Math.floor(Math.random() * row);
 let y = Math.floor(Math.random() * row);
 let snakeBody = snake.body;
 let enable = true;
 if(snake.head.x == x && snake.head.y == y){
  enable = false;
 }
 for(sBody of snakeBody){
  if(sBody.x == x && sBody.y == y){
  enable = false;
  break;
  }
 }
 if(enable){
  new Cell(x, y, foodColor);
  hasFood = true;
 }
 }
}

document.onkeydown = function(e){
 if(e.keyCode == 38){
 //上
 if(snake.dire != 3 && snake.dire != 1){
  snake.dire = 1;
 }else if(snake.dire == 1){
  if(!hasAccelerate){
  clearInterval(timer);
  hasAccelerate = true;
  speed = speed * speedUpMul;
  timer = setInterval(game, 10 / speed * 100)
  }
 }

 }

 if(e.keyCode == 39){
 //右
 if(snake.dire != 4 && snake.dire != 2){
  snake.dire = 2;
 }else if(snake.dire == 2){
  if(!hasAccelerate){
  clearInterval(timer);
  hasAccelerate = true;
  speed = speed * speedUpMul;
  timer = setInterval(game, 10 / speed * 100)
  }
 }
 }

 if(e.keyCode == 40){
 //下
 if(snake.dire != 1 && snake.dire != 3){
  snake.dire = 3;
 }else if(snake.dire == 3){
  if(!hasAccelerate){
  clearInterval(timer);
  hasAccelerate = true;
  speed = speed * speedUpMul;
  timer = setInterval(game, 10 / speed * 100)
  }
 }
 }

 if(e.keyCode == 37){
 //左
 if(snake.dire != 2 && snake.dire != 4){
  snake.dire = 4;
 }else if(snake.dire == 4){
  if(!hasAccelerate){
  clearInterval(timer);
  hasAccelerate = true;
  speed = speed * speedUpMul;
  timer = setInterval(game, 10 / speed * 100)
  }
 }
 }
 //空格键暂停
 if(e.keyCode == 32){
 if(gamestaus == 'start'){
  gamestaus = 'pause';
 }else if(gamestaus == 'pause'){
  gamestaus = 'start';
 }
 }
}

document.onkeyup = function(e){
 if(e.keyCode == 38){
 //上
 if(snake.dire == 1 && hasAccelerate){
  clearInterval(timer);
  hasAccelerate = false;
  speed = speed / speedUpMul;
  timer = setInterval(game, 10 / speed * 100)
 }

 }

 if(e.keyCode == 39){
 //右
  if(snake.dire == 2 && hasAccelerate){
  clearInterval(timer);
  hasAccelerate = false;
  speed = speed / speedUpMul;
  timer = setInterval(game, 10 / speed * 100)
 }
 }

 if(e.keyCode == 40){
 //下
  if(snake.dire == 3 && hasAccelerate){
  clearInterval(timer);
  hasAccelerate = false;
  speed = speed / speedUpMul;
  timer = setInterval(game, 10 / speed * 100)
 }
 }

 if(e.keyCode == 37){
 //左
 if(snake.dire == 4 && hasAccelerate){
  clearInterval(timer);
  hasAccelerate = false;
  speed = speed / speedUpMul;
  timer = setInterval(game, 10 / speed * 100)
 }
 }
}

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

(0)

相关推荐

  • JS写的贪吃蛇游戏(个人练习)

    JS贪吃蛇游戏,个人练习之用,放在这备份一下,   复制代码 代码如下: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JS贪吃蛇-练习</t

  • js贪吃蛇游戏实现思路和源码

    本文实例为大家分享了js贪吃蛇游戏的相关代码,供大家参考,具体内容如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>贪吃蛇小游戏</title> <style> *{margin:0; padding:0;} header { display: block; margin: 0 auto;

  • 20行js代码实现的贪吃蛇小游戏

    前言 最近在csdn上看到一位大神用20行代码就写出了一个贪吃蛇的小游戏,感觉被惊艳到了,就试着读了一下这段代码,阅读过程中不断为作者写法的巧妙而叫绝,其中我发现自己对运算符优先级和一些js的技巧不是很清楚,所以看完之后决定把思路分享出来,方便和我一样的小白学习. 我对代码稍稍做了些修改,并添加了一些注释,方便理解. 示例代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="

  • javascript实现简单的贪吃蛇游戏

    javascript实现简单的贪吃蛇游戏,功能很简单,代码也很实用,就不多废话了,小伙伴们参考注释吧. <html> <head> <meta http-equiv='content-type' content='text/html;charset=utf-8'> <title>贪吃蛇</title> <script type="text/javascript"> var map; //地图 var snake;

  • JS实现的贪吃蛇游戏完整实例

    本文实例讲述了JS实现的贪吃蛇游戏.分享给大家供大家参考,具体如下: 思想: 1.设计蛇:属性有宽.高.方向.状态(有多少节),方法:显示,跑 2.设计食物:属性宽.高 3.显示蛇:根据状态向地图里加元素 4.蛇跑起来:下一节到前一节的位置,蛇头根据方向变,删除原来的蛇,新建蛇:当出界时,死亡,初始化:当蛇头吃到自己的时候,死亡,初始化 5.食物被吃掉,蛇加一节,去掉原来的食物,生成新的食物 6.添加定时器,绑定按键 完整示例: <!doctype html> <html lang=&q

  • 原生js实现的贪吃蛇网页版游戏完整实例

    本文实例讲述了原生js实现的贪吃蛇网页版游戏.分享给大家供大家参考.具体实现方法如下: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>原生js写的贪吃蛇网页版游戏</title> </head> <body> </body> <sc

  • js编写“贪吃蛇”的小游戏

    贪吃蛇儿时的回忆,今天刚好学习到这了,就刚好做了一个,也是学习了吧,需要掌握的知识: 1.JS函数的熟练掌握, 2.JS数组的应用, 3.JS小部分AJAX的学习 4.JS中的splice.shift等一些函数的应用, 基本上就这些吧,下面提重点部分: 前端的页面,这里可自行布局,我这边提供一个我自己的布局: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org

  • javascript 贪吃蛇实现代码

    在习作的过程中尝试着贪吃蛇游戏用JS实现了.竟然成功了. 思路:使用10px*10px的div层担当"像素",然后使用40*40矩阵160个"像素"构成了游戏的界面. 下面是代码: 复制代码 代码如下: // JavaScript Document alert("键盘的方向键控制方向,空格键暂停.\nLIFE制作\nhttp://blog.csdn.net/anhulife"); // 添加基本的图形块,即160个10 * 10的层组成的二维矩阵

  • js实现贪吃蛇小游戏(容易理解)

    话不多说,请看代码: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>贪吃蛇</title> <link rel="stylesheet" href="style.css"> <script src="style.js" >

  • javascript贪吃蛇完整版(源码)

    javascript贪吃蛇完整版 注释完整,面向对象 复制代码 代码如下: <html><head>    <title>贪吃蛇 Snake v2.4</title><style>    body{        font-size:9pt;    }    table{        border-collapse: collapse;        border:solid #333 1px;    }    td{        heigh

随机推荐