当前位置 : 主页 > 网络编程 > JavaScript >

使用js做的炸弹人小游戏

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt //1,主角:mainhero2,可行走地面:thefloor3,墙壁(砖块)wall4,,不可破坏墙壁:stone5,游走怪物:monster6,终点门:thedoor//7,爆炸效果:boom8,炸弹:bomb9,放置炸弹动作 putbomb//所用到的方法
gistfile1.txt
//1,主角:mainhero	2,可行走地面:thefloor	3,墙壁(砖块)wall	4,,不可破坏墙壁:stone		5,游走怪物:monster	6,终点门:thedoor
//7,爆炸效果:boom	8,炸弹:bomb		9,放置炸弹动作 putbomb
//所用到的方法
//所用到的变量

//定义初始变量
var mainhero=new Object();//为了确定人物坐标而不是单纯的定义人物
var maps;//定义所有地图的一个数组
var current=0;//当前关卡数
var map;//当前关卡地图
var thedoor;//目的地
var monsterlist=new Array();
var starslist=new Array();
var starsnum;
var bomb=new Object();
var showbombs;

//跳关方法
function jump()
{
	var jumpplace=document.getElementById("text1").value;
	current=jumpplace-1;
	init();
}

//初始化地图界面
function init()
{
	var bgm1=document.getElementById("bgm1");
	bgm1.innerHTML='';
	
	showbombs=10;
	
	document.getElementById("span1").innerHTML=showbombs;
	
	onloaddata()
	
	onloadmap()
	
	mon1(monsterlist[0].x,monsterlist[0].y)
	mon2(monsterlist[1].x,monsterlist[1].y)
	mon3(monsterlist[2].x,monsterlist[2].y)
	mon4(monsterlist[3].x,monsterlist[3].y)
	mon5(monsterlist[4].x,monsterlist[4].y)
	mon6(monsterlist[5].x,monsterlist[5].y)
}

var mainptx;
var mainpty;
var hc=0;//此变量用来限制炸弹放置次数,当满足值为0时才可放下炸弹,放下炸弹后将值赋为1,炸弹爆炸结束后将值赋为零

//键盘按下事件
//上 38、下 40、左 37、右 39、放置炸弹(Ctrl)17
function keydown(event)
{
	var nextpoint=new Object();
	switch(event.keyCode)
	{
		case 38:
			nextpoint.x=mainhero.x;
			nextpoint.y=mainhero.y-1;
			walkpoint(nextpoint.x,nextpoint.y);
		break;
		case 40:
			nextpoint.x=mainhero.x;
			nextpoint.y=mainhero.y+1;
			walkpoint(nextpoint.x,nextpoint.y);
		break;
		case 37:
			nextpoint.x=mainhero.x-1;
			nextpoint.y=mainhero.y;
			walkpoint(nextpoint.x,nextpoint.y);
		break;
		case 39:
			nextpoint.x=mainhero.x+1;
			nextpoint.y=mainhero.y;
			walkpoint(nextpoint.x,nextpoint.y);
		break;
		case 17:
			if(showbombs>0)
			{
				if(hc==0)
				{
					putbomb(mainhero.x,mainhero.y);
					hc=1;
					showbombs=showbombs-1;
					document.getElementById("span1").innerHTML=showbombs;
				}
			}
		break;
	}
	backbomb();
}

//人物行走下一个位置(可行走地面2、可破坏墙壁3、不可破坏墙壁4、怪物5、终点门6、)【碰到炸弹不让他通过,对啊~】
function walkpoint(nptx,npty)
{
	switch(map[npty][nptx])
	{
		case 2:
			document.getElementById(nptx+"-"+npty).src="img/1.png";
			document.getElementById(mainhero.x+"-"+mainhero.y).src="img/2.png";
			
			mainptx=mainhero.x;//这两步用来存值
			mainpty=mainhero.y;
			
			//修改源数组数据
			map[npty][nptx]=1;
			map[mainhero.y][mainhero.x]=2;
			
			//刷新地图数据主角位置
			mainhero.x=nptx;
			mainhero.y=npty;
		break;
		case 3:
		break;
		case 4:
		break;
		case 5:
			document.getElementById(nptx+"-"+npty).src="img/7.png";//爆炸效果
			document.getElementById(mainhero.x+"-"+mainhero.y).src="img/2.png";
			
			//修改源数组数据
			map[npty][nptx]=7;
			map[mainhero.y][mainhero.x]=2;
			
			//刷新地图数据主角位置【哦后来发现不需要了
			//mainhero.x=npty;
			//mainhero.y=npty;
			var abc=0;
			gameover(abc);
		break;
		case 6:
			passgame();
		break;
		case 7:
			document.getElementById(nptx+"-"+npty).src="img/7.png";
			document.getElementById(mainhero.x+"-"+mainhero.y).src="img/2.png";
			setTimeout('gameover(0)',500);
		break;
		case 12:
			var bgm3=document.getElementById("bgm3");//播放音效
			bgm3.innerHTML='';
			
			document.getElementById(nptx+"-"+npty).src="img/1.png";
			document.getElementById(mainhero.x+"-"+mainhero.y).src="img/2.png";
			
			//修改源数组数据
			map[npty][nptx]=1;
			map[mainhero.y][mainhero.x]=2;
			
			//刷新地图数据主角位置
			mainhero.x=nptx;
			mainhero.y=npty;
			
			starsnum=starsnum-1//用来判断过关收集要素
			
			document.getElementById("span2").innerHTML=starsnum;
		break;
	}
}

//还原炸弹的方法
function backbomb()
{
	if(mainptx==bomb.x&&mainpty==bomb.y)
	{
		document.getElementById(mainptx+"-"+mainpty).src="img/8.png";
		
		map[mainpty][mainptx]=8;
	}
}
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓怪物部分↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
//第一个怪物的移动方法,先往上再往下
var mx1;
var my1;
var sid1;
var nmx1;
var nmy1;
var changes1=1;
function mon1(mon1x,mon1y)
{
	mx1=mon1x;
	my1=mon1y;
	change1(changes1);
	sid1=setTimeout('move1()',400);
}

//切换上下的方法
function change1(changes1)
{
	if(changes1==1)
	{
		nmx1=mx1;
		nmy1=my1-1;
	}else
	{
		nmx1=mx1;
		nmy1=my1+1;
	}
}	
function move1()
{
	clearTimeout(sid1);
	if(map[nmy1][nmx1]==2)
	{
		document.getElementById(nmx1+"-"+nmy1).src="img/5.png";
		document.getElementById(mx1+"-"+my1).src="img/2.png";
			
		//修改源数组数据
		map[nmy1][nmx1]=5;
		map[my1][mx1]=2;
			
		//刷新地图数据(怪物坐标)
		mx1=nmx1;
		my1=nmy1;
		
		//再次调用行走方法进行判断
		mon1(mx1,my1);
	}else if(map[nmy1][nmx1]==1)
	{
		document.getElementById(nmx1+"-"+nmy1).src="img/7.png";
		document.getElementById(mx1+"-"+my1).src="img/2.png";
		gameover(0);
	}else if(map[nmy1][nmx1]==7)
	{
		document.getElementById(nmx1+"-"+nmy1).src="img/7.png";
		document.getElementById(mx1+"-"+my1).src="img/2.png";
		//修改源数组数据
		map[nmy1][nmx1]=7;
		map[my1][mx1]=2;
	}else
	{
		changes1=changes1*-1;
		mon1(mx1,my1);
	}
}

//第2个怪物的移动方法,先往上再往下
var mx2;
var my2;
var sid2;
var nmx2;
var nmy2;
var changes2=1;
function mon2(mon2x,mon2y)
{
	mx2=mon2x;
	my2=mon2y;
	change2(changes2);
	sid2=setTimeout('move2()',400);
}

//切换上下的方法
function change2(changes2)
{
	if(changes2==1)
	{
		nmx2=mx2;
		nmy2=my2-1;
	}else
	{
		nmx2=mx2;
		nmy2=my2+1;
	}
}	
function move2()
{
	clearTimeout(sid2);
	if(map[nmy2][nmx2]==2)
	{
		document.getElementById(nmx2+"-"+nmy2).src="img/5.png";
		document.getElementById(mx2+"-"+my2).src="img/2.png";
			
		//修改源数组数据
		map[nmy2][nmx2]=5;
		map[my2][mx2]=2;
			
		//刷新地图数据(怪物坐标)
		mx2=nmx2;
		my2=nmy2;
		
		//再次调用行走方法进行判断
		mon2(mx2,my2);
	}else if(map[nmy2][nmx2]==1)
	{
		document.getElementById(nmx2+"-"+nmy2).src="img/7.png";
		document.getElementById(mx2+"-"+my2).src="img/2.png";
		gameover(0);
	}else if(map[nmy2][nmx2]==7)
	{
		document.getElementById(nmx2+"-"+nmy2).src="img/7.png";
		document.getElementById(mx2+"-"+my2).src="img/2.png";
		//修改源数组数据
		map[nmy2][nmx2]=7;
		map[my2][mx2]=2;
	}else
	{
		changes2=changes2*-1;
		mon2(mx2,my2);
	}
}

//第3个怪物的移动方法,先往上再往下
var mx3;
var my3;
var sid3;
var nmx3;
var nmy3;
var changes3=1;
function mon3(mon3x,mon3y)
{
	mx3=mon3x;
	my3=mon3y;
	change3(changes3);
	sid3=setTimeout('move3()',400);
}

//切换上下的方法
function change3(changes3)
{
	if(changes3==1)
	{
		nmx3=mx3;
		nmy3=my3-1;
	}else
	{
		nmx3=mx3;
		nmy3=my3+1;
	}
}	
function move3()
{
	clearTimeout(sid3);
	if(map[nmy3][nmx3]==2)
	{
		document.getElementById(nmx3+"-"+nmy3).src="img/5.png";
		document.getElementById(mx3+"-"+my3).src="img/2.png";
			
		//修改源数组数据
		map[nmy3][nmx3]=5;
		map[my3][mx3]=2;
			
		//刷新地图数据(怪物坐标)
		mx3=nmx3;
		my3=nmy3;
		
		//再次调用行走方法进行判断
		mon3(mx3,my3);
	}else if(map[nmy3][nmx3]==1)
	{
		document.getElementById(nmx3+"-"+nmy3).src="img/7.png";
		document.getElementById(mx3+"-"+my3).src="img/2.png";
		gameover(0);
	}else if(map[nmy3][nmx3]==7)
	{
		document.getElementById(nmx3+"-"+nmy3).src="img/7.png";
		document.getElementById(mx3+"-"+my3).src="img/2.png";
		//修改源数组数据
		map[nmy3][nmx3]=7;
		map[my3][mx3]=2;
	}else
	{
		changes3=changes3*-1;
		mon3(mx3,my3);
	}
}

//第4个怪物的移动方法,先往上再往下
var mx4;
var my4;
var sid4;
var nmx4;
var nmy4;
var changes4=1;
function mon4(mon4x,mon4y)
{
	mx4=mon4x;
	my4=mon4y;
	change4(changes4);
	sid4=setTimeout('move4()',400);
}

//切换上下的方法
function change4(changes4)
{
	if(changes4==1)
	{
		nmx4=mx4;
		nmy4=my4-1;
	}else
	{
		nmx4=mx4;
		nmy4=my4+1;
	}
}	
function move4()
{
	clearTimeout(sid4);
	if(map[nmy4][nmx4]==2)
	{
		document.getElementById(nmx4+"-"+nmy4).src="img/5.png";
		document.getElementById(mx4+"-"+my4).src="img/2.png";
			
		//修改源数组数据
		map[nmy4][nmx4]=5;
		map[my4][mx4]=2;
			
		//刷新地图数据(怪物坐标)
		mx4=nmx4;
		my4=nmy4;
		
		//再次调用行走方法进行判断
		mon4(mx4,my4);
	}else if(map[nmy4][nmx4]==1)
	{
		document.getElementById(nmx4+"-"+nmy4).src="img/7.png";
		document.getElementById(mx4+"-"+my4).src="img/2.png";
		gameover(0);
	}else if(map[nmy4][nmx4]==7)
	{
		document.getElementById(nmx4+"-"+nmy4).src="img/7.png";
		document.getElementById(mx4+"-"+my4).src="img/2.png";
		//修改源数组数据
		map[nmy4][nmx4]=7;
		map[my4][mx4]=2;
	}else
	{
		changes4=changes4*-1;
		mon4(mx4,my4);
	}
}

//第5个怪物的移动方法,先往上再往下
var mx5;
var my5;
var sid5;
var nmx5;
var nmy5;
var changes5=1;
function mon5(mon5x,mon5y)
{
	mx5=mon5x;
	my5=mon5y;
	change5(changes5);
	sid5=setTimeout('move5()',400);
}

//切换上下的方法
function change5(changes5)
{
	if(changes5==1)
	{
		nmx5=mx5;
		nmy5=my5-1;
	}else
	{
		nmx5=mx5;
		nmy5=my5+1;
	}
}	
function move5()
{
	clearTimeout(sid5);
	if(map[nmy5][nmx5]==2)
	{
		document.getElementById(nmx5+"-"+nmy5).src="img/5.png";
		document.getElementById(mx5+"-"+my5).src="img/2.png";
			
		//修改源数组数据
		map[nmy5][nmx5]=5;
		map[my5][mx5]=2;
			
		//刷新地图数据(怪物坐标)
		mx5=nmx5;
		my5=nmy5;
		
		//再次调用行走方法进行判断
		mon5(mx5,my5);
	}else if(map[nmy5][nmx5]==1)
	{
		document.getElementById(nmx5+"-"+nmy5).src="img/7.png";
		document.getElementById(mx5+"-"+my5).src="img/2.png";
		gameover(0);
	}else if(map[nmy5][nmx5]==7)
	{
		document.getElementById(nmx5+"-"+nmy5).src="img/7.png";
		document.getElementById(mx5+"-"+my5).src="img/2.png";
		//修改源数组数据
		map[nmy5][nmx5]=7;
		map[my5][mx5]=2;
	}else
	{
		changes5=changes5*-1;
		mon5(mx5,my5);
	}
}

//第6个怪物的移动方法,先往上再往下
var mx6;
var my6;
var sid6;
var nmx6;
var nmy6;
var changes6=1;
function mon6(mon6x,mon6y)
{
	mx6=mon6x;
	my6=mon6y;
	change6(changes6);
	sid6=setTimeout('move6()',400);
}

//切换上下的方法
function change6(changes6)
{
	if(changes6==1)
	{
		nmx6=mx6;
		nmy6=my6-1;
	}else
	{
		nmx6=mx6;
		nmy6=my6+1;
	}
}	
function move6()
{
	clearTimeout(sid6);
	if(map[nmy6][nmx6]==2)
	{
		document.getElementById(nmx6+"-"+nmy6).src="img/5.png";
		document.getElementById(mx6+"-"+my6).src="img/2.png";
			
		//修改源数组数据
		map[nmy6][nmx6]=5;
		map[my6][mx6]=2;
			
		//刷新地图数据(怪物坐标)
		mx6=nmx6;
		my6=nmy6;
		
		//再次调用行走方法进行判断
		mon6(mx6,my6);
	}else if(map[nmy6][nmx6]==1)
	{
		document.getElementById(nmx6+"-"+nmy6).src="img/7.png";
		document.getElementById(mx6+"-"+my6).src="img/2.png";
		gameover(0);
	}else if(map[nmy6][nmx6]==7)
	{
		document.getElementById(nmx6+"-"+nmy6).src="img/7.png";
		document.getElementById(mx6+"-"+my6).src="img/2.png";
		//修改源数组数据
		map[nmy6][nmx6]=7;
		map[my6][mx6]=2;
	}else
	{
		changes6=changes6*-1;
		mon6(mx6,my6);
	}
}
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑怪物部分↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑


//放置炸弹
function putbomb(mpx,mpy)
{
	document.getElementById(mpx+"-"+mpy).src="img/9.png";
	bomb.x=mpx;
	bomb.y=mpy;
	
	setTimeout('judge()',2000);
	
}
//判断炸弹周围的东西
function judge()
{
	document.getElementById(bomb.x+"-"+bomb.y).src="img/7.png";
	placex=bomb.x-1;
	placey=bomb.y;
	startboom(placex,placey);
	placex=bomb.x+1;
	placey=bomb.y;
	startboom(placex,placey);
	placex=bomb.x;
	placey=bomb.y-1;
	startboom(placex,placey);
	placex=bomb.x;
	placey=bomb.y+1;
	startboom(placex,placey);
}

//炸弹开始爆炸,及范围内一切东西都变成火花,
//除了4(不可破坏墙壁)6(终点门)
//墙壁(3)怪物(5)
//人物——游戏结束
function startboom(placex,placey)
{
	var bgm2=document.getElementById("bgm2");//播放音效
	bgm2.innerHTML='';
	
	switch(map[placey][placex])
	{
		case 1:
			map[bomb.y][bomb.x]=7;
			document.getElementById(placex+"-"+placey).src="img/7.png";
			map[placey][placex]=7;
			setTimeout('gameover(0)',500);
		break;
		case 2:
			map[bomb.y][bomb.x]=7;
			document.getElementById(placex+"-"+placey).src="img/7.png";
			map[placey][placex]=7;
			
		break;
		case 3:
			map[bomb.y][bomb.x]=7;
			document.getElementById(placex+"-"+placey).src="img/7.png";
			map[placey][placex]=7;
			
		break;
		case 4:
		break;
		case 5:
			map[bomb.y][bomb.x]=7;
			document.getElementById(placex+"-"+placey).src="img/7.png";
			map[placey][placex]=7;
			
		break;
		case 6:
		break;
	}
	setTimeout('stopboom()',1000);
}
//炸弹爆炸结束
function stopboom()
{
	for(var i=0;i
网友评论