📄 gameboard.java
字号:
//J2ME MIDlet 拼图游戏
//拼图游戏的实现
//翁荣建
//2005-06-20
//gameBoard.java
package game;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.util.Random;
import java.io.*;
import menu.*;
import gamepuzzle.*;
public class gameBoard extends Canvas implements CommandListener {
public static gameBoard instance;
private Command MenuCommand;
private gameBox box[];//盒子数组
private Image img[];//图片数组
private Random rand;//随机数
private String gameStart="Stop";//游戏状态
private Font font;
//menu
//定义菜单
//菜单组属性
private int menuGroup1Left;
private int menuGroup1Top;
private int menuGroup1Width;
private int menuGropu1Length;
//菜单组
private MenuGroup menuGroup1;
//菜单项
private Menu menuGroup1Menu1;
private Menu menuGroup1Line1;
private Menu menuGroup1Menu2;
private Menu menuGroup1Menu3;
private Menu menuGroup1Menu4;
public gameBoard() {
try {
this.instance=this;
//构造字体
font=Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_SMALL);
//构造随机数
rand=new Random();
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
//menu
//构造菜单
this.menuGroup1Top = 8;
this.menuGroup1Width = 75;
this.menuGroup1Left = (this.getWidth() - this.menuGroup1Width) / 2;
this.menuGropu1Length = 5;
this.menuGroup1 = new MenuGroup(menuGroup1Left,menuGroup1Top,menuGroup1Width,menuGropu1Length);
this.menuGroup1Menu1 = new Menu(null,"开始游戏");
this.menuGroup1Line1 = new Menu();
this.menuGroup1Menu2 = new Menu(null,"继续游戏");
this.menuGroup1Menu2.setEnabled(false);//设菜单项2为不可用状态
this.menuGroup1Menu3 = new Menu(null,"保存游戏");
this.menuGroup1Menu3.setEnabled(false);//设菜单项3为不可用状态
this.menuGroup1Menu4 = new Menu(null,"回主菜单");
//把菜单项加入到菜单组中
this.menuGroup1.Add(menuGroup1Menu1);
this.menuGroup1.Add(menuGroup1Line1);
this.menuGroup1.Add(menuGroup1Menu2);
this.menuGroup1.Add(menuGroup1Menu3);
this.menuGroup1.Add(menuGroup1Menu4);
this.menuGroup1.setVisable(false);//初始化菜单组不可见
//构造盒子
box=new gameBox[9];
img=new Image[9];
for (int i=0;i<9;i++)
{ String str="/game/images/"+i+".png";
img[i]=CreateImage.instance.getImage(str);
box[i]=new gameBox(i,img[i]);
}
box[8].setBlackBox(true);//设第9个盒子为黑盒
setCommandListener(this);
addCommand(MenuCommand=new Command("操作", Command.SCREEN, 1));
}
public void commandAction(Command command, Displayable displayable) {
if (command==MenuCommand)
{this.menuGroup1.setVisable(!this.menuGroup1.getVisable());
if(this.gameStart=="Stop")//如果游戏处于未开始状态
{this.menuGroup1Menu1.setEnabled(true);
this.menuGroup1Menu2.setEnabled(false);
this.menuGroup1Menu3.setEnabled(false);
this.menuGroup1.setSelectIndex(0);
}
if(this.menuGroup1.getVisable())
//如果游戏已开始,打开菜单游戏暂停,关闭菜单游戏继续
{if(this.gameStart=="Start")this.gameStart="Pause";}
else
{if(this.gameStart=="Pause")this.gameStart="Start";}
this.repaint();//每改变状态从绘
}
}
protected void paint(Graphics g) {
//g.setColor(255,255,255);
g.setColor(133,179,241);
g.fillRect(0,0,this.getWidth(),this.getHeight());
this.display(g);
//检查游戏是否完成,完成就在SCREEN中间画一个白色圆角矩形,并在上面写上红色"胜利!"
if(this.gameStart=="Start")
{if(this.box[8].getLocate()==8)
{if (gameOver())
{this.gameStart="Stop";
g.fillRoundRect((this.getWidth()-60)/2,(this.getHeight()-20)/2,60,20,10,10);
g.setColor(255,0,0);
g.drawString("胜利!",(this.getWidth()-g.getFont().stringWidth("胜利!"))/2,
(this.getHeight()-g.getFont().getHeight())/2,0);
}
}
}
if(this.menuGroup1.getVisable())
{//如果主菜单为可见.
menuGroup1.Display(g,font);//显示主菜单
}
}
private boolean gameOver()
//检查游戏是否完成方法
{for(int i=0;i<9;i++)
if(box[i].getLocate()!=i)return false;
return true;
}
private void randBox()
//随机移动盒子,用于开始游戏的初始化游戏面板
{int n;
for (int i=0;i<400;i++)
{n=-(randRange(4)+1);
moveGameBox(n);
}
}
private int randRange(int n)
//返回一个小于n的非负数
{
int r = rand.nextInt() % n;
if (r < 0)
r += n;
return r;
}
private void swapGameBox(int index1,int index2)
//改变两个盒子的位置
{int tmp=box[index1].getLocate();
int tmp2=box[index2].getLocate();
box[index1].setLocate(tmp2);
box[index2].setLocate(tmp);
}
private void moveGameBox(int key)
//移动盒子
{int locate=gateBlackBoxLocate();//得到黑盒的位置
int x=locate%3,y=locate/3;
int x1=0,y1=0,focu=0;
switch(key){
case -1://向上
if(y==2)//如果黑盒在最下一排就向上移不了
{}
else//向上移一个盒子
{x1=x;y1=y+1;
focu=getFocuBoxLocate(x1+y1*3);
swapGameBox(8,focu);
}
break;
case -2:
if(y==0)//如果黑盒在最上一排就下移不了
{}
else//向下移一个盒子
{x1=x;y1=y-1;
focu=getFocuBoxLocate(x1+y1*3);
swapGameBox(8,focu);
}
break;
case -3://左
if(x==2)//如果黑盒在最右就左移不了
{}
else//左移一个盒子
{x1=x+1;y1=y;
focu=getFocuBoxLocate(x1+y1*3);
swapGameBox(8,focu);
}
break;
case -4://右
if(x==0)//如果黑盒在最左就右移不了
{}
else//右移一个盒子
{x1=x-1;y1=y;
focu=getFocuBoxLocate(x1+y1*3);
swapGameBox(8,focu);
}
}
}
private int gateBlackBoxLocate()//取得黑盒的位置
{return box[8].getLocate();
}
private int getFocuBoxLocate(int locate)//取得焦点盒的位置
{for(int i=0;i<9;i++)
if(box[i].getLocate()==locate)
return i;
return 0;
}
private void display(Graphics g)//显示面板上的所有盒子
{
for (int i=0;i<9;i++)
box[i].dispaly(g);
}
protected void keyPressed(int keyCode) {
switch(keyCode) {
case -5://OK键
case -10://电话拨号键
if(this.menuGroup1.getVisable())
{//如果焦点在菜单上
switch (this.menuGroup1.getSelectIndex())
{case 0://开始游戏
this.menuGroup1Menu1.setEnabled(false);
this.menuGroup1Menu2.setEnabled(true);
this.menuGroup1Menu3.setEnabled(true);
this.menuGroup1.setSelectIndex(2);
this.randBox(); //初始化游戏面板
this.gameStart = "Start"; //设游戏状态为开始
this.repaint();
break;
case 1:
this.repaint();
break;
case 2:
if(this.gameStart=="Pause")this.gameStart="Start";
this.repaint();
break;
case 3:
this.SaveGame();
if(this.gameStart=="Pause")this.gameStart="Start";
this.repaint();
break;
case 4:
if(this.gameStart=="Pause")this.gameStart="Start";
Display.getDisplay(puzzleMIDlet.instance).setCurrent(puzzleMIDlet.instance.mainC);
this.repaint();
}
}
this.menuGroup1.setVisable(false);//更改菜单可视状态
break;
case -1://UP键
//按UP键
if(this.gameStart=="Start")//如果游戏开始状态就移动盒子
{moveGameBox(keyCode);this.repaint();}
if(this.menuGroup1.getVisable())//如果焦点在菜单上
{this.menuGroup1.changeSelectMenu(keyCode);this.repaint();
}
break;
case -2://DOWN键
//按向下键
if(this.gameStart=="Start")
{moveGameBox(keyCode);this.repaint();}
if(this.menuGroup1.getVisable())
{this.menuGroup1.changeSelectMenu(keyCode);this.repaint();
}
break;
case -3://左键
case -4://右键
if(this.gameStart=="Start")
{moveGameBox(keyCode);
this.repaint();}
break;
}
}
///////////////////////////////////////////////////////////////////
//以下是保存游戏和载入游戏方法
public void SaveGame()
{ byte store[]=new byte [9];
store=this.changeToByteArray();
RecordStore rs = null;
try{rs = RecordStore.openRecordStore("GDB",true);
try{int ID;
ID=rs.getNextRecordID();
if(ID==1)//如果还没有记录,添加记录
rs.addRecord(store,0,store.length);
else if (ID!=1)//已经有记录,就修改记录
{
rs.setRecord(ID-1,store,0,store.length);
}
}
catch(Exception e)
{}
}
catch(Exception e){}
finally{
try {
rs.closeRecordStore();
}
catch (Exception e) {}
}
}
private byte[] changeToByteArray(){//把整形数据流转换成字节流
byte[] data = null;
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
for (int i=0;i<9;i++)
dos.writeInt(box[i].getLocate());
data = baos.toByteArray();
baos.close();
dos.close();
}
catch(Exception e){
}
return data;
}
public void loadGame(){//载入游戏
byte store[] = new byte[9];
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore("GDB", true);
try {
int ID = 0;
ID = rs.getNextRecordID();
store = rs.getRecord(ID - 1);
}
catch (Exception e) {}
}
catch (Exception e) {}
finally {
try {
rs.closeRecordStore();
}
catch (Exception e) {}
}
try{
ByteArrayInputStream bais = new ByteArrayInputStream(store);
DataInputStream dis = new DataInputStream(bais);
int locate;
for (int i=0;i<9;i++)
{ locate=dis.readInt();
System.out.print(locate +" ");
box[i].setLocate(locate);}
bais.close();
dis.close();
}
catch(Exception e){
}
this.gameStart="Start";
this.menuGroup1Menu1.setEnabled(false);
this.menuGroup1Menu2.setEnabled(true);
this.menuGroup1Menu3.setEnabled(true);
this.menuGroup1.setSelectIndex(2);
}
public boolean hasGameStore()//检查数据库中是否有保存记录
{ RecordStore rs = null;
boolean store=false;
try{rs = RecordStore.openRecordStore("GDB",true);
try{int ID=-1;
ID=rs.getNextRecordID();
if(ID==1||ID==-1)
{store=false;}
else if(ID>1)
{store=true;
}
}
catch(Exception e)
{}
}
catch(Exception e){}
finally{
try {
rs.closeRecordStore();
}
catch (Exception e) {}
}
return store;
}
}
////////////////////////////////////////////////////////////////
class gameBox
{//封装盒子
private int top;//y
private int left;//x
private final int width=30;
private final int height=30;
private int index;//索引
private int locate;//位置标志
private Image img;//前景图片
private boolean isBlackBox;//黑盒标志
public gameBox(int index)//没有图片的盒子,也就是黑盒
{this.index=index;
this.locate=index;
this.img=null;
this.isBlackBox=true;
}
public gameBox(int index,Image img)
//一般盒子,也可以通用setBlackBox(true)的方法,把一般盒子转变为黑盒
{this.index=index;
this.locate=index;
this.img=img;
this.isBlackBox=false;
}
private void paint(Graphics g) {
//画出盒子
if(!this.isBlackBox)
{g.drawImage(this.img,this.left,this.top,0);
}
else
{g.setColor(0,0,0);
g.fillRect(this.left,this.top,this.width,this.height);
}
g.setColor(255,255,255);
g.drawRect(this.left,this.top,this.width,this.height);
}
private void setLocation()
//通过盒子位置标志,设置盒子的绝对坐标
{int x=(gameBoard.instance.getWidth()-this.width*3)/2;
int y=(gameBoard.instance.getHeight()-this.height*3)/2;
this.top=y+this.locate/3*this.height;
this.left=x+(this.locate%3)*this.width;
}
public void dispaly(Graphics g)//显示
{ setLocation();
paint(g);}
public void setBlackBox(boolean Blackbox)//设黑盒
{this.isBlackBox=Blackbox;}
public boolean getBlackBox()//取得黑盒
{return this.isBlackBox;}
public void setLocate(int locate)//更改位置标志
{this.locate=locate;}
public int getLocate()//取得盒子位置标志
{return this.locate;}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -