<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>贪吃蛇</title>
<style>
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
#main{
margin: 100px;
}
.btn{
width: 100px;
height: 40px;
/* margin: 10px; */
}
.canvas{
position: relative;
top: 40px;
border: 1px solid rgb(107, 221, 178);
background-color: rgb(218, 236, 245);
/* background-color: rgb(241, 159, 112); */
}
</style>
</head>
<body>
<div id="main">
<input class="btn" type="button" value="开始游戏" id="begin"/>
<input class="btn" type="button" value="暂停游戏" id="pause"/>
<script>
var timer; //变量可以提升
var showCanvas = true; //是否显示格子
/*设置地图的构造方法:
atom为原子初始化的宽高(应该一致)
xnum为地图x轴应该有多少个原子(横向)
ynum为地图y轴应该有多少个原子(纵向)
*/
function Map(atom,xnum,ynum){
this.atom = atom;
this.xnum = xnum;
this.ynum = ynum;
this.canvas = null; //初始化画布
this.create = function(){
this.canvas = document.createElement("div");
this.canvas.className = "canvas";
this.canvas.style.width = this.atom * this.xnum + "px"; //画布的宽
this.canvas.style.height = this.atom * this.ynum + "px"; //画布的高
main.appendChild(this.canvas);
//判断是否需要显示格子
if (showCanvas){
for (var i = 0;i<xnum;i++){
for (var j = 0;j<ynum;j++){
//定义格子的样式
var lattice = document.createElement("div");
lattice.style.border = "1px solid rgb(107, 221, 178)";
lattice.style.width = this.atom + "px";
lattice.style.height = this.atom + "px";
//添加格子到地图中
this.canvas.appendChild(lattice);
lattice.style.position = "absolute";
//利用绝对定位
lattice.style.left = i * this.atom + "px";
lattice.style.top = j * this.atom + "px";
}
}
}
}
}
//创建食物的构造方法
function Food(map){
this.width = map.atom;
this.height = map.atom;
this.bgcolor = "rgb(" + Math.floor(Math.random()*200) + "," + Math.floor(Math.random()*200) + ","+ Math.floor(Math.random()*200) + ")";
//基于x轴和y轴的位置
this.x = Math.floor(Math.random()*map.xnum);
this.y = Math.floor(Math.random()*map.ynum);
this.flag = document.createElement("div");
//设置食物的样式
this.flag.style.width = this.width + "px";
this.flag.style.borderRadius = "5px";
this.flag.style.height = this.height + "px";
this.flag.style.backgroundColor = this.bgcolor;
//定义食物的位置
this.flag.style.position = "absolute";
this.flag.style.left = this.x * this.width + "px";
this.flag.style.top = this.y * this.height + "px";
map.canvas.appendChild(this.flag);
}
//创建蛇的构造方法
function Sanke(map){
//设置宽、高
this.width = map.atom;
this.height = map.atom;
//默认走的方向
this.direction = "right";
//设置蛇的主体
this.body = [
{ x:2, y:0 }, //蛇头
{ x:1, y:0 }, //蛇体
{ x:0, y:0 }, //蛇尾
];
//显示蛇 var i in this.body
this.display = function(){
for (var i = 0;i < this.body.length; i++){
if (this.body[i].x != null){
var create_s = document.createElement("div");
//将节点保存到一个状态变量当中,以便之后删除
this.body[i].flag = create_s;
//设置蛇的样式
create_s.style.width = this.width + "px";
create_s.style.height = this.height + "px";
/* create_s.style.border = "2px solid rgb(241, 159, 112)" */
create_s.style.backgroundColor = "rgb(107, 109, 221)";
create_s.style.borderRadius = "5px";
//设置蛇的位置
create_s.style.position = "absolute";
create_s.style.left = this.body[i].x * this.width + "px";
create_s.style.top = this.body[i].y * this.height + "px";
//添加到地图中
map.canvas.appendChild(create_s);
}
}
}
//控制蛇的运动
this.run = function(){
/*
{ x:2, y:0 }, //蛇头
{ x:1, y:0 }, //蛇体
{ x:0, y:0 }, //蛇尾
让x轴的每个位置+1
*/
for (var 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;
}
//根据方向处理蛇头
switch (this.direction) {
case "left":this.body[0].x--; break;
case "right":this.body[0].x++; break;
case "up": this.body[0].y--; break;
case "down": this.body[0].y++; break;
}
//删除旧的元素
for (var i in this.body){
if (this.body[i].flag != null){ //当吃到食物,flag等于null,且不能删除
map.canvas.removeChild(this.body[i].flag);
}
}
//显示新的元素
this.display();
}
}
//实例化地图类
var map = new Map(20,40,20);
map.create(); //创建画布
//实例化食物类,并创建食物
var food = new Food(map);
//实例化蛇类,创建蛇
var sanke = new Sanke(map);
//显示蛇
sanke.display();
//加入键盘事件,控制蛇的方向
document.onkeydown = function(event){
event = event || window.event;
//上:38 右:39 下:40 左:37
switch (event.keyCode){
case 38:
if (sanke.direction != "down"){
sanke.direction = "up";
}
break;
case 39:
if (sanke.direction != "left"){
sanke.direction = "right";
}
break;
case 40:
if (sanke.direction != "up"){
sanke.direction = "down";
}
break;
case 37:
if (sanke.direction != "right"){
sanke.direction = "left";
}
break;
}
}
//给开始按钮绑定函数
document.querySelector("#begin").onclick = function(){
clearInterval(timer);
timer = setInterval(function(){
sanke.run();
},300);
}
document.querySelector("#pause").onclick = function(){
clearInterval(timer);
}
</script>
</div>
</body>
</html>