小游戏贪吃蛇之---------初级版本
贪吃蛇小游戏我相信大家都玩过,我就不仔细介绍了,直接上代码,我只是刚入门,希望看到的小伙伴给提意见,我自己也会慢慢优化
贪吃蛇的几个页面效果图如下
- 首页
- 游戏开始页面
- 游戏结束页面
代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#map{
width: 800px;
height: 800px;
background-color: green;
position: relative;
margin: 10px auto;
}
#mapinner{
display: block;
text-align: center;
padding-top:58px;
font-size: 80px;
color: white;
}
#btn1,#btn2,#btn0,#btn3,#btn4,#btn5{
width: 300px;
height: 60px;
line-height: 60px;
font-size: 40px;
position: absolute;
background-color: purple;
color:white;
border: 1px solid purple;
border-radius: 5px;
box-shadow: purple 0 0 2px 3px ;
left: 50%;
margin-left: -150px;
}
#btn0{
top: 300px;
}
#btn1{
top: 410px;
}
#btn2{
top: 510px;
}
#btn3{
top: 630px;
}
#btn5{
top: 500px;
display: none;
}
#btn4{
top: 350px;
display: none;
}
</style>
</head>
<body>
<div id="map">
<h1 id="mapinner">WELCOME TO SNAKE WORLD</h1>
<button id="btn0">开始游戏</button>
<button id="btn1">初出茅庐</button>
<button id="btn2">有把刷子</button>
<button id="btn3">无所畏惧</button>
<button id="btn4">返回首页</button>
<button id="btn5">再来一次</button>
</div>
<script src="js/common.js"></script>
<script>
var mapinner=myId("mapinner");//获取标题,即h1标签
var btns=myTN("button");//获取所有的button
var map=myId("map");//获取地图
var shiWu;//定义储存食物的盒子,以便在小蛇死亡的时候删除食物
//在首页开始游戏是,页面的变化
function start(){
mapinner.innerText="";//让h1标签文字为空,即标题为空
for(var i=0;i<4;i++){
btns[i].style.display="none";//让所有的按钮隐藏
}
}
//小蛇死亡的时候页面的变化
function close(){
btns[4].style.display="block";//显示返回首页
btns[5].style.display="block";//显示再来一次
map.removeChild(shiWu);//删掉食物
mapinner.style.fontSize=100+"px";//给h1标签设置字体大小
mapinner.style.paddingTop=120+"px";//给h1标签设置位置
}
//再来一次游戏时页面的效果
function again(){
btns[4].style.display="none";//让返回首页隐藏
btns[5].style.display="none";//让再来一次隐藏
}
//食物对象
((function(){
//要整体一点
//食物对象,小蛇对象,最后整合成游戏对象
//1.自定义食物构造函数-----一般属性放在构造函数里面
//@pramas(width 食物的宽 height 食物的宽 color食物的背景颜色 x 食物的left值)
function Food(width,height,color,x,y){
this.width=width||20;
this.height=height||20;
this.color=color||"purple";
this.x=x||0;
this.y=y||0;
this.element=document.createElement("div");
}
//2.在原型对象上初始化食物
Food.prototype.init=function(){
//2-1.设置div的样式,并添加进去
var div=this.element;
shiWu=this.element;
div.style.width=this.width+"px";
div.style.height=this.height+"px";
div.style.backgroundColor=this.color;
div.style.position="absolute";
//设置随机坐标(0-39)*20
//1.取随机坐标
this.x=parseInt(Math.random()*(map.offsetWidth/this.width))*this.width;
this.y=parseInt(Math.random()*(map.offsetHeight/this.height))*this.height;
//2.设置随机坐标
div.style.left=this.x+"px";
div.style.top=this.y+"px";
map.appendChild(div);
}
// 3.把Food暴露给window
window.Food=Food;
})());
//小蛇对象
((function(){
var elements=[];//存放蛇的部位
//1.小蛇的构造函数
function Snake(width,height,direction){
//小蛇的每个部位
this.width=width||20;
this.height=height||20;
//小蛇头的方向
this.direction=direction||"right";
//小蛇的身体刻度,颜色
this.body=[
{x:3,y:1,color:"green"},//小蛇的头
{x:2,y:1,color:"#82C734"},//身体
{x:1,y:1,color:"#82C734"}//身体
]
}
//2.在原型中初始化小蛇
Snake.prototype.init=function(){
//在创建新小蛇之前,先把最前面创建的小蛇删掉
remove();
//2.1遍历地创建div
for(var i=0;i<this.body.length;i++){
//每一个部位的刻度和颜色添加到obj对象中
var obj=this.body[i];
//创建div并添加样式
var div=document.createElement("div");
div.style.width=this.width+"px";
div.style.height=this.height+"px";
div.style.position="absolute";
div.style.backgroundColor=obj.color;
div.style.borderRadius="50%";
if(i==0){
div.style.backgroundImage="url(image/shetou.png)";
}
//设置坐标
div.style.left=obj.x*this.width+"px";
div.style.top=obj.y*this.height+"px";
map.appendChild(div);
//把蛇添加到数组里---目的:为了删除多画的蛇
elements.push(div);
}
}
//3.添加方法--移动小蛇
Snake.prototype.move=function(food){
//3.1改变小蛇身体的坐标,因为小蛇的头部判断方向
for(var i=this.body.length-1;i>0;i--){
//当i=2时,让第三块的x坐标=第二块x的坐标
this.body[i].x=this.body[i-1].x;
this.body[i].y=this.body[i-1].y;
}
//3.2 判断小蛇头部的坐标
//如果小蛇往上走,y-1;
switch(this.direction){
case "right":this.body[0].x++;//this.body[0]值得是小蛇的头部
break;
case "left":this.body[0].x--;
break;
case "top":this.body[0].y--;
break;
case "bottom":this.body[0].y++;
break;
}
//3.3判断小蛇是否吃到食物--即判断头部的坐标是否和食物的坐标一致
//头部的坐标
var headX=this.body[0].x*this.width;
var headY=this.body[0].y*this.height;
//食物的坐标
var foodX=food.x;
var foodY=food.y;
if(headX==foodX&&headY==foodY){
//追加一个蛇的身体到body最后
var last=this.body[this.body.length-1];//复制小蛇的尾巴
this.body.push({
x:last.x,
y:last.y,
color:last.color
})
//食物吃完了要重新刷新位置
food.init();
}
}
//4.封装删除小蛇的函数
function remove(){
//提问:先删除头还是尾巴
for(var i=elements.length-1;i>=0;i--){
var ele=elements[i];
//删除地图里面多余的div
ele.parentNode.removeChild(ele);
//删除数组里面存放的div
elements.splice(i,1);
}
}
window.Snake=Snake;
})());
// 游戏对象
((function(){
var that;
var map=myId("map");
//1.游戏的构造函数
function Game(){
this.food=new Food();
this.snake=new Snake();
// this.map=map;
that=this;
}
//2.初始化游戏
Game.prototype.init=function(){
this.food.init();
this.snake.init();
this.bindKey();
}
// 3.添加方法使小蛇跑起来
Game.prototype.runSnake=function(speed){
var timeId= setInterval(function(){//setInterval是由Window调用的
that.snake.move(that.food);
that.snake.init();
//判断横纵坐标的最大最小值
var maxX=map.offsetWidth/that.snake.width;
var maxY=map.offsetHeight/that.snake.height;
//蛇头的坐标
var headX=that.snake.body[0].x;
var headY=that.snake.body[0].y;
//判断小蛇是否吃了自己,如果是,则游戏结束
for(var i=1;i<that.snake.body.length-1;i++){
if(that.snake.body[0].x==that.snake.body[i].x&&
that.snake.body[0].y==that.snake.body[i].y){
mapinner.innerText="也许蛇肉很好吃";
close();
clearInterval(timeId);
}
}
//判断小蛇有没有撞墙,撞墙就游戏结束
if(headX<0||headX>=maxX){
//清除定时器
mapinner.innerText="GAME OVER";
close();
clearInterval(timeId);
}
if(headY<0||headY>=maxY){
//清除定时器
mapinner.innerText="GAME OVER";
close();
clearInterval(timeId);
}
},speed);
}
//4.设置用户按键,来改变小蛇移动的方向
Game.prototype.bindKey=function(){
document.addEventListener("keydown",function(e){
console.log(e.keyCode);
switch(e.keyCode){
case 37:
//添加判断是为了让小蛇不能掉头
if(that.snake.direction!="right"){
that.snake.direction="left";
}
break;
case 38:
if(that.snake.direction!="bottom"){
that.snake.direction="top";
}
break;
case 39:
if(that.snake.direction!="left"){
that.snake.direction="right";
}
break;
case 40:
if(that.snake.direction!="top"){
that.snake.direction="bottom";
}
break;
}
})
}
btns[4].onmousedown=function(){
window.location.reload();
}
window.Game=Game;
})());
//循环遍历首页按钮,并且设置对应的速度
for (var i=0;i<4;i++){
btns[i].index=i+1;//给每个btn添加一个索引,并将索引值设置给index
btns[i].onclick=function(){
start();
console.log(this.index);
var speed=200 / parseInt(this.index );//设置对应难度的速度
var game=new Game();
game.init();
game.runSnake(speed);
btns[5].onmousedown=function(){
var game=new Game();
game.init();
game.runSnake(speed);
start();
again();
}
}
}
</script>
</body>
</html>
以上代码及页面效果只是初级未加工的,后期会跟进页面的润色,贪吃蛇的基本效果都可以实现