资讯专栏INFORMATION COLUMN

JS用原型对象写的贪吃蛇,很粗糙的代码

YanceyOfficial / 1852人阅读

摘要:贪吃蛇类默识贪吃蛇速度,毫秒地图轴分为多少单位地图轴分为多少单位贪吃蛇运动速度贪吃蛇每节身体和食物的宽高地图轴分为多少单位初始化贪吃蛇属性蛇移动方向食物和食物的坐标游戏开始创建地图初始化食物初始化贪吃蛇绑定键盘方向更改贪吃蛇方向移动贪吃蛇创

/**

贪吃蛇类

@author 默识

@param {int} speed 贪吃蛇速度,毫秒

@param {int} x 地图x轴分为多少单位

@param {int} y 地图y轴分为多少单位

@returns {Snake} none
*/

function Snake(speed, x, y) {

</>复制代码

  1. //贪吃蛇运动速度
  2. this.speed = speed;
  3. //贪吃蛇每节身体和食物的宽高
  4. this.width = window.innerWidth / x;
  5. this.height = window.innerHeight / y;
  6. //地图xy轴分为多少单位
  7. this.x = x;
  8. this.y = y;
  9. //初始化贪吃蛇属性
  10. this.sBody = [
  11. [0, 1, "green"],
  12. [1, 1, "green"],
  13. [2, 1, "green"],
  14. [3, 1, "red"]
  15. ];
  16. //蛇移动方向
  17. this.sDirection = "right";
  18. //食物和食物的坐标
  19. this.food = null;
  20. this.foodX = 0;
  21. this.foodY = 0;

}
/**

游戏开始

@returns {undefined} none
*/

Snake.prototype.start = function () {

</>复制代码

  1. this.createMap();//创建地图
  2. this.createFood();//初始化食物
  3. this.createSnake();//初始化贪吃蛇
  4. this.bind();//绑定键盘方向更改贪吃蛇方向
  5. //移动贪吃蛇
  6. setInterval(function () {
  7. snake.moveSnake();
  8. }, this.speed);

};
/**

创建贪吃蛇地图

@returns {undefined}none
*/

Snake.prototype.createMap = function () {

</>复制代码

  1. document.body.style.backgroundColor = "black";

};
/**

创建贪吃蛇食物

@returns {undefined}none
*/

Snake.prototype.createFood = function () {

</>复制代码

  1. //创建食物
  2. var food = document.createElement("div");
  3. this.food = food;
  4. this.foodX = Math.floor(Math.random() * this.x);
  5. this.foodY = Math.floor(Math.random() * this.y);
  6. //食物的样式
  7. with (food.style) {
  8. position = "absolute";
  9. width = this.width + "px";
  10. height = this.height + "px";
  11. backgroundColor = "green";
  12. left = this.foodX * this.width + "px";//食物随机X轴坐标
  13. top = this.foodY * this.height + "px";//食物随机Y轴坐标
  14. }
  15. //食物添加到地图上
  16. document.body.appendChild(food);

};
/**

创建贪吃蛇

@returns {undefined}none
*/

Snake.prototype.createSnake = function () {

</>复制代码

  1. //绘制蛇
  2. for (var i = 0; i < this.sBody.length; i++) {
  3. this.sBody[i][3] = this.sBody[i][3] || document.createElement("div");
  4. //设置蛇的样式
  5. with (this.sBody[i][3].style) {
  6. position = "absolute";
  7. width = this.width + "px";
  8. height = this.height + "px";
  9. left = this.sBody[i][0] * this.width + "px";
  10. top = this.sBody[i][1] * this.height + "px";
  11. backgroundColor = this.sBody[i][2];
  12. }
  13. //放入地图中
  14. document.body.appendChild(this.sBody[i][3]);
  15. }

};
/**

绑定方向事件更改贪吃蛇运动方向

@returns {undefined}none
*/

Snake.prototype.bind = function () {

</>复制代码

  1. var tmp = this;
  2. document.onkeyup = function (e) {
  3. var e = window.event || e;
  4. switch (e.keyCode) {
  5. case 37:
  6. tmp.sDirection = "left";
  7. break;
  8. case 38:
  9. tmp.sDirection = "up";
  10. break;
  11. case 39:
  12. tmp.sDirection = "right";
  13. break;
  14. case 40:
  15. tmp.sDirection = "down";
  16. break;
  17. }
  18. }

};
/**

贪吃蛇行动

@returns {undefined}none
*/

Snake.prototype.moveSnake = function () {

</>复制代码

  1. //舍身跟随前一节运动,即改变坐标,注意蛇身先走,要不蛇头紧随的一段身体会跟蛇头重叠
  2. for (var i = 0; i < this.sBody.length - 1; i++) {
  3. this.sBody[i][0] = this.sBody[i + 1][0];
  4. this.sBody[i][1] = this.sBody[i + 1][1];
  5. }
  6. //蛇运动根据方向改变xy轴坐标
  7. switch (this.sDirection) {
  8. case "up":
  9. this.sBody[this.sBody.length - 1][1]--;
  10. break;
  11. case "right":
  12. this.sBody[this.sBody.length - 1][0]++;
  13. break;
  14. case "down":
  15. this.sBody[this.sBody.length - 1][1]++;
  16. break;
  17. case "left":
  18. this.sBody[this.sBody.length - 1][0]--;
  19. break
  20. }
  21. //贪吃蛇吃食物
  22. if (this.sBody[this.sBody.length - 1][0] === this.foodX
  23. && this.sBody[this.sBody.length - 1][1] === this.foodY
  24. ) {
  25. //创建一节身体
  26. var node = [this.sBody[0][0], this.sBody[0][1], "green"];
  27. //身体生长
  28. this.sBody.unshift(node);
  29. //食物位置重置
  30. this.foodX = Math.floor(Math.random() * this.x);
  31. this.foodY = Math.floor(Math.random() * this.y);
  32. with (this.food.style) {
  33. left = this.foodX * this.width + "px";//食物随机X轴坐标
  34. top = this.foodY * this.height + "px";//食物随机Y轴坐标
  35. }
  36. }
  37. //判断游蛇是否碰到边界
  38. if (this.sBody[this.sBody.length - 1][0] < 0
  39. || this.sBody[this.sBody.length - 1][0] >= this.x
  40. || this.sBody[this.sBody.length - 1][1] < 0
  41. || this.sBody[this.sBody.length - 1][1] >= this.y
  42. ) {
  43. this.gameover();
  44. return;
  45. }
  46. //显示新贪吃蛇位置
  47. this.createSnake();

};
/**

游戏结束

@returns {undefined} none
*/

Snake.prototype.gameover = function () {

</>复制代码

  1. alert("GAME OVER!");
  2. location.reload();

};
var snake = new Snake(100, 60, 40);
snake.start();

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/82702.html

相关文章

  • 深入理解js

    摘要:详解十大常用设计模式力荐深度好文深入理解大设计模式收集各种疑难杂症的问题集锦关于,工作和学习过程中遇到过许多问题,也解答过许多别人的问题。介绍了的内存管理。 延迟加载 (Lazyload) 三种实现方式 延迟加载也称为惰性加载,即在长网页中延迟加载图像。用户滚动到它们之前,视口外的图像不会加载。本文详细介绍了三种延迟加载的实现方式。 详解 Javascript十大常用设计模式 力荐~ ...

    caikeal 评论0 收藏0
  • js 写个自动寻路贪吃

    摘要:前言偶尔看到两年前写的贪吃蛇,当时没把自动寻路的算法写好,蛇容易挂,周末找了个时间把当年的坑填上,写了个不会挂的贪吃蛇。 前言 偶尔看到两年前写的贪吃蛇,当时没把自动寻路的算法写好,蛇容易挂,周末找了个时间把当年的坑填上,写了个不会挂的贪吃蛇。 两年前的版本_点击查看 这次更新的版本_点击查看 代码比较简单,使用 canvas 完成游戏的画图,抛开 A* 算法的实现,整个 html 代...

    gaara 评论0 收藏0
  • 小白成长日记:写个贪吃

    摘要:贪吃蛇并不是通过操作来完成移动的,而是通过记录贪吃蛇的路径来将身体渲染出来。目前没有内置的操作符判断对象的内容是否相同。 还是用的vue,本来以为不合适,但想法错了。贪吃蛇并不是通过操作dom来完成移动的,而是通过记录贪吃蛇的路径来将身体渲染出来。 一般移动元素,我们都是变动它的css达到目的,但我在写贪吃蛇的时候发现这样很难以实现,参考了网上的资源,发现大部分人是通过记录贪吃蛇的路径...

    archieyang 评论0 收藏0
  • 前端空间 - 收藏集 - 掘金

    摘要:封装手写的方笔记使用检测文件前端掘金副标题可以做什么以及使用中会遇到的坑。目的是帮助人们用纯中文指南实现复选框中多选功能前端掘金作者缉熙简介是推出的一个天挑战。 深入理解 JavaScript Errors 和 Stack Traces - 前端 - 掘金译者注:本文作者是著名 JavaScript BDD 测试框架 Chai.js 源码贡献者之一,Chai.js 中会遇到很多异常处理...

    you_De 评论0 收藏0
  • 前端空间 - 收藏集 - 掘金

    摘要:封装手写的方笔记使用检测文件前端掘金副标题可以做什么以及使用中会遇到的坑。目的是帮助人们用纯中文指南实现复选框中多选功能前端掘金作者缉熙简介是推出的一个天挑战。 深入理解 JavaScript Errors 和 Stack Traces - 前端 - 掘金译者注:本文作者是著名 JavaScript BDD 测试框架 Chai.js 源码贡献者之一,Chai.js 中会遇到很多异常处理...

    lwx12525 评论0 收藏0

发表评论

0条评论

YanceyOfficial

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<