import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
public class Tank extends MIDlet {
public static Tank instance;
ChoiceList cl = new ChoiceList("PLEASE CHOICE");
public Tank() {
instance = this;
}
public void startApp() {
Display.getDisplay(instance).setCurrent(cl);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
/*Choice List*/
class ChoiceList extends List implements CommandListener {
GameWelcomeCanvas gwc = new GameWelcomeCanvas(this);
Command CMD_OK = new Command("OK", Command.OK, 1);
public ChoiceList(String str) {
super(str, List.IMPLICIT);
this.append("GAME", null);
this.addCommand(CMD_OK);
this.setCommandListener(this);//
}
public void commandAction(Command c, Displayable arg1) {
if (c == CMD_OK) {
if (this.getSelectedIndex() == 0) {
Display.getDisplay(Tank.instance).setCurrent(gwc);
}
}
}
}
/*Welcome canvas of game*/
class GameWelcomeCanvas extends Canvas implements CommandListener {
Command CMD_OK = new Command("OK", Command.OK, 1);
ChoiceList cl;
MainScrean ms = new MainScrean(this);
int color;/*Identify which choice has different color*/
String NewGame = "NEW GAME";
String C;
String ExitGame = "EXIT";
Font font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);/*Set the font style*/
int fontHeight = font.getHeight();/*Get the height of the font*/
int fontWidthOfNewGame = font.stringWidth(NewGame);
int fontWidthOfContinueGame = font.stringWidth(ContinueGame);
int fontWidthOfExitGame = font.stringWidth(ExitGame);
public GameWelcomeCanvas(ChoiceList cl) {
this.cl = cl;
color = 0;
this.addCommand(CMD_OK);
this.setTitle("TANK");
this.setCommandListener(this);
}
/*Paint Method*/
protected void paint(Graphics g) {
BackGround_Black(g);/*Set the Screan background to BLACK*/
g.setColor(255, 255, 255);
g.setFont(font);
g.drawString(NewGame, (getWidth() - fontWidthOfNewGame) / 2, (getHeight() - 4 * fontHeight) / 2, Graphics.TOP | Graphics.LEFT);
g.drawString(ContinueGame, (getWidth() - fontWidthOfContinueGame) / 2, (getHeight() - fontHeight) / 2, Graphics.TOP | Graphics.LEFT);
g.drawString(ExitGame, (getWidth() - fontWidthOfExitGame) / 2, (getHeight() + 2 * fontHeight) / 2, Graphics.TOP | Graphics.LEFT);
g.setColor(255, 0, 0);
if (color % 3 == 0) {
g.drawString(NewGame, (getWidth() - fontWidthOfNewGame) / 2, (getHeight() - 4 * fontHeight) / 2, Graphics.TOP | Graphics.LEFT);
} else if (color % 3 == 1) {
g.drawString(ContinueGame, (getWidth() - fontWidthOfContinueGame) / 2, (getHeight() - fontHeight) / 2, Graphics.TOP | Graphics.LEFT);
} else if (color % 3 == 2) {
g.drawString(ExitGame, (getWidth() - fontWidthOfExitGame) / 2, (getHeight() + 2 * fontHeight) / 2, Graphics.TOP | Graphics.LEFT);
}
}
protected void BackGround_Black(Graphics g) {
g.setColor(0, 0, 0);
g.fillRect(0, 0, getWidth(), getHeight());
}
public void commandAction(Command cmd, Displayable arg1) {
/*Choiced*/
if (cmd == CMD_OK) {
if (color % 3 == 0) {
Display.getDisplay(Tank.instance).setCurrent(ms);
} else if (color % 3 == 1) {
} else if (color % 3 == 2) {
Display.getDisplay(Tank.instance).setCurrent(cl);
}
}
}
protected void keyPressed(int keyCode) {
if (getGameAction(keyCode) == Canvas.DOWN) {
color++;
} else if (getGameAction(keyCode) == Canvas.UP) {
color += 2;
}
/*The Event action when you press FIRE button*/
if (getGameAction(keyCode) == Canvas.FIRE) {
if (color % 3 == 0) {
Display.getDisplay(Tank.instance).setCurrent(ms);
} else if (color % 3 == 1) {
} else if (color % 3 == 2) {
Display.getDisplay(Tank.instance).setCurrent(cl);
}
}
repaint();
}
}
/*Game main screan*/
class MainScrean extends GameCanvas implements CommandListener {
GameWelcomeCanvas gwc;
Map1 map1 = new Map1();
Command CMD_BACK = new Command("BACK", Command.BACK, 1);
Command CMD_START = new Command("START", Command.OK, 1);
Command CMD_PAUSE = new Command("PAUSE", Command.OK, 1);
Graphics g = getGraphics();
int Broad_X = 16;/*The different fo Interal and Out*/
int Broad_Y = 16;
final int backgroundSize = 8;/*The size of each Cell*/
final int spriteSize_tank = 16;
TiledLayer tl;
LayerManager lm = new LayerManager();
Image background = null;
Image tank_Y = null;
Image tank_Y_fire = null;
Sprite sprite_tank = null;
Sprite sprite_tank_fire = null;
int tank_YOfX = 7 * spriteSize_tank;
int tank_YOfY = getHeight() - spriteSize_tank - spriteSize_tank / 2 - 3;
boolean GameState = false;/*Get the state of the Game*/
int tankFaced;
public boolean fireAgain = true;/*One buttle one time*/
public MainScrean(GameWelcomeCanvas gwc) {
super(true);
this.gwc = gwc;
this.addCommand(CMD_BACK);
this.addCommand(CMD_START);
paintScrean(g);/*Set the lowest background*/
getBackgroundImage();
LoadMap();/*Load the map*/
this.setCommandListener(this);
}
public void paintScrean(Graphics g) {
/*Outside color*/
g.setColor(80, 80, 80);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(255, 0, 0);
g.drawRect(Broad_X - 1, Broad_Y - 1, getWidth() - 2 * Broad_X + 1, getHeight() - 2 * Broad_Y - 2);
/*Inside color*/
g.setColor(255, 255, 255);
g.fillRect(Broad_X, Broad_Y, getWidth() - 2 * Broad_X, getHeight() - 2 * Broad_Y - 3);
}
public void getBackgroundImage() {
int count = Math.max(getWidth(), getHeight()) / backgroundSize;
try {
background = Image.createImage("/images/background.png");/*Load the background picture*/
tank_Y = Image.createImage("/images/tank_Y.png");
tank_Y_fire = Image.createImage("/images/tank_Y_fire.png");
sprite_tank = new Sprite(tank_Y, spriteSize_tank, spriteSize_tank);
sprite_tank.defineReferencePixel(spriteSize_tank / 2, spriteSize_tank / 2);/*Set the tank sprite default pix*/
sprite_tank_fire = new Sprite(tank_Y_fire, spriteSize_tank, spriteSize_tank);
sprite_tank_fire.defineReferencePixel(spriteSize_tank / 2, spriteSize_tank / 2);/*Set the fired tank sprite default pix*/
tl = new TiledLayer(count, count, background, backgroundSize, backgroundSize);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void LoadMap() {
// int mapWidth = 30;
int[][] map = map1.getMap();/*Get the map*/
/*The map has 36 rows and 30 columns*/
for (int row = 0; row < 36; row++) {
for (int column = 0; column < 30; column++) {
tl.setCell(column, row, map[row][column]);
}
}
lm.append(tl);
lm.paint(g, 0, 0);
}
public void commandAction(Command c, Displayable arg1) {
if (c == CMD_BACK) {
Display.getDisplay(Tank.instance).setCurrent(gwc);
} else if (c == CMD_START) {
GameState = true;
this.removeCommand(CMD_START);
this.addCommand(CMD_PAUSE);
Thread thread_tank = new Thread(new Tank_Y(g));
thread_tank.start();
} else if (c == CMD_PAUSE) {
GameState = false;
this.removeCommand(CMD_PAUSE);
this.addCommand(CMD_START);
}
}
/*The Yellow Tank thread*/
class Tank_Y implements Runnable {
int delay = 40;/*Wait time*/
Graphics g;
boolean fire = false;
int perX;
int perY;
public Tank_Y(Graphics g) {
this.g = g;
}
public void run() {
while (GameState) {
fire = false;
controlTank();
paintTank(g);
try {
Thread.sleep(delay);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
/*The method control the Yellow tank*/
public void controlTank() {
int keyState = getKeyStates();/*Get the key press inmidility*/
perX = tank_YOfX;
perY = tank_YOfY;
/*When the tank hit any object can't move forward*/
if ((keyState & LEFT_PRESSED) != 0) {
tank_YOfX = Math.max(spriteSize_tank + spriteSize_tank / 2, tank_YOfX - 1);
sprite_tank.setFrame(3);
} else if ((keyState & RIGHT_PRESSED) != 0) {
tank_YOfX = Math.min(getWidth() - spriteSize_tank - spriteSize_tank / 2, tank_YOfX + 1);
sprite_tank.setFrame(2);
} else if ((keyState & UP_PRESSED) != 0) {
tank_YOfY = Math.max(spriteSize_tank + spriteSize_tank / 2, tank_YOfY - 1);
sprite_tank.setFrame(0);
} else if ((keyState & DOWN_PRESSED) != 0) {
tank_YOfY = Math.min(getHeight() - spriteSize_tank - spriteSize_tank / 2 - 3, tank_YOfY + 1);
sprite_tank.setFrame(1);
} else if ((keyState & FIRE_PRESSED) != 0) {
sprite_tank_fire.setFrame(sprite_tank.getFrame());
fire = true;
/*Start the buttle thread*/
if (fireAgain) {
Thread thread = new Thread(new buttle(tank_YOfX, tank_YOfY, sprite_tank_fire.getFrame()));
thread.start();
}
}
sprite_tank.setRefPixelPosition(tank_YOfX, tank_YOfY);/*Virtual move the tank station*/
if (sprite_tank.collidesWith(tl, false)) {
tankFaced = sprite_tank.getFrame();
/*Get the perfor X&Y position*/
tank_YOfX = perX;
tank_YOfY = perY;
}
}
public void paintTank(Graphics g) {
sprite_tank.setRefPixelPosition(tank_YOfX, tank_YOfY);
if (fire) {
sprite_tank_fire.setRefPixelPosition(tank_YOfX, tank_YOfY);
sprite_tank_fire.paint(g);
} else {
sprite_tank.paint(g);
}
flushGraphics();
}
}
/*When the tank fired the buttle flying*/
class buttle implements Runnable {
Image b = null;
Sprite sprite_b = null;
int b_speed = 30;
boolean flying = true;
int b_X, b_Y;
int tank_faced;
public buttle(int tank_X, int tank_Y, int faced) {
b_X = tank_X;
b_Y = tank_Y;
tank_faced = faced;
fireAgain = false;
try {
b = Image.createImage("/images/buttle.png");
sprite_b = new Sprite(b, 2, 2);
sprite_b.defineReferencePixel(1, 1);
} catch (Exception e1) {
System.out.println(e1.getMessage());
}
}
public void run() {
while (flying) {
buttleFlyDirection();
try {
Thread.sleep(b_speed);
sprite_b.nextFrame();
sprite_b.paint(g);
sprite_b.prevFrame();
} catch (Exception e) {
System.out.println(e.getMessage());
}
if (sprite_b.getFrame() == 1) {
sprite_b.setVisible(false);
}
}
}
public void buttleFlyDirection() {
if (tank_faced == 0) {
b_Y--;
} else if (tank_faced == 1) {
b_Y++;
} else if (tank_faced == 2) {
b_X++;
} else if (tank_faced == 3) {
b_X--;
}
sprite_b.setRefPixelPosition(b_X, b_Y);/*Change the buttle position*/
/*Insure the buttle in the box*/
if (b_Y > getHeight() - 3 * (Broad_Y / 2) || b_Y < Broad_Y + 5 || b_X > getWidth() - 3 * (Broad_X / 2) + 3 || b_X < Broad_X + 4) {
// sprite_b.nextFrame();
flying = false;
fireAgain = true;
} else {
}
/*When the buttle hit object*/
if (sprite_b.collidesWith(tl, false)) {
if (tl.getCell((int) (b_X / 8), (int) (b_Y / 8)) == 4) {
tl.setCell((int) (b_X / 8), (int) (b_Y / 8), 5);
tl.paint(g);/*?tl must be paint here*/
tl.setCell((int) (b_X / 8), (int) (b_Y / 8), 0);
tl.paint(g);
flying = false;
fireAgain = true;
} else if (tl.getCell((int) (b_X / 8), (int) (b_Y / 8)) == 1 || tl.getCell((int) (b_X / 8), (int) (b_Y / 8)) == 2) {
tl.paint(g);/*刷地图,CPU消耗大*/
} else if (tl.getCell((int) (b_X / 8), (int) (b_Y / 8)) == 3) {
// sprite_b.nextFrame();
flying = false;
fireAgain = true;
}
}
sprite_b.paint(g);
flushGraphics();
}
}
}
public class Map1 {
final int MapID = 1;
int[][] Map;
public Map1() {
int[][] map = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},/*EMPTY*/
{0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0},
{0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0},
{0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4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, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 4, 4, 4, 0, 0},
{0, 0, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 0, 0, 4, 4, 4, 4, 0, 0, 4, 4, 4, 0, 0},
{0, 0, 4, 4, 4, 0, 0, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 4, 4, 0, 0, 4, 4, 4, 0, 0},
{0, 0, 4, 4, 4, 0, 0, 3, 4, 4, 0, 0, 0, 3, 3, 3, 0, 0, 0, 4, 4, 4, 3, 0, 0, 4, 4, 4, 0, 0},
{0, 0, 4, 4, 4, 0, 0, 3, 4, 4, 0, 0, 0, 3, 0, 3, 0, 0, 0, 4, 4, 4, 3, 0, 0, 4, 4, 4, 0, 0},
{0, 0, 4, 4, 4, 0, 0, 3, 4, 4, 0, 0, 0, 3, 0, 3, 0, 0, 0, 4, 4, 4, 3, 0, 0, 4, 4, 4, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
}
;
Map = map;
}
public int[][] getMap() {
return Map;
}
}
/*游戏实现了一些基本功能,但还有不少错误和不足,图片以16*16为单位*/
/*Q:如何解决Sprite图片重叠后背景图片被覆盖的问题,采用刷屏的方式效果不佳,有没有更好的方法*/
此主题相关图片如下:

此主题相关图片如下:

[此贴子已经被admin于2010-12-13 20:06:55编辑过]