?? 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 + -