📄 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 + -