蛇对象

function Snake(element) {
this.width = 20
this.height = 20 //蛇身 位置 颜色
this.body = [
{x: 6, y: 4, bc: 'red'},
{x: 5, y: 4, bc: 'blue'},
{x: 4, y: 4, bc: 'blue'},
]
this.direction = 'right'
this.elemen = element
//保存当前蛇
this.arr = []
} //吃食物
Snake.prototype.feed = function (food) { // 遇到食物 复制最后一个蛇身
if (this.body[0].x * this.width === food.x && this.body[0].y * this.height === food.y) {
let o = this.body[this.body.length - 1]
let pro = {x: o.x, y: o.y, bc: o.bc}
this.body.push(pro)
}
} Snake.prototype.remove = function () {
for (let i = 0; i < this.arr.length; i++) {
this.arr[i].parentNode.removeChild(this.arr[i])
}
this.arr = []
}
//蛇出现
Snake.prototype.show = function () {
this.remove()
//console.log(this.arr)
for (let i = 0; i < this.body.length; i++) {
let div = document.createElement('div')
this.elemen.appendChild(div)
div.style.width = this.width + 'px';
div.style.height = this.height + 'px'
div.style.position = 'absolute'
div.style.backgroundColor = this.body[i].bc
div.style.left = this.body[i].x * this.width + 'px'
div.style.top = this.body[i].y * this.height + 'px'
this.arr.push(div)
} }
//移动方法
Snake.prototype.udlr = function () {
//蛇身移动 根据最后一个的位置等于上一个的位置
for (let 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
}
// 边界
let herfX = this.body[0].x * this.width >= this.elemen.offsetWidth - this.width || this.body[0].x * this.width <= 0
let herfY = this.body[0].y * this.height >= this.elemen.offsetHeight - this.height || this.body[0].y * this.height <= 0
if (herfX || herfY) {
alert('完蛋')
return
}
switch (this.direction) {
case "right": {
this.body[0].x += 1
break;
}
case "letf": {
this.body[0].x -= 1
break;
}
case "up": {
this.body[0].y -= 1
break;
}
case "down": {
this.body[0].y += 1
break;
}
}
}

  

最新文章

  1. PHP+mysql数据库开发搜索功能:中英文分词+全文检索(MySQL全文检索+中文分词(SCWS))
  2. DNS劫持和DNS污染的区别
  3. 2014多校第十场1004 || HDU 4974 A simple water problem
  4. Adobe Photoshop CC 14.0简体中文特别版32位和64位下载
  5. iOS开发中关于UIImage的知识点总结
  6. POJ 3061 Subsequence(Two Pointers)
  7. Git Command Summary (Updated)
  8. 【C++继承与派生之二】有子对象的派生类的构造函数
  9. Jenkins安装plugin
  10. CodeForces 721B
  11. 细心!SQL语句进行运算时使用字符串时缺失精度的细节!
  12. 玩玩 Nginx 2-----给Nginx添加第三方模块(动态更新upstream)
  13. openssl命令
  14. 【SSH系列】Hibernate映射-- 多对一单向关联映射
  15. [Swift]LeetCode173. 二叉搜索树迭代器 | Binary Search Tree Iterator
  16. IDF-简单题目writeup
  17. xlwt模块的使用
  18. JavaScript最后的课程笔记
  19. vue中的指令v-model
  20. Django之WSGI 和MVC/MTV

热门文章

  1. uni-app解决小程序圆角样式不生效
  2. Asp.net MVC中ReturnUrl的使用
  3. 13.56mhz自动寻卡功能业界最低功耗:SI522
  4. 一百一十四、SAP查看事务代码对应工程源码
  5. 043-PHP简单获得一个类对应的反射信息
  6. 136-PHP 使用类名访问static修饰的类方法
  7. window下mysql-proxy简单使用
  8. servlet 之 servlet接口详解
  9. HTML元素 和 CSS (9.23 第十天)
  10. 记-OSPF学习