效果

代码

//食物对象
;(function () {
function Food(element) {
this.width = 20
this.height = 20
this.backgroundColor = '#ff8500'
this.x = 0
this.y = 0
this.elemen = element
this.arr = []
}
Food.prototype.remove=function() {
for (let i = 0; i < this.arr.length; i++) {
this.arr[i].parentNode.removeChild(this.arr[i]) }
this.arr=[]
} Food.prototype.show = function () {
this.remove()
this.x = randomNum(0, (this.elemen.offsetWidth - this.width) / this.width) * this.width
this.y = randomNum(0, (this.elemen.offsetHeight - this.height) / this.height) * this.height
let div = document.createElement('div')
this.elemen.appendChild(div)
div.style.width = this.width + 'px';
div.style.height = this.height + 'px'
div.style.backgroundColor = this.backgroundColor
div.style.position = 'absolute'
div.style.left = this.x + 'px'
div.style.top = this.y + 'px'
this.arr.push(div)
}
//外部访问
window.Food = Food })()
//蛇对象
;(function() {
function Snake(element) {
this.width = 20
this.height = 20
this.gameif=false
//蛇身 位置 颜色
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)
food.show()
}
}
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
console.log(herfX,herfY)
if (herfX || herfY) {
this.gameif=true
alert('游戏结束')
return
}
switch (this.direction) {
case "right": {
this.body[0].x += 1
break;
}
case "left": {
this.body[0].x -= 1
break;
}
case "up": {
this.body[0].y -= 1
break;
}
case "down": {
this.body[0].y += 1
break;
}
}
}
window.Snake=Snake;
})()
//游戏对象
;(function() {
function Game(map) {
this.map = map;
this.food = new Food(this.map)
this.snake = new Snake(this.map)
}
Game.prototype.go = function () {
let food=this.food
let snake=this.snake;
food.show()
snake.show()
let go_time = setInterval(function () {
console.log(snake,food) if (snake.gameif){
clearInterval(go_time)
}
document.addEventListener('keydown', function (e) {
//结束游戏。game over
// up:38 down:40 left:37 reight:39
switch (e.keyCode) {
case 37: {
//禁止反方向
if (snake.direction=='right'){
break;
}
snake.direction='left'
break;
}
case 38: {
if (snake.direction=='down'){
break;
}
snake.direction='up'
break;
}
case 39: {
if (snake.direction=='left'){
break;
}
snake.direction='right'
break;
}
case 40: {
if (snake.direction=='up'){
break;
}
snake.direction='down'
break;
}
}
}, false)
snake.udlr()
snake.show()
snake.feed(food)
}, 50)
}
window.Game=Game;
})() let map = document.getElementById('map')
let game = new Game(map)
game.go() function randomNum(minNum, maxNum) {
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum) }

总结

  效果图 速度太快。。。。。。。。

总得来说 并不是毫无头绪。抽象对象 一个个去写 。

    收获挺大

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

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

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

  2. JavaScript—面向对象 贪吃蛇_3 蛇对象

    蛇对象 function Snake(element) { this.width = 20 this.height = 20 //蛇身 位置 颜色 this.body = [ {x: 6, y: 4, ...

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

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

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

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

  5. javascript实现贪吃蛇

    <html> <head> <style> body { background:#444; } .rect { border:1px solid #94F; wid ...

  6. JavaScript版—贪吃蛇小组件

    最近在学习JavaScript,利用2周的时间看完了<JavaScript高级编程>,了解了Js是一门面向原型编程的语言,没有像C#语言中的class,也没有私有.公有.保护等访问限制的级 ...

  7. 使用javascript实现贪吃蛇游戏

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  8. javascript写贪吃蛇游戏(20行代码!)

    <!doctype html> <html> <body> <canvas id="can" width="400" ...

  9. Javascript仿贪吃蛇出现Bug的反思

    bug现象:    图一

随机推荐

  1. 六十五、SAP中通过BREAK-POINT下断点,进行调试

    一.代码如下,有2个断点的按钮,可以可以写入BREAK-POINT人工断点 二.运行之后,程序会被断下来, 四个执行按钮,意思分别为:单步进入子程序,单步不进入子程序,返回外面,执行到断点处 三.我们 ...

  2. 113-PHP使用instanceof判断变量是否为某个类对象

    <?php class ren{ //定义人类 } class mao{ //定义猫类 } $ren=new ren(); //实例化一个人类的对象 $mao=new mao(); //实例化一 ...

  3. Bootstrap 侧边栏 导航栏

    http://blog.csdn.net/shangmingchao/article/details/49763351 实测效果图:

  4. 实验吧-密码学-他的情书(进一步了解js代码调试和console.log)

    打开网站,在白色背景下的任一点上点击鼠标,白色部分都会消失(包括password输入框),那么就无法输入. 查看源码,发现是明显的从源码解决问题. 火狐F12查看器查看源码(如果是简单的操作,可以vi ...

  5. oracle中判断"非"

    在oracle中判断为"非"最常见的两种情况,一个是"不等于",一个的"非空". 通过查找资料得知,oracle中判断不等于的方法有好多种: ...

  6. 吴裕雄--天生自然C++语言学习笔记:C++ 数组

    C++ 支持数组数据结构,它可以存储一个固定大小的相同类型元素的顺序集合.数组是用来存储一系列数据,但它往往被认为是一系列相同类型的变量. 数组的声明并不是声明一个个单独的变量,比如 number0. ...

  7. js如何操作或是更改sass里的变量

    /*上网搜索了好多方法,最终只有这一种比较适合*/ 参考: https://blog.csdn.net/weixin_44392565/article/details/85755592 https:/ ...

  8. js原型链理解(2)--原型链继承

    1.原型链继承 2.constructor stealing(构造借用) 3.组合继承 js中的原型链继承,运用的js原型链中的__proto__. function Super(){ this.se ...

  9. POJ 1185:炮兵阵地

    炮兵阵地 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21366   Accepted: 8280 Description ...

  10. 9 ~ express ~ 用户注册

    一,/public/js/index.js //登陆 $login.find('button').on('click',()=>{ $.ajax({ type:'post', url:'/api ...