📄 gamectrl.java
字号:
/*
* 创建日期 2005-6-23
*
* 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 RacingMIDlet midlet;
private Graphics graph;
private Timer timer = new Timer();
private NextFrame nextFrame;
// 炸弹图象
public Image bombImage;
// 赛车图象
public Image carImage;
// 屏幕尺寸
public int width = 0;
public int height = 0;
// 公路中线模式
public boolean lineMode = true;
// 赛车水平位置
public int carPos = 0;
// 炸弹位置
public int[] bombPosX = {0, 0, 0, 0};
public int[] bombPosY = {0, 0, 0, 0};
// 炸弹是否出界
public boolean[] bombCanUse = {false, false, false, false};
// 游戏结束标志
public boolean isGameOver = false;
// 游戏开始标志
public boolean isGameRun = false;
// 游戏积分
public int score = 0;
/**
*
*/
public GameCtrl(RacingMIDlet midlet) {
super();
// 保存MIDlet类对象
this.midlet = midlet;
// 得到屏幕尺寸
width = getWidth();
height = getHeight();
try {
// 装载炸弹图象
bombImage = Image.createImage("/bomb.png");
// 装载赛车图象
carImage = Image.createImage("/car.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.setColor(0, 0, 255);
if (lineMode == true){
lineMode = false;
for (int i = 0; i < height; i += 20)
g.drawLine(width / 2, i, width / 2, i + 10);
}else{
lineMode = true;
for (int i = 10; i < height; i += 20)
g.drawLine(width / 2, i, width / 2, i + 10);
}
// 绘制赛车
g.drawImage(carImage, carPos, height - carImage.getHeight(), Graphics.HCENTER | Graphics.VCENTER);
// 绘制炸弹
for (int i = 0; i < 4; i++){
if (bombCanUse[i] == true)
g.drawImage(bombImage, bombPosX[i], bombPosY[i], Graphics.HCENTER | Graphics.VCENTER);
}
}
}
/* (非 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() {
// 初始化数据
carPos = width * 3 / 4;
score = 0;
for (int i = 0; i < 4; i++){
bombPosX[i] = 0;
bombPosY[i] = 0;
bombCanUse[i] = false;
}
// 白色清空画布
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 RIGHT:
// 右移赛车
carPos += 5;
// 防止越界
if (carPos > width - carImage.getWidth() / 2)
carPos = width - carImage.getWidth() / 2;
break;
case LEFT:
// 左移赛车
carPos -= 5;
// 防止越界
if (carPos < carImage.getWidth() / 2)
carPos = carImage.getWidth() / 2;
break;
default:
break;
}
// 重绘屏幕
repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -