// 地图
class map{
constructor(){
// this.width = 800;
// this.height = 500;
// this.c = “#333”;
// 创建地图
this.box = document.createElement(“div”);
console.log(this.box);
document.body.appendChild(this.box);
this.sty();
}
// 修饰地图
sty(){
this.box.style.cssText = width:800px;height:800px;background:#666;margin:0 auto;position:relative;border:solid 5px brown
;
}
}
var m =new map();
// ------------------------------------------------------
// 食物
class food{
// 创建食物
constructor(){
this.fooder = document.createElement(“div”);
m.box.appendChild(this.fooder);
this.sty();
}
// 食物样式和食物的位置设定
sty(){
// 这里将地图分成20*20的小格子,依据定位设置食物的随机位置
// 表示x轴格子的数量
this.x = 0;
// 表示y轴格子的数量
this.y = 0;
this.h = this.fooder.offsetWidth * this.y +“px”;
this.w = this.fooder.offsetWidth * this.x + “px”;
this.fooder.style.cssText = width:20px;height:20px;background:red;position:absolute;top:${this.h};left:${this.w};
;
}
// 设置随机食物位置
randompos(){
this.x = random(0,(m.box.offsetWidth - this.fooder.offsetWidth)/this.fooder.offsetWidth);
this.y = random(0,(m.box.offsetHeight - this.fooder.offsetHeight)/this.fooder.offsetHeight);
this.fooder.style.left = this.fooder.offsetWidth * this.x + "px";
this.fooder.style.top = this.fooder.offsetWidth * this.y +"px";
}
}
// ------------------------------------------------------------------------------------
// 蛇
class she{
constructor(){
// 提前设定将来的蛇节的样式数据
this.w = 20;
this.h = 20;
this.box = [{ele:null,x:4,y:3,c:randomColor()},{ele:null,x:3,y:3,c:randomColor()},{ele:null,x:2,y:3,c:randomColor()}]
this.d = “right”;
this.sty();
}
// 设置蛇的样式
sty(){
var that = this;
for (let i = 0; i < this.box.length; i++) {
if(!this.box[i].ele){
this.box[i].ele = document.createElement("div");
m.box.appendChild(this.box[i].ele);
}
this.box[i].ele.style.cssText = `width:${this.w}px;height:${this.h}px;background:${this.box[i].c};position:absolute;left:${this.box[i].x * this.w}px;top:${this.box[i].y * this.h}px;}`;
}
// 给蛇头做个标记
this.box[0].ele.innerHTML = "0";
// 设置延时器开始移动
setTimeout(function(){
that.move();
},300)
}
// 开始动起来
move(){
for(var i=this.box.length-1; i>0; i--){
this.box[i].x = this.box[i-1].x;
this.box[i].y = this.box[i-1].y;
}
//蛇头根据默认方向,决定想哪走
switch(this.d){
case "left":
this.box[0].x -= 1;
break;
case "right":
this.box[0].x += 1;
break;
case "top":
this.box[0].y -= 1;
break;
case "bottom":
this.box[0].y += 1;
break;
}
// 移动过程中,判断是否与食物的坐标重复,如果重复
if(this.box[0].x === f.x && this.box[0].y === f.y){
// 给蛇增加一个蛇节
this.box.push({
ele:null,
x:this.box[this.box.length-1].x,
y:this.box[this.box.length-1].y,
c:randomColor()
})
// 刷新食物的坐标
f.randompos();
}
// 游戏结束
if(this.box[0].x < 0 || this.box[0].y < 0 || this.box[0].x > ((m.w-this.w) / this.w) || this.box[0].y > ((m.h-this.h) / this.h)){
alert("撞墙了");
// 利用return的停止,结束程序
return;
}
//移动过程中,判断蛇头的坐标是否与某个任意一个蛇节的坐标重复
for(var i=1;i<this.box.length;i++){
if(this.box[0].x == this.box[i].x && this.box[0].y == this.box[i].y){
// 如果重复,撞到自己,结束程序
alert("撞到自己了");
return;
}
}
this.sty();
}
fangxiang(type){
// 14.处理键盘穿件来的code值
// 处理之前要先判断,当前是否按下了相反方向
// 如果是相反方向,直接结束判断,不执行
// 如果不是相反方向,改变初始的默认方向
switch(type){
case 37:
if(this.d === "right") break;
this.d = "left";
break;
case 38:
if(this.d === "bottom") break;
this.d = "top";
break;
case 39:
if(this.d === "left") break;
this.d = "right";
break;
case 40:
if(this.d === "top") break;
this.d = "bottom";
break;
}
}
}
var f = new food();
f.randompos();
var s = new she();
// 键盘事件
document.onkeydown = function(eve){
var e = eve || window.event;
var code = e.keyCode || e.which;
s.fangxiang(code);
}
// 封装随机数函数
function random(a,b){
return Math.round(Math.random()*(a-b)+b)
}
// 封装随机颜色值函数
function randomColor(){
return rgb(${random(0,255)},${random(0,255)},${random(0,255)})
}