//功能
1,碰撞到自己身体 游戏结束;
2,碰撞到墙壁 游戏结束
3,添加游戏的背景音乐,并且控制暂停和播放;
4,给蛇添加眼睛(两个眼睛);
5,设计项目的界面(开始游戏,暂停游戏,记录分数,选择关卡);/
/**HTML代码
<head>
<meta charset="UTF-8">
<style type="text/css">
#bt {
margin: 0;
padding: 0;
width: 800px;
height: 600px;
margin: 40px auto;
position: relative;
color: ;
}
img {
width: 800px;
height: 600px;
}
#hg {
width: 200px;
height: 100px;
position: absolute;
left: 300px;
top: 250px;
}
input {
font-size: 20px;
width: 150px;
height: 50px;
margin: 10px auto;
border-radius: 20%;
font-family: "宋体";
background: url(kaishi.jpg);
background-size: 100%;
overflow: hidden;
}
</style>
<title></title>
</head>
<body bgcolor="wheat">
<div id="bt"><img src="strat.jpg" />
<div id="hg">
<input type="button" id="id1" value="开始游戏" />
<input type="button" id="id2" value="中级模式" />
<input type="button" id="id3" value="困难模式" />
</div>
</div>
<audio id="bgm1" autoplay="autoplay">
<source src="1.mp3"></source>
</audio>
<script type="text/javascript" src="snake4.js">
</script>
</body>
**js代码块
var div_map; //创造地图的对象
var div_food; //创造食物的对象
var div_foodf;
var div_foodo;
var div_snake; //创建蛇的对象
var sum=0; //初始化的分数
var time; //接收定时器的变量
var div_eye; //创建眼睛的变量
var div_an1; //创建暂停继续
var sort; //分数的div
var sor;
var div;
var btn;
var bgm;
var bgm1;
window.onload=function(){
div=document.getElementById('bt');
btn=document.getElementById('hg');
var id=document.getElementById('id1');
var id2=document.getElementById('id2');
var id3=document.getElementById('id3');
id.onclick=function(){
shili('url(tim.jpg)',300);
}
id2.onclick=function(){
shili('url(lans.jpg)' ,400);
}
id3.onclick=function(){
shili('url(xue.jpg)',100);
}
bgm=document.getElementById('bgm1') ;
//console.log(bgm);
bgm.autoplay='autoplay';
function shili(m,n){
//开始页面的隐藏
div.style.display='none';
btn.style.display='none';
//实例化地图
div_map=new Map();
div_map.backgroundImage=m;
div_map.display()
//实例化食物
div_food=new Food();
div_food.display()
div_foodf=new Foodf();
div_foodf.display();
div_foodo=new Foodo();
div_foodo.display();
//实例化的蛇
div_snake=new Snake(n);
div_snake.display();
div_snake.move();
//实例化的眼睛
div_eye=new Eye();
div_eye.display();
//实例化按钮
div_an1=new Stop();
div_an1.display();
//计分板的创建
sort=document.createElement('div');
sort.style.position='absolute';
sort.style.background='lightpink';
sort.style.width=45+'px';
sort.style.height=45+'px';
sort.innerHTML='得分:';
document.getElementsByTagName('body')[0].appendChild(sort);
sort.style.margin='-420px 305px';
sor=document.createElement('div');
sor.innerHTML=sum;
sor.style.position='absolute';
sor.style.margin='2px auto';
sort.appendChild(sor);
//给body添加点击事件用来控制键盘的移动
document.getElementsByTagName('body')[0].onkeydown=function(evevt){
//console.log(event.keyCode);
div_snake.setDirect(event.keyCode);
}
}
}
//创造地图对象
function Map(){
//地图对象的属性
this.width=800;
this.height=600;
this._map=null;
this.backgroundImage=null;
//地图对象的样式
this.display=function(){
this._map=document.createElement('div');
this._map.style.background=this.backgroundImage;
this._map.style.width=this.width+'px';
this._map.style.height=this.height+'px';
this._map.style.margin='40px auto';
this._map.style.position='relative';
document.getElementsByTagName('body')[0].appendChild(this._map);
}
}
//创建食物对象
function Food(){
//食物的属性
this.width=20;
this.height=20;
this.color='darkmagenta';
this.food=null;
//x y用于后来蛇吃食物
this.x=0;
this.y=0;
//食物的样式
this.display=function(){
//解决蛇碰到食物后,食物随机重复出现
if(this.food==null){
this.food=document.createElement('div');
this.food.style.width=this.width+'px';
this.food.style.height=this.height+'px';
this.food.style.background=this.color;
this.food.style.position='absolute';
// this.food.style.borderRadius='50%';
div_map._map.appendChild(this.food);
}
this.x=Math.round(Math.random()*39);
this.y=Math.round(Math.random()*29);
this.food.style.left=this.x*this.width+'px';
this.food.style.top=this.y*this.height+'px';
}
}
function Foodf(){
//食物的属性
this.width=20;
this.height=20;
this.color='red';
this.x1=0;
this.y1=0;
this.food=null;
this.display=function(){
if(this.food==null){
this.food=document.createElement('div');
this.food.style.width=this.width+'px';
this.food.style.height=this.height+'px';
this.food.style.background=this.color;
this.food.style.position='absolute';
// this.food.style.borderRadius='50%';
div_map._map.appendChild(this.food)
}
this.x1=Math.round(Math.random()*39);
this.y1=Math.round(Math.random()*29);
this.food.style.left=this.x1*this.width+'px';
this.food.style.top=this.y1*this.height+'px';
}
}
function Foodo(){
//食物的属性
this.width=40;
this.height=40;
this.color='red';
this.x2=0;
this.y2=0;
this.food=null;
this.display=function(){
if(this.food==null){
this.food=document.createElement('div');
this.food.style.width=this.width+'px';
this.food.style.height=this.height+'px';
this.food.style.background=this.color;
this.food.style.position='absolute';
this.food.style.borderRadius='50%';
div_map._map.appendChild(this.food)
}
this.x2=Math.round(Math.random()*19);
this.y2=Math.round(Math.random()*14);
this.food.style.left=this.x2*this.width+'px';
this.food.style.top=this.y2*this.height+'px';
}
}
//创建蛇的对象 n传递速度的参数
function Snake(n){
this.width=20;
this.height=20;
this.direct=null;
//二维数组用来存储蛇的数据
this.snakeArr=[
[6,2,null,'pink'],
[5,2,null,'pink'],
[4,2,null,'pink']
];
//蛇的显示方法
this.display=function(){
for(var i=0;i<this.snakeArr.length;i++){
//解决了蛇移动时一直增加
if(this.snakeArr[i][2]==null){
this.snakeArr[i][2]=document.createElement('div');
this.snakeArr[i][2].style.width=this.width+'px';
this.snakeArr[i][2].style.height=this.height+'px';
this.snakeArr[i][2].style.background=this.snakeArr[i][3];
this.snakeArr[i][2].style.position='absolute';
this.snakeArr[i][2].style.borderRadius='50%';
div_map._map.appendChild(this.snakeArr[i][2]);
}
this.snakeArr[i][2].style.left=this.snakeArr[i][0]*this.width+'px';
this.snakeArr[i][2].style.top=this.snakeArr[i][1]*this.height+'px';
}
}
//传递按钮的键码
this.setDirect=function(code){
switch(code){
case 37:
//console.log('zuo');
// this.snakeArr[0][0]-=1;
if(this.direct!='right'){
this.direct='left';
this.snakeArr[0][2].style.transform='rotateZ(180deg)'; }''
break;
case 38:
if(this.direct!='down'){
//console.log('上')
this.direct='up'
this.snakeArr[0][2].style.transform='rotateZ(-90deg)';}
break;
case 39:
//console.log('右')
if(this.direct!='left'){
this.direct='right';
this.snakeArr[0][2].style.transform='rotateZ(0deg)';}
break;
case 40:
//console.log('下')
if(this.direct!='up'){
this.direct='down';
this.snakeArr[0][2].style.transform='rotateZ(90deg)';}
break;
default:
alert('请按住键盘')
break;
}
}
//通过按钮的键码控制蛇的移动
this.move=function(){
//解决蛇叠加
if(this.direct!=null){
//让后面的蛇节的坐标等于前面蛇节的坐标
for(var i=this.snakeArr.length-1;i>0;i--){
this.snakeArr[i][0]=this.snakeArr[i-1][0];
this.snakeArr[i][1]=this.snakeArr[i-1][1];
}
switch(this.direct){
case 'left':
this.snakeArr[0][0]-=1;
break;
case 'up':
this.snakeArr[0][1]-=1;
break;
case 'right':
this.snakeArr[0][0]+=1;
break;
case 'down':
this.snakeArr[0][1]+=1;
break;
default:
break;
}
if(this.snakeArr[0][0]==div_foodf.x1&&this.snakeArr[0][1]==div_foodf.y1){
div_foodf.display();
sum-=1;
sor.innerHTML=sum;
var x=this.snakeArr[this.snakeArr.length-1][0];
var y=this.snakeArr[this.snakeArr.length-1][1];
div_map._map.removeChild(this.snakeArr[this.snakeArr.length-1][2]);
this.snakeArr.pop([x,y,null,'blue']);
div_snake.display();
a=document.createElement('audio')
a.src='../img/7.mp3';
a.play();
document.getElementsByTagName('body')[0].appendChild(a)
console.log(a)
}
//蛇吃到食物增加身体;
if(this.snakeArr[0][0]==div_food.x &&this.snakeArr[0][1]==div_food.y){
// bgm1.play();
div_food.display();
//记分
sum+=2;
sor.innerHTML=sum;
var x=this.snakeArr[this.snakeArr.length-1][0];
var y=this.snakeArr[this.snakeArr.length-1][1];
this.snakeArr.push([x,y,null,'pink'])
a=document.createElement('audio')
a.src='../img/7.mp3';
a.play();
document.getElementsByTagName('body')[0].appendChild(a)
console.log(a)
}
if(Math.pow(Math.abs(div_foodo.x2*div_foodo.width)-div_snake.snakeArr[0][0]*div_snake.width,2)+Math.pow(Math.abs(div_foodo.y2*div_foodo.height)-div_snake.snakeArr[0][1]*div_snake.height,2)<625){
div_foodo.display();
//记分
sum+=4;
sor.innerHTML=sum;
var x=this.snakeArr[this.snakeArr.length-1][0];
var y=this.snakeArr[this.snakeArr.length-1][1];
this.snakeArr.push([x,y,null,'pink']);
this.snakeArr.push([x,y,null,'pink']);
a=document.createElement('audio')
a.src='../img/7.mp3';
a.play();
document.getElementsByTagName('body')[0].appendChild(a)
console.log(a)
}
//撞到墙壁死亡
if(this.snakeArr[0][0]==40||this.snakeArr[0][1]==30||this.snakeArr[0][0]==-1||this.snakeArr[0][1]==-1){
alert('Game Over!');
if(confirm('是否重新开始')){
window.location.reload();
}else{
return false;//阻止程序继续运行
}
}
}
//撞到自己后死亡
for(var i=1;i<this.snakeArr.length;i++){
if(this.snakeArr[0][0]==this.snakeArr[i][0]&&this.snakeArr[0][1]==this.snakeArr[i][1])
{
if(confirm('是否重新开始')){
window.location.reload();
}else{
return false;//阻止程序继续运行
}
}
}
div_snake.display();
time=setTimeout('div_snake.move()',n);
}
}
//创建蛇的眼睛
function Eye(){
this.width=5;
this.height=5;
this.color='green';
this.eyeArr=[[1,0,null,' aquamarine'],
[1,0,null,' aquamarine']];
this.display=function(){
for(var i=0;i<this.eyeArr.length;i++){
this.eyeArr[i][2]=document.createElement('div');
this.eyeArr[i][2].style.width=this.width+'px';
this.eyeArr[i][2].style.height=this.height+'px';
this.eyeArr[i][2].style.background=this.color;
this.eyeArr[i][2].style.margin='2px auto';
this.eyeArr[i][2].style.borderRadius='60%'
div_snake.snakeArr[0][2].appendChild(this.eyeArr[i][2]);
}
}
}
//按钮的暂停和继续
function Stop(){
var num = 0;
this.display=function(){
this.an=document.createElement('button');
this.an.style.width=50+'px';
this.an.style.height=50+'px';
this.an.style.background='pink';
this.an.innerHTML='暂停按钮';
this.an.style.position='absolute';
this.an.style.margin='-550px 300px';
this.an.style.overflow=' hidden';
document.getElementsByTagName('body')[0].appendChild(this.an);
this.an.onclick=function(){
if(num==0){
clearTimeout(time);
bgm.pause();
num=1;
}else{
div_snake.move();
bgm.play();
num = 0;
}
}
}
}
**