该拼图游戏的基本界面如下:
这是按OK键的帮助效果:
MIDlet代码:
- package com.javaME.PPuzzle;
- import javax.microedition.lcdui.Display;
- import javax.microedition.lcdui.Ticker;
- import javax.microedition.midlet.MIDlet;
- import javax.microedition.midlet.MIDletStateChangeException;
- public class PPuzzleMidlet extends MIDlet {
- private Display dis;
- public PPuzzleMidlet() {
- // TODO Auto-generated constructor stub
- }
- protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
- // TODO Auto-generated method stub
- }
- protected void pauseApp() {
- // TODO Auto-generated method stub
- }
- protected void startApp() throws MIDletStateChangeException {
- // TODO Auto-generated method stub
- dis = Display.getDisplay(this);
- PPuzzleCanvas canvas = new PPuzzleCanvas(this);
- canvas.setTicker(new Ticker("海滨拼图,越拼越勇!"));
- dis.setCurrent(canvas);
- }
- }
自定义Canvas代码:
- package com.javaME.PPuzzle;
- import java.io.IOException;
- import java.util.Random;
- import javax.microedition.lcdui.Canvas;
- 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.lcdui.game.Sprite;
- import javax.microedition.midlet.MIDlet;
- public class PPuzzleCanvas extends Canvas implements CommandListener, Runnable {
- private Image img1 = null;
- private Image img2 = null;
- private Image img3 = null;
- private Image img4 = null;
- private Image img = null;
- private Image[] myImage = new Image[4];
- private Command cmdExit = new Command("退出", Command.BACK, 1);
- private Command cmdNext = new Command("下一关", Command.SCREEN, 2);
- private Command cmdBefore = new Command("上一关", Command.SCREEN, 1);
- private Command cmdRefresh = new Command("重置", Command.SCREEN, 3);
- private int edge;
- private MIDlet parent;
- private int num = 0;
- private int second = 0;
- private int minute = 0;
- private int FLAG = 0;
- private int map[][] = { { 00, 01, 02, 03 }, { 10, 11, 12, 13 },
- { 20, 21, 22, 23 }, { 30, 31, 32, 33 } };
- public PPuzzleCanvas(MIDlet parent) {
- this.parent = parent;
- try {
- img1 = Image.createImage("/background1.png");
- img2 = Image.createImage("/background2.png");
- img3 = Image.createImage("/background3.png");
- img4 = Image.createImage("/background4.png");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- myImage[0] = img1;
- myImage[1] = img2;
- myImage[2] = img3;
- myImage[3] = img4;
- img = myImage[num];
- edge = img.getWidth() / 4;
- this.addCommand(cmdExit);
- this.addCommand(cmdRefresh);
- this.setCommandListener(this);
- initMap();
- }
- private void initMap() {
- Random random = new Random();
- int temp, x1, y1, x2, y2;
- for (int i = 0; i < 100; i++) {
- x1 = random.nextInt(4);
- y1 = random.nextInt(4);
- x2 = random.nextInt(4);
- y2 = random.nextInt(4);
- temp = map[x1][y1];
- map[x1][y1] = map[x2][y2];
- map[x2][y2] = temp;
- }
- if (num == 3) {
- this.removeCommand(cmdNext);
- //this.removeCommand(cmdRefresh);
- // this.addCommand(cmdExit);
- } else {
- this.addCommand(cmdNext);
- }
- if(num == 0) {
- this.removeCommand(cmdBefore);
- } else {
- this.addCommand(cmdBefore);
- }
- repaint();
- }
- protected void paint(Graphics g) {
- // TODO Auto-generated method stub
- if (FLAG == 0) {
- g.setColor(255, 255, 255);
- g.fillRect(0, 0, getWidth(), getHeight());
- // 根据地图编号,选出小块图片画出来
- for (int i = 0; i < 4; i++) {
- for (int j = 0; j < 4; j++) {
- if (map[i][j] != 33) {
- int x_src = map[i][j] / 10;
- int y_src = map[i][j] % 10;
- int x_dest = i * edge;
- int y_dest = j * edge;
- g.drawRegion(img, x_src * edge, y_src * edge, edge,
- edge, Sprite.TRANS_NONE, x_dest, y_dest,
- Graphics.LEFT | Graphics.TOP);
- }
- }
- }
- } else if (FLAG == 1) {
- g.drawImage(img, 0, 0, Graphics.LEFT | Graphics.TOP);
- }
- if (isSuccess()) {
- g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
- Font.SIZE_LARGE));
- g.setColor(255, 0, 0);
- g.drawString("恭喜顺利通关!您共耗时:" + minute + "分" + second + "秒", this
- .getWidth() / 2, this.getHeight() - 60, Graphics.TOP
- | Graphics.HCENTER);
- }
- }
- public void commandAction(Command cmd, Displayable d) {
- // TODO Auto-generated method stub
- if (cmd == cmdNext && num < 3) {
- num++;
- img = myImage[num];
- this.initMap();
- //this.removeCommand(cmdNext);
- // this.removeCommand(cmdExit);
- } else if (cmd == cmdExit) {
- parent.notifyDestroyed();
- } else if (cmd == cmdRefresh) {
- this.initMap();
- } else if (cmd == cmdBefore && num > 0) {
- num--;
- img = myImage[num];
- this.initMap();
- }
- }
- public boolean isSuccess() {
- for (int x = 0; x < 4; x++) {
- for (int y = 0; y < 4; y++) {
- int x_src = map[x][y] / 10;
- int y_src = map[x][y] % 10;
- if (x_src != x || y_src != y) {
- return false;
- }
- }
- }
- return true;
- }
- //控制上下左右,移动拼图
- protected void keyPressed(int keyCode) {
- int action = this.getGameAction(keyCode);
- int xOf33 = -1;
- int yOf33 = -1;
- for (int x = 0; x < 4; x++) {
- for (int y = 0; y < 4; y++) {
- if (map[x][y] == 33) {
- xOf33 = x;
- yOf33 = y;
- break;
- }
- }
- }
- switch (action) {
- case GameCanvas.UP:
- if (yOf33 != 3) {
- swap(xOf33, yOf33, xOf33, yOf33 + 1);
- }
- break;
- case GameCanvas.DOWN:
- if (yOf33 != 0) {
- swap(xOf33, yOf33, xOf33, yOf33 - 1);
- }
- break;
- case GameCanvas.LEFT:
- if (xOf33 != 3) {
- swap(xOf33, yOf33, xOf33 + 1, yOf33);
- }
- break;
- case GameCanvas.RIGHT:
- if (xOf33 != 0) {
- swap(xOf33, yOf33, xOf33 - 1, yOf33);
- }
- break;
- }
- this.repaint();
- }
- private void swap(int xOf33, int yOf33, int targetX, int targetY) {
- int temp = map[targetX][targetY];
- map[targetX][targetY] = 33;// 用33代替了map[xOf33][yOf33]
- map[xOf33][yOf33] = temp;
- }
- //按下OK键的时候,显示原图,帮助拼图
- protected void keyRepeated(int keyCode) {
- int action = this.getGameAction(keyCode);
- if (action == GameCanvas.FIRE) {
- FLAG = 1;
- this.repaint();
- // System.out.println("OK键按下");
- }
- }
- //松开OK键的时候,恢复游戏
- protected void keyReleased(int keyCode) {
- int action = this.getGameAction(keyCode);
- if (action == GameCanvas.FIRE) {
- FLAG = 0;
- this.repaint();
- // System.out.println("OK键松开");
- }
- }
- //计时器
- public void run() {
- // TODO Auto-generated method stub
- while (true) {
- second++;
- if (second == 10) {
- second = 0;
- minute++;
- }
- }
- }
- }