基于Nokia S40的猜数字游戏之一 |
| 作者:mingjava 来源:J2MEDV 发布时间:2005-9-7 12:04:58 |
|
笔者刚刚开始学习写游戏,并没有什么经验,因此选择了门槛比较低的猜数字游戏。花了一天的时间,基本能够在Nokia6108上运行了,界面比较简单,为学习之用。
下面介绍一下如何实现猜数字游戏,其实这是一个比较经典的游戏。游戏的原理是:游戏开始的时候会自动产生四个不重复的随机数字比如1234,用户输入四个数字,系统通过判断返回给用户xAyB的结果,其中A代表数字正确位置也正确,B代表数字正确但是位置不正确。如果用户猜对游戏就结束了,10次内没有猜对,游戏也结束。在这里我们重点介绍为游戏而实现的组件,简单的流程控制和游戏逻辑。 首先介绍组件,这里我们提供了两个组件,一个就是Button,他可以接收用户输入的数字,并且可以响应用户的按键事件。 首先我们构造一个基本的组件,这个组件需要包括左上角顶点的坐标(x,y),宽度w,高度h以及前景色、背景色。最重要的一点是我们需要给他提供一个容器来管理他,因此提供一个Manager类。 import javax.microedition.lcdui.*; //A root class for Canvas-based components. public abstract class Area extends FullCanvas 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) protected Area(int x, int y, int w, int h, Font f) // Erase the background using backcolor protected void eraseBackground(Graphics g) if (parent == null) public final int getBackColor() backcolor = 0xFFFFFF; return backcolor; protected final int getCanvasHeight() protected final int getCanvasWidth() public final int getHeight() public final int getWidth() public final int getX() public final int getY() public final Font getFont() font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, } return font; public final int getForeColor() forecolor = 0; return forecolor; public Manager getParent() public void keyPressed(int keyCode) public void keyReleased(int keyCode) public void keyRepeated(int keyCode) protected void moveFocus(boolean forward) // If the area is acting like a Canvas, call protected void paint(Graphics g) eraseBackground(g); // The manager calls this paint routine on each of public final void paint(Graphics g, boolean hasFocus) eraseBackground(g); g.setClip(x, y, w, h); paintArea(g, hasFocus); g.setClip(cx, cy, cw, ch); // 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 (now) public void setBackColor(int col) public void setForeColor(int col) protected void setParent(Manager parent) } public interface ButtonListener } public class Button extends Area private String label; private boolean selected; private ButtonListener listener; private boolean modifiable = true; |
| [] [返回上一页] [打 印] |
|
文章评论 |
