📄 gamemenu.java
字号:
import java.io.IOException;
import java.io.InputStream;
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.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
public class GameMenu extends GameCanvas implements Runnable, CommandListener {
// 与菜单有关的变量
private int menuWidth = 80;
private int menuHeight = 15;
private int menuStartY = 50;
private int screenM = this.getWidth() / 2;
private int tempi = 0;
private int currentMenuItemY = menuStartY;
private boolean isFlash = false;
private int menuSelect = 0; // 0显示主菜单 1显示高分榜 2显示选项菜单 3显示游戏规则说明 4显示游戏背景
// 5显示游戏版权信息
private String menuStr[] = { "开始游戏", "高分榜", "选项", "游戏规则说明", "游戏背景", "查看版权",
"退出" };
private Player bgMusicPlayer;// 游戏背景音乐
private Player moveMenuPlayer;// 移动菜单的声音
private boolean isShowMenu = true;
GameMain gameMain;
private int screenW; // 手机屏幕宽度
protected int screenH; // 手机屏幕高度
// 与存档有关的
static Score score;
static final String scoreRMSName = "EliminatorScore";
static Settings settings;
static final String settingsRMSName = "EliminatorSettings";
private static GameCenter gameCenter;
private Command com_Ok;
private Command com_Exit;
private String menuSetting[][] = { { "音乐关", "返回" }, { "音乐开", "返回" } };
private int menuSettingI = 1;
protected GameMenu(GameMain game, int menuSelect) {
super(true);
this.gameMain = game;
this.menuSelect = menuSelect;
screenW = this.getWidth();
screenH = this.getHeight();
score = new Score(scoreRMSName);
settings = new Settings(settingsRMSName);
try {
this.score.loadScores();
this.settings.loadSettings();
gameMain.gameSound = this.settings.sound;
} catch (Exception e) {
e.printStackTrace();
}
// 初始化音乐对象
InputStream is = getClass().getResourceAsStream("bgmusic.mid");
try {
bgMusicPlayer = Manager.createPlayer(is, "audio/midi");
bgMusicPlayer.prefetch();
bgMusicPlayer.setLoopCount(-1);
bgMusicPlayer.setMediaTime(0);
if (gameMain.gameSound == 1) {
bgMusicPlayer.start();
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (MediaException e1) {
e1.printStackTrace();
}
is = getClass().getResourceAsStream("movemenu.wav");
try {
moveMenuPlayer = Manager.createPlayer(is, "audio/X-wav");
moveMenuPlayer.prefetch();
} catch (IOException e1) {
e1.printStackTrace();
} catch (MediaException e1) {
e1.printStackTrace();
}
if (menuSelect == 1) {
com_Ok = new Command("返回", Command.OK, 0);
com_Exit = new Command("清空记录", Command.EXIT, 0);
this.addCommand(com_Ok);
this.addCommand(com_Exit);
this.setCommandListener(this);
}
Thread t = new Thread(this);
t.start();
}
public void setMenuSelect(int menuSelect) {
this.menuSelect = menuSelect;
if (menuSelect == 1) {
com_Ok = new Command("返回", Command.OK, 0);
com_Exit = new Command("清空记录", Command.EXIT, 0);
this.addCommand(com_Ok);
this.addCommand(com_Exit);
this.setCommandListener(this);
}
System.out.println("23423432");
if (gameMain.gameSound == 1) {
try {
bgMusicPlayer.start();
} catch (MediaException e) {
e.printStackTrace();
}
}
System.out.println("232211111132");
isShowMenu = true;
Thread t = new Thread(this);
t.start();
}
private void drawFlashRound(Graphics g, int width, int height, int menuY,
String str, boolean isFlash) {
g.setColor(19, 14, 243);
if (isFlash) {
g.setColor(197, 220, 252);
}
g.fillRoundRect(this.getWidth() / 2 - width / 2, menuY, width, height,
height, height);
switch (tempi) {
case 1:
g.setColor(197, 220, 252);
break;
case 2:
g.setColor(139, 139, 255);
break;
case 3:
g.setColor(10, 10, 188);
break;
case 4:
g.setColor(2, 2, 32);
break;
}
g.drawRoundRect(this.getWidth() / 2 - width / 2, menuY, width, height,
height, height);
g.setColor(197, 220, 252);
g.drawString(str, this.getWidth() / 2 - 12 * str.length() / 2,
menuY + 2, 0);
}
private void drawNotFlashRound(Graphics g, int width, int height,
int menuY, String str) {
g.setColor(19, 14, 243);
g.fillRoundRect(this.getWidth() / 2 - width / 2, menuY, width, height,
height, height);
g.setColor(197, 220, 252);
g.drawString(str, this.getWidth() / 2 - 12 * str.length() / 2,
menuY + 2, 0);
}
private void showMenu(Graphics g) {
// 显示菜单
g.setColor(0, 0, 0);
g.fillRect(0, 0, screenW, screenH);
g.setColor(255, 255, 255);
g.drawImage(GameCenter.createImage("/menu.png"), 0, 0, 0);
Font font;
font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN,
Font.SIZE_MEDIUM);
g.setFont(font);
int tempWidth = 120;
int tempHeight = 18;
int currentMenuY = menuStartY;
int tempj = 0;
while (menuStr.length - 1 >= tempj) {
if (currentMenuItemY == currentMenuY) {
drawFlashRound(g, tempWidth, tempHeight, currentMenuY,
menuStr[tempj], isFlash);
isFlash = false;
} else {
drawNotFlashRound(g, tempWidth, tempHeight, currentMenuY,
menuStr[tempj]);
}
currentMenuY += 24;
tempj++;
}
this.flushGraphics();
}
private void keyControl() throws MediaException {
// 处理用户按键输入
int keyCode = this.getKeyStates();
if ((keyCode & this.UP_PRESSED) != 0) {
try {
if (gameMain.gameSound == 1) {
moveMenuPlayer.start();
}
} catch (MediaException e) {
e.printStackTrace();
}
isFlash = true;
if (currentMenuItemY <= menuStartY) {
currentMenuItemY = menuStartY + 24 * (menuStr.length - 1);
} else {
currentMenuItemY = currentMenuItemY - 24;
}
} else if ((keyCode & this.DOWN_PRESSED) != 0) {
isFlash = true;
try {
if (gameMain.gameSound == 1) {
moveMenuPlayer.start();
}
} catch (MediaException e) {
e.printStackTrace();
}
if (currentMenuItemY >= menuStartY + 24 * (menuStr.length - 1)) {
currentMenuItemY = menuStartY;
} else {
currentMenuItemY = currentMenuItemY + 24;
}
} else if ((keyCode & this.FIRE_PRESSED) != 0) {
if (currentMenuItemY == menuStartY) {
if (menuSelect == 0) {
isShowMenu = false;
bgMusicPlayer.stop();
if (this.gameCenter == null) {
gameCenter = new GameCenter(gameMain, score, settings);
}
gameCenter.newGame(1);
gameMain.display.setCurrent(gameCenter);
} else if (menuSelect == 2) {// 如果显示的是选项菜单
if (menuSettingI == 0) {
try {
settings.updateSettings(1, 1, 1, 1);
} catch (Exception e) {
e.printStackTrace();
}
gameMain.gameSound = this.settings.sound;
if (gameMain.gameSound == 1) {
bgMusicPlayer.start();
}
} else {
try {
settings.updateSettings(1, 0, 1, 1);
} catch (Exception e) {
e.printStackTrace();
}
gameMain.gameSound = this.settings.sound;
if (gameMain.gameSound == 0) {
bgMusicPlayer.stop();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -