源码如下:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<meta >
<title></title>
<style>
.map {
position: relative;
margin:0 auto;
width: 900px;
height: 600px;
background-color: black;
}
.snake {
position: absolute;
width: 30px;
height: 30px;
border: 1px solid white;
box-sizing: border-box;
}
.food {
position: absolute;
width: 30px;
height: 30px;
border: 1px solid white;
box-sizing: border-box;
background-color: blue;
}
</style>
</head>
<body>
<script>
var Map;
function map(){
this._map=null;
this.creatMap=function(){
this._map=document.createElement("div");
this._map.className="map";
document.body.appendChild(this._map);
}
}
var Snake;
function snake(){
this._snake=[["red", 3, 1, null], ["yellow", 2, 1, null], ["yellow", 1, 1, null]]
this.direction="right";
this.createSnake=function(){
for(var i=0;i<this._snake.length;i++){
if(this._snake[i][3]==null){
this._snake[i][3]=document.createElement("div");
this._snake[i][3].className="snake";
this._snake[i][3].style.backgroundColor=this._snake[i][0];
console.log(this._snake[i][3]);
Map._map.appendChild(this._snake[i][3]);
}
this._snake[i][3].style.left=this._snake[i][1]*30+"px";
this._snake[i][3].style.top=this._snake[i][2]*30+"px";
}
this.snakemove = function(){
var len =this._snake.length-1;
//蛇移动,传递位置
for(var i = len;i>0;i--){
this._snake[i][1]=this._snake[i-1][1];
this._snake[i][2]=this._snake[i-1][2];
}
//判断移动方向行走
switch(this.direction){
case "right":this._snake[0][1]+=1;break;
case "left":this._snake[0][1]-=1;break;
case "up":this._snake[0][2]-=1;break;
case "down":this._snake[0][2]+=1;break;
}
//当蛇穿墙
if (this._snake[0][1] > 29) {
this._snake[0][1] = 0;
}
if (this._snake[0][1] < 0) {
this._snake[0][1] = 29;
}
if (this._snake[0][2] > 19) {
this._snake[0][2] = 0;
}
if (this._snake[0][2] < 0) {
this._snake[0][2] = 19;
}
//检测食物
if(this._snake[0][1] == Food.x&&this._snake[0][2] == Food.y){
this._snake.push(["yellow",
this._snake[this._snake.length-1][1],
this._snake[this._snake.length-1][2],
])
Food.createFood();
}
this.createSnake();
}
}
}
var Food;
function food(){
this._food = null;
this.x;
this.y;
this.createFood = function(){
this.x = Math.floor(Math.random()*30);
this.y = Math.floor(Math.random()*20);
if(this._food==null){
this._food = document.createElement("div");
this._food.className="food";
Map._map.appendChild(this._food);
}
this._food.style.left = this.x *30+"px";
this._food.style.top = this.y *30+"px";
}
}
window.onload=function(){
Map=new map();
Map.creatMap();
Snake=new snake();
Snake.createSnake();
Food=new food();
Food.createFood();
document.body.onkeyup = function (e) {
console.log(e.keyCode);
switch (e.keyCode) {
case 87:
if (Snake.direction != "down") {
Snake.direction = "up";
}
break;
case 65:
if (Snake.direction != "right") {
Snake.direction = "left";
}
break;
case 68:
if (Snake.direction != "left") {
Snake.direction = "right";
}
break;
case 83:
if (Snake.direction != "up") {
Snake.direction = "down";
}
break;
}
}
setInterval(function () {
Snake.snakemove();
}, 100);
}
</script>
</body>
</html>
JS贪吃蛇
最新推荐文章于 2024-01-28 17:49:34 发布