import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.util.Random;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Canvas;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
public class WallBall extends MIDlet {
public static WallBall instance;
WelcomeForm wf = new WelcomeForm("欢迎");
public WallBall() {
instance = this;
}
public void startApp() {
Display.getDisplay(this).setCurrent(wf);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
//
class WelcomeForm extends Form implements CommandListener {
Command CMD_Game = new Command("壁球", Command.OK, 1);
StartList sl = new StartList("CHOICE");
public WelcomeForm(String str) {
super(str);
this.addCommand(CMD_Game);
this.setCommandListener(this);
}
public void commandAction(Command c, Displayable display) {
if (c == CMD_Game) {
Display.getDisplay(WallBall.instance).setCurrent(sl);
}
}
}
//游戏的选项列界面
class StartList extends List implements CommandListener {
DrawPanel dp = new DrawPanel();
GameLevel gl = new GameLevel();
MusicList ml = new MusicList();
HeroList hl = new HeroList();
Command CMD_OK = new Command("OK", Command.OK, 1);
Command CMD_hBack = new Command("BACK", Command.BACK, 1);
int index;
TextBox helpTB = new TextBox("HELP DOCUMENT", "", 100, TextField.UNEDITABLE);
static musicPlayer mp;
public StartList(String str) {
super(str, List.IMPLICIT);
this.addCommand(CMD_OK);
this.setCommandListener(this);
this.append("新游戏", null);
this.append("难度 : " + GameLevel.hardOfGame, null);
this.append("音乐 : " + MusicList.selected, null);
this.append("英雄榜", null);
this.append("使用说明", null);
}
public void commandAction(Command c, Displayable arg1) {
if (c == CMD_OK) {
index = this.getSelectedIndex();
if (index == 0) {
DrawPanel.life = 3;
String str = "OPEN";
if (MusicList.selected == str) { //有警告提示,如何解决?
mp = new musicPlayer();
mp.player();
}
Display.getDisplay(WallBall.instance).setCurrent(dp);
} else if (index == 1) {
Display.getDisplay(WallBall.instance).setCurrent(gl);
} else if (index == 2) {
Display.getDisplay(WallBall.instance).setCurrent(ml);
} else if (index == 3) {
Display.getDisplay(WallBall.instance).setCurrent(hl);
} else if (index == 4) {
String str = "壁球游戏" + "\n" + "你可以使用方向键或数字键控制挡板移动" + "\n" + "小球接触底边界LIFE减少一" + "\n" + "当LIFE归零,游戏结束";
helpTB.setString(str);
helpTB.addCommand(CMD_hBack);
helpTB.setCommandListener(this);
Display.getDisplay(WallBall.instance).setCurrent(helpTB);
}
} else if (c == CMD_hBack) {
Display.getDisplay(WallBall.instance).setCurrent(new StartList("Chioce"));
}
}
}
//NEW GAME PANEL
class DrawPanel extends Canvas implements CommandListener {
//StartList startL = new StartList("选择");//新生成一个StartList对象,在后面的调用中会出现错误
Command CMD_Pause = new Command("PAUSE", Command.OK, 1);
Command CMD_Start = new Command("START", Command.OK, 1);
Command CMD_Back = new Command("BACK", Command.BACK, 1);
StoreInfo si;
int Broad_X; //挡板的相对位置
int ball = 30 - GameLevel.hardOfGame * 2; //小球的直径
int Broad_width = 60; //挡板的宽度
int Broad_height = 8; //挡板的高度
int ball_X; //小球在水平方向上的相对位置
int ball_Y; //小球在垂直方向上的相对位置
int level = 2 * (GameLevel.hardOfGame) + 3; //小球一次移动的步幅
int go_X = level; //小球水平方向上每步移动的距离
int go_Y = level; //小球垂直方向上每步移动的距离
static int life = 3; //游戏初始生命
static int recode; //记录游戏上一次所得的分数
int score = recode; //初始化分数
boolean flag = false; //确定是否重绘画面
boolean pause = false;//确定游戏是否处于暂停状态
public DrawPanel() {
this.addCommand(CMD_Start);
this.addCommand(CMD_Back);
this.setCommandListener(this);
}
protected void paint(Graphics g) {
Clear(g);
this.setTitle("壁球" + " Life : " + life + " Lelve :" + GameLevel.hardOfGame + " Score :" + score);
//挡板的初始位置及颜色
g.setColor(0, 0, 0);
g.fillRect((getWidth() - Broad_width) / 2 + Broad_X, getHeight() - Broad_height, Broad_width, Broad_height);
//小球的初始位置及颜色
g.setColor(0, 0, 255);
g.fillArc(getWidth() / 2 - ball / 2 + ball_X, getHeight() - ball - Broad_height + ball_Y, ball, ball, 0, 360);
//NOTCE,THE GAME IS PAUSING
if (pause == true) {
g.setColor(255, 0, 0);
Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);
g.setFont(font);
String str = "游戏暂停中";
int fontWidth = font.stringWidth(str);
g.drawString(str, getWidth() / 2 + fontWidth / 2, getHeight() / 2, Graphics.TOP | Graphics.RIGHT);
}
}
//清屏
private void Clear(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
}
protected void keyPressed(int keyCode) {
if (flag == true) {
int code = getGameAction(keyCode);//功能区按键
if (code == Canvas.LEFT || keyCode == Canvas.KEY_NUM4) {
if (getWidth() / 2 - 30 + Broad_X > 0) {
Broad_X -= 5;
}
}
if (code == Canvas.RIGHT || keyCode == Canvas.KEY_NUM6) {
if (getWidth() / 2 + 30 + Broad_X < getWidth()) {
Broad_X += 5;
}
}
repaint();
}
}
protected void keyRepeated(int keyCode) {
keyPressed(keyCode);
}
public void commandAction(Command c, Displayable display) {
if (c == CMD_Start) {
this.addCommand(CMD_Pause);
this.removeCommand(CMD_Start);
flag = true;
pause = false;
Thread thread = new Thread(new getChange());//由事件监听启动一个全新的线程
thread.start();//线程开始
} else if (c == CMD_Pause) {
this.removeCommand(CMD_Pause);
this.addCommand(CMD_Start);
flag = false;
pause = true;
} else if (c == CMD_Back) {
life = 3;//退出游戏,信息初始化
score = recode = 0;
try {//停止音乐的播放
StartList.mp.p.stop();
} catch (Exception e) {
System.err.println(e.getMessage());
}
Display.getDisplay(WallBall.instance).setCurrent(new StartList("Choice"));//如果使用上面生成的实例会出错
}
}
//内部类,用于获取小球的移动方向
class getChange implements Runnable {
public void run() {
while (flag) {
//小球至右边界
if ((getWidth() + ball) / 2 + ball_X >= getWidth()) {
go_X = -level;
System.out.println(go_X);
}
//小球至左边界
if ((getWidth() - ball) / 2 + ball_X <= 0) {
go_X = level;
}
//小球至上边界
if (getHeight() - Broad_height - ball + ball_Y <= 0) {
go_Y = level;
}
//小球至下边界
if (Broad_height / 2 - ball_Y <= 0) {
flag = false;
if (life > 0) {
life--;//生命减一
recode = score;//存储这一次的分数
Display.getDisplay(WallBall.instance).setCurrent(new DrawPanel());
} else if (life == 0) {
try {//停止音乐的播放
StartList.mp.p.stop();
} catch (Exception e) {
System.err.println(e.getMessage());
}
recode = score;
Display.getDisplay(WallBall.instance).setCurrent(new StoreInfo());
recode = 0;//将分数记录器数值归零
}
}
//小球碰撞挡板,判断语句可以简化,但为了便于理解这里不作简化
if (ball_Y >= 0 && getWidth() / 2 - ball / 2 + ball_X >= (getWidth() - Broad_width) / 2 + Broad_X - ball && getWidth() / 2 - ball / 2 + ball_X <= (getWidth() + Broad_width) / 2 + Broad_X) {
Random r = new Random();
go_Y = -level;
score += (int) (r.nextFloat() * 2 + GameLevel.hardOfGame);//小球每一次被挡板挡回,玩家所得的分数
}
ball_X += go_X;
ball_Y += go_Y;
try {
Thread.sleep(100);
} catch (Exception e) {
}
repaint();
}
}
}
}
//播放背景音乐,音乐功能有较多的漏洞,其加载音频文件的速度很忙
class musicPlayer {
InputStream in;
Player p;
public void player() {
try {
in = getClass().getResourceAsStream("/musics/music.wav");
p = Manager.createPlayer(in, "audio/x-wav");
p.setLoopCount(Player.UNREALIZED);//音乐循环的次数,在加载了指定的音频文件后才能指定其循环的次数
p.start();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
//游戏难度界面
class GameLevel extends Canvas implements CommandListener {
int process_width = 140;//进度条的宽度
int process_height = 20;//进度条的高度
int Level;
static int hardOfGame;//静态变量,游戏的难度等级
String title = "Choice the level of the game";
Command CMD_OK = new Command("OK", Command.OK, 1);
Command CMD_BACK = new Command("BACK", Command.BACK, 1);
// StartList s=new StartList("Choice");
public GameLevel() {
this.setTitle(title);
this.addCommand(CMD_OK);
this.addCommand(CMD_BACK);
this.setCommandListener(this);
Level = hardOfGame * (process_width / 5);//难度进度条初始位置
}
protected void paint(Graphics g) {
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());//清屏
//EASY OR HARD
g.setColor(255, 0, 0);
Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE);//字体设置
g.setFont(font);
String str = "Easy";
int fontWidth = font.stringWidth(str);
int fontHeight = font.getHeight();
g.drawString(str, getWidth() / 2 - fontWidth - process_width / 2 - 10, getHeight() / 2 + fontHeight / 2 - process_height, Graphics.TOP | Graphics.LEFT);
str = "Hard";
g.drawString(str, getWidth() / 2 + fontWidth + process_width / 2 + 10, getHeight() / 2 + fontHeight / 2 - process_height, Graphics.TOP | Graphics.RIGHT);
//进度条外框
g.setColor(0, 255, 0);
g.drawRect((getWidth() - process_width) / 2 - 1, (getHeight() - process_height) / 2 - 1, process_width + 1, process_height + 1);
//进度条
g.fillRect((getWidth() - process_width) / 2, (getHeight() - process_height) / 2, Level, process_height);
}
protected void keyPressed(int keyCode) {
int code = getGameAction(keyCode);
if (code == Canvas.RIGHT) {
if (Level < process_width) {
Level = Level + process_width / 5;
}
} else if (code == Canvas.LEFT) {
if (Level > 0) {
Level = Level - process_width / 5;
}
}
repaint();
}
public void commandAction(Command c, Displayable display) {
if (c == CMD_OK) {
hardOfGame = (int) Level / (process_width / 5);
Display.getDisplay(WallBall.instance).setCurrent(new StartList("CHOICE"));
} else if (c == CMD_BACK) {
Display.getDisplay(WallBall.instance).setCurrent(new StartList("CHOICE"));
}
}
}
//Music interface
class MusicList extends List implements CommandListener {
static String selected;
Command CMD_OK = new Command("OK", Command.OK, 1);
public MusicList() {
super("MUSIC", List.EXCLUSIVE);
this.append("CLOSE", null);
this.append("OPEN", null);
this.addCommand(CMD_OK);
this.setCommandListener(this);
}
public void commandAction(Command c, Displayable arg1) {
if (c == CMD_OK) {
if (this.getSelectedIndex() == 0) {
selected = "CLOSE";
} else if (this.getSelectedIndex() == 1) {
selected = "OPEN";
}
Display.getDisplay(WallBall.instance).setCurrent(new StartList("CHIOCE"));
}
}
}
//英雄榜界面
class HeroList extends List implements CommandListener {
Heros[] hs;
StoreInfo si = new StoreInfo();
Command CMD_BACK = new Command("BACK", Command.BACK, 1);
public HeroList() {
super("HERO LIST", List.IMPLICIT);
this.addCommand(CMD_BACK);
getList();
this.setCommandListener(this);
}
public void commandAction(Command c, Displayable arg1) {
if (c == CMD_BACK) {
Display.getDisplay(WallBall.instance).setCurrent(new StartList("CHIOCE"));
}
}
//信息的读取
public void getList() {
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore("HerosRMS", false);
int i = 0;
RecordEnumeration re = rs.enumerateRecords(null, null, false);
hs = new Heros[re.numRecords()];
while (re.hasNextElement()) {
byte[] data = re.nextRecord();
hs[i] = Heros.deserialize(data);
this.append(hs[i].toString(), null);
i++;
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
//储存玩家姓名及分数信息
class StoreInfo extends Form implements CommandListener {
Command CMD_SAVE = new Command("SAVE", Command.OK, 1);
TextField name = new TextField("YOUR NAME", "", 20, TextField.ANY);
Heros h;
Heros[] hs;
int s = DrawPanel.recode;
public StoreInfo() {
super("GAME OVER--SCORE : " + DrawPanel.recode);
this.append(name);
this.addCommand(CMD_SAVE);
this.setCommandListener(this);
}
public void commandAction(Command c, Displayable arg1) {
if (c == CMD_SAVE) {
//向内存管理器存储信息
//信息的储存
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore("HerosRMS", true);
h = new Heros(name.getString(), s);
byte[] data = h.serialize();
rs.addRecord(data, 0, data.length);
rs.closeRecordStore();
} catch (Exception e) {
System.err.println(e.getMessage());
}
Display.getDisplay(WallBall.instance).setCurrent(new StartList("CHIOCE"));
}
}
}
class Heros {
private String Name;
private int Score;
//无参构造函数
public Heros() {
}
//带参构造函数
public Heros(String Name, int Score) {
this.Name = Name;
this.Score = Score;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public int getScore() {
return Score;
}
public void setScore(int Score) {
this.Score = Score;
}
public byte[] serialize() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(this.Name);
dos.writeInt(this.Score);
baos.close();
dos.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return baos.toByteArray();
}
public static Heros deserialize(byte[] data) {
Heros heros = new Heros();
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
try {
heros.Name = dis.readUTF();
heros.Score = dis.readInt();
bais.close();
dis.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return heros;
}
//返回有效数据
public String toString() {
return Name + " : " + Score;
}
}
//该程序还有不是的漏洞和不足,有兴趣的可以根据自己的需求调整
[此贴子已经被admin于2010-12-13 20:04:50编辑过]