⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 playcanvas.java

📁 拼图游戏的手机源码 有三种不同难度的选择
💻 JAVA
字号:
package jigsawpuzzle;

import javax.microedition.lcdui.*;

public class PlayCanvas extends Canvas implements CommandListener {

    public static final int EASY = 1;
    public static final int NORMAL = 2;
    public static final int HARD = 3;
    private final Command CMD_BREAK = new Command("打碎", Command.OK, 1);
    private final Command CMD_BACK = new Command("返回", Command.BACK, 1);
    private final int startX = 30;
    private final int startY = 10;
    private Matrix matrix;
    private boolean showPicture = false;

    public PlayCanvas(Image picture, int level) {
        if (level == PlayCanvas.EASY) {
            matrix = new Matrix(picture, 3, 3, startX, startY);
        } else if (level == PlayCanvas.NORMAL) {
            matrix = new Matrix(picture, 3, 4, startX, startY);
        } else if (level == PlayCanvas.HARD) {
            matrix = new Matrix(picture, 4, 4, startX, startY);
        }
        addCommand(CMD_BREAK);
        addCommand(CMD_BACK);
        setCommandListener(this);
    }

    private void breakUp() {
        matrix.breakUp();
        showPicture = false;
        repaint();
    }

    public void commandAction(Command c, Displayable d) {
        if (c == CMD_BACK) {
            MainMidlet.popScreen();
        } else if (c == CMD_BREAK) {
            breakUp();
        }
    }

    protected void paint(Graphics g) {
        g.setColor(0x005380B7);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0x00FF0000);
        if (showPicture) {
            drawPicture(g);
        } else {
            drawPuzzle(g);
        }
    }

    void drawPuzzle(Graphics g) {
        for (int i = 0; i < matrix.getM(); i++) {
            for (int j = 0; j < matrix.getN(); j++) {
                if (!(i == matrix.getHiddenI() && j == matrix.getHiddenJ())) {
                    g.setClip(startX + matrix.getBlockWidth() * i, startY + matrix.getBlockHeight() * j, matrix.getBlockWidth(), matrix.getBlockHeight());
                    g.drawImage(matrix.getPicture(), matrix.getBlockAt(i, j).getStartX(), matrix.getBlockAt(i, j).getStartY(), Graphics.TOP | Graphics.LEFT);
                }
            }
        }

        g.setClip(0, 0, getWidth(), getHeight());
        for (int i = 0; i < matrix.getM(); i++) {
            for (int j = 0; j < matrix.getN(); j++) {
                g.drawRect(startX + matrix.getBlockWidth() * i, startY + matrix.getBlockHeight() * j, matrix.getBlockWidth(), matrix.getBlockHeight());
            }
        }
    }

    void drawPicture(Graphics g) {
        g.drawImage(matrix.getPicture(), startX, startY, Graphics.TOP | Graphics.LEFT);
        g.drawRect(startX, startY, matrix.getBlockWidth() * matrix.getM(), matrix.getBlockHeight() * matrix.getN());
    }

    protected void keyPressed(int keyCode) {
        switch (getGameAction(keyCode)) {
            case Canvas.UP:
                if (!showPicture) {
                    matrix.moveTo(Matrix.UP);
                    if (matrix.isFinish()) {
                        showPicture = true;
                    }
                }
                break;
            case Canvas.DOWN:
                if (!showPicture) {
                    matrix.moveTo(Matrix.DOWN);
                    if (matrix.isFinish()) {
                        showPicture = true;
                    }
                }
                break;
            case Canvas.LEFT:
                if (!showPicture) {
                    matrix.moveTo(Matrix.LEFT);
                    if (matrix.isFinish()) {
                        showPicture = true;
                    }
                }
                break;
            case Canvas.RIGHT:
                if (!showPicture) {
                    matrix.moveTo(Matrix.RIGHT);
                    if (matrix.isFinish()) {
                        showPicture = true;
                    }
                }
                break;
            case Canvas.FIRE:
                showPicture = (showPicture == false) ? true : false;
                break;
        }
        repaint();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -