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

📄 gamescreen.java

📁 J2ME飞机设计游戏,希望对学习J2ME的程序员有所帮助
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
//import com.nokia.mid.ui.FullCanvas;
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;

/*
 * GameScreen.java
 *
 * Created on 2007年9月7日, 下午2:46
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 * 
 * @author wangquan84
 */
public class GameScreen extends Canvas implements Runnable,
		ProgressObserver {
	public static GameScreen getInstance() {
		if(gameScreen == null){
			gameScreen = new GameScreen();
		}
		return gameScreen;
	}

	/** Creates a new instance of GameScreen */
	private GameScreen() {
//		// 得到初始数据
//		getNormalData();

		// 设置初始状态
		initialGame();

		// 初始话加载图片
		initialLoadData();

		//System.out.println("" + Runtime.getRuntime().totalMemory());
		//System.out.println("" + Runtime.getRuntime().freeMemory());
		// 开启线程
		Thread planeRun = new Thread(this);
		planeRun.start();
	}

	public void initialGame() {
		
		
		
		setFullScreenMode(true); //设置全屏
//		offScreenBuffer = Image.createImage(GameConstant.iSCREEN_WIDTH,
//				GameConstant.iSCREEN_HEIGHT);
		
		gameState = GameConstant.STATE_LOGO1;
		//gameState = GameConstant.STATE_MENU_AUTOSHOW;
		//gameState = GameConstant.STATE_GAME_RUN;
		//得到帮助的行数
		//helpRows = (byte) gameMenu.getMenuHelp().size();
		//System.out.println("helpRows===" + helpRows);

		// 初始化武器
		for (int i = 1; i <= 4; i++) {
			weaponType[i - 1] = (byte) i;
		}
	}

	private void main() {
		try {
			acquireKey(key);
			switch (gameState) {
			case GameConstant.STATE_LOGO1: // 第一个登陆界面
				if (++stateCount > 20) {
					stateCount = 0;
					gameState = GameConstant.STATE_LOGO2;
				}
				break;
			case GameConstant.STATE_LOGO2: // 第二个登陆界面
				if (++stateCount > 20) {
					gameState = GameConstant.STATE_ISSOUNDON;
				}
				break;
			case GameConstant.STATE_ISSOUNDON: // 选择是否需要声音界面
				GameConstant.MUSIC_GAME_STATE = GameConstant.MUSIC_GAME_TITLE;
				if ((pressedKey & GameConstant.KEY_SOFT1) != 0
						|| (pressedKey & GameConstant.KEY_ATTACK) != 0) {
					isPlaySound = true;
					
					//playSound(GameConstant.MUSIC_GAME_TITLE);
					playSound(GameConstant.MUSIC_GAME_STATE);
					gameState = GameConstant.STATE_MENU_AUTOSHOW;
				} else if ((pressedKey & GameConstant.KEY_SOFT2) != 0) {
					isPlaySound = false;
					//playSound(GameConstant.MUSIC_GAME_TITLE);
					playSound(GameConstant.MUSIC_GAME_STATE);
					gameState = GameConstant.STATE_MENU_AUTOSHOW;
				}

				keyReset();
				break;
			case GameConstant.STATE_MENU_AUTOSHOW: // 第三个登陆界面
				gameMenu.releaseLogoObject();
				if (menuShowTimes == 105 && menu2ShowTimes == 42) {
					gameState = GameConstant.STATE_MENU_SELECT;
					break;
				}

				menuShowTimes = menuShowTimes + 5;
				menu2ShowTimes = menu2ShowTimes + 2;

				if (menu2ShowTimes % 14 == 0) {
					lightChangeTimes = ++lightChangeTimes > 2 ? 0
							: lightChangeTimes;
				}

				keyReset();
				break;
			case GameConstant.STATE_MENU_SELECT: // 选择菜单界面
				if ((pressedKey & GameConstant.KEY_L) != 0
						|| (pressedKey & GameConstant.KEY_U) != 0) {
					selectCount = --selectCount < 0 ? maxSelectNum
							: selectCount;
				} else if ((pressedKey & GameConstant.KEY_R) != 0
						|| (pressedKey & GameConstant.KEY_D) != 0) {
					selectCount = ++selectCount > maxSelectNum ? 0
							: selectCount;
				} else if ((pressedKey & GameConstant.KEY_ATTACK) != 0
						|| (pressedKey & GameConstant.KEY_SOFT1) != 0
						) {
					switch (selectCount) {
					case 0: // 新游戏
						StopSound();
						GameConstant.MUSIC_GAME_STATE = GameConstant.MUSIC_GAME_MENU;
						//playSound(GameConstant.MUSIC_GAME_MENU);
						playSound(GameConstant.MUSIC_GAME_STATE);
						gameState = GameConstant.STATE_MENU_SPLANE;
						break;
					case 1: // 帮助
						gameState = GameConstant.STATE_MENU_HELP;
						break;
					case 2: // 设置
						gameState = GameConstant.STATE_MENU_SETTING;
						break;
					case 3: // 显示排行榜
						rmsMobile.openRecordStore();
						rmsMobile.loadRecordStore();
						rmsMobile.closeRecordStore();
						engToNum(rmsMobile.getPlayerName());
						gameState = GameConstant.STATE_MENU_SCORE;
						break;
					case 4: // 退出游戏
						isShow = false;
						isExit = true;
						break;
					}
				}
				keyReset();
				break;
			case GameConstant.STATE_MENU_SETTING:
				if((pressedKey & GameConstant.KEY_L) != 0
						|| (pressedKey & GameConstant.KEY_U) != 0){
					setCount = --setCount < 0 ? 1 : setCount;
				}else if((pressedKey & GameConstant.KEY_R) != 0
						|| (pressedKey & GameConstant.KEY_D) != 0){
					setCount = ++setCount > 1 ? 0 : setCount;
				}else if((pressedKey & GameConstant.KEY_ATTACK) != 0
						|| (pressedKey & GameConstant.KEY_SOFT1) != 0){
					if(setCount == 0){
						if(player == null){
							isPlaySound = true;
							GameConstant.MUSIC_GAME_STATE = GameConstant.MUSIC_GAME_TITLE;
							//playSound(GameConstant.MUSIC_GAME_TITLE);
							playSound(GameConstant.MUSIC_GAME_STATE);
						}
					}else if(setCount == 1){
						StopSound();
						isPlaySound = false;
					}
				}else if((pressedKey & GameConstant.KEY_SOFT2) != 0){
					setCount = 0;
					gameState = GameConstant.STATE_MENU_SELECT;
				}

				keyReset();
				break;
			case GameConstant.STATE_MENU_HELP: // 选择帮助界面
				if ((pressedKey & GameConstant.KEY_L) != 0
						|| (pressedKey & GameConstant.KEY_U) != 0) {
					helpCount = --helpCount >= 0 ? helpCount : 3;
				} else if ((pressedKey & GameConstant.KEY_R) != 0
						|| (pressedKey & GameConstant.KEY_D) != 0) {
					//helpCount = (byte) (++helpCount < (helpRows - 9) ? helpCount : (helpRows - 9));
					helpCount = ++helpCount > 3 ? 0 : helpCount;
				}

				if ((pressedKey & GameConstant.KEY_SOFT2) != 0) {
					helpCount = 0;
					gameState = GameConstant.STATE_MENU_SELECT;
				}

				keyReset();
				break;
			case GameConstant.STATE_MENU_SCORE:  //选择排行榜界面
				if((pressedKey & GameConstant.KEY_SOFT2) != 0){
					gameState = GameConstant.STATE_MENU_SELECT;
				}

				keyReset();
				break;
			case GameConstant.STATE_MENU_SPLANE: // 选择飞机界面
				if ((pressedKey & GameConstant.KEY_D) != 0) {
					planeType = ++planeType > 2 ? 1 : planeType;

				} else if ((pressedKey & GameConstant.KEY_U) != 0) {
					planeType = --planeType < 1 ? 2 : planeType;

				} else if ((pressedKey & GameConstant.KEY_ATTACK) != 0
						|| (pressedKey & GameConstant.KEY_SOFT1) != 0) {
					gameState = GameConstant.STATE_MENU_SWEAPON;
				}
				keyReset();
				break;
			case GameConstant.STATE_MENU_SWEAPON: // 选择武器界面
				if ((pressedKey & GameConstant.KEY_NUM1) != 0) {
					getNextWeapon((byte) 0);
				} else if ((pressedKey & GameConstant.KEY_NUM3) != 0) {
					getNextWeapon((byte) 1);
				} else if ((pressedKey & GameConstant.KEY_NUM7) != 0) {
					getNextWeapon((byte) 2);
				} else if ((pressedKey & GameConstant.KEY_NUM9) != 0) {
					getNextWeapon((byte) 3);
				} else if ((pressedKey & GameConstant.KEY_ATTACK) != 0
						|| (pressedKey & GameConstant.KEY_SOFT1) != 0) {

					if (world == null) {
						world = World.getInstance();
					}
					world.setWeaponeType(weaponType);
					StopSound();
					gameState = GameConstant.STATE_GAME_LOAD;
				}
				keyReset();
				break;
			case GameConstant.STATE_GAME_LOAD: // 加载资源界面
				try {
					world.loadGameDate(loadCount);
					planeActor = BattlePlaneActor.getInstance();
				} catch (Exception e) {
					System.out.println("is hear!!");
					e.printStackTrace();
				}

				break;
			case GameConstant.STATE_GAME_RUN: // 运行游戏状态
				if ((pressedKey & GameConstant.KEY_SOFT1) != 0) { //调用子菜单
					gameMenu.initialChildMusicMenu();   //初始化字菜单对象
					gameState = GameConstant.STATE_GAME_MENU;

					keyReset();
				}
				
				try {
					world.doMain(releasedKey,pressedKey);
				} catch (Exception e) {
					System.out.println("main!!!!!!!!!!!!!!!!!!!!!");
					e.printStackTrace();
				}

				//keyReset();
				break;
			case GameConstant.STATE_GAME_OVER: // 游戏结束状态
				if ((pressedKey & GameConstant.KEY_L) != 0
						|| (pressedKey & GameConstant.KEY_U) != 0) {
					egNum[egNumIdx] = --egNum[egNumIdx] < 0 ? 25
							: egNum[egNumIdx];
				} else if ((pressedKey & GameConstant.KEY_R) != 0
						|| (pressedKey & GameConstant.KEY_D) != 0) {
					egNum[egNumIdx] = ++egNum[egNumIdx] > 25 ? 0
							: egNum[egNumIdx];
				} else if ((pressedKey & GameConstant.KEY_ATTACK) != 0) {
					whichEng(egNum[egNumIdx]);
					++egNumIdx;
					// System.out.println("strPlayId = " + strPlayId);
					if (egNumIdx > 4) {
						rmsMobile.openRecordStore();
						rmsMobile.saveRecordStore(strPlayId.toString(),
								BattlePlaneActor.iScore);
						rmsMobile.closeRecordStore();

						strPlayId.delete(0, strPlayId.length());
						// planeActor.setIScore(0);

						egNumIdx = 0;

						world.cleanPlane();
						world.setBossAppear(false);
						gameState = GameConstant.STATE_MENU_SELECT;
					}
				} else if ((pressedKey & GameConstant.KEY_SOFT1) != 0) {
					System.out.println("soft1!!!!!!!!!!!!!");
					rmsMobile.openRecordStore();
					rmsMobile.saveRecordStore(strPlayId.toString(),
							BattlePlaneActor.iScore);
					rmsMobile.closeRecordStore();

					strPlayId.delete(0, strPlayId.length());
					// planeActor.setIScore(0);

					egNumIdx = 0;

					world.cleanPlane();
					world.setBossAppear(false);
					gameState = GameConstant.STATE_MENU_SELECT;
				} else if ((pressedKey & GameConstant.KEY_SOFT2) != 0) {
					--egNumIdx;
					if (egNumIdx < 0) {
						egNumIdx = 0;
					}

					strPlayId.delete(egNumIdx, egNumIdx + 1);
				}

				keyReset();
				break;
			case GameConstant.STATE_GAME_HELP:
				if ((pressedKey & GameConstant.KEY_L) != 0
						|| (pressedKey & GameConstant.KEY_U) != 0) {
					helpCount = --helpCount >= 0 ? helpCount : 3;
				} else if ((pressedKey & GameConstant.KEY_R) != 0
						|| (pressedKey & GameConstant.KEY_D) != 0) {
//					helpCount = (byte) (++helpCount < (helpRows - 9) ? helpCount
//							: (helpRows - 9));
					helpCount = ++helpCount > 3 ? 0 : helpCount;
				}

				if ((pressedKey & GameConstant.KEY_SOFT2) != 0) {
					helpCount = 0;
					gameState = GameConstant.STATE_GAME_MENU;
				}

				keyReset();
				break;
			case GameConstant.STATE_GAME_MENU: // 游戏子菜单
				if ((pressedKey & GameConstant.KEY_L) != 0
						|| (pressedKey & GameConstant.KEY_U) != 0) {
					gameMenuCount = --gameMenuCount < 0 ? 4 : gameMenuCount;
				} else if ((pressedKey & GameConstant.KEY_R) != 0
						|| (pressedKey & GameConstant.KEY_D) != 0) {
					gameMenuCount = ++gameMenuCount > 4 ? 0 : gameMenuCount;
				} else if ((pressedKey & GameConstant.KEY_ATTACK) != 0
						|| (pressedKey & GameConstant.KEY_SOFT1) != 0) {
					switch (gameMenuCount) {
					case 0: // 返回游戏
						gameMenu.releaseChildMusicMenu();
						gameMenu.releaseHelp();
						gameState = GameConstant.STATE_GAME_RUN;
						break;
					case 1: // 回主菜单
						//清除游戏中的所有的对象
//						world.cleanNotUsedObject();
//
//						if (world.getGameLevel() == 2) {
//							world.cleanObjectBeforeSecBoss();
//						} else if (world.getGameLevel() == 3) {
//							world.cleanObjectBeforeThBoss();
//						}
						
						if (world.isBossAppear() != true) { //BOSS没有死亡
							world.cleanNotUsedObject();

							if (world.getGameLevel() == 2) {
								world.cleanObjectBeforeSecBoss();
							} else if (world.getGameLevel() == 3) {
								world.cleanObjectBeforeThBoss();
							}
						}

						world.cleanAllObecjt();
						if (world.isBossAppear() == true) {
							world.setBossAppear(false);
						}
						
						world.cleanPlane();

						//加载游戏菜单
						gameMenu.releaseChildMusicMenu();
						gameMenu.initGameMenu();

						StopSound();
						GameConstant.MUSIC_GAME_STATE = GameConstant.MUSIC_GAME_TITLE;
						if (isPlaySound == true) {
							//playSound(GameConstant.MUSIC_GAME_TITLE);
							playSound(GameConstant.MUSIC_GAME_STATE);

⌨️ 快捷键说明

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