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

📄 shotplanegamecanvas.java

📁 Java射击小游戏源代码和图片都在里面可以直接玩的!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 15, 23, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 57, 58, 59, 7, 7, 7, 7, 7 },
					{ 65, 66, 67, 7, 7, 7, 7, 7 },
					{ 73, 74, 75, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 39, 23 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 15, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 81, 82, 83, 84, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 23 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 72 },
					{ 3, 4, 5, 6, 68, 70, 71, 80 },
					{ 11, 12, 13, 14, 35, 1, 87, 80 },
					{ 19, 20, 21, 22, 35, 1, 79, 88 },
					{ 27, 28, 29, 30, 24, 77, 24, 86 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 2, 1, 1, 1, 1 },
					{ 1, 1, 1, 2, 1, 1, 1, 1 },
					{ 1, 1, 1, 2, 1, 1, 1, 1 },
					{ 1, 1, 1, 2, 1, 1, 1, 1 },
					{ 1, 1, 1, 2, 1, 1, 1, 1 },
					{ 1, 1, 1, 10, 10, 1, 1, 1 },
					{ 1, 1, 1, 10, 10, 10, 1, 1 },
					{ 1, 1, 1, 10, 10, 10, 1, 1 },
					{ 1, 1, 1, 10, 10, 10, 1, 1 },
					{ 1, 1, 1, 10, 10, 1, 10, 1 },
					{ 32, 1, 10, 10, 10, 1, 10, 1 },
					{ 1, 1, 10, 2, 10, 1, 10, 1 },
					{ 1, 1, 10, 2, 10, 1, 10, 1 },
					{ 1, 1, 10, 2, 10, 1, 10, 1 },
					{ 1, 1, 10, 2, 10, 10, 10, 1 },
					{ 1, 1, 10, 10, 10, 10, 1, 1 },
					{ 1, 1, 10, 10, 10, 10, 2, 1 },
					{ 1, 1, 10, 10, 10, 10, 2, 24 },
					{ 1, 1, 10, 10, 1, 10, 2, 1 },
					{ 1, 1, 10, 10, 10, 10, 2, 1 },
					{ 1, 1, 10, 10, 10, 1, 2, 1 },
					{ 1, 1, 10, 10, 10, 1, 2, 1 },
					{ 1, 1, 10, 10, 10, 1, 2, 1 },
					{ 1, 1, 10, 10, 10, 2, 1, 1 },
					{ 1, 1, 10, 10, 1, 2, 1, 1 },
					{ 1, 1, 10, 10, 1, 2, 1, 1 },
					{ 1, 1, 10, 10, 1, 2, 1, 1 },
					{ 1, 1, 10, 10, 1, 2, 1, 1 },
					{ 1, 1, 10, 10, 1, 2, 1, 24 },
					{ 1, 1, 1, 2, 2, 1, 1, 1 },
					{ 1, 1, 1, 2, 2, 1, 1, 1 },
					{ 1, 1, 1, 2, 2, 1, 1, 1 },
					{ 1, 1, 1, 2, 2, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 32, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 2, 1, 1, 1, 1 },
					{ 1, 1, 2, 2, 1, 1, 1, 1 },
					{ 1, 1, 2, 1, 3, 4, 5, 6 },
					{ 1, 1, 2, 1, 11, 12, 13, 14 },
					{ 1, 2, 2, 1, 19, 20, 21, 22 },
					{ 1, 2, 1, 1, 27, 28, 29, 30 },
					{ 1, 2, 1, 1, 1, 1, 1, 1 },
					{ 1, 2, 1, 1, 1, 1, 1, 1 },
					{ 1, 2, 1, 1, 1, 1, 1, 1 },
					{ 1, 2, 24, 1, 1, 1, 1, 1 },
					{ 1, 2, 2, 1, 1, 1, 1, 1 },
					{ 1, 1, 2, 1, 1, 1, 1, 1 },
					{ 1, 1, 2, 1, 1, 1, 1, 1 },
					{ 1, 1, 2, 1, 1, 1, 1, 1 },
					{ 1, 1, 2, 1, 1, 1, 1, 1 },
					{ 24, 1, 2, 1, 1, 1, 1, 1 },
					{ 1, 1, 2, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 35 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 9, 9, 24, 1, 1 },
					{ 1, 1, 1, 9, 1, 9, 1, 1 },
					{ 1, 1, 1, 9, 1, 9, 1, 1 },
					{ 1, 1, 1, 9, 1, 9, 1, 1 },
					{ 24, 1, 1, 9, 1, 9, 1, 1 },
					{ 1, 1, 1, 9, 1, 9, 1, 1 },
					{ 1, 1, 1, 9, 1, 9, 1, 1 },
					{ 1, 1, 1, 9, 1, 9, 1, 1 },
					{ 1, 1, 9, 9, 1, 9, 1, 1 },
					{ 1, 1, 9, 1, 1, 9, 1, 1 },
					{ 1, 1, 9, 1, 1, 9, 1, 1 },
					{ 1, 9, 9, 1, 1, 9, 1, 1 },
					{ 1, 9, 1, 1, 1, 9, 9, 1 },
					{ 1, 9, 24, 1, 1, 1, 9, 1 },
					{ 1, 9, 1, 1, 1, 1, 1, 9 },
					{ 1, 9, 1, 1, 1, 1, 1, 9 },
					{ 1, 9, 1, 1, 1, 1, 1, 1 },
					{ 1, 9, 1, 1, 1, 1, 1, 1 },
					{ 1, 9, 1, 1, 1, 1, 1, 24 },
					{ 1, 9, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 35, 36, 37, 38, 1, 1 },
					{ 41, 42, 43, 44, 45, 46, 47, 48 },
					{ 49, 50, 51, 52, 53, 54, 55, 56 },
					{ 57, 58, 59, 68, 69, 70, 71, 82 },
					{ 65, 66, 67, 1, 1, 1, 81, 83 },
					{ 73, 74, 75, 1, 1, 1, 1, 1 },
					{ 1, 1, 10, 10, 10, 10, 1, 1 },
					{ 1, 10, 10, 10, 10, 10, 1, 1 },
					{ 1, 10, 10, 10, 10, 10, 10, 1 },
					{ 1, 10, 10, 10, 10, 10, 10, 1 },
					{ 1, 10, 10, 10, 10, 10, 10, 1 },
					{ 1, 1, 10, 10, 24, 10, 10, 1 },
					{ 1, 1, 10, 10, 10, 10, 1, 1 },
					{ 1, 1, 1, 10, 10, 1, 1, 1 },
					{ 1, 1, 10, 10, 10, 1, 1, 1 },
					{ 1, 1, 10, 10, 10, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 3, 4, 5, 6, 1, 1 },
					{ 1, 1, 11, 12, 13, 14, 1, 1 },
					{ 1, 1, 19, 20, 21, 22, 1, 1 },
					{ 1, 1, 27, 28, 29, 30, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 },
					{ 1, 1, 1, 1, 1, 1, 1, 1 }
					};
			layerManager.remove(layerBackground);
			layerBackground=null;
			imgGameBackground=null;
			boss=null;
			imgBoss=null;
			imgGameBackground=createImage("/secondgamebackground.png");
			layerBackground=new TiledLayer(8, 200, imgGameBackground, 22, 22);
			for (int i = 0; i < 200; i++) {
				for (int j = 0; j < 8; j++) {
					layerBackground.setCell(j, i,map2[i][j]);
				}
			}
			imgBoss=createImage("/secondboss.png");
			boss=new Boss(imgBoss,imgBoss.getWidth(),imgBoss.getHeight(),level,this,Plane.BOSS);
			break;
		}//end switch level
		layerManager.append(layerBackground);
		gameBackgroundHeight = layerBackground.getCellHeight()* layerBackground.getRows();
		gameBackgroundY = 0;
		gameBackgroundWidth=layerBackground.getCellWidth()*layerBackground.getColumns();
		boss.setCanvasSize(gameBackgroundWidth, canvasHeight);
		enemyPlane.setCanvasSize(gameBackgroundWidth, canvasHeight);
		playerPlane.setCanvasSize(gameBackgroundWidth, canvasHeight);
		
		playerPlane.init();
		enemyPlane.init();
		
		boss.init();
		playerPlane.bulletVector.removeAllElements();
		enemyVector.removeAllElements();
		bonusVector.removeAllElements();
	}
	
	public void start(){
		if(isPaused){
			
		}else{
			gameThread=new Thread(this);
			gameThread.start();
		}
	}
	public void run() {
		// TODO Auto-generated method stub
		Graphics g=getGraphics();
		Thread currentThread = Thread.currentThread();
		while (currentThread==gameThread) {
			long startTime = System.currentTimeMillis();
			tick();
			input();
			render(g);
			long takenTime = System.currentTimeMillis() - startTime;
			if (takenTime < gameSpeed) {
				try {
					Thread.sleep(gameSpeed - takenTime);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	public void tick(){
		switch (gameState) {
		case GAME_STATE_LOGO:
			gameSpeed = 100;
			break;
		case GAME_STATE_MENU:
			gameSpeed = 100;
			if (arrowX <= 1)
				arrowX++;
			else
				arrowX = 0;
			break;
		case GAME_STATE_PLAY:
			if(!isPaused){
			gameSpeed = 100;
			if (!isShowLevel) {
				playerPlane.tick();
				for (int i = 0; i < bonusVector.size(); i++) {
					bonus = (Bonus) bonusVector.elementAt(i);
					bonus.tick();
					if (!bonus.isAlive()) {
						bonusVector.removeElementAt(i);
						i--;
					}
				}
				if (canvasHeight - gameBackgroundHeight + gameBackgroundY < 0)
					gameBackgroundY += 2;
				else {// 出BOSS
					hasBoss = true;
					gameBackgroundY = 3200;
				}
				if (hasBoss) {
					
					boss.tick();
				}
				enemyCnt++;
				if((enemyCnt%101==0||enemyCnt%39==0)&&!hasBoss){
					if(enemyCnt%39==0){
						for (int i = 0; i < 2; i++) {
							enemyPlane = new EnemyPlane(imgBlueEnemy,imgBlueEnemy.getWidth() / 5, imgBlueEnemy.getHeight(),Plane.BLUEENEMY,this);
							enemyPlane.setCanvasSize(gameBackgroundWidth, canvasHeight);
							enemyPlane.init(i*enemyPlane.frameWidth,-enemyPlane.frameHeight);
							enemyVector.addElement(enemyPlane);
						}
					}
					if(enemyCnt%101==0){
						enemyPlane = new EnemyPlane(imgEnemy,imgEnemy.getWidth() / 3, imgEnemy.getHeight(),Plane.ENEMYPLANE,this);
						enemyPlane.setCanvasSize(gameBackgroundWidth, canvasHeight);
						enemyPlane.init();
						enemyVector.addElement(enemyPlane);
					}
				}
				for (int i = 0; i < enemyVector.size(); i++) {
					enemyPlane = (EnemyPlane) enemyVector.elementAt(i);
					if (enemyPlane.getY() > canvasHeight) {
						enemyPlane.isAlive = false;
						enemyVector.removeElementAt(i);
						i--;
					} else {
						if (playerPlane.collidesWith(enemyPlane, false)&& playerPlane.canAttack) {// 玩家飞机与敌人飞机碰撞
							playerPlane.isExplod = true;
							if(hasSound){
								//Sound.getInstance().explodeSound();
							}
							enemyPlane.setAlive(false);
							life--;
							playerPlaneEnergy = 4;
							playerPlane.explodX = playerPlane.getX();
							playerPlane.explodY = playerPlane.getY();
							playerPlane.init();
							enemyVector.removeElementAt(i);
							i--;
							playerPlane.bulletVector.removeAllElements();
						}	
						for (int j = 0; j < enemyPlane.bulletVector.size(); j++) {// 玩家飞机与敌人飞机子弹碰撞
							bullet = (Bullet) enemyPlane.bulletVector.elementAt(j);
							if (playerPlane.collidesWith(bullet, false)&& playerPlane.canAttack) {
								playerPlane.isExplod = true;
								if(hasSound){
									//Sound.getInstance().explodeSound();
								}
								playerPlaneEnergy--;
								playerPlane.explodX = playerPlane.getX();
								playerPlane.explodY = playerPlane.getY();
								enemyPlane.bulletVector.removeElementAt(j);
								j--;
							}
						}
						for (int j = 0; j < playerPlane.bulletVector.size(); j++) {// 玩家飞机子弹与敌人飞机碰撞
							try {
								bullet = (Bullet) playerPlane.bulletVector.elementAt(j);
								if (bullet.collidesWith(enemyPlane, true)) {
									enemyPlane.isExplod = true;
									enemyPlane.explodX = bullet.getX();
									enemyPlane.explodY = bullet.getY();
									switch (bullet.bulletType) {
									case Bullet.SMALL:
									case Bullet.DISPERSE:
										enemyPlane.energy--;
										break;
									case Bullet.MIDDLE:
										enemyPlane.energy-=2;
										break;
									case Bullet.BIG:
										enemyPlane.energy-=3;
										break;		
									}
									switch (enemyPlane.kind) {
									case Plane.ENEMYPLANE:
										if(enemyPlane.energy<=0){
											score += 100;
											k = Math.abs(random.nextInt() % 5);
											bonus = new Bonus(bonusNum[k], 14, 13, k);
											hasBonus = true;
											bonus.setAlive(enemyPlane.explodX, enemyPlane.explodY);
											bonus.setCanvasSize(gameBackgroundWidth,
													canvasHeight);
											bonusVector.addElement(bonus);
											enemyVector.removeElementAt(i);
											i--;
										}
										break;
									case Plane.BLUEENEMY:
										if(enemyPlane.energy<=0){
											score += 50;
											enemyVector.removeElementAt(i);
											i--;
										}
										break;
									}
									playerPlane.bulletVector.removeElementAt(j);
									j--;
								}	
							} catch (Exception e) {
								System.out.println("玩家飞机子弹与敌人飞机碰撞错误");
								e.printStackTrace();
							}
						}//end player bulletVector for
					}//end else	
				}//end enemyVector for
				if (boss.isAlive) {
					for(int j = 0; j < playerPlane.bulletVector.size(); j++) {// 玩家飞机子弹与boss碰撞
						bullet = (Bullet) playerPlane.bulletVector.elementAt(j);
						if(bullet.collidesWith(boss, true)&& playerPlane.canAttack){
							boss.isExplod=true;
							boss.explodX=bullet.getX();
							boss.explodY=bullet.getY();
							playerPlane.bulletVector.removeElementAt(j);
							j--;
							switch (bullet.bulletType) {
							case Bullet.SMALL:
							case Bullet.DISPERSE:
								boss.energy--;
								break;
							case Bullet.MIDDLE:
								boss.energy-=2;
								break;
							case Bullet.BIG:
								boss.energy-=3;
								break;	
							}
//							
						}//end collidesWith boss 
					}//end playerPlane bulletVector for
					for (int i = 0; i < boss.bulletVector.size(); i++) {//玩家飞机与boss子弹碰撞
						bullet = (Bullet) boss.bulletVector.elementAt(i);
						if(bullet.collidesWith(playerPlane, false)){
							playerPlane.isExplod=true;
							if(hasSound){
								//Sound.getInstance().explodeSound();
							}
							playerPlane.explodX=bullet.getX();
							playerPlane.explodY=bullet.getY();
							boss.bulletVector.removeElementAt(i);
							i--;
							playerPlaneEnergy--;
						}
					}
					if (playerPlane.collidesWith(boss, true)) {//玩家飞机与boss碰撞
						playerPlane.isExplod=true;
						if(hasSound){
							//Sound.getInstance().explodeSound();
						}
						playerPlane.explodX=playerPlane.getX();
						playerPlane.explodY=playerPlane.getY();
						life--;
						playerPlaneEnergy = 4;
						playerPlane.bulletVector.removeAllElements();
						playerPlane.init();
					}
				}else{//end boss isalive  if
					score+=300;
					level++;
					isShowLevel=true;
					isFadeIn=true;
					loadMap();
					boss.isAlive=true;
					if(level==3){
						isWin=true;
					}
				}
				for (int j = 0; j < bonusVector.size(); j++) {// 玩家飞机与奖励物品碰撞
					bonus = (Bonus) bonusVector.elementAt(j);
					if (bonus.collidesWith(playerPlane, false)) {
						switch (bonus.bonusType) {
						case Bonus.ADD_BULLET_BONUS:
							playerPlane.bulletVector.removeAllElements();
							switch (playerPlane.bullet.bulletType) {
							case Bullet.SMALL:
								playerPlane.bullet.bulletType = Bullet.MIDDLE;
								break;
							case Bullet.MIDDLE:
								playerPlane.bullet.bulletType = Bullet.BIG;
								break;
							case Bullet.DISPERSE:
								playerPlane.bullet.bulletType = Bullet.SMALL;
							}
							break;
						case Bonus.SPEED_UP_BONUS:
							playerPlane.speedUp();
							break;
						case Bonus.ADD_LIFE_BONUS:
							if (life < 3) {
								life++;
							}
							break;
						case Bonus.DISPERSE_BULLET_BOUNS:// 散弹
							playerPlane.bulletVector.removeAllElements();
							playerPlane.bullet.bulletType = Bullet.DISPERSE;
							break;
						case Bonus.ADD_SCORE:
							score += 150;
							break;
						}
						bonus.setAlive(false);
						bonusVector.removeElementAt(j);
						j--;
					}

⌨️ 快捷键说明

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