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

📄 shotplanegamecanvas.java

📁 Java射击小游戏源代码和图片都在里面可以直接玩的!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
					if (bonusVector.isEmpty()) {
						hasBonus = false;
					}
				}//end bonusVector for
				
				if (playerPlaneEnergy == 0) {
					life--;
					playerPlaneEnergy = 4;
				}
				if (life <= -1) {
					isFail = true;
					}
				for (int i = 0; i < enemyVector.size(); i++) {
					enemyPlane = (EnemyPlane) enemyVector.elementAt(i);
					enemyPlane.tick();
					if(enemyPlane.isAlive==false){
						enemyVector.removeElementAt(i);
						i--;
					}
				}	
				}// end isShowLevel
			}// end isPaused
			break;
		}// end gameState switch
	}// end tick()
	public void render(Graphics g){
		switch (gameState) {
		case GAME_STATE_LOGO:
			drawLogo(g);
			break;
		case GAME_STATE_MENU:
			drawMenu(g);
			break;
		case GAME_STATE_PLAY:
			drawGame(g);
			break;
		case GAME_STATE_ABOUT:
			drawAbout(g);
			break;
		case GAME_STATE_HELP:
			drawHelp(g);
			break;
		default:
			break;
		}
	}
	public void input() {
		int keyStates = getKeyStates();
		if (gameState == GAME_STATE_MENU) {
			isPlay = true;
			if (((keyStates & LEFT_PRESSED) != 0)
					|| ((keyStates & UP_PRESSED) != 0)) {
				if (menuY > -imgMenuHeight / 6 * 5) {
					menuY = menuY - imgMenuHeight / 6;
				} else {
					menuY = 0;
				}
			} else if (((keyStates & DOWN_PRESSED) != 0)
					|| ((keyStates & RIGHT_PRESSED) != 0)) {
				if (menuY < 0) {
					menuY = menuY + imgMenuHeight / 6;
				} else {
					menuY = -imgMenuHeight / 6 * 5;
				}
			} else if ((keyStates & FIRE_PRESSED) != 0) {
				switch (menuY) {
				case 0:// start game
					gameState = 3;
					break;
				case -13:// game help
					gameState = 5;
					break;
				case -26:// about game
					gameState = 4;
					break;
				case -65:// exit game
					try {
						midlet.destroyApp(true);
					} catch (MIDletStateChangeException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					midlet.notifyDestroyed();
				case -39:// open sound
					hasSound=true;
					break;
				case -52:// close sound
					hasSound=false;
					break;
				}
			}
		}
		if(gameState == GAME_STATE_ABOUT||gameState == GAME_STATE_HELP){
			if (!isPlay) {
				if ((keyStates & FIRE_PRESSED) != 0) {
					gameState = 2;
				}
			}
		}
		if (gameState == GAME_STATE_PLAY) {
			if (playerPlane.isAlive()&&playerPlane.canAttack) {
				if ((keyStates & LEFT_PRESSED) != 0) {
					playerPlane.move(PlayerPlane.LEFT);
					playerPlane.setFrameSequence(playerPlane.left);
					playerPlane.nextFrame();
				} else if ((keyStates & RIGHT_PRESSED) != 0) {
					playerPlane.move(PlayerPlane.RIGHT);
					playerPlane.setFrameSequence(playerPlane.right);
					playerPlane.nextFrame();
				} else if ((keyStates & UP_PRESSED) != 0) {
					playerPlane.move(PlayerPlane.UP);
					playerPlane.setFrameSequence(playerPlane.up);
					playerPlane.nextFrame();
				} else if ((keyStates & DOWN_PRESSED) != 0) {
					playerPlane.move(PlayerPlane.DOWN);
					playerPlane.setFrameSequence(playerPlane.up);
					playerPlane.nextFrame();
				}else if ((keyStates & RIGHT_PRESSED) == 0||(keyStates & LEFT_PRESSED) == 0) {
					playerPlane.setFrameSequence(playerPlane.up);
					playerPlane.nextFrame();
				}
			}
		}
	}
	protected void keyPressed(int keyCode) {
		// TODO Auto-generated method stub
		super.keyPressed(keyCode);
		if (keyCode==35){//#
			hasSound=!hasSound;
		}
		if (gameState == GAME_STATE_PLAY) {
			 if(getGameAction(keyCode)==FIRE){
				 if(hasSound){
					 //Sound.getInstance().fireSound();
				 }
				 playerPlane.fire(playerPlane.getRefPixelX()-playerPlane.imgBullet.getWidth()/2,playerPlane.getY(),playerPlane.bullet.bulletType);
			 }
			 if (!isShowLevel) {
				switch (keyCode) {
				case -7:
					isPaused = !isPaused;
					break;
				case -6:
					midlet.exitRequested();
					break;	
				}
			 }
		}	
	}
	public void drawLogo(Graphics g) {
		switch (splashCnt) {
		case 1:
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
			imgSplash = createImage("/splash1.png");
			g.setColor(0xffffff);
			g.fillRect(0, 0, canvasWidth, canvasHeight);
			g.drawImage(imgSplash, canvasWidth / 2, canvasHeight / 2,
					Graphics.HCENTER | Graphics.VCENTER);
			break;
		case 11:
		case 12:
		case 13:
		case 14:
		case 15:
		case 16:
		case 17:
		case 18:
		case 19:
		case 20:
			imgSplash = createImage("/splash2.png");
			g.setColor(0x000000);
			g.fillRect(0, 0, canvasWidth, canvasHeight);
			g.drawImage(imgSplash, canvasWidth / 2, canvasHeight / 2,
					Graphics.HCENTER | Graphics.VCENTER);
			break;
		case 21:
		case 22:
		case 23:
		case 24:
		case 25:
		case 26:
		case 27:
		case 28:
		case 29:
		case 30:
			imgSplash = createImage("/splash3.png");
			g.setColor(0xffffff);
			g.fillRect(0, 0, canvasWidth, canvasHeight);
			g.drawImage(imgSplash, canvasWidth / 2, canvasHeight / 2,
					Graphics.HCENTER | Graphics.VCENTER);
			break;
		}
		splashCnt++;
		if (splashCnt > 30) {
			gameState = 2;
			splashCnt = 0;
		}
		flushGraphics();
	}

	public void drawMenu(Graphics g) {
		g.setColor(0xffffff);
		g.fillRect(0, 0, canvasWidth, canvasHeight);
		g.drawImage(imgMenuBackground, canvasWidth / 2 - imgMenuBackgroundWidth
				/ 2, canvasHeight / 2 - imgMenuBackgroundHeight / 2, 0);

		g.setClip(canvasWidth / 2 - imgMenuWidth / 2 + menuX - arrowX
				- imgArrowWidth, canvasHeight / 2 - imgMenuHeight / 6,
				imgArrowWidth / 2, imgArrowHeight);
		g.drawImage(imgArrow, canvasWidth / 2 - imgMenuWidth / 2 + menuX
				- arrowX - imgArrowWidth, canvasHeight / 2 - imgMenuHeight / 6,
				0);

		g.setClip(canvasWidth / 2 - imgMenuWidth / 2 + menuX + arrowX
				+ imgMenuWidth + imgArrowWidth / 2, canvasHeight / 2
				- imgMenuHeight / 6, imgArrowWidth / 2, imgArrowHeight);
		g.drawImage(imgArrow, canvasWidth / 2 - imgMenuWidth / 2 + menuX
				+ arrowX + imgMenuWidth, canvasHeight / 2 - imgMenuHeight / 6,
				0);

		g.setClip(canvasWidth / 2 - imgMenuWidth / 2 + menuX, canvasHeight / 2
				- imgMenuHeight / 6, imgMenuWidth, imgMenuHeight / 6);
		g.drawImage(imgMenu, canvasWidth / 2 - imgMenuWidth / 2 + menuX,
				canvasHeight / 2 - imgMenuHeight / 6 + menuY, 0);
		g.setClip(0, 0, canvasWidth, canvasHeight);
		flushGraphics();

	}
	public void drawGame(Graphics g) {
		if(!isPaused){
			g.setColor(0xffffff);
			g.fillRect(0, 0, canvasWidth, canvasHeight);
			g.setClip(0, 0, canvasWidth, canvasHeight);
			if (isShowLevel&&!(isWin||isFail))
	        {
				g.setColor(0);
	            g.fillRect(0, 0, canvasWidth, canvasHeight);
	            g.setColor(fadeColor, fadeColor, fadeColor);
	            g.setFont(myFont);
	            g.drawString("第 " + level + " 关", canvasWidth / 2, canvasHeight /2, 17);
	            if (isFadeIn)
	            {
	                if (fadeColor < 0xfc)
	                {
	                 
	                    fadeColor += 16;
	                    if (fadeColor > 0xfc)
	                    {
	                        fadeColor -= 16;
	                        isFadeIn = false;
	                    }
	                }
	                else
	                    isFadeIn = false;
	            }
	            else
	            {
	                if (fadeColor > 0)
	                {
	                   
	                    fadeColor -= 16;
	                    if (fadeColor < 0)
	                    {
	                        isShowLevel = false;
	                    }
	                }
	                else
	                {
	                    isShowLevel = false;
	                }
	            }
	            flushGraphics();
	            return ;
	        }
			layerManager.paint(g, 0, canvasHeight - gameBackgroundHeight+ gameBackgroundY);
			playerPlane.draw(g);
			if(playerPlane.isExplod){
				g.setClip(playerPlane.explodX, playerPlane.explodY, 37, 26);
				g.drawImage(imgExplod, playerPlane.explodX-37*playerPlane.explodCount, playerPlane.explodY, 0);
				playerPlane.explodCount++;
				if (playerPlane.explodCount == 8) {
					playerPlane.isExplod = false;
					playerPlane.explodCount = 0;
				}
			}
			if(boss.isExplod){
				g.setClip(boss.explodX, boss.explodY, 37, 26);
				g.drawImage(imgExplod, boss.explodX-37*boss.explodCount, boss.explodY, 0);
				boss.explodCount++;
				if (boss.explodCount == 8) {
					boss.isExplod = false;
					boss.explodCount = 0;
				}
			}
			for (int i = 0; i < enemyVector.size(); i++) {
				enemyPlane=(EnemyPlane)enemyVector.elementAt(i);
				g.setClip(enemyPlane.getX(), enemyPlane.getY(), enemyPlane.frameWidth, enemyPlane.frameHeight);
				enemyPlane.draw(g);
				if(enemyPlane.isExplod){
					g.setClip(enemyPlane.explodX, enemyPlane.explodY, 37, 26);
					g.drawImage(imgExplod, enemyPlane.explodX-37*enemyPlane.explodCount, enemyPlane.explodY, 0);
					enemyPlane.explodCount++;
					if (enemyPlane.explodCount == 8) {
						enemyPlane.isExplod = false;
						enemyPlane.explodCount = 0;
					}
				}
			}
			if(hasBonus){
				for (int i = 0; i < bonusVector.size(); i++) {
					bonus=(Bonus)bonusVector.elementAt(i);
	//				bonus.getBonus(bonus);
	//				bonus.draw(g);
					g.setClip(bonus.getX(), bonus.getY(), 14, 13);
					g.drawImage(bonus.getImage(), bonus.getX(), bonus.getY(), 0);
					
				}
			}
			if(boss.isAlive){
				g.setClip(boss.getX(),boss.getY(),imgBoss.getWidth(),imgBoss.getHeight());
				g.drawImage(imgBoss, boss.getX(), boss.getY(), 0);
				if(boss.bulletVector.size()>0){
					boss.bullet.draw(g);
				}
			}
			i=score/10000;//画分数条
			g.setClip(gameBackgroundWidth-5*7-5, 0, 40, 11);
			g.drawImage(num[i], gameBackgroundWidth-5*7-5, 0, 0);
			i=(score%10000)/1000;
			g.drawImage(num[i], gameBackgroundWidth-4*7-4, 0, 0);
			i=(score%1000)/100;
			g.drawImage(num[i], gameBackgroundWidth-3*7-3, 0, 0);
			i=(score%100)/10;
			g.drawImage(num[i], gameBackgroundWidth-2*7-2, 0, 0);
			i=score%10;
			g.drawImage(num[i], gameBackgroundWidth-1*7-1, 0, 0);
			
			g.setClip(0, 0, 55, 10);
			g.drawImage(imgState, 0, 0, 0);//画状态条
			g.setColor(0xffff00);
			g.fillRect(9, 3, playerPlaneEnergy*10, 4);
			g.setClip(0, 10, 45, 12);
			switch (life) {//画剩余生命
			case 1:
				g.drawImage(imgLife, 0,imgState.getHeight(), 0);
				break;
			case 2:
				g.drawImage(imgLife, 0, imgState.getHeight(), 0);
				g.drawImage(imgLife, imgLife.getWidth(), imgState.getHeight(), 0);
				break;
			case 3:
				g.drawImage(imgLife, 0, imgState.getHeight(), 0);
				g.drawImage(imgLife, imgLife.getWidth(), imgState.getHeight(), 0);
				g.drawImage(imgLife, 2*imgLife.getWidth(), imgState.getHeight(), 0);
				break;
			}
			if(isFail||isWin){//游戏结束
				g.setClip(0, 0, canvasWidth, canvasHeight);
				g.setColor(0x000000);
				g.fillRect(0, 0, canvasWidth, canvasHeight);
				
				if(isWin){
					g.drawImage(imgWin, 0, 0, 0);
				}
				if(isFail){
					g.drawImage(imgFail, 0, canvasHeight/2, 0);
				}
				destroy();
			}
		}
		if (isPaused == false){
			g.setColor(0xffffff);
			g.fillRect(gameBackgroundWidth-2*fontWidth, canvasHeight - fontHeight, 2*fontWidth, fontHeight);
			g.setColor(0x000000);
			g.setFont(myFont);
			g.setClip(gameBackgroundWidth-2*fontWidth, canvasHeight-fontHeight, 2*fontWidth, fontHeight);
			g.drawString("暂停", gameBackgroundWidth - 2 * fontWidth, canvasHeight- fontHeight, 0);
		}
		else{
			g.setColor(0x000000);
			g.fillRect(gameBackgroundWidth-2*fontWidth, canvasHeight - fontHeight, 2*fontWidth, fontHeight);
			g.setColor(0xffffff);
			g.setFont(myFont);
			g.setClip(gameBackgroundWidth-2*fontWidth, canvasHeight-fontHeight, 2*fontWidth, fontHeight);
			g.drawString("继续", gameBackgroundWidth - 2 * fontWidth, canvasHeight- fontHeight, 0);
		}
		g.setColor(0xffffff);
		g.fillRect(0, canvasHeight - fontHeight, 2*fontWidth, fontHeight);
		g.setColor(0x000000);
		g.setFont(myFont);
		g.setClip(0, canvasHeight-fontHeight, 2*fontWidth, fontHeight);
		g.drawString("退出", 0, canvasHeight - fontHeight, 0);
		flushGraphics();
	}
	public void drawAbout(Graphics g) {
		g.setColor(0xffffff);
		g.fillRect(0, 0, canvasWidth, canvasHeight);
		g.setColor(0x000000);
		g.setFont(myFont);
		g.drawString("关于游戏", canvasWidth / 2, 0, Graphics.HCENTER
				| Graphics.TOP);
		g.drawString("版本:     1.0", 0, 20, 0);
		g.drawString("作者:     陆璇", 0, 40, 0);
		g.drawString("指导:     刘丹丹", 0, 60, 0);
		g.drawString("制作日期: 2008年3月", 0, 80, 0);
		g.drawImage(imgFlower, canvasWidth - 96, canvasHeight - 20, 0);
		isPlay = false;
		flushGraphics();
	}

	public void drawHelp(Graphics g) {
		g.setColor(0xffffff);
		g.fillRect(0, 0, canvasWidth, canvasHeight);
		g.setColor(0x000000);
		g.setFont(myFont);
		g.drawString("老少皆宜的让你热血沸腾", 0, 0, 0);
		g.drawString("的经典版射击游戏,雷电", 0, 20, 0);
		g.drawString("依然有着不可磨灭的冲击", 0, 40, 0);
		g.drawString("力,想重温在街机里美好", 0, 60, 0);
		g.drawString("的回忆?想再现枪林弹雨", 0, 80, 0);
		g.drawString("中的纵横洒脱?", 0, 100, 0);
		g.drawString("那么开始游戏吧!", 0, 120, 0);
		g.drawString("上下左右控制飞行方向,", 0, 140, 0);
		g.drawString("开火键发射子弹。", 0, 160, 0);
		g.drawImage(imgFlower, canvasWidth - 96, canvasHeight - 20, 0);
		isPlay = false;
		flushGraphics();
	}
	public static Image createImage(String fileName) {
		Image image = null;
		try {
			image = Image.createImage(fileName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("无法读取图片");
		}
		return image;
	}
	public void destroy() {
        gameThread = null;
    }


}

⌨️ 快捷键说明

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