norains的专栏

只专注于WINCE开发

原创 TestLatency反应测试游戏完整源代码收藏

新一篇: 漫谈WinCE的手写识别技术(三) | 旧一篇: NotePad完整源代码

    一个非常简单的测试个人反应的游戏源代码
//-----------------------------
//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("返回"))