📄 mgame.java
字号:
package mine;
import java.util.Date;
import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class MGame extends Canvas implements CommandListener{
private static Mine pMain;
private static Display display;
private static final int GRIDSIZE = 8;
private static final int MINECOUNT = 15;
private static int Width, Height, selx, sely, leftbomb;
private static byte grid[][]; // 用于存放扫雷数据
private static boolean gameover; // 是否结束
private static Random rand; // 产生随机数
private static Image ExScreenImg; // 扩展图像层
private static Graphics Exg;
private static Image TitleImg, MineImg, fMineImg, HideImg, fHideImg, FlagImg, fFlagImg, NumImg[], fNumImg[];
// 定义菜单
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 mine) {
super();
pMain = mine;
Width = (getWidth()-145) / GRIDSIZE;//(getWidth()-1) / GRIDSIZE;
Height = (getHeight()-145) / GRIDSIZE;
System.out.println(Width+"==="+Height);
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 ex){ex.printStackTrace();}
// 初始化图像缓存(宽度必须为8的倍数)
ExScreenImg = Image.createImage(Width*GRIDSIZE+15, Height*GRIDSIZE+1);
Exg = ExScreenImg.getGraphics();
Snd = true;
Vib = true;
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 activate(Display disp)
{
display = disp;
display.setCurrent(this); // 设置显示目标
setCommandListener(this); // 监视菜单选择
}
protected void paint(Graphics g) {
// TODO Auto-generated method stub
g.fillRect(0,0,this.getWidth(),this.getHeight());
g.drawImage(ExScreenImg,0,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: //上键
case -1:
sely --;
break;
case '4': // 4
case -61: // 左向键
case -3:
selx--;
break;
case '6': // 6
case -62: // 右向键
case -4:
selx++;
break;
case '5': // 5
case '8': // 8
case -60: // 下键
case -2:
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);
}
this.repaint();
// 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
case -1:
sely --;
break;
case '4':
case -61: // LEFT
case -3:
selx--;
break;
case '6':
case -62: // RIGHT
case -4:
selx++;
break;
case '5':
case '8':
case -60: // DOWN
case -2:
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);
this.repaint();
// ExScreenImg.blitToScreen(0,0);
}
// 是否全部完成判断
private boolean Judge()
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -