最近迷恋上了一款小游戏“数独”,它的玩法主要是:有一个9x9的格子里面,在每个格子里面填写一个数字(1-9任意一个),使得每一行,每一列,每一宫(3x3为一宫)的数字都不相同。如图
“数独”源于中国的九宫格(骄傲啊!),它大约有6.67×10的21次方种组合。目前这种游戏在世界范围内十分的流行,可以说是居家旅行必备游戏啊!那么接下来就开始制作这款游戏。
首先介绍一下游戏的结构,它由:
Sudoku.java;
Logo.java;
Menu.java;
SudokuGame.java;
Sudo.java;
Help.java;
SudoRecordStore.java
GameTime.java
这几个类组成,今天主要介绍的是Sudoku.java以及Logo.java类。
Sudou.java主要是对整个游戏来说起到一个控制器的作用,负责游戏画面之间的转换,代码如下:
public class Sudoku extends MIDlet {
private Display dis;
private Logo logo;
public Sudoku() {
dis=Display.getDisplay(this);
logo = new Logo(this);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
dis.setCurrent(logo); //调用logo中的画布显示在手机上
}
}
在Sudou中,很明显调用了Logo这个类。Logo主要是实现了一个游戏的开场画面代码如下:
public class Logo extends GameCanvas implements Runnable {
private Image logop_w_picpath;
private Image black;
private Graphics g;
private Sudoku sudoku;
private Display dis;
private Menu menu;
private Thread thread;
private int flag;
public Logo(Sudoku sudoku) {
super(false);
this.sudoku = sudoku;
menu = new Menu(sudoku);
dis = Display.getDisplay(sudoku);
try {
logop_w_picpath = Image.createImage("/sudoku.png");
black = Image.createImage("/black.png");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g = this.getGraphics();
thread = new Thread(this);
thread.start();
}
protected void keyPressed(int keyCode){
if(this.getGameAction(keyCode)==FIRE){
flag = 1;
dis.setCurrent(menu);
}
}
public void run() {
int i = 0;
int j = 0;
for(;i<20;i++){
g.setColor(255, 255, 255);
g.setColor(0, 0, 0);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.drawImage(logop_w_picpath,
(this.getWidth() - logop_w_picpath.getWidth()) / 2, (this
.getHeight() - logop_w_picpath.getHeight()) / 2,
Graphics.TOP | Graphics.LEFT);
g.drawImage(black, i, 0, Graphics.TOP | Graphics.LEFT);
try {
thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.flushGraphics();
paint(g);
}
for(;i>0;i--){
if(flag == 1){
return;
}
g.setColor(255, 255, 255);
g.setColor(0, 0, 0);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.drawImage(logop_w_picpath,
(this.getWidth() - logop_w_picpath.getWidth()) / 2, (this
.getHeight() - logop_w_picpath.getHeight()) / 2,
Graphics.TOP | Graphics.LEFT);
g.drawImage(black, i, j++, Graphics.TOP | Graphics.LEFT);
try {
new Thread(this).sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.flushGraphics();
paint(g);
}
for(;i>-20;i--){
if(flag == 1){
return;
}
g.setColor(255, 255, 255);
g.setColor(0, 0, 0);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.drawImage(logop_w_picpath,
(this.getWidth() - logop_w_picpath.getWidth()) / 2, (this
.getHeight() - logop_w_picpath.getHeight()) / 2,
Graphics.TOP | Graphics.LEFT);
g.drawImage(black, i, j--, Graphics.TOP | Graphics.LEFT);
try {
thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.flushGraphics();
paint(g);
}
for(i=(getWidth() - logop_w_picpath.getWidth()) / 2;i>0;i--){
if(flag == 1){
return;
}
g.setColor(255, 255, 255);
g.setColor(44, 142, 164);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.drawImage(logop_w_picpath,
(getWidth() - logop_w_picpath.getWidth()) / 2, i,
Graphics.TOP | Graphics.LEFT);
try {
new Thread(this).sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.flushGraphics();
paint(g);
}
dis.setCurrent(menu);
}
}
好了,今天就写到这吧,我学j2me的时间不长,所有还有很多地方需要多多的学习,程序中还存在很多的不足。希望能有高手指点。