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

一款简易的反应速度测试游戏源代码分享,包含游戏界面、记录管理等功能。玩家需在画面变化时尽快按键,系统将记录并展示最佳成绩。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

importjava.util.*;
publicclassGameScreenextendsCanvasimplementsCommandListener
...{
Displaydisplay;
privatelongstartTime;
privatelongendTime;
privatelongresultTime;
privatebooleanchanged;
privatebooleanpaintFlag;
privateFontfont;
privateintleft;
privateinttop;
privateTimertimer;
privateRandomrand;
privateTestLatencyMIDletMIDlet;

publicvoidpaint(Graphicsg)
...{
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();
}

}


publicGameScreen(Displaydisplay,TestLatencyMIDletm)
...{
MIDlet
=m;
paintFlag
=true;
changed
=false;
this.display=display;
this.setFullScreenMode(true);
startGame();
}


publicvoidstartGame()
...{
rand
=newRandom();
timer
=newTimer();
changeTaskst
=newchangeTask();
longt=rand.nextInt();
if(t<0)
...{t=-1L*t;}
timer.schedule(st,(t
*2000L)/1000000000L);
}


publicclasschangeTaskextendsTimerTask
...{
publicvoidrun()
...{
repaint();
cancel();
}

}


protectedvoidkeyPressed(intkeycode)
...{
ResultScreenresultScreen;
endTime
=System.currentTimeMillis();
resultTime
=endTime-startTime;
if(changed==true)
...{
resultScreen
=newResultScreen(display,true,resultTime,MIDlet);
display.setCurrent(resultScreen);
}

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

}


publicvoidcommandAction(Commandc,Displayabledisp)
...{}
}


//-----------------------------
//InfoScreen.java
//-----------------------------
packagesrc;
importjavax.microedition.lcdui.*;

publicclassInfoScreenextendsFormimplementsCommandListener
...{
DisplayablenextDisp;
Displaydisplay;
publicInfoScreen(Displaydisplay,DisplayablenextDisp)
...{
super("");
this.nextDisp=nextDisp;
this.display=display;
this.append("版本:1.0 "+"作者:norains "+"联系:norains@163.com "+"说明:游戏玩法相当简单,当画面变化的时候,请你以最快的速度按键");
this.addCommand(newCommand("返回",Command.BACK,0));
this.setCommandListener(this);
}

publicvoidcommandAction(Commandc,Displayables)
...{
Stringcmd
=c.getLabel();
if(cmd.equalsIgnoreCase("返回"))
...{
display.setCurrent(nextDisp);
}

}

}



//-----------------------------
//ListScreen.java
//-----------------------------
packagesrc;
importjavax.microedition.lcdui.*;
publicclassListScreenextendsListimplementsCommandListener
...{
privateDisplaydisplay;
privateTestLatencyMIDletMIDlet;
//传入TestLatencyMIDlet主要是为了能进行退出程序的操作.
publicListScreen(Displaydisplay,TestLatencyMIDletm)
...{
super("",Choice.IMPLICIT);
MIDlet
=m;
this.display=display;
Imageimg1,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(Exceptionerro)
...{}
this.append("开始游戏",img1);
this.append("高手排行",img2);
this.append("游戏说明",img3);
this.append("退出游戏",img4);
this.setCommandListener(this);
}


publicvoidcommandAction(Commandc,Displayables)
...{
if(c==List.SELECT_COMMAND)
...{
Listtmp
=(List)s;
intselected=tmp.getSelectedIndex();
Stringlist
=tmp.getString(selected);
if(list.equals("开始游戏"))
...{
GameScreengameScreen
=newGameScreen(display,MIDlet);
display.setCurrent(gameScreen);
}

elseif(list.equals("高手排行"))
...{
RecordScreenrecordScreen
=newRecordScreen(display,this);
display.setCurrent(recordScreen);
}

elseif(list.equals("游戏说明"))
...{
InfoScreeninfoScreen
=newInfoScreen(display,this);
display.setCurrent(infoScreen);
}

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

}

}

}


//-----------------------------
//RecordScreen.java
//-----------------------------
packagesrc;

importjavax.microedition.lcdui.*;
importjavax.microedition.rms.*;


publicclassRecordScreenextendsFormimplementsCommandListener
...{
privateDisplaydisplay;
privateDisplayablenextDisp;
privateStringappraiseText;

publicRecordScreen(Displaydisplay,DisplayablenextDisp)
...{
super("高手排行");
this.display=display;
this.nextDisp=nextDisp;
this.addCommand(newCommand("返回",Command.BACK,0));
this.setCommandListener(this);

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

else
...{
intid;
try
...{
//测试开始
//ResultDataEntitytest1=newResultDataEntity("norains",1000);
//byte[]tmp=test1.encode();
//intid1=rs.addRecord(tmp,0,tmp.length);
//rs.deleteRecord(id1);
//id=rs.addRecord(tmp,0,tmp.length);
//测试结束

ResultDataEntityresultRecord
=newResultDataEntity();
RecordEnumerationre
=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(Exceptionerro)
...{}

}

}

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

publicvoidcommandAction(Commandc,Displayabledisp)
...{
//TODO自动生成方法存根
Stringcmd=c.getLabel();
if(cmd.equalsIgnoreCase("返回"))
...{
display.setCurrent(nextDisp);
}

}


}


//-----------------------------
//ResultDataEntity.java
//-----------------------------
packagesrc;
importjava.io.*;
publicclassResultDataEntity
...{
Stringname;
longresultTime;

publicResultDataEntity(Stringname,longresultTime)
...{
this.name=name;
this.resultTime=resultTime;
}


publicResultDataEntity()
...{}

publicbyte[]encode()
...{
byte[]bytResult=null;
try
...{
ByteArrayOutputStreambos
=newByteArrayOutputStream();
DataOutputStreamdos
=newDataOutputStream(bos);
dos.writeUTF(name);
dos.writeLong(resultTime);
bytResult
=bos.toByteArray();
dos.close();
bos.close();
}

catch(Exceptionerro)
...{}
returnbytResult;
}


publicvoiddecode(byte[]data)
...{
try
...{
ByteArrayInputStreambis
=newByteArrayInputStream(data);
DataInputStreamdis
=newDataInputStream(bis);
name
=dis.readUTF();
resultTime
=dis.readLong();
dis.close();
bis.close();
}

catch(Exceptionerro)
...{}
}


}


//-----------------------------
//ResultScreen.java
//-----------------------------
packagesrc;
importjavax.microedition.lcdui.*;
importjavax.microedition.rms.*;

publicclassResultScreenextendsFormimplementsCommandListener
...{
privateDisplaydisplay;
privateStringstrText;
privatelongresultTime;
privatebooleanbreakRecord;
privatelongrecordTime;
privateStringnewName;
privateTextFieldnameField;
privateTestLatencyMIDletMIDlet;

publicResultScreen(Displaydisplay,booleanflag,longresultTime,TestLatencyMIDletm)
...{
super("测试结果");
MIDlet
=m;
this.display=display;
this.resultTime=resultTime;
testResult();
if(flag==true)
...{
if(breakRecord==true)
...{
this.append(strText+" 您的反应时间是:"+resultTime+"毫秒 请在下面方框输入您的大名:");
nameField
=newTextField("姓名","",8,TextField.ANY);
this.append(nameField);
this.addCommand(newCommand("确定",Command.OK,0));
}

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

}

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

this.setCommandListener(this);
}



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

}


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

else
...{
intrecordID;
try
...{
ResultDataEntityresultRecord
=newResultDataEntity();
RecordEnumerationre
=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(Exceptionerro)
...{}
}


}


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

else
...{
intrecordID;
try
...{
RecordEnumerationre
=rs.enumerateRecords(null,null,false);
if(re.numRecords()==0)//不存在记录
...{
ResultDataEntityrecordData
=newResultDataEntity(newName,resultTime);
byte[]tmp=recordData.encode();
rs.addRecord(tmp,
0,tmp.length);
}

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

rs.closeRecordStore();
}

catch(Exceptionerro)
...{}
}

}


publicvoidcommandAction(Commandc,Displayabledisp)
...{
Stringcmd
=c.getLabel();
if(cmd=="再来一次")
...{
GameScreengameScreen
=newGameScreen(display,MIDlet);
display.setCurrent(gameScreen);
}

elseif(cmd=="返回")
...{
ListScreenlistScreen
=newListScreen(display,MIDlet);
display.setCurrent(listScreen);
}

elseif(cmd=="确定")
...{
updataRecord();
ListScreenlistScreen
=newListScreen(display,MIDlet);
display.setCurrent(listScreen);
}

}

}


//-----------------------------
//RMSUtil.java
//-----------------------------
packagesrc;
importjavax.microedition.rms.*;
//数据库操作公共类
publicclassRMSUtil
...{
publicstaticRecordStoreopenRSAnyway(Stringrsname)
...{
RecordStorers
=null;
if(rsname.length()>32)
...{
returnnull;
}

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

catch(Exceptionerro)
...{
returnnull;
}

}

}



publicstaticRecordStoreopenRSExisted(Stringrsname)
...{
RecordStorers
=null;
if(rsname.length()>32)
...{
returnnull;
}

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

catch(Exceptionerro)
...{
returnnull;
}

}

}



publicstaticbooleandeletRS(Stringrsname)
...{
if(rsname.length()>32)
...{
returnfalse;
}

else
...{
try
...{
RecordStore.deleteRecordStore(rsname);
returntrue;
}

catch(Exceptionerro)
...{
returnfalse;
}

}

}



}









//-----------------------------
//TestLatencyMIDlet.java
//-----------------------------
packagesrc;

importjavax.microedition.midlet.
*;
importjavax.microedition.lcdui.
*;
importjavax.microedition.midlet.MIDletStateChangeException;

publicclassTestLatencyMIDletextendsMIDlet...{

privateDisplaydisplay;
publicTestLatencyMIDlet()...{
super();
//TODO自动生成构造函数存根
display=Display.getDisplay(this);
AlertflashForm
=newAlert("");
flashForm.setString(
"norains作品");
flashForm.setType(AlertType.INFO);
flashForm.setTimeout(
5000);
ListScreenlistScreen
=newListScreen(display,this);
WelcomeCanvaswelcomeCanvas
=newWelcomeCanvas(display,listScreen,this);
display.setCurrent(welcomeCanvas);
}


publicvoidexitProgram()
...{
try
...{
destroyApp(
false);
notify();
}

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

}


protectedvoidstartApp()throwsMIDletStateChangeException...{
//TODO自动生成方法存根

}


protectedvoidpauseApp()...{
//TODO自动生成方法存根

}


protectedvoiddestroyApp(booleanarg0)//throwsMIDletStateChangeException
...{
//TODO自动生成方法存根

}


}


//-----------------------------
//WelcomeCanvas.java
//-----------------------------
packagesrc;
importjavax.microedition.lcdui.*;
importjavax.microedition.midlet.*;
importjava.util.*;
publicclassWelcomeCanvasextendsCanvas
...{
privatestaticfinalStringstrText="测测看";
privateinti=0;
privateDisplaydisplay;
privateDisplayablenext;
privateintdispWidth;
privateintdispHeight;
privateFontfont;
privateintleft;
privatebooleanblnGoing=true;
privateintfontHeight;
privatebooleanblnFlag=true;
privateTimertimer=newTimer();

privateTestLatencyMIDletMIDlet;
//传入MIDlet,方便退出程序
publicWelcomeCanvas(Displaydisplay,Displayabledisp,TestLatencyMIDletm)
...{
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();
ScrollTaskst
=newScrollTask();
timer.schedule(st,
100,100);
}




publicvoidpaint(Graphicsarg)
...{
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(
"Pressanykey..",left-5,i+fontHeight,Graphics.LEFT|Graphics.TOP);
}

}


publicvoidkeyPressed(intkeyCode)
...{
if(blnGoing==false)
...{
timer.cancel();
display.setCurrent(next);
}

}


privateclassScrollTaskextendsTimerTask
...{
publicvoidrun()
...{

if(i<dispWidth/2-fontHeight/2)
...{
i
=i+fontHeight/3;
repaint();

}

else
...{
blnGoing
=false;
repaint();
}


}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值