TestLatency反应测试游戏完整源代码

    一个非常简单的测试个人反应的游戏源代码
//-----------------------------
//GameScreen.java
//-----------------------------
package src;
import javax.microedition.lcdui.*;

import java.util.*;
public class GameScreen extends Canvas implements CommandListener
{
    Display display;
    
private long startTime;
    
private long endTime;
    
private long resultTime;
    
private boolean changed;
    
private boolean paintFlag;
    
private Font font;
    
private int left;
    
private int top;
    
private Timer timer;
    
private Random rand;
    
private TestLatencyMIDlet MIDlet;
    
    
public void paint(Graphics g)
    
{        
        font
=font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE);        
        left
=(getWidth()-font.stringWidth("准备.."))/2;
        top
=(getHeight()-font.getHeight())/2;
        
if (paintFlag==true)
        
{
            g.setColor(
0,0,0);
            g.fillRect(
0,0,this.getWidth(),this.getHeight());
            g.setColor(
0,255,255);
            g.drawString(
"准备..",left,top,Graphics.LEFT|Graphics.TOP);
            paintFlag
=false;
            changed
=false;
        }

        
else
        
{
            g.setColor(
0,0,255);
            g.fillRect(
0,0,this.getWidth(),this.getHeight());
            g.setColor(
0,255,0);
            g.drawString(
"按!!",left,top,Graphics.LEFT|Graphics.TOP);
            paintFlag
=true;
            changed
=true;
            startTime
=System.currentTimeMillis();
        }

    }

    
    
public GameScreen(Display display,TestLatencyMIDlet m)
    
{
        MIDlet
=m;
        paintFlag
=true;
        changed
=false;
        
this.display=display;
        
this.setFullScreenMode(true);    
        startGame();
    }

    
    
public void startGame()
    
{        
        rand
=new Random();
        timer
=new Timer();
        changeTask st
=new changeTask();
        
long t=rand.nextInt();        
        
if (t<0)
        
{    t=-1L*t;}
        timer.schedule(st,(t 
* 2000L/ 1000000000L);
    }

    
    
public class changeTask extends TimerTask
    
{
        
public void run()
        
{
            repaint();
            cancel();
        }

    }

    
    
protected void keyPressed(int keycode)
    
{
        ResultScreen resultScreen;
        endTime
=System.currentTimeMillis();
        resultTime
=endTime-startTime;
        
if(changed==true)
        
{
            resultScreen
=new ResultScreen(display,true,resultTime,MIDlet);
            display.setCurrent(resultScreen);
        }

        
else
        
{
            resultScreen
=new ResultScreen(display,false,resultTime,MIDlet);
            display.setCurrent(resultScreen);
        }

    }

    
    
public void commandAction(Command c ,Displayable disp)
    
{}
}


 
//-----------------------------
//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 "+"说明:游戏玩法相当简单,当画面变化的时候,请你以最快的速度按键");
        
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);
        }

    }

}



//-----------------------------
//ListScreen.java
//-----------------------------
package src;
import javax.microedition.lcdui.*;
public class ListScreen extends List implements CommandListener
{    
    
private Display display;
    
private TestLatencyMIDlet MIDlet;
    
//传入TestLatencyMIDlet主要是为了能进行退出程序的操作.
    public ListScreen(Display display,TestLatencyMIDlet m)
    
{
        
super("",Choice.IMPLICIT);    
        MIDlet
=m;
        
this.display=display;        
        Image img1,img2,img3,img4;
        img1
=img2=img3=img4=null;
        
try
        
{
            img1
=Image.createImage("/res/1.PNG");
            img2
=Image.createImage("/res/2.PNG");
            img3
=Image.createImage("/res/3.PNG");
            img4
=Image.createImage("/res/4.PNG");
        }

        
catch(Exception erro)
        
{}
        
this.append("开始游戏",img1);
        
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("开始游戏"))
            
{
                GameScreen gameScreen
=new GameScreen(display,MIDlet);
                display.setCurrent(gameScreen);
            }

            
else if (list.equals("高手排行"))
            
{
                RecordScreen recordScreen
=new RecordScreen(display,this);
                display.setCurrent(recordScreen);
            }

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

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

        }

    }

}


//-----------------------------
//RecordScreen.java
//-----------------------------
package src;

import javax.microedition.lcdui.*;
import javax.microedition.rms.*;


public class RecordScreen extends Form implements CommandListener 
{
    
private Display display;
    
private Displayable nextDisp;
    
private String appraiseText;
    
    
public RecordScreen(Display display, Displayable nextDisp)
    
{
        
super("高手排行");
        
this.display=display;
        
this.nextDisp=nextDisp;
        
this.addCommand(new Command("返回",Command.BACK,0));
        
this.setCommandListener(this);

        String dbname
="resultDataBase";
        RecordStore rs
=RMSUtil.openRSAnyway(dbname);
        
if(rs==null)
        
{
            
this.append("打开数据库失败!可能尚未储存数据,请进行游戏后再尝试.如果依然出现错误,请与我联系:norains@163.com");
        }

        
else
        
{
            
int id;
            
try
            
{
                
//测试开始
                
//ResultDataEntity test1=new ResultDataEntity("norains",1000);
                
//byte[]tmp=test1.encode();
                
//int id1=rs.addRecord(tmp,0,tmp.length);
                
//rs.deleteRecord(id1);
                
//id=rs.addRecord(tmp,0,tmp.length);
                
//测试结束
                
                ResultDataEntity resultRecord
=new ResultDataEntity();
                RecordEnumeration re
=rs.enumerateRecords(null,null,false);
                id
=re.nextRecordId();                
                resultRecord.decode(rs.getRecord(id));
                buildAppraise(resultRecord.name,resultRecord.resultTime);
                
this.append("榜上有名的高手是:"+resultRecord.name+"!他的反应时间是"+resultRecord.resultTime+"毫秒! "+appraiseText);
                rs.closeRecordStore();
                
//RMSUtil.deletRS(dbname);
            }

            
catch(Exception erro)
            
{}
            
        }
                    
    }

    
public void buildAppraise(String name,long time)
    
{
        
if(time<200)
            appraiseText
="这完全是一个不可思议的记录!"+name+"居然做到了!";
        
else if(time<250)
            appraiseText
="非常高兴"+name+"能创造出一个如此完美的成绩,估计这记录已经很难打破了吧?!";
        
else if(time<300)
            appraiseText
="很高兴"+name+"能打出一个很不错的记录!不知道还有没有人可以更上一层楼呢?";
        
else if(time<400)
            appraiseText
=name+"的这个记录还不赖嘛!";
        
else if(time<500)
            appraiseText
=name+"的这个记录嘛,一般般啦~";
        
else
            appraiseText
="不是吧?这都能打进高手榜啊?我的天啊~!难道后继无人了么?";
    }

    
public void commandAction(Command c, Displayable disp) 
    
{
        
// TODO 自动生成方法存根
        String cmd=c.getLabel();
        
if (cmd.equalsIgnoreCase("返回"))
        
{
            display.setCurrent(nextDisp);
        }

    }


}


//-----------------------------
//ResultDataEntity.java
//-----------------------------
package src;
import java.io.*;
public class ResultDataEntity 
{
    String name;
    
long resultTime;
    
    
public ResultDataEntity(String name,long resultTime)
    
{
        
this.name=name;
        
this.resultTime=resultTime;
    }

    
    
public ResultDataEntity()
    
{}
    
    
public byte[] encode()
    
{
        
byte[] bytResult=null;
        
try
        
{
            ByteArrayOutputStream bos
=new ByteArrayOutputStream();
            DataOutputStream dos
=new DataOutputStream(bos);
            dos.writeUTF(name);
            dos.writeLong(resultTime);
            bytResult
=bos.toByteArray();
            dos.close();
            bos.close();
        }

        
catch(Exception erro)
        
{}
        
return bytResult;
    }

    
    
public void decode(byte[] data)
    
{
        
try
        
{
            ByteArrayInputStream bis
=new ByteArrayInputStream(data);
            DataInputStream dis
=new DataInputStream(bis);
            name
=dis.readUTF();
            resultTime
=dis.readLong();
            dis.close();
            bis.close();
        }

        
catch(Exception erro)
        
{}
    }

    
}


//-----------------------------
//ResultScreen.java
//-----------------------------
package src;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

public class ResultScreen extends Form implements CommandListener
{
    
private Display display;
    
private String strText;
    
private long resultTime;
    
private boolean breakRecord;
    
private long recordTime;
    
private String newName; 
    
private TextField nameField;
    
private TestLatencyMIDlet MIDlet;
    
    
public ResultScreen(Display display, boolean flag,long resultTime,TestLatencyMIDlet m)
    
{        
        
super("测试结果");
        MIDlet
=m;
        
this.display=display;
        
this.resultTime=resultTime;
        testResult();
        
if (flag==true)
        
{
            
if(breakRecord==true)
            
{
                
this.append(strText+" 您的反应时间是:"+resultTime+"毫秒 请在下面方框输入您的大名:");
                nameField
=new TextField("姓名","",8,TextField.ANY);
                
this.append(nameField);
                
this.addCommand(new Command("确定",Command.OK,0));
            }

            
else
            
{
                
this.append(strText+" 您的反应时间是:"+Long.toString(resultTime)+"毫秒");
                
this.addCommand(new Command("再来一次",Command.OK,0));
                
this.addCommand(new Command("返回",Command.STOP,1));
            }

        }

        
else
        
{
            
this.append("您也太急了吧?这把不算,犯规!");
            
this.addCommand(new Command("再来一次",Command.OK,0));
            
this.addCommand(new Command("返回",Command.STOP,1));
        }

        
this.setCommandListener(this);
    }

    
    
    
public void testResult()
    
{
        testRecord();
        
if(breakRecord==false)
        
{
            
if (resultTime<50L)
                strText 
= "晕!您今天买彩票肯定能得大奖!";
            
else if(resultTime < 100L)
                strText 
= "老大,这样都能给你蒙中啊?我无语...";
            
else if(resultTime < 200L)
                strText 
="太厉害了!这个级别没多少人能达到哦!";
            
else if(resultTime< 300L)
                strText 
= "看不出来你还挺强的嘛,继续努力哦!";
            
else if(resultTime < 400L)
                strText 
="还不错啦,只是成绩依然不太理想,继续再来一把吧?";
            
else if(resultTime < 500L)
                strText 
="成绩好像不太好哦,我也只能为你默默加油了...";
            
else
                strText 
="老大,你是不是睡着了?喂~~该起来了喔~";
        }

    }

    
    
public void testRecord()
    
{
        String dbname
="resultDataBase";
        RecordStore rs
=RMSUtil.openRSAnyway(dbname);
        
if(rs==null)
        
{
            
this.append("打开数据库失败!请与我联系:norains@163.com,谢谢!");
        }

        
else
        
{            
            
int recordID;
            
try
            
{                
                ResultDataEntity resultRecord
=new ResultDataEntity();
                RecordEnumeration re
=rs.enumerateRecords(null,null,false);
                
if(re.numRecords()==0//不存在记录
                {
                    breakRecord
=true;
                    strText
="你是第一个玩这个游戏的人哦,所以这是空前的记录啦!";
                }

                
else
                
{
                    recordID
=re.nextRecordId();                
                    resultRecord.decode(rs.getRecord(recordID));
                    recordTime
=resultRecord.resultTime;
                    
if(recordTime>resultTime)
                    
{
                        breakRecord
=true;
                        strText
="恭喜你打破"+resultRecord.name+"保持的记录!";
                    }

                    
else
                    
{
                        breakRecord
=false;
                    }

                }

                rs.closeRecordStore();                
            }

            
catch(Exception erro)
            
{}
        }


    }

    
    
public void updataRecord()
    
{
        newName
=nameField.getString();
        String dbname
="resultDataBase";
        RecordStore rs
=RMSUtil.openRSAnyway(dbname);
        
if(rs==null)
        
{
            
this.append("打开数据库失败!请与我联系:norains@163.com,谢谢!");
        }

        
else
        
{            
            
int recordID;
            
try
            
{                
                RecordEnumeration re
=rs.enumerateRecords(null,null,false);
                
if(re.numRecords()==0//不存在记录
                {
                    ResultDataEntity recordData
=new ResultDataEntity(newName,resultTime);
                    
byte[]tmp=recordData.encode();
                    rs.addRecord(tmp,
0,tmp.length);
                }

                
else
                
{
                    recordID
=re.nextRecordId();            
                    ResultDataEntity recordData
=new ResultDataEntity(newName,resultTime);
                    rs.deleteRecord(recordID);
                    
byte[]tmp=recordData.encode();
                    rs.addRecord(tmp,
0,tmp.length);
                }

                rs.closeRecordStore();                
            }

            
catch(Exception erro)
            
{}
        }

    }

    
    
public void commandAction(Command c,Displayable disp)
    
{
        String cmd
=c.getLabel();
        
if(cmd=="再来一次")
        
{
            GameScreen gameScreen
=new GameScreen(display,MIDlet);
            display.setCurrent(gameScreen);
        }

        
else if(cmd=="返回")
        
{
            ListScreen listScreen
=new ListScreen(display,MIDlet);
            display.setCurrent(listScreen);
        }

        
else if(cmd=="确定")
        
{
            updataRecord();
            ListScreen listScreen
=new ListScreen(display,MIDlet);
            display.setCurrent(listScreen);
        }

    }

}


//-----------------------------
//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 null;
            }

            
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;
            }

        }

    }

    
    
}









//-----------------------------
//TestLatencyMIDlet.java
//-----------------------------
package src;

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

public class TestLatencyMIDlet extends MIDlet {

    
private Display display;
    
public TestLatencyMIDlet() {
        super();
        
// TODO 自动生成构造函数存根        
        display=Display.getDisplay(this);
        Alert flashForm
=new Alert("");
        flashForm.setString(
"norains作品");
        flashForm.setType(AlertType.INFO);
        flashForm.setTimeout(
5000);
        ListScreen listScreen
=new ListScreen(display,this);        
        WelcomeCanvas welcomeCanvas
=new WelcomeCanvas(display,listScreen,this);
        display.setCurrent(welcomeCanvas);
    }


    
public void exitProgram()
    
{
        
try 
        
{
            destroyApp(
false);
            notify();
        }

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

    }


    
protected void startApp() throws MIDletStateChangeException {
        
// TODO 自动生成方法存根

    }


    
protected void pauseApp() {
        
// TODO 自动生成方法存根

    }


    
protected void destroyApp(boolean arg0) //throws MIDletStateChangeException 
    {
        
// TODO 自动生成方法存根

    }


}


//-----------------------------
//WelcomeCanvas.java
//-----------------------------
package src;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.util.*;
public class WelcomeCanvas extends Canvas 
{
    
private static final String strText="测 测 看";
    
private int i=0;
    
private Display display;
    
private Displayable next;
    
private int dispWidth;
    
private int dispHeight;
    
private Font font;
    
private int left;
    
private boolean blnGoing=true;
    
private int fontHeight;
    
private boolean blnFlag=true;
    
private Timer timer=new Timer();    
    
    
private TestLatencyMIDlet MIDlet;
    
//传入MIDlet,方便退出程序
    public WelcomeCanvas(Display display, Displayable disp,TestLatencyMIDlet m)
    
{    
        MIDlet
=m;
        
this.display=display;
        
this.next=disp;
        
this.setFullScreenMode(true);
        dispWidth
=getWidth();
        dispHeight
=getHeight();
        font
=font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE);
        left
=(dispWidth-font.stringWidth(strText))/2;
        fontHeight
=font.getHeight();
        ScrollTask st
=new ScrollTask();        
        timer.schedule(st,
100,100);
    }

    

    
    
public void paint(Graphics arg)
    
{
        
if (blnGoing==true)
        
{
            arg.setColor(
0,0,0);
            arg.fillRect(
0,0,dispWidth,dispHeight);
            arg.setColor(
0,255,0);
            arg.setFont(font);
            arg.drawString(strText,left,i,Graphics.LEFT
|Graphics.TOP);        }

        
else
        
{            
            arg.setColor(
0,0,0);
            arg.fillRect(
0,0,dispWidth,dispHeight);
            arg.setColor(
0,255,0);
            font
=font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE);
            arg.setFont(font);
            arg.drawString(strText,left,i,Graphics.LEFT
|Graphics.TOP);
            
if (blnFlag==true//令字体闪烁
            {
                arg.setColor(
0,255,255);
                blnFlag
=false;
            }

            
else
            
{
                arg.setColor(
0,255,0);
                blnFlag
=true;
            }

            font
=font.getFont(Font.FACE_SYSTEM,Font.STYLE_ITALIC,Font.SIZE_SMALL);
            arg.setFont(font);
            arg.drawString(
"Press any key..",left-5,i+fontHeight,Graphics.LEFT|Graphics.TOP);            
        }

    }

    
    
public void keyPressed(int keyCode)
    
{
        
if(blnGoing==false)
        
{    
            timer.cancel();
            display.setCurrent(next);
        }

    }

    
    
private class ScrollTask extends TimerTask
    
{
        
public void run()
        
{
            
            
if (i < dispWidth / 2-fontHeight/2)            
            
{
                i
=i+fontHeight/3;                
                repaint();

            }

            
else
            
{
                blnGoing
=false;    
                repaint();
            }


        }

    }

}

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