蛇对象

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. Elasticsearch开启试用x-pack license

    一.Elasticsearch 6.7.2开启trial  x-pack license:x-pack的license试用期只有30天 1.ES6.7.2版本默认已经安装了x-pack插件,这里就没有 ...

  2. 三十八、SAP设置默认语言

    一.点击系统->用户参数文件->用户数据 二.设置成需要的语言 三.重新登录,并在登录时选择EN 四.进入界面

  3. StackExchange.Redis.DLL 操作redis简化版

    直接引入StackExchange.Redis.dll来操作redis using Newtonsoft.Json; using StackExchange.Redis; using System; ...

  4. java方法中使用js的alert。

    out.println("<script type=\"text/javascript\">"); out.println("alert( ...

  5. 如何下载安装python安装包

    1.从360搜索python,找到“python官网”              python官网地址:https://www.python.org/ 2.进入python官网,出现下面的页面 3.点 ...

  6. gdb 常用选项

    gdb 常用选项 help:查看命令帮助,具体命令查询在gdb中输入help + 命令,简写h run:重新开始运行文件(run-text:加载文本文件,run-bin:加载二进制文件),简写r st ...

  7. Docker 搭建开源 CMDB平台 “OpsManage” 之 Redis

    整体结构如下图   先来在 172.16.0.200 安装docker-ce (新)或 docker-io(旧)      0: Docker-ce  (新版本  Docker version 17. ...

  8. 【转】selenium技巧 - 通过js来控制滚动条,通过xpath定位最上层的div层

    http://blog.csdn.net/iceryan/article/details/8162703 业务流程:   1.打开此网页 http://nanjing.xiaomishu.com/sh ...

  9. NSPredicate实现数据筛选

    一:基本语法 1.什么是NSPredicate apple官方文档这样写的: The NSPredicate class is used to define logical conditions us ...

  10. JAXB工具

    在JDK6之后,都自带了JAXB工具,所以在jdk类库与tomcat WEBAPP类库之间,会造成冲突 https://blog.csdn.net/iteye_13776/article/detail ...