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:04:58
笔者刚刚开始学习写游戏,并没有什么经验,因此选择了门槛比较低的猜数字游戏。花了一天的时间,基本能够在Nokia6108上运行了,界面比较简单,为学习之用。

下面介绍一下如何实现猜数字游戏,其实这是一个比较经典的游戏游戏的原理是:游戏开始的时候会自动产生四个不重复的随机数字比如1234,用户输入四个数字,系统通过判断返回给用户xAyB的结果,其中A代表数字正确位置也正确,B代表数字正确但是位置不正确。如果用户猜对游戏就结束了,10次内没有猜对,游戏也结束。在这里我们重点介绍为游戏而实现的组件,简单的流程控制和游戏逻辑。

首先介绍组件,这里我们提供了两个组件,一个就是Button,他可以接收用户输入的数字,并且可以响应用户的按键事件。

首先我们构造一个基本的组件,这个组件需要包括左上角顶点的坐标(x,y),宽度w,高度h以及前景色、背景色。最重要的一点是我们需要给他提供一个容器来管理他,因此提供一个Manager类。
package com.j2medev.numbergame;

import javax.microedition.lcdui.*;
import com.nokia.mid.ui.*;

//A root class for Canvas-based components.
//Because Area extends Canvas, you can actually
//use a component directly as a Canvas, although
//it's recommended you place it on Manager.

public abstract class Area extends FullCanvas
{
protected int x;

protected int y;

protected int w;

protected int h;

protected Font font;

protected Manager parent;

protected int backcolor = -1;

protected int forecolor = -1;

protected Area(int x, int y, int w, int h)
{
this(x, y, w, h, null);
}

protected Area(int x, int y, int w, int h, Font f)
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.font = f;
}

// Erase the background using backcolor

protected void eraseBackground(Graphics g)
{
g.setColor(getBackColor());

if (parent == null)
{
g.fillRect(0, 0, getCanvasWidth(), getCanvasHeight());
} else
{
g.fillRect(0, 0, w, h);
}
}

public final int getBackColor()
{
if (backcolor == -1)
{
if (parent != null)
{
return parent.getBackColor();
}

backcolor = 0xFFFFFF;
}

return backcolor;
}

protected final int getCanvasHeight()
{
return super.getHeight();
}

protected final int getCanvasWidth()
{
return super.getWidth();
}

public final int getHeight()
{
return h;
}

public final int getWidth()
{
return w;
}

public final int getX()
{
return x;
}

public final int getY()
{
return y;
}

public final Font getFont()
{
if (font == null)
{
if (parent != null)
{
return parent.getFont();
}

font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL);
;

}

return font;
}

public final int getForeColor()
{
if (forecolor == -1)
{
if (parent != null)
{
return parent.getForeColor();
}

forecolor = 0;
}

return forecolor;
}

public Manager getParent()
{
return parent;
}

public void keyPressed(int keyCode)
{
}

public void keyReleased(int keyCode)
{
}

public void keyRepeated(int keyCode)
{
}

protected void moveFocus(boolean forward)
{
if (parent != null)
{
parent.moveFocus(forward);
}
}

// If the area is acting like a Canvas, call
// the real paint routine

protected void paint(Graphics g)
{

eraseBackground(g);
g.setColor(getForeColor());
g.setFont(getFont());
paintArea(g, true);
}

// The manager calls this paint routine on each of
// its children

public final void paint(Graphics g, boolean hasFocus)
{
int cx = g.getClipX();
int cy = g.getClipY();
int ch = g.getClipHeight();
int cw = g.getClipWidth();
Font f = g.getFont();
int col = g.getColor();

eraseBackground(g);

g.setClip(x, y, w, h);
g.setFont(getFont());
g.setColor(getForeColor());

paintArea(g, hasFocus);

g.setClip(cx, cy, cw, ch);
g.setFont(f);
g.setColor(col);
}

// Subclass implements to do actual painting

protected abstract void paintArea(Graphics g, boolean hasFocus);

// Repaint the area of the given child

public void repaintArea(Area child, boolean now)
{
if (parent != null)
{
parent.repaintArea(child, now);
} else
{
repaint(child.getX(), child.getY(), child.getWidth(), child
.getHeight());

if (now)
{
serviceRepaints();
}
}
}

public void setBackColor(int col)
{
backcolor = col;
}

public void setForeColor(int col)
{
forecolor = col;
}

protected void setParent(Manager parent)
{
this.parent = parent;
}

}
这是一个抽象类,他的抽象方法protected abstract void paintArea(Graphics g, boolean hasFocus);是留给他的子类实现的。因此我们在子类中重点需要关注的就是如何来绘制自己和根据用户的输入做出响应。下面我们来实现Button类,我们需要提供一个ButtonListener类,通过他我们可以实现回调的功能,即在按键按下的时候去做ButtonListener中规定的方法,非常类似于CommandListener的功能。

package com.j2medev.numbergame;

public interface ButtonListener
{
public void buttonPressed(Button pushed);

}
下面是Button类的代码

package com.j2medev.numbergame;
import javax.microedition.lcdui.*;

public class Button extends Area
{

private String label;

private boolean selected;

private ButtonListener listener;

private boolean modifiable = true;

private int marginx = 4;

privat

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

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

用户名: 查看更多评论

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

内 容:

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