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

📄 shotplanegamecanvas.java

📁 Java射击小游戏源代码和图片都在里面可以直接玩的!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.io.IOException;
import java.util.Random;
import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.TiledLayer;
import javax.microedition.media.control.FramePositioningControl;
import javax.microedition.midlet.MIDletStateChangeException;


public class ShotPlaneGameCanvas extends GameCanvas implements Runnable{

	private GameMIDlet midlet;
	
	private int gameSpeed;
	private boolean isPaused;
	
	private static final int GAME_STATE_LOGO = 1;
	private static final int GAME_STATE_MENU = 2;
	private static final int GAME_STATE_PLAY = 3;
	private static final int GAME_STATE_ABOUT = 4;
	private static final int GAME_STATE_HELP = 5;
	private int gameState = 3;
	
	private Thread gameThread = null;
	
	private Image imgMenuBackground;
	private Image imgMenu;
	private Image imgSplash;
	private Image imgArrow;
	private int menuX, menuY, arrowX, arrowY;

	private Image imgFlower;
	private Font myFont;
	private int fontHeight, fontWidth;
	private boolean isPlay;

	private int splashCnt;
	private int canvasWidth, canvasHeight, imgMenuBackgroundWidth,
			imgMenuBackgroundHeight, imgMenuWidth, imgMenuHeight,
			imgArrowWidth, imgArrowHeight;
	
	private PlayerPlane playerPlane;
	private Image imgPlayer;
	private EnemyPlane enemyPlane;
	
	private Image imgEnemy;
	private Image imgBlueEnemy;
	private int enemyCnt;
	public Vector enemyVector;
	private Image imgExplod;
	private Bullet bullet;
	
	
	private Image num[];
	private Image imgScore;
	private int score;
	private int i,k;
	private Image imgLife;
	private Image imgState;
	private Image imgWin;
	private Image imgFail;
	private Image bonusNum[];
	private Image imgBonus;
	private int life;
	private int playerPlaneEnergy;
	private boolean isFail;
	private boolean isWin;
	private boolean hasBonus;
	private Bonus bonus;
	public Vector bonusVector;
	private Random random;
	public boolean hasBoss;
	public Image imgBoss;
	public Boss boss;
	private boolean isChange;
	
	public int level=1;
	private boolean isShowLevel;
	private boolean isFadeIn ;
	private int fadeColor;
	
	private int gameBackgroundX, gameBackgroundY;
	private int gameBackgroundWidth, gameBackgroundHeight;
	private Image imgGameBackground;
	private TiledLayer layerBackground;
	private LayerManager layerManager;
	
	private boolean hasSound;
	
	public ShotPlaneGameCanvas(boolean suppressKeyEvents,GameMIDlet midlet) {
		super(suppressKeyEvents);
		// TODO Auto-generated constructor stub
		this.midlet=midlet;
		setFullScreenMode(true);
		canvasWidth = getWidth();
		canvasHeight = getHeight();
		load();
		init();
	}
	public void load(){
		imgMenuBackground = createImage("/menubackground.png");
		imgMenu = createImage("/menu.png");
		imgArrow = createImage("/arrow.png");
		imgFlower = createImage("/flower.png");
		myFont = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD,
				Font.SIZE_LARGE);
		fontHeight = myFont.getHeight();
        fontWidth = myFont.charWidth('飞');
        layerManager=new LayerManager();
        imgPlayer=createImage("/player.png");
		imgEnemy=createImage("/enemy1.png");
		imgBlueEnemy=createImage("/blueenemy.png");
		playerPlane=new PlayerPlane(imgPlayer,imgPlayer.getWidth()/5,imgPlayer.getHeight(),Plane.PLAYERPLANE);
		enemyPlane=new EnemyPlane(imgEnemy,imgEnemy.getWidth()/3,imgEnemy.getHeight(),Plane.ENEMYPLANE,this);
		
		
		enemyVector=new Vector();
		imgExplod=createImage("/explod.png");
		
		num=new Image[10];
		imgScore=createImage("/num.png");
		for (int i = 0; i <=9; i++) {
			num[i]=Image.createImage(7, 11);
			Graphics g=num[i].getGraphics();
			g.drawImage(imgScore, 0, -i*11, 0);
		}
		bonusNum=new Image[5];
		imgBonus=createImage("/bonus.png");
		for (int i = 0; i <=4; i++) {
			bonusNum[i]=Image.createImage(14, 13);
			Graphics g=bonusNum[i].getGraphics();
			g.drawImage(imgBonus, -i*14, 0, 0);
		}
		imgState=createImage("/energy_bar.png");
		imgLife=createImage("/life.png");
		imgWin=createImage("/win.png");
		imgFail=createImage("/fail.png");
		bonusVector=new Vector();
		random=new Random();	
		loadMap();
	}
	public void init(){
		imgMenuBackgroundWidth = imgMenuBackground.getWidth();
		imgMenuBackgroundHeight = imgMenuBackground.getHeight();
		imgMenuWidth = imgMenu.getWidth();
		imgMenuHeight = imgMenu.getHeight();
		imgArrowWidth = imgArrow.getWidth();
		imgArrowHeight = imgArrow.getHeight();

		
		splashCnt = 1;
		menuX = 0;
		menuY = 0;
		arrowX = 0;
		arrowY = 0;
		enemyCnt=0;
		
		score=0;
		life=2;
		playerPlaneEnergy=4;
		isFail=false;
		isWin=false;
		hasBonus=false;
		
		isPlay = true;
		hasBoss=false;
		
		level=1;
		isShowLevel=false;
		isFadeIn=true ;
		fadeColor=0;
		
		isChange=false;
		
		isPaused=false;
		
		hasSound=false;
		
//		boss.init();
//		playerPlane.init();
//		enemyPlane.init();
//		enemyVector.addElement(enemyPlane);
	}
	public void loadMap(){
		switch (level) {
		case 1:
			short map1[][]={
					{ 42, 42, 42, 42, 42, 42, 42, 45 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 65, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 36, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 41, 41, 41, 42, 42, 42, 42 },
					{ 42, 41, 41, 41, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 41, 86, 87 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 36, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 36, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 45, 46 },
					{ 42, 42, 42, 42, 42, 42, 49, 50 },
					{ 45, 46, 47, 48, 41, 41, 42, 42 },
					{ 41, 41, 41, 41, 41, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 65, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 36, 42, 42, 42, 45 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 41, 45, 46, 47, 48, 42, 42 },
					{ 42, 41, 41, 41, 41, 41, 41, 42 },
					{ 42, 42, 41, 41, 41, 41, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 65, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 36, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 45 },
					{ 42, 42, 42, 42, 42, 42, 41, 49 },
					{ 42, 36, 42, 42, 42, 41, 41, 41 },
					{ 42, 42, 42, 42, 42, 41, 41, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 65, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 36, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 36, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 42, 42, 42, 42, 42, 42, 42, 42 },
					{ 53, 53, 53, 54, 54, 54, 54, 53 },
					{ 60, 60, 60, 60, 60, 68, 69, 70 },
					{ 24, 24, 68, 68, 68, 68, 59, 59 },
					{ 31, 31, 31, 31, 31, 31, 31, 31 },
					{ 62, 62, 62, 62, 24, 61, 61, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 24 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 6, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 24, 62, 27, 28, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 24, 62 },
					{ 62, 62, 62, 24, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 6, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 5, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 24, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 77, 77, 77, 40, 62, 24, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 6, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 24, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 24, 62, 62, 62, 62, 62 },
					{ 62, 62, 24, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 24, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 6, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 24, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 7, 8, 22, 5, 3, 4 },
					{ 62, 62, 15, 16, 2, 3, 19, 11 },
					{ 62, 62, 38, 18, 10, 18, 19, 10 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 6, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 7, 8, 62, 62, 62, 62, 62, 62 },
					{ 15, 16, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 24, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 62, 62, 62, 62, 62, 62, 62, 62 },
					{ 32, 27, 28, 32, 32, 32, 32, 32 },
					{ 6, 19, 19, 19, 19, 19, 19, 20 },
					{ 30, 19, 19, 19, 19, 19, 19, 29 },
					{ 30, 18, 18, 19, 19, 18, 18, 29 },
					{ 30, 18, 18, 19, 10, 18, 18, 29 },
					{ 30, 18, 18, 18, 18, 18, 18, 29 },
					{ 30, 18, 18, 18, 18, 18, 18, 29 },
					{ 30, 18, 18, 18, 18, 18, 18, 29 },
					{ 30, 18, 18, 18, 18, 18, 18, 29 },
					{ 30, 18, 6, 18, 18, 18, 18, 29 },
					{ 30, 18, 18, 10, 18, 18, 18, 29 },
					{ 30, 18, 18, 18, 18, 18, 18, 29 },
					{ 30, 18, 18, 18, 19, 18, 18, 29 },
					{ 30, 18, 18, 18, 18, 18, 18, 29 },
					{ 30, 18, 18, 18, 18, 6, 18, 29 },
					{ 30, 18, 18, 18, 18, 18, 18, 29 },
					{ 30, 19, 19, 18, 18, 18, 18, 29 },
					{ 30, 19, 19, 19, 19, 18, 18, 29 },
					{ 30, 19, 19, 19, 13, 14, 19, 29 },
					{ 30, 19, 19, 19, 19, 18, 19, 29 },
					{ 30, 19, 19, 19, 19, 19, 19, 29 },
					{ 30, 19, 19, 19, 19, 19, 19, 29 },
					{ 30, 19, 19, 19, 19, 19, 19, 29 },
					{ 30, 19, 19, 19, 19, 19, 19, 29 },
					{ 30, 19, 19, 19, 19, 19, 19, 29 },
					{ 30, 19, 19, 19, 19, 19, 19, 29 },
					{ 30, 19, 19, 19, 19, 19, 19, 29 },
					{ 30, 19, 19, 19, 19, 10, 19, 29 },
					{ 30, 19, 19, 19, 19, 10, 10, 29 },
					{ 30, 19, 19, 19, 19, 10, 10, 29 },
					{ 30, 19, 19, 19, 19, 10, 10, 29 },
					{ 7, 8, 19, 6, 19, 10, 10, 29 },
					{ 15, 16, 17, 6, 6, 69, 70, 29 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 6, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 83, 39, 39 },
					{ 39, 35, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 73, 74 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 5, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 77, 77, 77, 40, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 6, 39, 39, 7, 8, 73, 74 },
					{ 39, 39, 39, 39, 15, 16, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 6, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 83, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 35, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 5, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 73, 74, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 6, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 75, 78, 77, 77, 77 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 5, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 39, 39, 39, 39, 39, 39, 39, 39 },
					{ 77, 77, 77, 77, 77, 77, 77, 77 }
					};
 
			imgGameBackground=createImage("/firstgamebackground.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, map1[i][j]);
				}
			}
			imgBoss=createImage("/firstboss.png");
			boss=new Boss(imgBoss,imgBoss.getWidth(),imgBoss.getHeight(),level,this,Plane.BOSS);
			break;
		case 2:
			short map2[][]={
					{ 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, 15, 86, 72 },
					{ 7, 7, 7, 7, 7, 34, 79, 80 },
					{ 7, 7, 7, 7, 85, 34, 87, 80 },
					{ 7, 7, 7, 7, 81, 71, 84, 88 },
					{ 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 },
					{ 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, 23 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },
					{ 7, 81, 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, 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, 39 },
					{ 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, 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, 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, 7, 7, 7, 7, 81, 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, 39 },
					{ 7, 7, 7, 7, 7, 7, 7, 7 },

⌨️ 快捷键说明

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