HTML+js实现贪吃蛇小游戏(内含完整代码)

案例分析

看图拆解游戏

首先我们根据图片上的内容把这个游戏拆解成几个部分去单独看:

  1. 最外面的大盒子包裹着内容加边框限制蛇的活动范围,整个范围可以看成由许多小方格排列构成的,例如这样子的:
  2. 两个按钮,一个控制开始游戏,一个控制游戏中途的暂停继续功能;
  3. 盒子里面有可以移动的蛇,最开始状态的蛇分为蛇头、蛇身、蛇尾三个部分,蛇只能走直线,通过上⬆下⬇左⬅右⬅的功能键去控制蛇的走向;
  4. 还有一个随机产生在限制区域内的食物;

这个游戏是当点击开始游戏按钮才显示蛇和食物的,所以最开始我们不在结构里面书写,后面通过js构造函数来生成,但是可以先把样式写了来,由此可知:我们的结构代码可以这么去写:

 
  1. <body>

  2. <!-- 最外面盒子 -->

  3. <div class="content">

  4. <!-- 开始按钮 -->

  5. <div class="btn startBtn">

  6. <button></button>

  7. </div>

  8. <!-- 暂停按钮 -->

  9. <div class="btn pauseBtn">

  10. <button></button>

  11. </div>

  12. <!-- 蛇的活动范围 -->

  13. <div id="snakeWrap">

  14. </div>

  15. </div>

  16. </body>

CSS样式代码是这样的:

 
  1. .content {

  2. width: 640px;

  3. height: 640px;

  4. margin: 35px auto;

  5. position: relative;

  6. }

  7. .btn {

  8. width: 100%;

  9. height: 100%;

  10. position: absolute;

  11. left: 0;

  12. top: 0;

  13. background-color: rgba(0, 0, 0, .3);

  14. z-index: 2;

  15. }

  16. .btn button {

  17. background: none;

  18. border: none;

  19. background-size: 100% 100%;

  20. cursor: pointer;

  21. outline: none;

  22. position: absolute;

  23. left: 50%;

  24. top: 50%;

  25. }

  26. .startBtn button {

  27. width: 200px;

  28. height: 80px;

  29. background-image: url(../images/start.gif);

  30. margin-left: -100px;

  31. margin-top: -40px;

  32. }

  33. .pauseBtn {

  34. display: none;

  35. }

  36. .pauseBtn button {

  37. width: 70px;

  38. height: 70px;

  39. background-image: url(../images/pause.png);

  40. margin-left: -35px;

  41. margin-top: -35px;

  42. }

  43. /* snakeWrap */

  44. #snakeWrap {

  45. position: relative;

  46. width: 600px;

  47. height: 600px;

  48. background-color: greenyellow;

  49. border: 20px solid green;

  50. }

  51. #snakeWrap div {

  52. width: 20px;

  53. height: 20px;

  54. }

  55. .snakeHead {

  56. background-image: url(../images/蛇头.png);

  57. background-size: cover;

  58. }

  59. .snakeBody {

  60. background-color: #808ca5;

  61. border-radius: 50%;

  62. }

  63. .food {

  64. background-image: url(../images/食物.png);

  65. background-size: cover;

  66. }

蛇的活动范围分析:

下面的代码方块构造函数就是创建一个一个的方格,给每个方格设置了宽高,这个大小是经过计算的可以正好铺满整个蛇的活动范围的地砖,就好比我准备了一整个房间需要的地砖,但是只是准备了并没有拿出来;给原型添加或删除的函数是我具备了铺地装这样子的技术;现在就是人和了,坐等天时地利的时候使用。

js代码:

 
  1. var sw = 20, //一个方格的宽

  2. sh = 20, //一个方格的高

  3. tr = 30, //行数

  4. td = 30; //列数

  5. //方块构造函数

  6. function Square(x, y, classname) {

  7. //0,0 0,0

  8. //20,0 1,0

  9. //40,0 2,0

  10. this.x = x * sw;

  11. this.y = y * sh;

  12. this.class = classname;

  13. this.viewContent = document.createElement('div'); //方块对应的DOM元素

  14. this.viewContent.className = this.class; //方块所指元素

  15. this.parent = document.getElementById('snakeWrap'); //方块的父级

  16. }

  17. //单词prototype指的是原型

  18. //给原型添加

  19. Square.prototype.create = function() { //创建方块DOM,并添加到页面里

  20. this.viewContent.style.position = 'absolute';

  21. this.viewContent.style.width = sw + 'px';

  22. this.viewContent.style.height = sh + 'px';

  23. this.viewContent.style.left = this.x + 'px';

  24. this.viewContent.style.top = this.y + 'px';

  25. this.parent.appendChild(this.viewContent);

  26. };

  27. //删除

  28. Square.prototype.remove = function() {

  29. this.parent.removeChild(this.viewContent);

  30. };

创建对象——蛇的分析:

下面的代码看蛇的部分,想要蛇移动,必须先有蛇,开始的时候就说了蛇又分为三个部分蛇头、蛇身和蛇尾,蛇需要移动,蛇移动又分为上下左右四个方向,所以我们需要根据x和y的正负来判断它走的方向,当蛇转向的时候需要蛇头跟着转向,所以要添加一个rotate属性来确定蛇头的方向。

js代码:

 
  1. //蛇

  2. var snake = null;

  3. function Snake() {

  4. this.head = null; //存一下蛇头信息

  5. this.tail = null; //存一下蛇尾信息

  6. this.pos = []; //存蛇身上每一个方块的位置

  7. this.directionNum = { //存储蛇走的方向,用一个对象来表示

  8. left: {

  9. x: -1,

  10. y: 0,

  11. rotate: 180 // 蛇头在不同方向中应该进行旋转

  12. },

  13. right: {

  14. x: 1,

  15. y: 0

  16. },

  17. up: {

  18. x: 0,

  19. y: -1,

  20. rotate: -90

  21. },

  22. down: {

  23. x: 0,

  24. y: 1,

  25. rotate: 90

  26. }

  27. }

  28. }

初始化对象分析:

蛇有了,我们现在需要把蛇放到最开始的位置,设置坐标,这时候就初始化蛇,这里有个新的知识点就是链表关系,如下图,相当于在已知关系中确定各自的相对位置。

 js代码:

 
  1. //单词init指的是初始化

  2. Snake.prototype.init = function() {

  3. //创建一个蛇头

  4. var snakeHead = new Square(2, 0, 'snakeHead');

  5. snakeHead.create();

  6. this.head = snakeHead; //存储蛇头信息

  7. this.pos.push([2, 0]); //把蛇头的位置存起来

  8. //创建蛇身体1

  9. var snakeBody1 = new Square(1, 0, 'snakeBody');

  10. snakeBody1.create();

  11. this.pos.push([1, 0]); //把蛇身1的坐标也存起来

  12. //创建蛇身体2

  13. var snakeBody2 = new Square(0, 0, 'snakeBody');

  14. snakeBody2.create();

  15. this.tail = snakeBody2; //把蛇尾信息存起来

  16. this.pos.push([0, 0]); //把蛇身2的坐标也存起来

  17. //形成链表关系

  18. snakeHead.last = null;

  19. snakeHead.next = snakeBody1;

  20. snakeBody1.last = snakeHead;

  21. snakeBody1.next = snakeBody2;

  22. snakeBody2.last = snakeBody1;

  23. snakeBody2.next = null;

  24. //给蛇添加一个属性,用来表示蛇走的方向

  25. this.direction = this.directionNum.right; //默认让蛇往右走

  26. };

创建食物以及生成新的食物:

首先定义一个食物实例,食物的产生是随机的所以会用到随机数,但是食物产生的位置是有范围的,一是只能在一定区域内,所以需要设置参数使他不能超过边界;二是食物不能出现在蛇的身上,所以就需要判断是否在蛇身上,如果食物的位置与蛇身上的每一个身体部分的坐标不重合,就代表没有在蛇身上。

新的食物的生成条件是当蛇吃了食物(就代表场上没有食物了),才会生成新的食物。食物的消失条件就是去判断蛇头的下一个位置是否和食物的位置重合,如果重合就让食物消失。

js代码:

 
  1. //创建食物

  2. var food = null,//食物的实例

  3. function createFood() {

  4. //食物小方块的随机坐标

  5. var x = null;

  6. var y = null;

  7. var include = true; //循环跳出的条件,true表示随机生成食物的坐标在蛇身上(需要继续循环);false表示食物坐标不在蛇身上(不循环了)

  8. while (include) {

  9. x = Math.round(Math.random() * (td - 1));

  10. y = Math.round(Math.random() * (tr - 1));

  11. snake.pos.forEach(function(value) {

  12. if (x != value[0] && y != value[1]) {

  13. //这个条件成立说明在随机出来的坐标,在蛇身上并没有找到

  14. include = false;

  15. }

  16. });

  17. }

  18. //生成食物

  19. food = new Square(x, y, 'food');

  20. food.pos = [x, y]; //存储一下生成食物的坐标,用于跟蛇头要走的下一个点做对比

  21. var foodDom = document.querySelector('.food');

  22. if (foodDom) {

  23. foodDom.style.left = x * sw + 'px';

  24. foodDom.style.top = y * sh + 'px';

  25. } else {

  26. food.create();

  27. }

  28. }

判断蛇头下一步的位置分析:

蛇头下一步又分成4种情况:一是撞到自己的身体了,游戏结束;二是撞到围墙了,游戏结束;三是碰到食物了,吃掉食物;四是前面方格子什么也没有,继续走;每一种情况都对应一种结果。

js代码:

 
  1. //这个方法用来获取蛇头下一个位置对应的元素,要根据元素做不同的事情

  2. Snake.prototype.getNextPos = function() {

  3. var nextPos = [

  4. this.head.x / sw + this.direction.x,

  5. this.head.y / sh + this.direction.y

  6. ]

  7. //单词forEach代表遍历数组

  8. //(1)下个点是自己,代表撞到了自己,游戏结束

  9. var selfCollied = false; //是否撞到自己

  10. this.pos.forEach(function(value) {

  11. if (value[0] == nextPos[0] && value[1] == nextPos[1]) {

  12. //如果数组中两个数据都相等,说明下一个点在蛇身体里面能找到自己了

  13. selfCollied = true;

  14. }

  15. });

  16. if (selfCollied) {

  17. this.strategies.die.call(this);

  18. return;

  19. }

  20. //(2)下个点是围墙,代表撞到了围墙,游戏结束

  21. if (nextPos[0] < 0 || nextPos[1] < 0 || nextPos[0] > td - 1 || nextPos[1] > tr - 1) {

  22. this.strategies.die.call(this);

  23. return;

  24. }

  25. //(3)下个点是食物,吃

  26. if (food && food.pos[0] == nextPos[0] && food.pos[1] == nextPos[1]) {

  27. //如果这个条件成立,说明蛇头要走的下一个点是食物

  28. this.strategies.eat.call(this);

  29. return;

  30. }

  31. //(4)下个点什么都不是,走

  32. this.strategies.move.call(this); //call作用调用,也可以传参

  33. };

碰撞后要做的事情分析:

碰撞后要做的三件事:eat(吃)、die(死亡)、move(走)。

(一)eat很简单,调用一下创建食物的函数,食物被吃掉和产生新的食物;

(二)die也很简单,结束游戏,弹出得分对话框;

(三)move可能有点复杂,这里涉及到一个链表重新链接的情况。这个时候又要分为两种情况了:第一种:还没吃到食物的情况下,保持长度不变,每走一步就需要往前面一个方格里增加一个新身体,更新蛇头和蛇尾;第二种:吃到食物的情况,增加蛇身长度,继续更新蛇头位置并且在蛇头后的位置增加一个蛇身,原先的身上位置不改。可能文字看了还不懂,为此博主做了图示让大家能更好的理解。

图示:一句话概括,第一种情况如图第三步:新头是新头,旧头是新身,旧身是新尾;第二种情况如图第二步:新头是新头,旧头是新身,旧身还是旧身,旧尾还是旧尾。

第一步:newBody.next = this.head.next;新身体的next指向现在旧蛇头;

第二步:newBody.next.last = newBody;旧蛇头的上一个就指向现在的新蛇头了;

第三步:newBody.last = null;让新蛇头指向null,this.head.remove();删除旧蛇头,新身体替代旧蛇头的位置,除了吃食物增加身体长度外,其他的移动的情况都是this.tail.remove();  this.tail = this.tail.last;。每次移动都要创建一个新蛇头,更新一下链表关系:newHead.next = newBody;newHead.last = null;newBody.last = newHead;  并且根据移动方向调整一下头的方向。

js代码:

 
  1. //处理碰撞后要做的事

  2. Snake.prototype.strategies = {

  3. move: function(format) { //括号内参数用于决定要不要删除最后一个方块(蛇尾),当传来参数就表示要做的事情是吃

  4. //创建新身体(在旧蛇头的位置)

  5. var newBody = new Square(this.head.x / sw, this.head.y / sh, 'snakeBody');

  6. //更新链表关系

  7. newBody.next = this.head.next;

  8. newBody.next.last = newBody;

  9. newBody.last = null;

  10. this.head.remove(); //把旧蛇头从原来的位置删除

  11. newBody.create();

  12. //创建一个新蛇头(蛇头下一个要走到的点nextPos)

  13. var newHead = new Square(this.head.x / sw + this.direction.x, this.head.y / sh + this.direction.y, 'snakeHead');

  14. //更新链表关系

  15. newHead.next = newBody;

  16. newHead.last = null;

  17. newBody.last = newHead;

  18. newHead.viewContent.style.transform = 'rotate(' + this.direction.rotate + 'deg)';

  19. newHead.create();

  20. //蛇身上的每一个方块的坐标也要更新

  21. this.pos.splice(0, 0, [this.head.x / sw + this.direction.x, this.head.y / sh + this.direction.y])

  22. this.head = newHead; //还要把this.head的信息更新一下

  23. if (!format) { //如果format的值为false,表示需要删除(除了吃之外的操作)

  24. this.tail.remove();

  25. this.tail = this.tail.last;

  26. this.pos.pop();

  27. }

  28. },

  29. eat: function() {

  30. this.strategies.move.call(this, true);

  31. createFood();

  32. game.score++;

  33. },

  34. die: function() {

  35. // console.log('die');

  36. game.over();

  37. }

  38. }

  39. snake = new Snake();

游戏逻辑

开启游戏和暂停游戏,通过点击按钮的方法控制,游戏过程中通过功能键的which值去判断玩家按下的是上下左右哪个键来移动蛇。当蛇正在往左走的时候,⬅➡这两个键按下去就没有反应,只能按其他两个个键⬆⬇,同理可得,其他的也是一样。食物的生成需要通过一个定时器去控制它的自动出现。

js代码:

 
  1. //创建游戏逻辑

  2. function Game() {

  3. this.timer = null;

  4. this.score = 0;

  5. }

  6. Game.prototype.init = function() {

  7. snake.init();

  8. createFood();

  9. document.onkeydown = function(ev) {

  10. if (ev.which == 37 && snake.direction != snake.directionNum.right) { //用户按下左键时,这条蛇不能是正在往右走

  11. snake.direction = snake.directionNum.left;

  12. } else if (ev.which == 38 && snake.direction != snake.directionNum.down) {

  13. snake.direction = snake.directionNum.up;

  14. } else if (ev.which == 39 && snake.direction != snake.directionNum.left) {

  15. snake.direction = snake.directionNum.right;

  16. } else if (ev.which == 40 && snake.direction != snake.directionNum.up) {

  17. snake.direction = snake.directionNum.down;

  18. }

  19. }

  20. this.start();

  21. }

  22. Game.prototype.start = function() { //开始游戏

  23. this.timer = setInterval(function() {

  24. snake.getNextPos();

  25. }, 200);

  26. }

  27. Game.prototype.pause = function() {

  28. clearInterval(this.timer);

  29. }

  30. Game.prototype.over = function() {

  31. clearInterval(this.timer);

  32. alert('你的得分为' + this.score);

  33. //游戏回到最初始的状态

  34. var snakeWrap = document.getElementById('snakeWrap');

  35. snakeWrap.innerHTML = '';

  36. snake = new Snake();

  37. game = new Game();

  38. var startBtnWrap = document.querySelector('.startBtn');

  39. startBtnWrap.style.display = 'block';

  40. }

  41. //开启游戏

  42. game = new Game();

  43. var startBtn = document.querySelector('.startBtn button');

  44. startBtn.onclick = function() {

  45. startBtn.parentNode.style.display = 'none';

  46. game.init();

  47. };

  48. //暂停游戏

  49. var snakeWrap = document.getElementById('snakeWrap');

  50. var pauseBtn = document.querySelector('.pauseBtn button');

  51. snakeWrap.onclick = function() {

  52. game.pause();

  53. pauseBtn.parentNode.style.display = 'block';

  54. };

  55. pauseBtn.onclick = function() {

  56. game.start();

  57. pauseBtn.parentNode.style.display = 'none';

  58. }

  59. })

完整js代码

JavaScript:

 
  1. window.addEventListener('load', function() {

  2. var sw = 20, //一个方格的宽

  3. sh = 20, //一个方格的高

  4. tr = 30, //行数

  5. td = 30; //列数

  6. var snake = null, //蛇的实例

  7. food = null, //食物的实例

  8. game = null; //游戏的实例

  9. //方块构造函数

  10. function Square(x, y, classname) {

  11. //0,0 0,0

  12. //20,0 1,0

  13. //40,0 2,0

  14. this.x = x * sw;

  15. this.y = y * sh;

  16. this.class = classname;

  17. this.viewContent = document.createElement('div'); //方块对应的DOM元素

  18. this.viewContent.className = this.class; //方块所指元素

  19. this.parent = document.getElementById('snakeWrap'); //方块的父级

  20. }

  21. //原型prototype

  22. //给原型添加

  23. Square.prototype.create = function() { //创建方块DOM,并添加到页面里

  24. this.viewContent.style.position = 'absolute';

  25. this.viewContent.style.width = sw + 'px';

  26. this.viewContent.style.height = sh + 'px';

  27. this.viewContent.style.left = this.x + 'px';

  28. this.viewContent.style.top = this.y + 'px';

  29. this.parent.appendChild(this.viewContent);

  30. };

  31. //删除

  32. Square.prototype.remove = function() {

  33. this.parent.removeChild(this.viewContent);

  34. };

  35. //蛇

  36. function Snake() {

  37. this.head = null; //存一下蛇头信息

  38. this.tail = null; //存一下蛇尾信息

  39. this.pos = []; //存蛇身上每一个方块的位置

  40. this.directionNum = { //存储蛇走的方向,用一个对象来表示

  41. left: {

  42. x: -1,

  43. y: 0,

  44. rotate: 180 // 蛇头在不同方向中应该进行旋转

  45. },

  46. right: {

  47. x: 1,

  48. y: 0

  49. },

  50. up: {

  51. x: 0,

  52. y: -1,

  53. rotate: -90

  54. },

  55. down: {

  56. x: 0,

  57. y: 1,

  58. rotate: 90

  59. }

  60. }

  61. }

  62. //init初始化

  63. Snake.prototype.init = function() {

  64. //创建一个蛇头

  65. var snakeHead = new Square(2, 0, 'snakeHead');

  66. snakeHead.create();

  67. this.head = snakeHead; //存储蛇头信息

  68. this.pos.push([2, 0]); //把蛇头的位置存起来

  69. //创建蛇身体1

  70. var snakeBody1 = new Square(1, 0, 'snakeBody');

  71. snakeBody1.create();

  72. this.pos.push([1, 0]); //把蛇身1的坐标也存起来

  73. //创建蛇身体2

  74. var snakeBody2 = new Square(0, 0, 'snakeBody');

  75. snakeBody2.create();

  76. this.tail = snakeBody2; //把蛇尾信息存起来

  77. this.pos.push([0, 0]); //把蛇身2的坐标也存起来

  78. //形成链表关系

  79. //蛇头

  80. snakeHead.last = null;

  81. snakeHead.next = snakeBody1;

  82. //蛇身

  83. snakeBody1.last = snakeHead;

  84. snakeBody1.next = snakeBody2;

  85. //蛇尾

  86. snakeBody2.last = snakeBody1;

  87. snakeBody2.next = null;

  88. //给蛇添加一个属性,用来表示蛇走的方向

  89. this.direction = this.directionNum.right; //默认让蛇往右走

  90. };

  91. //这个方法用来获取蛇头下一个位置对应的元素,要根据元素做不同的事情

  92. Snake.prototype.getNextPos = function() {

  93. var nextPos = [

  94. this.head.x / sw + this.direction.x,

  95. this.head.y / sh + this.direction.y

  96. ]

  97. //forEach代词代表遍历数组

  98. //下个点是自己,代表撞到了自己,游戏结束

  99. var selfCollied = false; //是否撞到自己

  100. this.pos.forEach(function(value) {

  101. if (value[0] == nextPos[0] && value[1] == nextPos[1]) {

  102. //如果数组中两个数据都相等,说明下一个点在蛇身体里面能找到自己了

  103. selfCollied = true;

  104. }

  105. });

  106. if (selfCollied) {

  107. this.strategies.die.call(this);

  108. return;

  109. }

  110. //下个点是围墙,代表撞到了围墙,游戏结束

  111. if (nextPos[0] < 0 || nextPos[1] < 0 || nextPos[0] > td - 1 || nextPos[1] > tr - 1) {

  112. this.strategies.die.call(this);

  113. return;

  114. }

  115. //下个点是食物,吃

  116. if (food && food.pos[0] == nextPos[0] && food.pos[1] == nextPos[1]) {

  117. //如果这个条件成立,说明蛇头要走的下一个点是食物

  118. this.strategies.eat.call(this);

  119. return;

  120. }

  121. //下个点什么都不是,走

  122. this.strategies.move.call(this); //call作用调用,也可以传参

  123. };

  124. //处理碰撞后要做的事

  125. Snake.prototype.strategies = {

  126. move: function(format) { //括号内参数用于决定要不要删除最后一个方块(蛇尾),当传来参数就表示要做的事情是吃

  127. //创建新身体(在旧蛇头的位置)

  128. var newBody = new Square(this.head.x / sw, this.head.y / sh, 'snakeBody');

  129. //更新链表关系

  130. newBody.next = this.head.next;

  131. newBody.next.last = newBody;

  132. newBody.last = null;

  133. this.head.remove(); //把旧蛇头从原来的位置删除

  134. newBody.create();

  135. //创建一个新蛇头(蛇头下一个要走到的点nextPos)

  136. var newHead = new Square(this.head.x / sw + this.direction.x, this.head.y / sh + this.direction.y, 'snakeHead');

  137. //更新链表关系

  138. newHead.next = newBody;

  139. newHead.last = null;

  140. newBody.last = newHead;

  141. newHead.viewContent.style.transform = 'rotate(' + this.direction.rotate + 'deg)';

  142. newHead.create();

  143. //蛇身上的每一个方块的坐标也要更新

  144. this.pos.splice(0, 0, [this.head.x / sw + this.direction.x, this.head.y / sh + this.direction.y])

  145. this.head = newHead; //还要把this.head的信息更新一下

  146. if (!format) { //如果format的值为false,表示需要删除(除了吃之外的操作)

  147. this.tail.remove();

  148. this.tail = this.tail.last;

  149. this.pos.pop();

  150. }

  151. },

  152. eat: function() {

  153. this.strategies.move.call(this, true);

  154. createFood();

  155. game.score++;

  156. },

  157. die: function() {

  158. // console.log('die');

  159. game.over();

  160. }

  161. }

  162. snake = new Snake();

  163. //创建食物

  164. function createFood() {

  165. //食物小方块的随机坐标

  166. var x = null;

  167. var y = null;

  168. var include = true; //循环跳出的条件,true表示随机生成食物的坐标在蛇身上(需要继续循环);false表示食物坐标不在蛇身上(不循环了)

  169. while (include) {

  170. x = Math.round(Math.random() * (td - 1));

  171. y = Math.round(Math.random() * (tr - 1));

  172. snake.pos.forEach(function(value) {

  173. if (x != value[0] && y != value[1]) {

  174. //这个条件成立说明在随机出来的坐标,在蛇身上并没有找到

  175. include = false;

  176. }

  177. });

  178. }

  179. //生成食物

  180. food = new Square(x, y, 'food');

  181. food.pos = [x, y]; //存储一下生成食物的坐标,用于跟蛇头要走的下一个点做对比

  182. var foodDom = document.querySelector('.food');

  183. if (foodDom) {

  184. foodDom.style.left = x * sw + 'px';

  185. foodDom.style.top = y * sh + 'px';

  186. } else {

  187. food.create();

  188. }

  189. }

  190. //创建游戏逻辑

  191. function Game() {

  192. this.timer = null;

  193. this.score = 0;

  194. }

  195. Game.prototype.init = function() {

  196. snake.init();

  197. createFood();

  198. document.onkeydown = function(ev) {

  199. if (ev.which == 37 && snake.direction != snake.directionNum.right) { //用户按下左键时,这条蛇不能是正在往右走

  200. snake.direction = snake.directionNum.left;

  201. } else if (ev.which == 38 && snake.direction != snake.directionNum.down) {

  202. snake.direction = snake.directionNum.up;

  203. } else if (ev.which == 39 && snake.direction != snake.directionNum.left) {

  204. snake.direction = snake.directionNum.right;

  205. } else if (ev.which == 40 && snake.direction != snake.directionNum.up) {

  206. snake.direction = snake.directionNum.down;

  207. }

  208. }

  209. this.start();

  210. }

  211. Game.prototype.start = function() { //开始游戏

  212. this.timer = setInterval(function() {

  213. snake.getNextPos();

  214. }, 200);

  215. }

  216. Game.prototype.pause = function() {

  217. clearInterval(this.timer);

  218. }

  219. Game.prototype.over = function() {

  220. clearInterval(this.timer);

  221. alert('你的得分为' + this.score);

  222. //游戏回到最初始的状态

  223. var snakeWrap = document.getElementById('snakeWrap');

  224. snakeWrap.innerHTML = '';

  225. snake = new Snake();

  226. game = new Game();

  227. var startBtnWrap = document.querySelector('.startBtn');

  228. startBtnWrap.style.display = 'block';

  229. }

  230. //开启游戏

  231. game = new Game();

  232. var startBtn = document.querySelector('.startBtn button');

  233. startBtn.onclick = function() {

  234. startBtn.parentNode.style.display = 'none';

  235. game.init();

  236. };

  237. //暂停游戏

  238. var snakeWrap = document.getElementById('snakeWrap');

  239. var pauseBtn = document.querySelector('.pauseBtn button');

  240. snakeWrap.onclick = function() {

  241. game.pause();

  242. pauseBtn.parentNode.style.display = 'block';

  243. };

  244. pauseBtn.onclick = function() {

  245. game.start();

  246. pauseBtn.parentNode.style.display = 'none';

  247. }

  248. })

案例素材

喜欢就拿去试试吧,不能嫌弃蛇头丑,找不到素材,没得审美的博主自己用PS软件画的蛇头,以后教你如何画简单的图片,这样就再也不担心找不到素材了,敬请期待吧。

游戏效果展示

 动图上传仅限5M以内,咱就不讲究了😥,将就看吧

Immersive Reader (youkuaiyun.com)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值