⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newgame.java

📁 一个同学的毕业设计作品:JAVA版的手机游戏——海底探险 。互相学习一下吧!~
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
			gx[j]=rdm.nextInt(this.getWidth());
			gy[j]=rdm.nextInt(this.getWidth());
		}
	}
	//changeMap()
	//地图更新;
	//id为地图编号;
	//作者:石明华
	//2007-04-10
	protected void changeMap(int id){
		if(id == 0){
			
			rout1_1.move(20, 10);
			rout1_4.move(150, -200);
			
			rout2_2.move(100, 180);
			rout2_3.move(238, 220);
			rout2_4.move(0, 250);
			
			layManager.append(rout1_1);
			layManager.append(rout1_4);
			layManager.append(rout2_2);
			layManager.append(rout2_3);
			layManager.append(rout2_4);
			
			layManager.remove(rout1_2);
			layManager.remove(rout1_3);
			
			layManager.remove(background);
			background = background_2;
			background.move(0,0);
			layManager.append(background);
		}else if(id == 1){
			layManager.remove(background);
			
			layManager.remove(rout1_1);
			layManager.remove(rout1_4);
			
			layManager.remove(rout2_1);
			layManager.remove(rout2_2);
			layManager.remove(rout2_3);
			layManager.remove(rout2_4);
			
			background = background_3;
			background.move(0,0);
			layManager.append(background);
		}
		drawX = this.getWidth()/2-32; 
		drawY = 20;
		dx = 0;
		dy = 0;
		allded = 0;
	}
	//createMap();
	//第一次建立地图;
	//作者:石明华
	//2007-04-10
	protected void createMap(){
		background.move(0,0);
		layManager.append(background);
		layManager.append(rout1_1);
		layManager.append(rout1_2);
		layManager.append(rout1_3);
		layManager.append(rout1_4);
		
		layManager.append(rout2_1);
	}
	//disp();
	//显示内容;
	//作者:石明华
	//2007-04-10
	protected void disp(){
		
		g.setColor(0xffffff);
		g.fillRect(80, 1, this.getWidth(), 18);
		
		g.setColor(0x000000);
		g.drawString("海底大探险", 0, 0, Graphics.TOP|Graphics.LEFT);
		g.drawString("SCROE "+scroe, 135, 1, Graphics.TOP|Graphics.LEFT);
		g.drawLine(0,19,this.getWidth(),19);
		
		g.setColor(0xff0000);
		g.drawString("HP", 65, 1, Graphics.TOP|Graphics.LEFT);
		g.fillRect(80, 4, 50, 10);
		g.setColor(0xffffff);
		g.fillRect(81, 5, 48, 8);
		g.setColor(0xff0000);
		g.fillRect(81, 5, hp/2-1, 8);
		
		layManager.paint(g,0,20);
						
		flushGraphics();
	}
	//calcWAndc();
	//子弹轨迹运算;
	//作者:石明华
	//2007-04-10
	protected void calcWAndc(){
		for(int i = 0;i < 5 ; i++){
			if(worder[i] == 1){
				if((wx[i] < 0) || (wx[i] > this.getWidth()+40)){
					worder[i] = 0;
					layManager.remove(cw[i].kitsSprite);
					continue;
				}
				//计算当前子弹的坐标;
				calcWxy(wx[i], wy[i],tag[i],i);
				cw[i].kitsSprite.setPosition(wx[i], wy[i]);
				layManager.append(cw[i].kitsSprite);
				//检测子弹是否击中怪物并计算分数值;
				for(int j=0;j<cnt;j++){
					if((cw[i].kitsSprite.collidesWith(m[j].g1sprite, true))&&(alive[j] == 1)){	
						scroenum = srdm.nextInt(200);
						sid = j;
						alive[sid] =j + 5;
						worder[i] = 0;
						allded--;
						layManager.remove(cw[i].kitsSprite);
						layManager.remove(m[sid].g1sprite);
						if(MapID==0)
						{
							scroe++;
						}else if(MapID==1){
							scroe+=2;
						}else {
							scroe+=3;
						}
						break;
					}
				}
				if(alive[sid]-sid-5 == 0){
					
					if(scroenum < 30){
						m[sid].sc.setPosition(m[sid].g1sprite.getX(), m[sid].g1sprite.getY());
						layManager.append(m[sid].sc);
						alive[sid] += 10;
					}
				}
			}
		}
	}
	//calcg();
	//怪物运动路径运算;
	//作者:石明华
	//2007-04-10
	protected void calcg(){
		for (int k=0;k<cnt;k++){
			if(alive[k] == 1){
					//计算怪物与游戏主角的距离;
					near(drawX+dx,drawY+dy,gx[k],gy[k],k);
					//怪物i主角;
					m[k].g1sprite.nextFrame();
					m[k].g1sprite.setPosition(gx[k],gy[k]);
					layManager.append(m[k].g1sprite);
				}
			//刷新奖励;	
			if(alive[k]-15 == k){
				//获得物品奖励;
				if(sprite.collidesWith(m[k].sc, false)){
					if(MapID == 0){
						hp += 5;
					}else if(MapID == 1){
						hp+=8;

					}else if(MapID == 2){
						hp += 12;
						
					}
					if(hp>100) hp =100;
					layManager.remove(m[k].sc);
					alive[k] = 0;
				}else{
					m[k].sc.nextFrame();
				}
			}
		}
	}
	//calchp();
	//检测怪物是否击中游戏主角并计算主角的HP值;
	//作者:石明华
	//2007-04-10
	protected void calchp(){
		for(int i=0;i<cnt;i++){
			//如果游戏主角的HP值为0时游戏结束;
			if(hp/2<=0){
				gameflag = false;
			}
			//检测怪物是否击中游戏主角并计算主角的HP值;
			if(m[i].g1sprite.collidesWith(sprite, true)){
				if(MapID==0)
				{
					if(alive[i]==1)
						hp-=2;
				}else if(MapID==1){
					if(alive[i]==1)
						hp-=4;
				}else {
					if(alive[i]==1)
						hp-=6;
				}
			}
		}
	}
	//保存游戏进度;
	//作者:石明华
	//2007-04-10
	protected void saveGame(){
		//游戏主角坐标;
		data[0] = drawX;
		data[1] = drawY;
		data[2] = dx;
		data[3] = dy;
		
		//地图;
		data[4] = MapID;
		
		//怪物坐标;
		data[5] = gx[0];
		data[6] = gy[0];
		data[7] = gx[1];
		data[8] = gy[1];;
		data[9] = gx[2];
		data[10] = gy[2];;
		data[11] = gx[3];
		data[12] = gy[3];;
		data[13] = gx[4];
		data[14] = gy[4];;
		
		//主角生命值,得分;
		data[15] = hp;
		data[16] = scroe;
		
		//子弹坐标;
		data[17] = wx[0];
		data[18] = wy[0];
		data[19] = wx[1];
		data[20] = wy[1];
		data[21] = wx[2];
		data[22] = wy[2];
		data[23] = wx[3];
		data[24] = wy[3];
		data[25] = wx[4];
		data[26] = wy[4];
		
		//子弹方向坐标;
		data[27] = tag[0];
		data[28] = tag[1];
		data[29] = tag[2];
		data[30] = tag[3];
		data[31] = tag[4];
		
		//其它,奖励概率,怪物存活数;
		data[32] = sid;
		data[33] = scroenum;
		data[34] = allded;
		
		//怪物存活状态;
		data[35] = alive[0];
		data[36] = alive[1];
		data[37] = alive[2];
		data[38] = alive[3];
		data[39] = alive[4];
		
		//子弹存活状态;
		data[40] = worder[0];
		data[41] = worder[1];
		data[42] = worder[2];
		data[43] = worder[3];
		data[44] = worder[4];


	    try {
			   recordStroe = RecordStore.openRecordStore("进度1", true);
			   ByteArrayOutputStream baos = new ByteArrayOutputStream();
			   DataOutputStream dos = new DataOutputStream(baos);
			   for (int i = 0; i<45; i++)
			     dos.writeInt(data[i]);
			   byte[] b = baos.toByteArray();
			   if (recordStroe.getNumRecords() == 0) { // 没有记录
				   recordStroe.addRecord(b, 0, b.length);
			     }else{
				   recordStroe.setRecord(1, b, 0, b.length);
			     }
		   }catch(Exception ex){ }
		   finally {
		     try {
		    	 recordStroe.closeRecordStore();    
		     }
		     catch (Exception e){e.printStackTrace();}
		   }
	}
	
	//载入游戏;
	//作者:石明华
	//2007-04-10
	protected void loadGame(){
		try {	
			  recordStroe = RecordStore.openRecordStore("进度1", true);
		       ByteArrayInputStream bais = new ByteArrayInputStream(recordStroe.getRecord(1)); //就读第一个记录
		       DataInputStream dis = new DataInputStream(bais);
		       for (int i = 0; i<44;i++)
		       { 
				    data[i] = dis.readInt();  
				   //  把数据都读出来
		       }
		   }
		   catch (Exception ex) {
		   }
		   finally {
		     try {
		    	 recordStroe.closeRecordStore();    
		     }
		     catch (Exception e) {
		       e.printStackTrace();
		     }
		 }

	//游戏主角坐标;
	drawX = data[0];
	drawY = data[1];
	dx = data[2];
	dy = data[3];
	
	//地图;
	MapID = data[4];
	
	//怪物坐标;
	gx[0] = data[5];
	gy[0] = data[6];
	gx[1] = data[7];
	gy[1] = data[8];
	gx[2] = data[9];
	gy[2] = data[10];
	gx[3] = data[11];
	gy[3] = data[12];
	gx[4] = data[13];
	gy[4] = data[14];
	
	//主角生命值,得分;
	hp = data[15];
	scroe = data[16];
	
	//子弹坐标;
	wx[0] = data[17];
	wy[0] = data[18];
	wx[1] = data[19];
	wy[1] = data[20];
	wx[2] = data[21];
	wy[2] = data[22];
	wx[3] = data[23];
	wy[3] = data[24];
	wx[4] = data[25];
	wy[4] = data[26];
	
	//子弹方向坐标;
	tag[0] = data[27];
	tag[1] = data[28];
	tag[2] = data[29];
	tag[3] = data[30];
	tag[4] = data[31];
	
	//其它,奖励概率,怪物存活数;
	sid = data[32];
	scroenum = data[33];
	allded = data[34];
	
	//怪物存活状态;
	alive[0] = data[35];
	alive[1] = data[36];
	alive[2] = data[37];
	alive[3] = data[38];
	alive[4] = data[39];
	
	//子弹存活状态;
	worder[0] = data[40];
	worder[1] = data[41];
	worder[2] = data[42];
	worder[3] = data[43];
	worder[4] = data[44];
	}
	
	protected void clearw(){
		for(int i = 0;i<5;i++){
			worder[i] = 0;
			layManager.remove(cw[i].kitsSprite);
		}
		try{
			Thread.sleep(50);
		}catch(InterruptedException e){
			e.printStackTrace();
		}
	}
	//保存游戏分数;
	//作者:石明华
	//2007-04-10
	protected void saveScroe(){
		if(gameflag){
	    try {
			   recordStroe = RecordStore.openRecordStore("scroe", true);
			   ByteArrayOutputStream baos = new ByteArrayOutputStream();
			   DataOutputStream dos = new DataOutputStream(baos);
			   dos.writeInt(scroe);
			   byte[] b = baos.toByteArray();
			   recordStroe.addRecord(b, 0, b.length);
		   }catch(Exception ex){ }
		   finally {
		     try {
		    	 recordStroe.closeRecordStore();    
		     }
		     catch (Exception e){e.printStackTrace();}
		   }
		   }
	}
	//查看游戏排名;
	//作者:石明华
	//2007-04-10
	protected void showScroe(){
		try {	
			  recordStroe = RecordStore.openRecordStore("scroe", true);
			  data[0] = recordStroe.getNumRecords();
			  for(int j=2;j<=recordStroe.getNumRecords();j++){
		       ByteArrayInputStream bais = new ByteArrayInputStream(recordStroe.getRecord(j)); //就读第一个记录
		       DataInputStream dis = new DataInputStream(bais);			      
			   data[j] = dis.readInt(); 
		   }
		}
		   catch (Exception ex) {
		   }
		   finally {
		     try {
		    	 recordStroe.closeRecordStore();    
		     }
		     catch (Exception e) {
		       e.printStackTrace();
		     }
		 }
	}
	
	//设置游戏参数;
	//作者:石明华
	//2007-04-10
	protected void setPara(int a,int b){
		ManSpeed = a;
		GSpeed = b;
	}
	
	protected boolean testRoad(){
		boolean go = true;
		if(MapID == 0)
		{
			if(sprite.collidesWith(rout1_1, false)){
				go = false;
			}
			if(sprite.collidesWith(rout1_2, false)){
				go = false;
			}
			if(sprite.collidesWith(rout1_3, false)){
				go = false;
			}
			if(sprite.collidesWith(rout1_4, false)){
				go = false;
			}
		}
		if(MapID == 1)
		{
			if(sprite.collidesWith(rout1_1, false)){
				go = false;
			}
			if(sprite.collidesWith(rout1_4, false)){
				go = false;
			}
			
			if(sprite.collidesWith(rout2_2, false)){
				go = false;
			}
			if(sprite.collidesWith(rout2_3, false)){
				go = false;
			}
			if(sprite.collidesWith(rout2_4, false)){
				go = false;
			}
		}
		return go;
	}
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -