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

📄 playcanvas.java

📁 初学者游戏框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	      //isShotPlane
	      if (Math.abs(plane_XY[i][0] - shot_x[j]) < (plane[i].getWidth() / 2 -3 ) &&
	          (plane_XY[i][1] + plane[i].getHeight()) >= (shot_y[j] + 5) && (shot_y[j]+shot[j].getHeight() ) > plane_XY[i][1] &&
	          !isShotPlane[i] && isFly[i]) {
	        isShotPlane[i] = true;
	        isFly[i] = false;
	        shotReset(j);
	      }
	      if(isSendBomb[i]){
	        //isBombShot
	        if (Math.abs(plane_XY[i][2] - shot_x[j]) < (bomb.getWidth() / 2 + shot[j].getWidth() / 2) &&
	            (plane_XY[i][3] + bomb.getHeight()) >= shot_y[j] && (shot_y[j]+shot[j].getHeight()) > plane_XY[i][3] &&
	            !isBombShot[i] && isSendBomb[i]) {
	          shotReset(j);
	          isBombShot[i] = true;
	        } else
	        //isBombGun
	        if (Math.abs(WIDTH / 2 - plane_XY[i][2]) < (bomb.getWidth() / 2 + gun[0].getWidth() / 2) &&
	            HEIGHT * 4 / 5 + 5 <= (plane_XY[i][3] + bomb.getHeight()) && !isBombGun[i] && isSendBomb[i]) {
	          isBombGun[i] = true;
	        } else
	        //isBombGround
	        if (plane_XY[i][3] + bomb.getHeight()  >= HEIGHT && plane_XY[i][2] > 0 &&
	            plane_XY[i][2] < WIDTH && !isBombGround[i] && isSendBomb[i]) {
	          isBombGround[i] = true;
	        }
	      }
	    }
	  }
	}
	
	  private void rotateGun() {
	    if(rotate){
	    if (gun_left) {
	      if (degree >= 90) {
	        degree = 90;
	      }
	      else {
	        degree += 30;
	      }
	    }
	    if (gun_right) {
	      if (degree <= -90) {
	        degree = -90;
	      }
	      else {
	        degree -= 30;
	      }
	    }
	    switch (degree) {
	      case 0:
	        cgun = gun[0];
	        cdegree = 0;
	        break;
	      case 30:
	        cgun = gun[1];
	        cdegree = 0;
	        break;
	      case 60:
	        cgun = gun[2];
	        cdegree = 0;
	        break;
	      case 90:
	        cgun = gun[0];
	        cdegree = 90;
	        break;
	      case -30:
	        cgun = gun[2];
	        cdegree = 270;
	        break;
	      case -60:
	        cgun = gun[1];
	        cdegree = 270;
	        break;
	      case -90:
	        cgun = gun[0];
	        cdegree = 270;
	        break;
	      default:
	        break;
	    }
	    rotate = false;
	  }else{
	    rotatedelaycount++;
	    if(rotatedelaycount>=20){
	      rotate = true;
	    }
	  }
	}

	  private void updateShot() {
	    for(int j=0;j<shot.length;j++){
	    if (fire[j]) {                                                              //如果发射了炮弹 则不断更新炮弹的位置
	      switch (tdegree[j]) {
	        case 0:
	          shot_y[j] -= 6;
	          break;
	        case 30:
	          shot_x[j] -= 3;
	          shot_y[j] -= 6;
	          break;
	        case 60:
	          shot_x[j] -= 6;
	          shot_y[j] -= 3;
	          break;
	        case 90:
	          shot_x[j] -= 6;
	          shot_y[j] = HEIGHT * 4 / 5;
	          break;
	        case -30:
	          shot_x[j] += 3;
	          shot_y[j] -= 6;
	          break;
	        case -60:
	          shot_x[j] += 6;
	          shot_y[j] -= 3;
	          break;
	        case -90:
	          shot_x[j] += 6;
	          shot_y[j] = HEIGHT * 4 / 5;
	          break;
	        default:
	          break;
	      }
	      if (shot_y[j] < 0 || shot_x[j] > WIDTH || shot_x[j] < 0) {                //只有炮弹出了屏幕才可以
	        shotReset(j);
	      }
	    }
	    else {                                                                      //如果没有发射 则不断将炮弹发射的角度用当前角度更新
	      tdegree[j] = degree;
	    }
	  }
	  }

	  private void checkPlaneOut(int i){
	    if(i%2==0) {
	        if (plane_XY[i][0] < 0) out[i] = true; else out[i] = false;
	      } else {
	          if (plane_XY[i][0] > WIDTH) out[i] = true; else out[i] = false;
	        }
	      }

	  private void updatePlane() {
	    fly = Math.abs(rand.nextInt() % difficulty);                                //根据难度变量的改变 修改飞机出发的频率
	    for (int i = 0; i < plane.length; i++) {
	      checkPlaneOut(i);
	      if (out[i] || !isFly[i]) {
	        if(!isSendBomb[i] && i==fly){
	          tfly = fly;
	          planeReset(i) ;
	          isFly[i] = true ;
	        }
	      }

	      if (isFly[i] ) {                                                          //如果第I只飞机起飞了
	        plane_XY[i][0] += plane_XY[i][5];
	        plane_XY[i][1] += 1;                                                    //Y坐标则以1的单位递增 形成飞机向下飞的效果
	        if(i%2==1){
	          if(plane_XY[i][0]<=plane_XY[i][2]&&plane_XY[i][0] + plane_XY[i][5]>plane_XY[i][2] && !isSendBomb[i]){
	            plane_XY[i][3] = plane_XY[i][1];
	            isSendBomb[i] = true;
	          }
	        }else{
	          if(plane_XY[i][0]>=plane_XY[i][2]&&plane_XY[i][0] + plane_XY[i][5]<plane_XY[i][2] && !isSendBomb[i]){
	            plane_XY[i][3] = plane_XY[i][1];
	            isSendBomb[i] = true;
	          }
	        }
	      }
	      if (isSendBomb[i]) {                                                      //如果不允许投弹 则有一个炸弹处在屏幕中下落状态
	        plane_XY[i][3] += bomb_move_step_h;                                     //炸弹以bomb_move_step_h为单位,向下运动
	      }
	    }                                                                           //end for
	  }

	  private int getBombX() {
	    bombX = Math.abs(rand.nextInt() % (WIDTH*2));                               //产生一个-5~WIDTH+5之间的随机数,来确定投弹的X坐标,
	    return bombX;                                                               //只有X在屏幕范围才投弹 从而形成不一定每架飞机过都投弹的效果
	  }

	  private void planeReset(int i) {
	    planeSpeed =  Math.abs(rand.nextInt() % 3)+3 ;                              //产生速度
	    plane_XY[i][1]  =  Math.abs(rand.nextInt() % HEIGHT/6);                     //飞机起飞的Y坐标
	    if (i % 2 == 0) {                                                           //确定起飞的X坐标
	      plane_XY[i][0] = WIDTH;
	      plane_XY[i][5] = -1*planeSpeed;                                           //确定速度
	    } else if(i%2==1){
	      plane_XY[i][0] = 0;
	      plane_XY[i][5] = planeSpeed;
	    }

	    plane_XY[i][2] = getBombX();                                                //投弹的X坐标
	    plane_XY[i][3] = 0;    //投弹的Y坐标
	    hitPlane[i] = 0;
	    hitBomb[i] = 0;
	    hitGun[i] = 0;
	    hitGround[i] = 0;
	    isShotPlane[i] = false;
	    isSendBomb[i] = false;
	    isBombShot[i] = false;
	    isBombGun[i] = false;
	    isBombGround[i] = false;
	    isFly[i] = false;      //飞机可以飞
	    out[i] = false;
	  }

	  private void shotReset(int j) {                                               //炮弹复位
	    shot_x[j] = WIDTH / 2;
	    shot_y[j] = HEIGHT * 4 / 5 + 7;
	    fire[j] = false;                                                            //允许发射下一颗炮弹
	    tdegree[j] = degree;                                                        //将当前的角度赋给临时的炮弹发射角度
	  }

	  private void checkStage() {
	    if (life == 0) {
	      gameover = true;
	      doGameOver();
	    } else if (left_plane <= 0) {
	      if (stage < maxstage) {
	        initPlot();
	      }else {
	        win = true;
	        gameover = true;
	        doGameOver();
	      }
	    }
	    if(isNewStage){
	      stageinterval++;
	      if(stageinterval>=20){
	        isNewStage=false;
	      }
	    }
	  }
	
	public void keyPressed(int keyCode)
	{
		switch(keyCode)
		{
			case SOFTKEY1:
				if(isStop())
				{
					resume();
				}
				else 
				{
					stop();
				}
				repaint();
				break;
			case SOFTKEY2:
				kill();
				go.changeCanvas(go.CVS_MAIN, true);
				break;
		}
				
		if(!isStop())
		{
			switch(getGameAction(keyCode))
			{

				/***********************************************
				以下是游戏进行过程按键处理
				***********************************************/    	
			      case LEFT:
			        gun_left = true;
			        rotateGun();
			        break;
			      case RIGHT:
			        gun_right = true;
			        rotateGun();
			        break;
			      case FIRE:
			      case UP:
			          if(shotNum>=shot.length || shotNum==0) {
			            fireinterval = 4;
			            shotNum = 0;
			          }
			          if (!fire[shotNum]) {
			            if(fireinterval<4) fireinterval++;
			            if(fireinterval>=4){
			              fire[shotNum] = true;
			              fireinterval = 0;
			              shotNum ++ ;
			            }
			          }
			          
			        break;			
			}
		}
		else
		{
			switch(keyCode)
			{
				case FIRE:
					if(gameover)
					{
						kill();
						go.changeCanvas(go.CVS_MAIN, true);
					}		
					break;
			}
		}
	} 
		
	public void keyReleased(int keyCode)
	{		
		if(!isStop())	
			switch(getGameAction(keyCode))
			{			

				/***********************************************
				以下是游戏进行过程按键处理
				***********************************************/    	
			      case LEFT:
			        gun_left = false;
			        break;
			      case RIGHT:
			        gun_right = false;
			        break;
			      case FIRE:
			      case UP:
			        fireinterval = 4;
			        break;			
			}
		else
			switch(getGameAction(keyCode))
			{

				/***********************************************
				以下是游戏结束或暂停过程按键处理
				***********************************************/    	
			
			}
	}
	
	  public void doGameOver(){
	  	kill();
		Score highscore = new Score(go.STR_DBNAME,5,5000);
	
		if(gameover){
			if(highscore.isHighScore(score)){
				go.setScore(score);
				go.changeCanvas(go.CVS_INPUT, true);
			}else{
				go.changeCanvas(go.CVS_MAIN, true);
			}
		}
	  }
	  
	  private void updateLeftPlane(int i){
	    left_plane--;                                                               //玩家的任务数减1
	    if (i < 2) {                                                                //这里是根据被击中的不同种类飞机,对玩家的成绩进行更新
	      score += 100;
	    }
	    else if (i < 4 && i > 2) {
	      score += 150;
	    }
	    else {
	      score += 200;
	    }
	  }
	
}

⌨️ 快捷键说明

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