import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class TankCanvas
extends GameCanvas
implements Runnable,CommandListener {
public static boolean mTrucking;
public static TankSprite mTank;
public static ETankSprite eTank ;
public static TiledLayer mBoard;
public Image AA;
public static int num =1;
public static int ll =5;
public static int fx;
public static int sd;
public static boolean gameover=false;
public static LayerManager mLayerManager;
public static int w;
public static int h;
private TankMIDlet midlet;
private Command command ;
private Command exitCommand = new Command("退出", Command.EXIT, 0);
public TankCanvas(TankMIDlet midlet) throws IOException {
super(true);
this.midlet=midlet;
AA=Image.createImage("/AA.png");
addCommand(exitCommand);
setCommandListener(this);
mBoard = createBoard();
mLayerManager = new LayerManager();
createTank();
createETank();
mLayerManager.append(mBoard);
splash();
}
public static void createTank() {
try{ Image image = Image.createImage("/tank.png");
mTank= new TankSprite(image, 14, 14);
mLayerManager.append(mTank);
mLayerManager.append(mTank.bullets);
mTank.setPosition(100, 52);
fx=3;
sd=-4;
}catch(IOException ioe){
}catch(Exception e){
}
}
public static void createETank() {
try{ Image images = Image.createImage("/enemyTank.png");
eTank= new ETankSprite(images, 14, 14);
mLayerManager.append(eTank);
mLayerManager.append(eTank.ebullets);
eTank.setPosition(10, 110);
eTank.start();
}catch(IOException ioe){
}catch(Exception e){
}
}
private TiledLayer createBoard() throws IOException {
Image image = Image.createImage("/bg.png");
TiledLayer tiledLayer = new TiledLayer(11, 11, image, 15, 15);
int[] map = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 3, 3, 3, 3, 4, 4, 4, 4, 4, 0,
0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0,
0, 4, 0, 0, 4, 0, 0, 0, 0, 0, 0,
0, 4, 0, 0, 4, 0, 0, 0, 0, 0, 0,
0, 4, 0, 0, 1, 1, 1, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0,
0, 0, 0, 0, 4, 0, 0, 2, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
for (int i = 0; i < map.length; i++) {
int column = i % 11;
int row = (i - column) / 11;
tiledLayer.setCell(column, row, map[i]);
}
return tiledLayer;
}
public void start() {
Thread t = new Thread(this);
t.start();
}
public void run() {
Graphics g = getGraphics();
while (mTrucking) {
long times_s= System.currentTimeMillis();
input();
tick();
render(g);
//得到系统当前时间,并将时间换算成毫秒
long times_e=System.currentTimeMillis();
long times=times_e-times_s;
if( times >70 ){
times =70;
}
try{
Thread.sleep(70- times );
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
private void tick() {
if (mTank.collidesWith(mBoard, true))
{mTank.undo();}
if (eTank.collidesWith(mBoard, true))
{ eTank.undo();}
if (eTank.collidesWith(mTank, true))
{
eTank.setVisible(false);
mTank.setVisible(false);
mTank.setVisible(false);
mLayerManager.remove(TankCanvas.mTank);
eTank.setVisible(false);
mLayerManager.remove(TankCanvas.eTank);
eTank.elift=false;
createTank();
createETank();
}
}
private void input() {
int keyStates = getKeyStates();
if ((keyStates & LEFT_PRESSED) != 0)
{mTank.forward(-3,1);
fx=1;
sd=-10;
if ((keyStates & FIRE_PRESSED) != 0)
{ mTank.bullets.start(mTank.getX(), mTank.getY(),fx,sd,1);
}}
else if ((keyStates & RIGHT_PRESSED) != 0)
{mTank.forward(3,2);
fx=2;
sd=10;
if ((keyStates & FIRE_PRESSED) != 0)
{ mTank.bullets.start(mTank.getX(), mTank.getY(),fx,sd,1);
}}
else if ((keyStates & UP_PRESSED) != 0)
{mTank.forward(-3,3);
fx=3;
sd=-10;
if ((keyStates & FIRE_PRESSED) != 0)
{ mTank.bullets.start(mTank.getX(), mTank.getY(),fx,sd,1);
}}
else if ((keyStates & DOWN_PRESSED) != 0)
{mTank.forward(3,4);
fx=4;
sd=10;
if ((keyStates & FIRE_PRESSED) != 0)
{ mTank.bullets.start(mTank.getX(), mTank.getY(),fx,sd,1);
}
}
else if ((keyStates & FIRE_PRESSED) != 0)
{
mTank.bullets.start(mTank.getX(), mTank.getY(),fx,sd,1);
}
}
private void render(Graphics g) {
w = getWidth();
h = getHeight();
g.setColor(0,0,0);
g.fillRect(0, 0, w, h);
g.setColor(255,255,255);
g.drawString("我方",80,0,g.LEFT|g.TOP);
g.drawString(String.valueOf(num),110,0,g.LEFT|g.TOP);
g.drawString("敌方",120,0,g.LEFT|g.TOP);
g.drawString(String.valueOf(ll),150,0,g.LEFT|g.TOP);
mLayerManager.paint(g, 1, 1);
flushGraphics();
if (gameover)
{ mTrucking=false;
w = getWidth();
h = getHeight();
g.setColor(0,0,0);
g.fillRect(0, 0, w, h);
g.setColor(255,0,255);
g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE));
g.drawImage(AA, getWidth()/2, getHeight()/2, Graphics.VCENTER|Graphics.HCENTER);
g.drawString("再来一局",w/2-10,h/2+40,g.LEFT|g.TOP);
g.drawString("你太菜了",w/2-75,h/2,g.LEFT|g.TOP);
g.drawString("GAME OVER",getWidth()/2-60,getHeight()/2+20,g.LEFT|g.TOP);
flushGraphics();
}
// 如果敌机数为0,则表示敌机已全部被歼灭,玩爱获得最终胜利
if(ll==0){
mTrucking=false;
g.setColor(0,0,0);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(255,0,255);
g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE));
g.drawImage(AA, getWidth()/2, getHeight()/2, Graphics.VCENTER|Graphics.HCENTER);
g.drawString("敌机已全部歼灭",40,getHeight()/2,g.LEFT|g.TOP); ;
flushGraphics();
}
}
public void splash() {
Image tanklogo = null;
try {
tanklogo = Image.createImage("/tanklogo.png");
} catch (IOException e) { }
Graphics g = getGraphics();
g.setColor(0);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(tanklogo, 0, 0, 0);
g.setColor(255,255,255);
g.setFont(Font.getFont(Font.FACE_SYSTEM,Font.STYLE_BOLD,Font.SIZE_LARGE));
g.drawString("歼灭之战", 60, 60, 0);
g.drawString("07游戏", 60, 80, 0);
repaint();
command = new Command(mTrucking == false ? "开始" : "重新开始",
Command.OK, 1);
addCommand(command);
}
public void commandAction(Command c, Displayable s) {
if (c.getCommandType() == Command.EXIT) {
if(mTrucking){
// 游戏暂停
mTrucking=false;
}
else
midlet.notifyDestroyed();
//单击开始按钮,控制游戏的开始实
}else if (c.getCommandType() == Command.OK)
{
ll =5;
num =1;
start();
gameover=false;
mTrucking=true;
}
}
public void stop() {
// mTrucking = false;
}
}