疯狂拼图完整源代码

        CrazyPuzzle的完整源代码,整个架构已经基本打好,但因为某些原因,部分功能尚未实现,在此一并贴出来.如果各位朋友有兴趣,可自行完善相应功能.

//------------------------------
//MyGameCanvas.java
//------------------------------

package src;

import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;//西门子C65不支持,故无法以此方法播放声音,在此不插入
import java.util.*;
import java.io.*;

public class MyGameCanvas extends GameCanvas implements CommandListener 
{

    
/** 代表标题显示状态的常量 */
    
private static final short    STAGE_TITLE            = 0;
    
/** 代表关卡开始显示状态的常量 */
    
private static final short    STAGE_START            = 1;
    
/** 代表关卡显示状态的常量 */
    
private static final short    STAGE_PLAYING        = 2;
    
/** 代表游戏中显示完整图案的常量 */
    
private static final short    STAGE_PLAYING_SEE_PERFECT_PIC        = 3;
    
/** 代表关卡结束显示状态的常数 */
    
private static final short    STAGE_OVER            = 5;
    
/** 代表过关显示状态的常量 */
    
private static final short    STAGE_CLEAR            = 7;
    
/** 代表全关卡过关显示状态的常量 */
    
private static final short    ALL_STAGE_CLEAR        = 9;
    
/** 代表游戏结束的常量 */
    
private static final short    GAME_END        = 10;
    
/** 最大关卡数 */
    
private static final short    MAX_STAGE            = 5;
    
/** 正常模式常量 */
    
public static final short    MODE_NORMAL            = 1;
    
/** 极限模式常量 */
    
public static final short    MODE_UTMOST            = 2;
    
    
/** 储存现在之显示状态的变量 */
    
private short                gameState;
    
    
/** 储存游戏模式的变量 */
    
private short                gameMode;
    
    
/** 储存目前的关卡 */    
    
private short                 stageNum;    
    
    
/** 储存已过关的关卡 */    
    
private short                 stagePassedNum;    
    
    
/** 储存剩余的时间 */    
    
private long                 lastTime;
    
    
/** 储存损耗的时间,普通模式用到 */    
    
private long                 usedTime;
    
    
/** 储存单关通过的最短时间 */    
    
private long                 quicklyPassOneStageTime;
    
    
/** 游戏过程暂停状态所用的标志 */
    
private boolean                isGamePaused;
    
    
/** 储存显示的对象 */
    
private Display             display;
    
    
/** 储存音乐的对象 */
    
private Player              player;//西门子C65不支持
    
    
/** 储存主类的对象,方便进行退出之类的操作 */
    
private CrazyPuzzleMIDlet    mainMIDlet;
    
    
/**层管理*/
    
private LayerManager lm;
    
    
/**背景*/
    
private TiledLayer puzzlePic;
    
    
/**建一个画图对象*/
    
private Graphics g;
    
    
/**时间倒计时,类为继承*/
    
private TimeCounterTask timeCounter;
    
    
/** 储存各关卡谜题资讯的变量 */
    
private static final short[][] PUZZLE_MAP    = new short[][] {
        
{    //关卡1的谜题
             1,  5,  913,
             
2,  61014,
             
3,  71115,
             
4,  812,  0
        }
,
        
{    //关卡2的谜题
             4,  3,  2,  1,
             
8,  7,  6,  5,
            
121110,  9,
            
151314,  0
        }
,
        
{    //关卡3的谜题
            12,  8,  4,  1,
            
15,  9,  5,  2,
            
1310,  6,  3,
            
1411,  7,  0
        }
,
        
{    //关卡4的谜题
            14151213,
             
8,  91011,
             
4,  5,  6,  7,
             
1,  2,  3,  0
        }
,
        
{    //关卡5的谜题
            14121513,
            
1110,  9,  8,
             
7,  6,  5,  4,
             
3,  2,  1,  0
        }

    }
;
    
    
//norains:*构造函数*
    public MyGameCanvas(Display disp,CrazyPuzzleMIDlet m,short mode)
    
{
        
super(false);
        
this.display=disp;
        
this.mainMIDlet=m;
        
this.gameMode=mode;
        stageNum
=1;
        stagePassedNum
=0;
        lastTime
=0;
        usedTime
=0;
        quicklyPassOneStageTime
=0;
        isGamePaused
=false;
        g
=this.getGraphics();
        gameState
=STAGE_TITLE;
        
this.setFullScreenMode(true);//全屏幕
        
//增加命令
        
//this.addCommand(new Command("重新开始",Command.OK,1));
        this.addCommand(new Command("取消",Command.BACK,1));
        
this.addCommand(new Command("返回列表",Command.BACK,1));
        
this.addCommand(new Command("退出游戏",Command.EXIT,1));
        
this.setCommandListener(this);
        render();
    }

    
    
public void commandAction(Command command, Displayable arg1)
    
{
        
// TODO Auto-generated method stub
        String cmd=command.getLabel();
        
if(cmd.equals("重新开始"))
        
{
            
//不知为何,下面这段代码无法正常关闭音乐流,无论在模拟器还是在实体机上
            
//造成的后果是两首音乐一起播放,以致于系统变慢,无法正常游戏
            
//估计产生原因是上一首歌还没退出来,下一首就播放了
            
//关闭本节声音播放,以方便下一关播放新音乐
            gameState=STAGE_TITLE;
            
try
            
{
                player.stop();
            }

            
catch(Exception exp)
            
{}
                
    

            render();
            
        }

        
else if(cmd.equals("返回列表"))
        
{
            
if(player!=null)
            
{
                
//关闭本节声音播放
                try
                
{
                    player.stop();
                }

                
catch(Exception exp)
                
{}
            
            }

            ListChoiceScreen lcs 
=new ListChoiceScreen(display,mainMIDlet);
            display.setCurrent(lcs);
        }

        
else if(cmd.equals("退出游戏"))
        
{
            mainMIDlet.notifyDestroyed();
        }

        
else if(cmd.equals("取消"))
        
{
            
//什么都不做,返回原画面
        }

    }

    
    
//*主要是用来绘图
    public void render()
    
{
        
switch(gameState)
        
{
            
case STAGE_TITLE:
                doTitlePaint();
                
break;
            
case STAGE_START:
                doStartPaint();
                
break;
            
case STAGE_PLAYING:
                doPlayingPaint();
                
break;
            
case STAGE_PLAYING_SEE_PERFECT_PIC:
                doPlayingSeePerfectPicPaint();
                
break;
            
case STAGE_OVER:
                doStageOverPaint();
                
break;
            
case STAGE_CLEAR:
                doStageClearPaint();
                
break;
            
case ALL_STAGE_CLEAR:
                doStageAllClearPaint();
                
break;
            
case GAME_END:
                doGameEndPaint();
                
break;
        }


    }
    
    
    
//*画出关卡画面*
    private void doTitlePaint()
    
{
        
int dispWidth=this.getWidth();
        
int dispHeight=this.getHeight();
        g.setColor(
255,255,255);
        g.fillRect(
0,0,getWidth(),getHeight());
        Image img1
=creatImage("/res/ready.png");
        g.drawImage(img1,(dispWidth
-img1.getWidth())/2,(dispHeight-img1.getHeight())/2-4,0);
        Image img2
=creatImage("/res/num"+stageNum+".png");
        g.drawImage(img2,(dispWidth
-img2.getWidth())/2,(dispHeight-img1.getHeight())/2+img1.getHeight()-1,0);
        
this.flushGraphics();    
    }

    
    
//*关卡开始前
    boolean firstStart=true;//是否首次运行标志
    short musicNum=1;//因为音乐不够,以此变量做两首歌更换标志
    private void doStartPaint()
    
{    
        
//*关卡初始化
        lm=new LayerManager();
        puzzlePic
=creatPuzzlePic();
        
//*测试用
        
//puzzlePic=creatTestPuzzlePic();//测试用
        lm.append(puzzlePic);    
            

            
//播放声音
            
//西门子模拟器不支持下面这段代码,但实体机子完美支持
        try
        
{
            InputStream in ;
            
//in= getClass().getResourceAsStream("/res/music"+stageNum+".mid");这是有5首音乐的时候使用的
            
//只有两首歌的时候使用
            if(musicNum==1)
            
{
                in
= getClass().getResourceAsStream("/res/music1.mid");
                musicNum
=2;
            }

            
else
            
{
                in
= getClass().getResourceAsStream("/res/music2.mid");
                musicNum
=1;
            }

            
            player 
= Manager.createPlayer(in,"audio/midi");
            player.setLoopCount(
10);
            player.start();
        }

        
catch(IOException e){}
        
catch(MediaException e){}        

        
        
        gameState
=STAGE_PLAYING;
        
//创建并启动计时器
        if(firstStart==true||gameMode==MODE_NORMAL)
        
{
            
if(gameMode==MODE_NORMAL)
            
{
                timeCounter
=new TimeCounterTask(300);
            }

            
else if(gameMode==MODE_UTMOST)//此段代码只在第一次时运行
            {
                timeCounter
=new TimeCounterTask(900);
            }

            Timer timer
=new Timer();        
            timer.schedule(timeCounter,
1000,1000);
            firstStart
=false;        
        }

        
else
        
{
            timeCounter
=new TimeCounterTask(lastTime);
            Timer timer
=new Timer();        
            timer.schedule(timeCounter,
1000,1000);
            firstStart
=false;
            System.out.println(
"first");
        }

        
//
        render();
    }

    
    
//*游戏进行中
    private void doPlayingPaint()
    
{
        
//*设置计时器的字体
        Font font;
        font
=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL);        
        g.setColor(
1,16,57);
        g.fillRect(
0,0,this.getWidth(),this.getHeight());
        lm.setViewWindow(
0,0,this.getWidth(),this.getHeight());        
        lm.paint(g,(
this.getWidth()-112)/2,font.getHeight()+2);
        
//*画格子
        g.setColor(255,255,255);
        
int x1,y1,x2,y2;
        
//*横线
        y1=y2=font.getHeight()+2;
        x1
=(this.getWidth()-112)/2;
        x2
=((this.getWidth()-112)/2)+112;
        
for(int i=0;i<5;i++)
        
{
            g.drawLine(x1,y1,x2,y2);
            y1
=y2=y2+28;
        }


        
//*直线
        x1=x2=(this.getWidth()-112)/2;
        y1
=font.getHeight()+2;
        y2
=font.getHeight()+2+112;
        
for(int i=0;i<5;i++)
        
{
            g.drawLine(x1,y1,x2,y2);
            x1
=x2=x2+28;
        }
        
        g.setColor(
255,255,255);
        g.drawString(timeCounter.timeString,(
this.getWidth()-font.stringWidth(timeCounter.timeString))/2 ,1,0);
        
        flushGraphics();
    }

    
//    *游戏进行中观看全图
    private void doPlayingSeePerfectPicPaint()
    
{
        
//*在游戏中切换到完整图案
        g.setColor(1,16,57);
        g.fillRect(
0,0,this.getWidth(),this.getHeight());
        Font font;
        font
=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL);
        Image img
=creatImage("/res/stage"+stageNum+".png");
        
//*画出计时器
        g.setColor(255,255,255);
        g.drawString(timeCounter.timeString,(
this.getWidth()-font.stringWidth(timeCounter.timeString))/2,1,0);
        g.drawImage(img,(
this.getWidth()-112)/2,font.getHeight()+2,0);
        
//*画出右下角的小缺口    
        g.setColor(1,16,57);
        g.fillRect((
this.getWidth()-112)/2+82,font.getHeight()+2+82,30,30);
        
//*画出图片的方框
        g.setColor(255,255,255);
        g.drawRect((
this.getWidth()-112)/2,font.getHeight()+2,111,111);


        flushGraphics();
    }

    
    
//*游戏过一关    
    private void doStageClearPaint()
    
{
        g.setColor(
255,255,255);
        g.fillRect(
0,0,this.getWidth(),this.getHeight());
        Image img
=creatImage("/res/clear.png");
        g.drawImage(img,(
this.getWidth()-img.getWidth())/2,(this.getHeight()-img.getHeight())/3,0);
        
//*描绘字体
        g.setColor(1,16,57);
        Font font;
        font
=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL);
        
if(gameMode==MODE_NORMAL)
        
{
            String writeString
="最快过关:"+numToTime(quicklyPassOneStageTime);
            g.drawString(writeString,(
this.getWidth()-font.stringWidth(writeString))/2,(this.getHeight()-img.getHeight())/3+img.getHeight()+2,0);
            writeString
="全部耗费:"+numToTime(usedTime);
            g.drawString(writeString,(
this.getWidth()-font.stringWidth(writeString))/2,(this.getHeight()-img.getHeight())/3+img.getHeight()+font.getHeight()+2,0);
        }

        
else if(gameMode==MODE_UTMOST)
        
{
            String writeString
="已经通过"+stagePassedNum+"关!坚持下去!" ;
            g.drawString(writeString,(
this.getWidth()-font.stringWidth(writeString))/2,(this.getHeight()-img.getHeight())/3+img.getHeight()+2,0);
        }
        

        
//关闭本节声音播放,以方便下一关播放新音乐
        try
        
{
            player.stop();
        }

        
catch(Exception exp)
        
{}
    
        
//重画
        flushGraphics();
    }

    
//*游戏全部过关    
    private void doStageAllClearPaint()
    
{
        g.setColor(
255,255,255);
        g.fillRect(
0,0,this.getWidth(),this.getHeight());
        
//Image img=creatImage("/res/clear.png");
        
//g.drawImage(img,(this.getWidth()-img.getWidth())/2,(this.getHeight()-img.getHeight())/2,0);
        g.setColor(255,0,0);
        Font font;
        font
=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_LARGE);
        
if(gameMode==MODE_NORMAL)
        
{
            String writeString
="顺利过关!";
            g.drawString(writeString,(
this.getWidth()-font.stringWidth(writeString))/2 ,this.getHeight()/2-font.getHeight()-1,0);
            writeString
="最快过关:"+numToTime(quicklyPassOneStageTime);
            g.drawString(writeString,(
this.getWidth()-font.stringWidth(writeString))/2 ,this.getHeight()/2,0);
            writeString
="全部耗费:"+numToTime(usedTime);
            g.drawString(writeString,(
this.getWidth()-font.stringWidth(writeString))/2 ,this.getHeight()/2+font.getHeight()+1,0);
        }

        
else if(gameMode==MODE_UTMOST)
        
{
            String writeString
="时间到!";
            g.drawString(writeString,(
this.getWidth()-font.stringWidth(writeString))/2 ,this.getHeight()/2-font.getHeight()-1,0);
            writeString
="总过关数:"+stageNum;
            g.drawString(writeString,(
this.getWidth()-font.stringWidth(writeString))/2 ,this.getHeight()/2,0);
        }

        
//关闭本节声音播放,以方便下一画面播放新音乐
        try
        
{
            player.stop();
        }

        
catch(Exception exp)
        
{}            
        
//重画
        flushGraphics();

    }

    
//*游戏结束
    private void doStageOverPaint()
    
{
        g.setColor(
255,255,255);
        g.fillRect(
0,0,this.getWidth(),this.getHeight());
        Image img
=creatImage("/res/gameover.png");
        g.drawImage(img,(
this.getWidth()-img.getWidth())/2,(this.getHeight()-img.getHeight())/2,0);

        flushGraphics();
    }

    
    
//全部通关
    private void doGameEndPaint()
    
{
        
//返回选择画面
        ListChoiceScreen lcs =new ListChoiceScreen(display,mainMIDlet);
        display.setCurrent(lcs);
    }

    
//过关检查
    private void doStageClearCheck()
    
{
        
for(int i=0,k=1;i<4;i++)
            
for(int j=0;j<4;j++)
            
{            
                
if(i!=3||j!=3)
                
{
                    
if(puzzlePic.getCell(j,i)!=k)
                    
{    
                        
return;    //直接跳出函数                    
                    }

                    k
++;
                }

            }

        
        
if(gameMode==MODE_NORMAL)//普通模式
        {
            
if(stageNum==MAX_STAGE)//达到最大关数
            {
                gameState
=ALL_STAGE_CLEAR;
                timeCounter.cancel();
//过关,停止计时器
                if(quicklyPassOneStageTime>(300-lastTime))//判断耗费的最少时间
                {
                    quicklyPassOneStageTime
=(300-lastTime);
                }

                usedTime
=usedTime+(300-lastTime);
            }

            
else//未达到最大关数
            {
                gameState
=STAGE_CLEAR;
                stageNum
++;//进入下一关
                timeCounter.cancel();//过关,停止计时器
                
//如果保存时间至变量
                if(quicklyPassOneStageTime==0)
                
{
                    
//此为第一关,quicklyPassOneStageTime中未存数据
                    quicklyPassOneStageTime=300-lastTime;//300为普通模式默认的时间
                    usedTime=300-lastTime;//总耗时                    
                }

                
else //不是第一关,比较quicklyPassOneStageTime里数字大小
                {
                    
if(quicklyPassOneStageTime>(300-lastTime))
                    
{
                        quicklyPassOneStageTime
=(300-lastTime);
                    }

                    usedTime
=usedTime+(300-lastTime);
                }

                
            }

        }

        
        
else if(gameMode==MODE_UTMOST)//极限模式
        {
            
if(stageNum==MAX_STAGE)//达到最大关卡数
            {
                gameState
=STAGE_CLEAR;
                stageNum
=1;//重新进入第一关
                timeCounter.cancel();//过关,停止计时器
            }

            
else//未到最大关数
            {
                gameState
=STAGE_CLEAR;
                stageNum
++;//进入下一关
                timeCounter.cancel();//过关,停止计时器
                
//如果是普通模式则保存时间至变量
            }

            stagePassedNum
++;//过关数增加1
        }

        
        render();
//重新绘画
    }

    
//*创建图片
    private Image creatImage(String fileName)
    
{
        Image img
=null;
        
try
        
{
            img
=Image.createImage(fileName);
        }

        
catch(Exception exp)
        
{
            System.out.println(
"打开图片出错:"+fileName);
        }

        
return img;

    }

    
    
//*创建随机背景拼图
    private TiledLayer creatPuzzlePic()
    
{
        Image img
=null;
        
try
        
{
            img
=Image.createImage("/res/stage"+stageNum+".png");
        }

        
catch(Exception exp)
        
{}
        TiledLayer tiledLayer
=new TiledLayer(4,4,img,28,28);
        
//*产生随机数选择地图分散方式
        Random rdm=new Random();
        
int mapNum=(rdm.nextInt()>>>1)%4;        

        
for(int i=0;i<PUZZLE_MAP[mapNum].length;i++)
        
{
            
int column=i%4;
            
int row=(i-column)/4;
            tiledLayer.setCell(column,row,PUZZLE_MAP[mapNum][i]);
        }

        
return tiledLayer;
    }

    
//*创建简单背景拼图,测试用
    private TiledLayer creatTestPuzzlePic()
    
{
        Image img
=null;
        
try
        
{
            img
=Image.createImage("/res/stage"+stageNum+".png");
        }

        
catch(Exception exp)
        
{}
        TiledLayer tiledLayer
=new TiledLayer(4,4,img,28,28);
        
//*产生随机数选择地图分散方式
        int []map=
        
{
          
1,  2,  3,  4,
          
5,  6,  7,  8,
          
9101112,
         
1314,  015
        }
;
        
for(int i=0;i<map.length;i++)
        
{
            
int column=i%4;
            
int row=(i-column)/4;
            tiledLayer.setCell(column,row,map[i]);
        }

        
return tiledLayer;
    }

    
//*游戏中按下左键
    private void doRight()
    
{
        
boolean canChanged=true;
        
for(int i=0;i<4;i++)
        
{
            
//*判断最空格是否在最左边
            if(    puzzlePic.getCell(0,i)==0)
                
{
                    canChanged
=false;
                    
break;
                }

        }
        
        
if(canChanged==true)
        
{
            
//*可以移动方块
            int spaceColumn,spaceRow,picCell;
            spaceColumn
=spaceRow=0;
            
for(int i=0;i<puzzlePic.getColumns();i++)
                
for(int j=0;j<puzzlePic.getRows();j++)
                
{
                    
if(puzzlePic.getCell(i,j)==0)
                    
{
                        spaceColumn
=i;
                        spaceRow
=j;
                        
break;
                    }

                }

            
//*交换空格位置
            picCell=puzzlePic.getCell(spaceColumn-1,spaceRow);
            puzzlePic.setCell(spaceColumn,spaceRow,picCell);
            puzzlePic.setCell(spaceColumn
-1,spaceRow,0);
            
//*绘图
            render();
            doStageClearCheck();
        }

        

    }

   
//*游戏中按下右键
    private void doLeft()
    
{
        
boolean canChanged=true;
        
for(int i=0;i<4;i++)
        
{
            
//*判断最空格是否在最右边
            if(    puzzlePic.getCell(3,i)==0)
                
{
                    canChanged
=false;
                    
break;
                }

        }
        
        
if(canChanged==true)
        
{
            
//*可以移动方块
            int spaceColumn,spaceRow,picCell;
            spaceColumn
=spaceRow=0;
            
for(int i=0;i<puzzlePic.getColumns();i++)
                
for(int j=0;j<puzzlePic.getRows();j++)
                
{
                    
if(puzzlePic.getCell(i,j)==0)
                    
{
                        spaceColumn
=i;
                        spaceRow
=j;
                        
break;
                    }

                }

            
//*交换空格位置
            picCell=puzzlePic.getCell(spaceColumn+1,spaceRow);
            puzzlePic.setCell(spaceColumn,spaceRow,picCell);
            puzzlePic.setCell(spaceColumn
+1,spaceRow,0);
            
//*绘图
            render();
            doStageClearCheck();
        }

        

    }

   
//*游戏中按下上键
    private void doDown()
    
{
        
boolean canChanged=true;
        
for(int i=0;i<4;i++)
        
{
            
//*判断最空格是否在最上边
            if(    puzzlePic.getCell(i,0)==0)
                
{
                    canChanged
=false;
                    
break;
                }

        }
        
        
if(canChanged==true)
        
{
            
//*可以移动方块
            int spaceColumn,spaceRow,picCell;
            spaceColumn
=spaceRow=0;
            
for(int i=0;i<puzzlePic.getColumns();i++)
                
for(int j=0;j<puzzlePic.getRows();j++)
                
{
                    
if(puzzlePic.getCell(i,j)==0)
                    
{
                        spaceColumn
=i;
                        spaceRow
=j;
                        
break;
                    }

                }

            
//*交换空格位置
            picCell=puzzlePic.getCell(spaceColumn,spaceRow-1);
            puzzlePic.setCell(spaceColumn,spaceRow,picCell);
            puzzlePic.setCell(spaceColumn,spaceRow
-1,0);
            
//*绘图
            render();
            doStageClearCheck();
        }

        

    }

   
//*游戏中按下下键
    private void doUp()
    
{
        
boolean canChanged=true;
        
for(int i=0;i<4;i++)
        
{
            
//*判断最空格是否在最下边
            if(    puzzlePic.getCell(i,3)==0)
                
{
                    canChanged
=false;
                    
break;
                }

        }
            
        
if(canChanged==true)
        
{
            
//*可以移动方块
            int spaceColumn,spaceRow,picCell;
            spaceColumn
=spaceRow=0;
            
for(int i=0;i<puzzlePic.getColumns();i++)
                
for(int j=0;j<puzzlePic.getRows();j++)
                
{
                    
if(puzzlePic.getCell(i,j)==0)
                    
{
                        spaceColumn
=i;
                        spaceRow
=j;
                        
break;
                    }

                }

            
//*交换空格位置
            picCell=puzzlePic.getCell(spaceColumn,spaceRow+1);
            puzzlePic.setCell(spaceColumn,spaceRow,picCell);
            puzzlePic.setCell(spaceColumn,spaceRow
+1,0);
            
//*绘图
            render();
            doStageClearCheck();
        }

    }

   
    
//*按下键盘时;
    protected void keyPressed(int keyCode)
    
{
    
        
if(gameState==STAGE_TITLE )
        
{
            gameState
=STAGE_START;
            render();
        }

        
else if(gameState==STAGE_PLAYING&&isGamePaused!=true)
        
{

            
switch(getGameAction(keyCode))
            
{
                
case Canvas.FIRE:
                    gameState
=STAGE_PLAYING_SEE_PERFECT_PIC;
                    render();
                    
break;
                
case Canvas.DOWN:
                    doDown();
                    
break;
                
case Canvas.UP:
                    doUp();
                    
break;
                
case Canvas.LEFT:
                    doLeft();
                    
break;
                
case Canvas.RIGHT:
                    doRight();
                    
break;                
            }

        }

        
else if(gameState==STAGE_CLEAR)
        
{
            gameState
=STAGE_TITLE;
            render();
        }

        
else if(gameState==ALL_STAGE_CLEAR)
        
{
            gameState
=GAME_END;
            render();
        }


    }

    
    
//*松开键盘时
    protected void keyReleased(int keyCode)
    
{
        
if(gameState==STAGE_PLAYING_SEE_PERFECT_PIC&&isGamePaused!=true&&getGameAction(keyCode)==Canvas.FIRE)
        
{
            gameState
=STAGE_PLAYING;
            render();
        }


    }
    

    
//*时间转换函数
    private String numToTime(long timeLong)
    
{
        
int hour = (int)(timeLong / 3600);
        
int minute = (int)((timeLong - (hour * 3600)) / 60);
        String minuteSt 
= String.valueOf(minute);
        minuteSt 
= (minuteSt.length() < 2 ? "0"+minuteSt : minuteSt);//加上0变成两位数
        int sec = (int)(timeLong - (hour * 3600- (minute * 60));
        String secSt 
= String.valueOf(sec);
        secSt 
= (secSt.length() < 2 ? "0"+secSt : secSt);//加上0变成两位数傞
        String timeString = String.valueOf(hour) + ":" + minuteSt + ":" + secSt;
        
return timeString;        
    }


    
//*计时器
    public class TimeCounterTask extends TimerTask
    
{
        
public String timeString;
        
private long timeLong;
        
public TimeCounterTask(long time)
        
{
            timeLong
=time;
            changeTime();
        }

        
public void run()
        
{
            changeTime();
            render();
//重新绘图
        }

        
private void changeTime()
        
{
            timeLong
=timeLong-1;//倒计时
            if (timeLong==0)
            
{
                gameState
=STAGE_OVER; //时间到
            }

            
else
            
{
                timeString
=numToTime(timeLong);
                
//把剩余时间储存到类外面变量(因为类的声明在doStartPainting()函数中,在函数外无法调用所建立的对象)
                lastTime=timeLong;
            }

        }

    }

}

 
//------------------------------
//CrazyPuzzleMIDlet.java
//------------------------------

package src;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class CrazyPuzzleMIDlet extends MIDlet {

    
private Display display;
    
public CrazyPuzzleMIDlet()
    
{
        
super();
        display
=Display.getDisplay(this);
        WelcomeCanvas welcomeCanvas
=new WelcomeCanvas(display,this);
        display.setCurrent(welcomeCanvas);
    }

    
protected void startApp() throws MIDletStateChangeException {
        
//norains: TODO Auto-generated method stub        
    }


    
protected void pauseApp() {
        
// TODO Auto-generated method stub

    }


    
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        
// TODO Auto-generated method stub

    }


}


//------------------------------
//ListChoiceScreen.java
//------------------------------

package src;
import javax.microedition.lcdui.*;
public class ListChoiceScreen extends List implements CommandListener
{    
    
private Display display;
    
private CrazyPuzzleMIDlet mainMIDlet;
    
//传入TestLatencyMIDlet主要是为了能进行退出程序的操作.
    public ListChoiceScreen(Display display,CrazyPuzzleMIDlet m)
    
{
        
super("疯狂拼图",Choice.IMPLICIT);    
        mainMIDlet
=m;
        
this.display=display;        
        Image img1,img2,img3,img4;
        img1
=img2=img3=img4=null;
        
try
        
{
            img1
=Image.createImage("/res/lis1.png");
            img2
=Image.createImage("/res/lis2.png");
            img3
=Image.createImage("/res/lis3.png");
            img4
=Image.createImage("/res/lis4.png");
        }

        
catch(Exception erro)
        
{}
        
this.append("普通模式",img1);
        
this.append("极限模式",img2);
        
//this.append("高手排行",img2);
        this.append("游戏说明",img3);
        
this.append("退出游戏",img4);    
        
this.setCommandListener(this);
    }

    
    
public void commandAction(Command c,Displayable s)
    
{     
        
if (c==List.SELECT_COMMAND)
        
{
            List tmp
=(List) s;
            
int selected=tmp.getSelectedIndex();
            String list
=tmp.getString(selected);
            
if(list.equals("普通模式"))
            
{
                MyGameCanvas myGameCanvas
=new MyGameCanvas(display,mainMIDlet,MyGameCanvas.MODE_NORMAL);
                display.setCurrent(myGameCanvas);
            }

            
else if (list.equals("极限模式"))
            
{
                MyGameCanvas myGameCanvas
=new MyGameCanvas(display,mainMIDlet,MyGameCanvas.MODE_UTMOST);
                display.setCurrent(myGameCanvas);
            }

            
else if (list.equals("高手排行"))
            
{

            }

            
else if (list.equals("游戏说明"))
            
{
                InfoScreen infoScreen
=new InfoScreen(display,this);
                display.setCurrent(infoScreen);
            }

            
else if (list.equals("退出游戏"))
            
{
                mainMIDlet.notifyDestroyed();
            }

        }

    }

}


//------------------------------
//RecordScreen.java
//------------------------------

package src;

public class RecordScreen {

}


//------------------------------
//InfoScreen.java
//------------------------------

package src;
import javax.microedition.lcdui.*;

public class InfoScreen extends Form implements CommandListener 
{
    Displayable nextDisp;
    Display display;
    
public InfoScreen(Display display, Displayable nextDisp)
    
{
        
super("");
        
this.nextDisp=nextDisp;
        
this.display=display;        
        
this.append("游戏:疯狂拼图 "+"版本:1.0 "+"型号:泡泡堂 "+"作者:norains "+"联系:norains@163.com "
                
+"说明:游戏有两种模式选择,普通模式每关都重新计时,极限模式计算时间完毕前通过的关数.在游戏中按住5键可以切换到完整图案.");
        
this.addCommand(new Command("返回",Command.BACK,0));
        
this.setCommandListener(this);
    }

    
public void commandAction(Command c,Displayable s)
    
{
        String cmd
=c.getLabel();
        
if (cmd.equalsIgnoreCase("返回"))
        
{
            display.setCurrent(nextDisp);
        }

    }

}


//------------------------------
//RMSUtil.java
//------------------------------

package src;
import javax.microedition.rms.*;
//数据库操作公共类
public class RMSUtil 
{
    
public static RecordStore openRSAnyway(String rsname)
    
{
        RecordStore rs
=null;
        
if(rsname.length()>32)
            
{
                
return null;
            }

        
else
        
{
            
try
            
{
                rs
=RecordStore.openRecordStore(rsname,true);
                
return rs;
            }

            
catch (Exception erro)
            
{
                
return null;
            }

        }

    }

    
    
    
public static RecordStore openRSExisted(String rsname)
    
{
        RecordStore rs
=null;
        
if (rsname.length()>32)
        
{
            
return null;
        }

        
else
        
{
            
try
            
{
                rs
=RecordStore.openRecordStore(rsname,false);
                
return rs;
            }

            
catch(Exception erro)
            
{
                
return null;
            }

        }

    }

    
    
    
public static boolean deletRS(String rsname)
    
{
        
if(rsname.length()>32)
        
{
            
return false;
        }

        
else
        
{
            
try
            
{
                RecordStore.deleteRecordStore(rsname);
                
return true;
            }

            
catch(Exception erro)
            
{
                
return false;
            }

        }

    }

    
    
}




//------------------------------
//WelcomeCanvas.java
//------------------------------

package src;
import java.io.IOException;
import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.media.*;

import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
public class WelcomeCanvas extends Canvas
{
    
private Display display;
    
private CrazyPuzzleMIDlet mainMIDlet;
    
private Image img;
    
private int dispWidth;
    
private int dispHeight;
    
private int imgState;
    
private Player player;
    
    
//**构造函数
    public WelcomeCanvas(Display disp,CrazyPuzzleMIDlet midlet)
    
{
        
this.display=disp;
        
this.mainMIDlet=midlet;
        
this.setFullScreenMode(true);
        dispWidth
=this.getWidth();
        dispHeight
=this.getHeight();
        imgState
=1;
        
        
//播放开头音乐
        try
        
{
            InputStream in ;
            in
= getClass().getResourceAsStream("/res/welcome.mid");
            player 
= Manager.createPlayer(in,"audio/midi");
            player.setLoopCount(
1);
            player.start();
        }

        
catch(IOException e){}
        
catch(MediaException e){}
        
    }


    
    
// **角色创建函数,以整张图
    private Image creatImage(String fileName)
    
{
        Image img
=null;
        
try
        
{
            img
=Image.createImage(fileName);            
        }

        
catch (Exception exp)
        
{
            System.out.println(exp);
        }

        
return img;
    }

    
    
//**绘图
    public void paint(Graphics g)
    
{
        
        
switch(imgState)
        
{
            
case 1:
                img
=creatImage("/res/flash1.png");
                g.setColor(
1,16,57);
                
break;
            
case 2:
                img
=creatImage("/res/flash2.png");
                
break;
            
case 3:
                img
=creatImage("/res/flash3.png");
                g.setColor(
255,255,255);
                
break;
        }


            g.fillRect(
0,0,dispWidth,dispHeight);
            g.drawImage(img,(dispWidth
-img.getWidth())/2,(dispHeight-img.getHeight())/2,0);
    }
 

    
    
protected void keyPressed(int keycode)
    
{
        
//conti=false;
        if(imgState!=3)
        
{
            imgState
++;
            repaint();
        }

        
else
        
{
            ListChoiceScreen listChoiceScreen
=new ListChoiceScreen(display,mainMIDlet);
            display.setCurrent(listChoiceScreen);
            
//停止播放音乐
            try
            
{
                
if(player!=null)
                
{
                    player.stop();
                }

            }

            
catch(Exception erro)
            
{}
            
        }

        
    }

}

posted @ 2007-01-30 23:28  我的一天  阅读(507)  评论(0编辑  收藏  举报