?? gamectrl.java
字號:
/*
* 創(chuàng)建日期 2005-6-24
*
* TODO 要更改此生成的文件的模板,請轉(zhuǎn)至
* 窗口 - 首選項(xiàng) - 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 要更改此生成的類型注釋的模板,請轉(zhuǎn)至
* 窗口 - 首選項(xiàng) - 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;
// 游戲結(jié)束標(biāo)志
public boolean isGameOver = false;
// 游戲開始標(biāo)志
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};
// 運(yùn)動(dòng)方向 [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();
// 設(shè)置食物位置
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);
// 偵聽按鍵響應(yīng)
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[i], snakeY[i], 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){
// 結(jié)束游戲
isGameOver = true;
isGameRun = false;
// 關(guān)閉定時(shí)器
nextFrame.cancel();
}
// 用戶退出游戲
try{
midlet.quit();
}
catch(MIDletStateChangeException e){}
}
}
private void initialize() {
// 初始化數(shù)據(jù)
score = 0;
// 白色清空畫布
graph.setColor(255, 255, 255);
graph.fillRect(0, 0, width, height);
// 開啟定時(shí)器
nextFrame = new NextFrame(this);
timer.schedule(nextFrame, 300, 300);
// 重繪屏幕
isGameOver = false;
isGameRun = true;
repaint();
}
protected void keyPressed(int keyCode) {
// 游戲結(jié)束后不處理按鍵
if (isGameOver == true)
return;
// 得到按鍵動(dòng)作
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();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -