超级玛丽游戏开发五(击碎障碍物砖块)

超级玛丽游戏开发(击碎障碍物)

在Background类里面需要有一个击碎砖块的方法:

  • 击碎的砖块必须不再被绘制出0来(allObjects队列内移除)
  • 击碎的砖块不再成为障碍物,不阻碍角色运动(allConstructions队列里面移除被击碎的部分,可能之前的整体被分割为几个部分)
	/*
	 * 击碎砖块
	 * */
	public void removeObjects(int posx){
		//击碎的Object的左右边界
		int splitl = 0,splitr = 0;
		//处理绘制的Object队列
		for(int i=0; iposx)||(object.getL()posx+50)){
				splitl = object.getL();
				splitr = object.getR();
				allObjects.remove(i);
				break;
			}
		}
		//处理障碍物Construction队列
		for(int i=0; i< allConstructions.size();i++){
			Construction construction = allConstructions.get(i);
			if((construction.lposx)||(construction.lposx+50)){
				allConstructions.remove(i);
				if(splitl==construction.l&&splitr!=construction.r){//移除最左端的砖块
					Construction con = new Construction(splitr,construction.r,construction.up,construction.down);
					allConstructions.add(con);
				}
				else if(splitl!=construction.l&&splitr==construction.r){//移除最右端的砖块
					Construction con = new Construction(construction.l,splitl,construction.up,construction.down);
					allConstructions.add(con);
				}
				else if(splitl!=construction.l&&splitr!=construction.r){//移除中间的砖块
					Construction con1 = new Construction(construction.l,splitl,construction.up,construction.down);
					allConstructions.add(con1);
					Construction con2 = new Construction(splitr,construction.r,construction.up,construction.down);
					allConstructions.add(con2);
				}
			}
		}
	}

在Collide类里面判断从下侧撞击的时候,导致砖块击碎,返回一个布尔值true

		/*
		 * 
		 * 1检测与障碍物的互动
		 * 返回true表示击碎障碍物/
		 * */
		public boolean testCrash (Background.Construction construction){
			int l = construction.l, r = construction.r, up = construction.up, down = construction.down;
			if(mario.isMoving==1&&mario.posx<=l&&mario.posx+50>=l&&mario.posy+100>up&&mario.posy=r&&mario.posy+100>up&&mario.posy=down&&mario.posx+50>l&&mario.posx=up&&mario.posyl&&mario.posx

在Collide类检测障碍物与角色互动的方法里面调用Background对象的击碎砖块方法:

		@Override
		public void run() {
			while(!marioDead){
				List  enemies = bg.getEnemies();
				
				//敌人
				for(int i=0; i constructions = bg.getConstructions();
				onConstruction = false;
				//障碍物
				for(int i=0; i

 

你可能感兴趣的:(java项目)