想编一个简单的手机游戏,但自己无从下手,请高手帮帮忙.具体是:系统随机产生一个范围的整数(假设0-50),玩家输入数字猜,系统会给出提示"大了"\"小了",知道猜对为止.谢谢,能提供下主要框架就行,有代码最好.
參考答案:说那么多有什么用,反正不复杂,完全就用JME,WTK就好了.我直接给你代码好了,有什么不懂的看API就OK了.
我完全是按照LZ的要求写的啊,不选我就不厚道了哈~ ^_^
import java.util.Random;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class Guest extends MIDlet implements CommandListener{
Form form;
Command commit,newGame;
TextField tf;
Display display;
Random rd;
int randomNumber;
boolean flag;
public Guest(){
display=Display.getDisplay(this);
form=new Form("猜数字");
tf=new TextField("输入0-50之间的整数","",25,TextField.NUMERIC);
commit =new Command("提交",Command.BACK,1);
newGame=new Command("重新开始",Command.BACK,1);
rd=new Random();
flag=true;
}
protected void startApp() throws MIDletStateChangeException {
init();
}
protected void pauseApp() {
}
protected void destroyApp(boolean arg0)
throws MIDletStateChangeException {
}
private void init(){
randomNumber=Math.abs(rd.nextInt())%50;
if(flag)
form.append(tf);
form.addCommand(commit);
form.addCommand(newGame);
form.setCommandListener(this);
display.setCurrent(form);
flag=false;
}
public void commandAction(Command arg0,Displayable arg1){
StringItem temp=new StringItem("","");
String input=tf.getString();
if(arg0==commit&&!"".equals(input)){
if(Integer.parseInt(input)>randomNumber){
temp.setText("大了");
}
if(Integer.parseInt(input)<randomNumber){
temp.setText("小了");
}
if(Integer.parseInt(input)==randomNumber){
temp.setText("OK了");
}
deleteAllItem();
form.append(temp);
tf.setString("");
}else if(arg0==newGame){
deleteAllItem();
init();
}
}
public void deleteAllItem(){
for(int i=0;i<form.size();i++){
Item item=form.get(i);
if(item instanceof StringItem){
form.delete(i);
}
}
}
}