学习J2ME有一个星期了,收获不小! 开发出自己的手机游戏来,感觉很不错!就想到了和大家分享一下子,怎样,够意思吧! 撞球游戏,这个不是最终的,因为最终的在我的笔记本里,这是U盘里的! 学过J2ME 的人都知道J2ME的代码不是很好移动的!
package com.li;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class test extends MIDlet implements CommandListener
{
private TestCanvas tc;
private Display display;
private Command cmd1;
private Command cmd2;
private Command aOK;
private Command aEXIT;
private boolean isRun;
private boolean isOver = false,isOver2 = true;
private Form form = null;
private Alert alert =null;
private TestCanvas _tc = new TestCanvas();
public test()
{
alert = new Alert("游戏说明","这是一个小小游戏,用档板去接小球,每接一次就是10分"+"\n"+"每100分就进入下一关卡.操作说明:数字4,6或者方向键左右!",null,AlertType.INFO);
form = new Form("小游戏");
isRun = true;
cmd1 = new Command("开始", 4, 1);
cmd2 = new Command("退出", 7, 2);
aOK = new Command("重新开始", 4, 1);
aEXIT = new Command("退出", 7, 1);
}
public void nofifystart()
{
}
protected void destroyApp(boolean flag1)
throws MIDletStateChangeException
{
}
protected void pauseApp()
{
}
protected void startApp()
throws MIDletStateChangeException
{
alert.addCommand(cmd1);
alert.addCommand(cmd2);
alert.setCommandListener(this);
display = Display.getDisplay(this);
display.setCurrent(alert,form);
}
class TestCanvas extends Canvas implements Runnable,CommandListener
{
private Command isOK;
private Command isEXIT,isHelp;
private int x;
private int y;
private int xSign;
private int ySign;
private int w;
private int h;
private int _x;
private float score;
public TestCanvas()
{
isOK = new Command("开始游戏",Command.OK,1);
isEXIT = new Command("暂停游戏游戏",Command.EXIT,1);
isHelp = new Command("帮助",Command.HELP,1);
score = 0.0F;
_x = 0;
w = 10;
h = 10;
xSign = 0;
ySign = 0;
this.Menu();
}
public void Menu()
{
this.addCommand(isOK);
this.addCommand(isEXIT);
this.addCommand(isHelp);
this.setCommandListener(this);
this.setFullScreenMode(true);
}
protected void paint(Graphics g)
{
g.setColor(0, 215, 10);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(100, 125, 150);
g.fillArc(x, y, w, h, 0, 360);
g.setColor(0xffffff);
g.fillRect(_x + 100, 310, 50, 319);
g.setColor(255, 255, 102);
g.setFont(Font.getFont(32, 1, 16));
g.drawString("你的积分是:" + score, 50, 0, 20);
g = null;
System.gc();
}

