WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术程序开发基于Nokia S40的猜数字游戏之二

基于Nokia S40的猜数字游戏之二
作者:mingjava  来源:J2MEDV  发布时间:2005-9-7 12:03:10
现在我们已经有一能够接收用户输入事件的Button类了,下面我们应该考虑如何实现游戏中相关的逻辑,猜数字中的游戏逻辑都比较简单,主要是产生一个4位随机数字且不能重复,其次是根据输入返回给用户结果。我们提供一个Engine类来完成这个工作。
package com.j2medev.numbergame;

import java.util.Random;

public class Engine
{
private int[] answer = new int[4];

private Random random = new Random();

public void init()
{
int[] number = new int[10];
for (int i = 0; i < number.length; i++)
{
number[i] = i;
}

int n = 10;
for (int index = 0; index < answer.length; index++)
{
int r = Math.abs(random.nextInt() % n);
answer[index] = number[r];
number[r] = number[n - 1];
n--;
}
}

public int[] getAnswer()
{
return answer;
}

public int[] queryResult(int[] input)
{
int[] state = new int[2];
int a = 0;
int b = 0;
for (int i = 0; i < answer.length; i++)
{
for (int j = 0; j < answer.length; j++)
{

if ((input[j] ^ answer[i]) == 0)
{
if (i == j)
{
a++;
} else
{
b++;
}

}
}
}
state[0] = a;
state[1] = b;
return state;
}
}

游戏的运行中,我们有时候需要提示用户它的操作有误,比如输入数字为空,或者显示给用户已经答对了,一个庆祝的界面。当然这些你可以通过MIDP中提供的Alert来完成,我在这里实现了一个简单的界面类,这是一个抽象类,扩展了FullCanvas但是并没有实现paint()方法,把这个方法留给他的字类来实现。
/*
* Created on 2004-12-21
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.j2medev.numbergame;

import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;

import com.nokia.mid.ui.FullCanvas;

public abstract class NumScreen extends FullCanvas
{
protected Timer timer = new Timer();
protected Display display;
protected Manager next;
protected int timeout;
protected Font font;

public NumScreen(Display display,Manager next)
{
this.display = display;
this.next = next;

setTimeout(3000);
}

public Font getFont()
{
if(font == null)
{
font = Font.getDefaultFont();
}
return font;
}

public void setFont(Font font)
{
this.font = font;
}

public void setTimeout(int time)
{
this.timeout = time;
}


public void dismiss()
{
timer.cancel();
display.setCurrent(next);
}

public void showNotify()
{
timer.schedule(new TimerTask()
{
public void run()
{
dismiss();
}
},3000);
}

}
基于这个类,我们实现一个SplashScreen用在程序启动的时候的欢迎界面上,还提供一个类CongScreen用于显示用户的成绩和提示。

package com.j2medev.numbergame;

import java.io.IOException;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class SplashScreen extends NumScreen
{

public SplashScreen(Display display,Manager next)
{
super(display, next);
}

protected void paint(Graphics arg0)
{
Image welcome = null;
try
{
welcome = Image.createImage("/welcome.png");
} catch (IOException e)
{
e.printStackTrace();
}

arg0.drawImage(welcome, 2, 2, Graphics.TOP | Graphics.LEFT);

}

}


package com.j2medev.numbergame;

import java.io.IOException;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class CongScreen extends NumScreen
{

private String title;

private Mark mark;

private Image image;

private Image star;

private Manager manager;

private int type = CONG;

public static final int CONG = 1;
public static final int WARNING = 2;

public CongScreen(Display display, Manager next)
{
super(display, next);

}

public CongScreen(Display display,Manager next, Mark mark)
{
this(display, next);
this.mark = mark;

}


protected void paint(Graphics g)
{
int width = this.getWidth();
int height = this.getHeight();
if (image == null)
{
try
{
image = Image

[1] [2] [3]  下一页

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码