📄 puzzlecanvas.java
字号:
package picturepuzzle;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
/**
* 拼图游戏的屏幕类,将根据图片随即生成 4 x 4 的游戏图片
*/
public class PuzzleCanvas extends Canvas implements CommandListener {
static final int IMAGE_WIDTH = 160;
static final int IMAGE_HEIGHT = 120;
static final int COLS = 4;
static final int ROWS = 4;
private TiledLayer imageLayer;
private Image image;
//光标的位置
private int cursorX , cursorY;
private GameMIDlet midlet;
private boolean doublePaired;
private Command exitCommand;
private Command hintCommand;
private Command newCommand;
private int firstBlock = 0;
private int secondBlock = 0;
private int firstCol = 0;
private int firstRow = 0;
private int secondCol = 0;
private int secondRow = 0;
public PuzzleCanvas(GameMIDlet midlet , Image image) {
super();
this.midlet = midlet;
this.image = image;
exitCommand = new Command("Exit", Command.EXIT, 1);
hintCommand = new Command("Hint" , Command.SCREEN , 2) ;
newCommand = new Command("New game" , Command.SCREEN , 2);
addCommand(exitCommand);
addCommand(hintCommand);
setCommandListener(this);
createBoard();
cursorX = getWidthDiff() / 2;
cursorY = getHeightDiff() / 2;
imageLayer.setPosition(getWidthDiff() / 2, getHeightDiff() / 2);
doublePaired = true;
}
public int getWidthDiff() {
return getWidth() - IMAGE_WIDTH;
}
public int getHeightDiff() {
return getHeight() - IMAGE_HEIGHT;
}
/**随即创建拼图的顺序 */
private void createBoard() {
imageLayer = new TiledLayer(COLS, ROWS, image, IMAGE_WIDTH/COLS, IMAGE_HEIGHT/ROWS);
Random ran = new Random();
Vector v = new Vector(ROWS*COLS);
boolean b = true;
int i;
while (b) {
i = ran.nextInt()%(ROWS*COLS)+1;
if (i > 0 && i <= (ROWS*COLS)) {
if (!contain(v , i)) {
v.addElement(new Integer(i));
}
if (v.size() == ROWS*COLS) {
b = false;
}
}
}
for (int m = 0; m < ROWS*COLS; m++) {
int integer = ( (Integer) v.elementAt(m)).intValue();
imageLayer.setCell( m/ROWS, m%ROWS, integer);
}
}
private boolean contain(Vector v , int element) {
for (int i = 0; i < v.size(); i++) {
if ( ( (Integer) v.elementAt(i)).intValue() == element) {
return true;
}
}
return false;
}
public void paint(Graphics g) {
g.setColor(255 , 255 , 255);
g.fillRect(0 , 0 , getWidth() , getHeight());
imageLayer.paint(g);
g.setColor(255 , 0 , 0);
drawFrame(cursorX , cursorY , g);
if (isWinning()){
g.setFont(Font.getFont(Font.FACE_MONOSPACE , Font.STYLE_BOLD , Font.SIZE_LARGE));
g.drawString("Game Over!!" , getWidth() / 2 , getHeight() / 2 , Graphics.HCENTER | Graphics.TOP);
}
}
/**
* 响应用户的操作
*/
public void keyPressed(int keyCode) {
int key = getGameAction(keyCode);
if (key == LEFT) {
moveLeft();
} else if (key == RIGHT) {
moveRight();
} else if (key == UP) {
moveUp();
} else if (key == DOWN) {
moveDown();
} else if (key == FIRE && !doublePaired) {
setSecondBlock();
if (isWinning()) {
addCommand(newCommand);
}
} else if (key == FIRE && doublePaired) {
setFirstBlock();
}
repaint();
}
/**
* 判断用户是否正确完成游戏拼图
*/
public boolean isWinning() {
int count = 1 ;
for (int row = 0 ; row < imageLayer.getRows() ; row++) {
for (int col = 0 ; col < imageLayer.getColumns() ; col++) {
if (imageLayer.getCell(col , row) != count) {
return false ;
}
count++;
}
}
return true;
}
private void drawFrame(int x , int y , Graphics g) {
g.drawRect(x , y , imageLayer.getCellWidth() , imageLayer.getCellHeight());
}
/**
* 向上移动光标
*/
public void moveUp() {
cursorY = cursorY - imageLayer.getCellHeight();
if (cursorY < getHeightDiff() / 2) {
cursorY = getHeightDiff() / 2;
}
}
/**
* 向下移动光标
*/
public void moveDown() {
cursorY = cursorY + imageLayer.getCellHeight();
if ( cursorY > IMAGE_HEIGHT + getHeightDiff() / 2 - imageLayer.getCellHeight()) {
cursorY = IMAGE_HEIGHT + getHeightDiff() / 2 - imageLayer.getCellHeight();
}
}
/**
* 向左移动光标
*/
public void moveLeft() {
cursorX = cursorX - imageLayer.getCellWidth();
if (cursorX < getWidthDiff() / 2) {
cursorX = getWidthDiff() / 2;
}
}
/**
* 向右移动光标
*/
public void moveRight() {
cursorX = cursorX + imageLayer.getCellWidth();
if ( cursorX > IMAGE_WIDTH + getWidthDiff() / 2 - imageLayer.getCellWidth()) {
cursorX = IMAGE_WIDTH + getWidthDiff() / 2 - imageLayer.getCellWidth();
}
}
/**
* 得到要交换的第一个拼图位置
*/
public void setFirstBlock() {
firstCol = (cursorX - getWidthDiff() / 2) / imageLayer.getCellWidth();
firstRow = (cursorY - getHeightDiff() / 2) / imageLayer.getCellHeight();
firstBlock = imageLayer.getCell(firstCol , firstRow);
doublePaired = false;
}
/**
* 得到要交换的第二个拼图位置
*/
public void setSecondBlock() {
secondCol = (cursorX - getWidthDiff() / 2) / imageLayer.getCellWidth();
secondRow = (cursorY - getHeightDiff() / 2) / imageLayer.getCellHeight();
secondBlock = imageLayer.getCell(secondCol , secondRow);
// 交换两个拼图
imageLayer.setCell(firstCol , firstRow , secondBlock);
imageLayer.setCell(secondCol , secondRow , firstBlock);
doublePaired = true;
}
public void commandAction(Command command , Displayable displayable) {
if (command == exitCommand) {
midlet.exit();
} else if (command == hintCommand){
new Thread() {
public void run(){
midlet.displayHintCanvas(image);
}
}.start();
} else if (command == newCommand) {
midlet.displayChoiceForm();
image = null;
midlet = null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -