今天学习了一个贪吃蛇小游戏,过程给我的感觉是非常痛苦的📡 不过到最后面就感觉也还好吧,好啦,让我们一起去看看我们的小蛇叭🐍
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#dvs {
width: 800px;
height: 600px;
background: gray;
position: relative;
}
</style>
</head>
<body>
<div id="dvs"></div>
</body>
<script src="../js/day01.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
(function() {
function Food(x, y, width, height, color) {
this.x = x || 0;
this.y = y || 0;
this.width = width || 20;
this.height = height || 20;
this.color = color || "green";
console.log(this.x);
}
var dvarr = [];
Food.prototype.init = function(map) {
remove();
var div = document.createElement("div");
map.appendChild(div);
div.style.width = this.width + "px";
div.style.height = this.height + "px";
div.style.backgroundColor = this.color;
div.style.position = 'absolute';
this.x = parseInt(Math.random() * (map.offsetWidth / this.width)) * this.width;
this.y = parseInt(Math.random() * (map.offsetHeight / this.height)) * this.height;
div.style.left = this.x + "px";
div.style.top = this.y + "px";
dvarr.push(div);
}
function remove() {
for(var i = 0; i < dvarr.length; i++) {
var ele = dvarr[i];
ele.parentNode.removeChild(ele);
dvarr.splice(i,1);
}
}
window.food = new Food();
}());
(function() {
var element = [];
function Snake(width, height, direction) {
this.width = width || 20;
this.height = height || 20;
this.body = [{
x: 3,
y: 2,
color: "red"
},
{
x: 2,
y: 2,
color: "orange"
},
{
x: 1,
y: 2,
color: "orange"
}
];
this.direction = direction || 'right';
};
Snake.prototype.init = function(map) {
remove();
for(var i = 0; i < this.body.length; i++) {
var obj = this.body[i];
var div = document.createElement("div");
div.style.position = "absolute";
map.appendChild(div);
div.style.width = this.width + "px";
div.style.height = this.height + "px";
div.style.backgroundColor = obj.color;
div.style.left = obj.x * this.width + "px";
div.style.top = obj.y * this.height + "px";
element.push(div);
}
}
Snake.prototype.move = function(food, map) {
var i = this.body.length - 1;
for(; 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 "right":
this.body[0].x += 1;
break;
case "left":
this.body[0].x -= 1;
break;
case "top":
this.body[0].y -= 1;
break;
case "bottom":
this.body[0].y += 1;
break;
}
var headX = this.body[0].x*this.width;
var headY = this.body[0].y*this.height;
if (headX == food.x && headY==food.y) {
var last = this.body[this.body.length-1];
this.body.push({
x:last.x,
y:last.y,
color:last.color
});
food.init(map);
}
};
function remove() {
var i = element.length - 1;
for(; i >= 0; i--) {
var ele = element[i];
ele.parentNode.removeChild(ele);
element.splice(i, 1);
}
}
window.Snake = new Snake();
}());
(function() {
var that = null;
function Game() {
this.snake = Snake;
this.food = food;
this.map = map;
that = this;
};
Game.prototype.init = function() {
this.food.init(this.map);
this.snake.init(this.map);
this.snakerun(this.food,this.map);
this.bindkey(this.food,this.map);
};
Game.prototype.snakerun = function() {
var snakego = setInterval(function() {
this.snake.init(map);
this.snake.move(food, map);
var maxX = map.offsetWidth/this.snake.width;
var maxY = map.offsetHeight/this.snake.height;
var headX = this.snake.body[0].x;
var headY = this.snake.body[0].y;
if (headX<0||headX>maxX-1||headY<0||headY>maxY-1) {
clearInterval(snakego);
alert('游戏结束');
}
}.bind(that), 150);
};
Game.prototype.bindkey = function () {
document.addEventListener('keydown',function(e){
switch (e.keyCode){
case 37:this.snake.direction = "left";
break;
case 38:this.snake.direction = "top";
break;
case 39:this.snake.direction = "right";
break;
case 40:this.snake.direction = "bottom";
break;
}
}.bind(that),false);
};
window.Game = Game;
}());
var map = document.querySelector("#dvs");
var game = new Game(map);
game.init();
</script>
</html>
好啦,案例到这就结束啦,拜拜咯📅~
|