import java.util.Random;
import java.util.Vector;
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.Font;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
public class gameCanvas extends Canvas implements CommandListener {
private static final int BLACK = 0x00000000;
private static final int WHITE = 0x00FFFFFF;
private static final int RED = 0x00FF0000;
private static final int BLUE = 0x000000FF;
private static final int NO_MOVE = -1;
private final control midlet;
private final gamelogic game;
private final Command exitCommand;
private final Random random = new Random();
private int[][] pan = new int[10][10];
private Random random1 = new Random();
private Command help;
private Command jibie1;
private Command jibie2;
private Command jibie3;
private String openSnd = "开启声音", closeSnd = "关闭声音";
private Command sheng;
public boolean Snd;
private Display dis;
private int screenWidth;
private int screenHeight;
private int boardCellSize, boardTop, boardLeft;
private int preCursorPosition, cursorPosition;
private Vector BombVector = new Vector();
private boolean isRestart;
private boolean isGameOver;
public gameCanvas(control midlet) {
this.midlet = midlet;
game = new gamelogic(random);
dis = Display.getDisplay(midlet);
initializeBoard();
// 初始化菜单
sheng = new Command("声音", Command.SCREEN, 2);
jibie1 = new Command("高级", Command.SCREEN, 1);
jibie2 = new Command("中级", Command.SCREEN, 1);
jibie3 = new Command("初级", Command.SCREEN, 1);
help = new Command("关于扫雷", Command.SCREEN, 1);
exitCommand = new Command("退出", Command.EXIT, 2);
addCommand(sheng);
addCommand(jibie1);
addCommand(jibie2);
addCommand(jibie3);
addCommand(exitCommand);
addCommand(help);
setCommandListener(this);
helpta();
}
// 添加一个地雷位置标记
private void addBomb(int matrix) {
BombVector.addElement(Integer.toString(matrix));
}
// 删除一个地雷位置标记
private void delBomb(int matrix) {
BombVector.removeElement(Integer.toString(matrix));
}
// 收索该位置是否被标记
private boolean searchBomb(int matrix) {
return BombVector.indexOf(Integer.toString(matrix)) == -1;
}
// 初始化屏幕,取得棋盘初始位置
private void initializeBoard() {
screenWidth = getWidth();
screenHeight = getHeight();
if (screenWidth > screenHeight) {
boardCellSize = (screenHeight - 1) / 8;
boardLeft = (screenWidth - (boardCellSize * 8)) / 2;
boardTop = 1;
} else {
boardCellSize = (screenWidth - 1) / 8;
boardLeft = (screenHeight - (boardCellSize * 8)) / 2;
boardLeft = 1;
}
}
// 初始化游戏、屏幕,使游戏重启
private void initialize1() {
preCursorPosition = cursorPosition = 0;
game.setNewGame();
game.InitArray1();
isRestart = true;
BombVector.removeAllElements();
repaint();
}
private void initialize2() {
preCursorPosition = cursorPosition = 0;
game.setNewGame();
game.InitArray2();
isRestart = true;
BombVector.removeAllElements();
repaint();
}
private void initialize3() {
preCursorPosition = cursorPosition = 0;
game.setNewGame();
game.InitArray3();
isRestart = true;
BombVector.removeAllElements();
repaint();
}
// 重画canvas
protected void paint(Graphics g) {
game.isWin();
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
Font.SIZE_LARGE));
if (!game.isGameOver()) {
paintGame(g);
} else {
paintGameOver(g);
}
}
// 游戏未结束的重画动作
private void paintGame(Graphics g) {
if (isRestart) {
g.setColor(0xbbbbbb);
g.fillRect(0, 0, screenWidth, screenHeight);
drawBoard(g);
paintAll(g);
drawBombTag(g);
}
drawCursor(g);
}
private void drawCursor(Graphics g) {
g.setColor(0xbbbbbb);
g.drawRect(((preCursorPosition % 8) * boardCellSize) + 3 + boardLeft,
((preCursorPosition / 8) * boardCellSize) + 1 + boardTop,
boardCellSize - 2, boardCellSize - 2);
g.setColor(this.RED);
g.drawRect(((cursorPosition % 8) * boardCellSize) + 3 + boardLeft,
((cursorPosition / 8) * boardCellSize) + 1 + boardTop,
boardCellSize - 2, boardCellSize - 2);
}
private void drawBombTag(Graphics g) {
int s, j, k;
g.setColor(RED);
for (int i = 0; i < BombVector.size(); i++) {
s = Integer.parseInt((String) BombVector.elementAt(i));
j = s % 8;
k = s / 8;
g.drawString("!", boardLeft + j * boardCellSize + 13, boardTop + k
* boardCellSize + 3, Graphics.TOP | Graphics.LEFT);
}
}
private void paintAll(Graphics g) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (game.getBomb(i * 8 + j) >= 10
&& game.getBomb(i * 8 + j) < 20) {
paintNum(g, i * 8 + j);
}
}
}
}
private void paintNum(Graphics g, int matrix) {
int i, j, s;
Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL);
s = game.getBomb(matrix);
i = matrix / 8;
j = matrix % 8;
g.setColor(WHITE);
if (this.searchBomb(matrix)) {
if (s != 20) {
g.drawString(String.valueOf(s - 10), boardLeft + j
* boardCellSize + 10, boardTop + i * boardCellSize + 3,
Graphics.TOP | Graphics.LEFT);
}
}
}
protected void keyPressed(int keyCode) {
if (game.isGameOver()) {
return;
}
int gameAction = getGameAction(keyCode);
switch (gameAction) {
case FIRE:
if (searchBomb(cursorPosition)) {
doPlayerMove();
}
break;
case RIGHT:
doMoveCursor(1, 0);
break;
case DOWN:
doMoveCursor(0, 1);
break;
case LEFT:
doMoveCursor(-1, 0);
break;
case UP:
doMoveCursor(0, -1);
break;
default:
if (searchBomb(cursorPosition)) {
addBomb(cursorPosition);
} else {
delBomb(cursorPosition);
}
repaint();
break;
}
}
private void doMoveCursor(int dx, int dy) {
int newCursorPosition = cursorPosition + dx + 8 * dy;
if ((newCursorPosition >= 0 && (newCursorPosition < 64))) {
preCursorPosition = cursorPosition;
cursorPosition = newCursorPosition;
repaint();
}
}
private void drawNull(Graphics g) {
g.setColor(this.BLUE);
g.drawRect(((cursorPosition % 8) * boardCellSize) + 3 + boardLeft,
((cursorPosition / 8) * boardCellSize) + 1 + boardTop,
boardCellSize - 2, boardCellSize - 2);
}
private void drawBoard(Graphics g) {
g.setColor(0xbbbbbb);
g.fillRect(0, 0, screenWidth, screenHeight);
g.setColor(BLACK);
for (int i = 0; i < 9; i++) {
g.fillRect(boardLeft + 2, boardCellSize * i + boardTop,
(boardCellSize * 8) + 1, 1);
g.fillRect(boardCellSize * i + boardLeft + 2, boardTop, 1,
boardCellSize * 8);
}
}
// 游戏结束 重画动作
private void paintGameOver(Graphics g) {
if (game.isGameOver()) {
if (game.isWin()) {
GameOver(g);
} else {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (game.getBomb(i * 8 + j) == 20) {
drawCircle(g, i * 8 + j);
}
}
}
}
}
}
private void drawCircle(Graphics g, int matrix) {
int x, y;
y = matrix / 8;
x = matrix % 8;
g.setColor(BLACK);
g.fillArc(x * boardCellSize + boardLeft + 3, y * boardCellSize
+ boardTop + 2, boardCellSize - 2, boardCellSize - 2, 0, 360);
}
private void drawCross(Graphics g, int x, int y) {
g.setColor(BLACK);
g.drawLine(x + 1 + boardLeft, y + boardTop, x + boardCellSize - 4 - 4
+ boardLeft, y + boardCellSize - 5 + boardTop);
g.drawLine(x + 1 + boardLeft, y + boardCellSize - 5 + boardTop, x
+ boardCellSize - 4 - 4 + boardLeft, y + boardTop);
}
private void doPlaye