第4楼 2006-03-23 23:05 金属烟灰缸 写道:
/*
* 创建日期 2005-6-24
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package game;
import java.util.Timer;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class GameCtrl extends Canvas implements CommandListener{
private final Command startCommand;
private final Command quitCommand;
private final SnakeMIDlet midlet;
private Graphics graph;
private Timer timer = new Timer();
private NextFrame nextFrame;
// 游戏结束标志
public boolean isGameOver = false;
// 游戏开始标志
public boolean isGameRun = false;
// 游戏积分
public int score = 0;
// 屏幕尺寸
public int width = 0;
public int height = 0;
// 食物位置
public int foodX = 0;
public int foodY = 0;
// 蛇身位置
public int[] snakeX = {10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50 ,54, 58, 62, 66, 70, 74, 78, 82, 86};
public int[] snakeY = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10};
// 运动方向 [0,1,2,3——上,下,左,右]
public int direction = 0;
// 食物图象
public Image foodImage = null;
/**
*
*/
public GameCtrl(SnakeMIDlet midlet) {
super();
// 保存MIDlet类对象
this.midlet = midlet;
// 得到屏幕尺寸
width = getWidth();
height = getHeight();
// 设置食物位置
foodX = width / 2;
foodY = height / 2;
try{
// 装载食物图象
foodImage = Image.createImage("/food.png");
}catch(Exception e){}
// 添加命令按键
quitCommand = new Command("退出", Command.EXIT, 2);
addCommand(quitCommand);
startCommand = new Command("开始", Command.OK, 1);
addCommand(startCommand);
// 侦听按键响应
setCommandListener(this);
}
/* (非 Javadoc)
* @see javax.microedition.lcdui.Displayable#paint(javax.microedition.lcdui.Graphics)
*/
protected void paint(Graphics g) {
graph = g;
if (isGameOver == true && isGameRun == true){
isGameRun = false;
// 显示logo
Alert result = new Alert("本局积分", String.valueOf(score), null, AlertType.INFO);
// 延迟4秒
result.setTimeout(2000);
// 显示闪屏界面
midlet.display.setCurrent(result, this);
return;
}
if (isGameRun == true){
// 白色清空画布
graph.setColor(255, 255, 255);
graph.fillRect(0, 0, width, height);
// 绘制食物
g.drawImage(foodImage, foodX, foodY, Graphics.HCENTER | Graphics.VCENTER);
// 绘制蛇身
graph.setColor(0, 0, 255);
for (int i = 0; i < 19; i++){
graph.drawLine(snakeX, snakeY, snakeX[i + 1], snakeY[i + 1]);
}
}
}
/* (非 Javadoc)
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
*/
public void commandAction(Command arg0, Displayable arg1) {
if (arg0 == startCommand){
// 用户开始游戏
initialize();
}
if (arg0 == quitCommand){
if (isGameRun == true){
// 结束游戏
isGameOver = true;
isGameRun = false;
// 关闭定时器
nextFrame.cancel();
}
// 用户退出游戏
try{
midlet.quit();
}
catch(MIDletStateChangeException e){}
}
}
private void initialize() {
// 初始化数据
score = 0;
// 白色清空画布
graph.setColor(255, 255, 255);
graph.fillRect(0, 0, width, height);
// 开启定时器
nextFrame = new NextFrame(this);
timer.schedule(nextFrame, 300, 300);
// 重绘屏幕
isGameOver = false;
isGameRun = true;
repaint();
}
protected void keyPressed(int keyCode) {
// 游戏结束后不处理按键
if (isGameOver == true)
return;
// 得到按键动作
int gameAction = getGameAction(keyCode);
switch (gameAction) {
case UP:
direction = 0;
break;
case DOWN:
direction = 1;
break;
case LEFT:
direction = 2;
break;
case RIGHT:
direction = 3;
break;
default:
break;
}
// 重绘屏幕
repaint();
}
}