蛇对象

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;
}
}
}

  

JavaScript—面向对象 贪吃蛇_3 蛇对象的更多相关文章

  1. javascript:面向对象和常见内置对象及操作

    本文内容: 面向对象 常见内置对象及操作 首发日期:2018-05-11 面向对象: JavaScript 是面向对象的编程语言 (OOP).OOP 语言使我们有能力定义自己的对象和变量类型. 对象是 ...

  2. JavaScript—面向对象 贪吃蛇_2 游戏对象

    游戏对象 function Game(map) { this.map = map; this.food = new Food(this.map) this.snake = new Snake(this ...

  3. JavaScript—面向对象 贪吃蛇_2 食物对象

    食物对象 //自调用 (function (){ function Food(element) { this.width = 20 this.height = 20 this.backgroundCo ...

  4. JavaScript—面向对象贪吃蛇_1

    前面说了.面向对象的思考方式和面向过程的思考方式有着本质的区别. 贪吃蛇.作为各大培训机构.面向对象的练手项目,的确好.我昨天看完视频,有一种领悟面向对象的感觉,当然可能只针对贪吃蛇..要想在实际开发 ...

  5. JavaScript—面向对象 贪吃蛇最终

    效果 代码 //食物对象 ;(function () { function Food(element) { this.width = 20 this.height = 20 this.backgrou ...

  6. 再谈javascript面向对象编程

    前言:虽有陈皓<Javascript 面向对象编程>珠玉在前,但是我还是忍不住再画蛇添足的补上一篇文章,主要是因为javascript这门语言魅力.另外这篇文章是一篇入门文章,我也是才开始 ...

  7. 前端笔记之JavaScript面向对象(三)初识ES6&underscore.js&EChart.js&设计模式&贪吃蛇开发

    一.ES6语法 ES6中对数组新增了几个函数:map().filter().reduce() ES5新增的forEach(). 都是一些语法糖. 1.1 forEach()遍历数组 forEach() ...

  8. JavaScript面向对象编程小游戏---贪吃蛇

    1 面向对象编程思想在程序项目中有着非常明显的优势: 1- 1 代码可读性高.由于继承的存在,即使改变需求,那么维护也只是在局部模块 1- 2 维护非常方便并且成本较低. ​ 2 这个demo是采用了 ...

  9. javascript面向对象系列第一篇——构造函数和原型对象

    × 目录 [1]构造函数 [2]原型对象 [3]总结 前面的话 一般地,javascript使用构造函数和原型对象来进行面向对象编程,它们的表现与其他面向对象编程语言中的类相似又不同.本文将详细介绍如 ...

随机推荐

  1. 基于Ambari的WebUI实现服务缩容

    基于Ambari的WebUI实现服务缩容 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.实现服务的扩容 1>.进入到主机的管理界面 2>.查看主机的信息概览 3&g ...

  2. HDU - 6006 Engineer Assignment (状压dfs)

    题意:n个工作,m个人完成,每个工作有ci个阶段,一个人只能选择一种工作完成,可以不选,且只能完成该工作中与自身标号相同的工作阶段,问最多能完成几种工作. 分析: 1.如果一个工作中的某个工作阶段没有 ...

  3. 滑条滚动发请求要用Debounce

    import debounce from 'lodash.debounce'; this.deboucedFunc = debounce(this.viewModel.v_onHomeworkRequ ...

  4. return , return true , return false的区别

    return true:  返回正确的处理结果. return false :  返回错误的处理结果 和 中断代码继续向下 执行. return:    返回null,起到中断方法执行的效果,只要不r ...

  5. java简写名词解释

    RPC(Remote Procedure Call)—远程过程调用 实时编译器(Just In Time Compiler,JIT) XML 指可扩展标记语言(EXtensible Markup La ...

  6. Centos6 import tensorflow遇到的问题

    1.ImportError: /lib64/libc.so.6: version `GLIBC_2.16' not found 解决办法:升级系统glibc库至2.17版本 首先在网上下载glibc- ...

  7. 160-PHP 文本替换函数str_replace(一)

    <?php $str='Hello world!'; //定义源字符串 $search='o'; //定义将被替换的字符 $replace='O'; //定义替换的字符串 $res=str_re ...

  8. 055-for循环中break的使用

    <?php ;;$x++){ //省略表达式2的for循环将是无限循环 echo "$x<br />"; ){ break; //使用if语句控制退出无限循环 } ...

  9. css mix-blend-mode 颜色滤镜混合模式

    CSS3混合模式种类 在CSS3混合模式中,目前仅有16种:normal,multiply,screen,overlay,darken,lighten,color-dodge,color-burn,h ...

  10. Elasticsearch 搜索数据

    章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...