?? mgame.java
字號:
package mine;
import java.util.*;
import com.siemens.mp.io.*;
import com.siemens.mp.game.*;
import java.io.IOException;
import javax.microedition.lcdui.*;
public class MGame extends Canvas implements CommandListener
{
private static final int GRIDSIZE = 8;
private static final int MINECOUNT = 15;
private static Mine pMain;
private static Display display;
private static int Width, Height, selx, sely, leftbomb;
private static byte grid[][]; // 用于存放掃雷數據
private static boolean gameover; // 是否結束
private static Random rand; // 產生隨機數
private static ExtendedImage ExScreenImg; // 擴展圖像層
private static Graphics Exg;
private static Image TitleImg, MineImg, fMineImg, HideImg, fHideImg, FlagImg, fFlagImg, NumImg[], fNumImg[];
private static MelodyComposer melody; // 音調
// 定義菜單
private static Command ContCmd = new Command("繼續游戲",Command.OK, 0);
private static Command StartCmd = new Command("新游戲",Command.OK, 1);
private static Command ExitCmd = new Command("退出",Command.EXIT, 2);
private static Command HelpCmd = new Command("幫助信息",Command.OK, 3);
private static Command OKCmd = new Command("確定",Command.OK, 0);
final private static String openSnd="開啟聲音", closeSnd="關閉聲音", openVib="開啟振動", closeVib="關閉振動";
private static Command SndCmd, VibCmd;
public static boolean Snd, Vib;
public MGame(Mine pmine)
{
pMain = pmine;
Width = 88 / GRIDSIZE;
Height = (getHeight()-1) / GRIDSIZE;
grid = new byte[Height][Width];
rand = new Random((new Date()).getTime());
NumImg = new Image[8];
fNumImg = new Image[8];
gameover = true;
int i;
// 預先載入圖片
try
{
TitleImg = Image.createImage("images/title.png");
MineImg = Image.createImage("images/mine.png");
fMineImg = Image.createImage("images/minef.png");
HideImg = Image.createImage("images/hide.png");
fHideImg = Image.createImage("images/hidef.png");
FlagImg = Image.createImage("images/flag.png");
fFlagImg = Image.createImage("images/flagf.png");
for (i=1; i<=8; i++)
{
NumImg[i-1] = Image.createImage("images/n"+i+".png");
fNumImg[i-1] = Image.createImage("images/n"+i+"f.png");
}
}
catch(Exception exception){}
// 初始化圖像緩存(寬度必須為8的倍數)
ExScreenImg = new ExtendedImage(Image.createImage(104, getHeight()));
Exg = ExScreenImg.getImage().getGraphics();
melody = new MelodyComposer();
Snd = true;
Vib = true;
/////////////////////////////////////////////
// 讀取參數
try
{
byte bflag[] = new byte[2];
bflag[0] = bflag[1] = 1;
File keyfile = new File();
int fid = keyfile.open("para");
if(keyfile.length(fid) > 0) keyfile.read(fid, bflag, 0, 2);
Snd = (bflag[0] == 1);
Vib = (bflag[1] == 1);
keyfile.close(fid);
}
catch(IOException ioexception) { }
catch(NullPointerException npe){ }
//////////////////////////////////////////////
if (Snd) SndCmd = new Command(closeSnd, Command.OK, 4);
else SndCmd = new Command(openSnd, Command.OK, 4);
if (Vib) VibCmd = new Command(closeVib, Command.OK, 5);
else VibCmd = new Command(openVib, Command.OK, 5);
// 添加菜單
addCommand(StartCmd);
addCommand(ExitCmd);
addCommand(HelpCmd);
addCommand(SndCmd);
addCommand(VibCmd);
// 畫標題
Exg.drawImage(TitleImg,0,0,20);
}
public void paint(Graphics grap)
{
ExScreenImg.blitToScreen(0,0); // 將圖像緩存內容在屏幕上顯示出來
}
protected void keyPressed(int kcode) // 按鍵響應
{
if (gameover) return;
int bomb = 0;
int oldx = selx;
int oldy = sely;
switch(kcode)
{
case '1': // 1,挖開
System.gc(); // 先釋放無用的資源
if (grid[sely][selx] >= 10 && grid[sely][selx] < 20)
{
grid[sely][selx] -= 10;
if (grid[sely][selx] == 0) Expand(selx, sely);
}
else if (grid[sely][selx] < 10)
{
if (!SafeExp(selx, sely)) bomb = 1;
}
break;
case '3': // 3,作標記
System.gc();
if (grid[sely][selx] >= 10)
{
if (grid[sely][selx] < 20)
{
grid[sely][selx] += 10;
leftbomb --;
}
else
{
grid[sely][selx] -= 10;
leftbomb ++;
}
}
break;
case '2': // 2
case -59: //上鍵
sely --;
break;
case '4': // 4
case -61: // 左向鍵
selx--;
break;
case '6': // 6
case -62: // 右向鍵
selx++;
break;
case '5': // 5
case '8': // 8
case -60: // 下鍵
sely++;
break;
}
if (selx < 0) selx = 0;
if (selx > Width-1) selx = Width-1;
if (sely < 0) sely = 0;
if (sely > Height-1) sely = Height-1;
DrawBlock(oldx, oldy, false);
if (bomb == 0)
bomb = DrawBlock(selx, sely, true);
else
DrawBlock(selx, sely, true);
Exg.setColor(0xffffff);
Exg.fillRect(89, 26, 13, 13);
Exg.setColor(0);
Exg.drawString(""+leftbomb, 101, 26, Graphics.RIGHT | Graphics.TOP);
if (bomb == 1)
{
gameover = true;
SoundPlay(1);
if (Vib) Vibrator.triggerVibrator(150);
Exg.drawString("爆", 101, 39, Graphics.RIGHT | Graphics.TOP);
Exg.drawString("炸", 101, 52, Graphics.RIGHT | Graphics.TOP);
}
if (Judge())
{
gameover = true;
SoundPlay(0);
Exg.drawString("成", 101, 39, Graphics.RIGHT | Graphics.TOP);
Exg.drawString("功", 101, 52, Graphics.RIGHT | Graphics.TOP);
}
ExScreenImg.blitToScreen(0,0);
}
protected void keyRepeated(int kcode) //按鈕連按響應
{
if (gameover) return;
int oldx = selx;
int oldy = sely;
switch(kcode)
{
case '2':
case -59: //up
sely --;
break;
case '4':
case -61: // LEFT
selx--;
break;
case '6':
case -62: // RIGHT
selx++;
break;
case '5':
case '8':
case -60: // DOWN
sely++;
break;
}
if (selx < 0) selx = 0;
if (selx > Width-1) selx = Width-1;
if (sely < 0) sely = 0;
if (sely > Height-1) sely = Height-1;
DrawBlock(oldx, oldy, false);
DrawBlock(selx, sely, true);
ExScreenImg.blitToScreen(0,0);
}
// 菜單選擇響應
public void commandAction(Command cmd, Displayable displayable)
{
boolean savepara = false;
if (cmd == ExitCmd)
pMain.destroyApp(true);
else if (cmd == StartCmd)
{
newGame();
}
else if (cmd == HelpCmd)
{
Form pHelp = new Form("掃雷 v0.1.0");
pHelp.append("\u00A9Siemens Shanghai Mobile Communication Ltd.\n\n"
+"按鍵說明:\n2、4、6、8-移動焦點,1-挖,3-做標記。");
pHelp.addCommand(OKCmd);
display.setCurrent(pHelp);
pHelp.setCommandListener(this);
}
else if (cmd == OKCmd)
{
activate(display);
}
else if (cmd == SndCmd)
{
Snd = !Snd;
removeCommand(SndCmd);
if (Snd) SndCmd = new Command(closeSnd,Command.OK, 4);
else SndCmd = new Command(openSnd,Command.OK, 4);
addCommand(SndCmd);
savepara = true;
}
else if (cmd == VibCmd)
{
Vib = !Vib;
removeCommand(VibCmd);
if (Vib) VibCmd = new Command(closeVib,Command.OK, 5);
else VibCmd = new Command(openVib,Command.OK, 5);
addCommand(VibCmd);
savepara = true;
}
if (savepara)
{
/////////////////////////////////////////////
// 寫入參數
try
{
byte bflag[] = new byte[2];
File keyfile = new File();
int fid = keyfile.open("para");
bflag[0] = (byte)(Snd ? 1 : 0);
bflag[1] = (byte)(Vib ? 1 : 0);
keyfile.write(fid, bflag, 0, 2);
keyfile.close(fid);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -