📄 planegamecanvas.java
字号:
package twplanegame;
import javax.microedition.lcdui.*;
import java.util.*;
/*
public class PlaneGameCanvas extends Canvas {
public PlaneGameCanvas() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
// private void jbInit() throws Exception {
// Set up this Displayable to listen to command events
// setCommandListener(new PlaneGameCanvas_CommandAdapter(this));
// add the Exit command
// addCommand(new Command("Exit", Command.EXIT, 1));
// }
/**Handle command events*/
// public void this_commandPerformed(Command command, Displayable displayable) {
/** @todo Add command handling code */
// if (command.getCommandType() == Command.EXIT) {
// stop the MIDlet
// PlaneGameMIDlet.quitApp();
// }
// }
/** Required paint implementation */
// protected void paint(Graphics g) {
/** @todo Add paint codes */
// }
//}
/*
class PlaneGameCanvas_CommandAdapter implements CommandListener {
private PlaneGameCanvas instance;
public PlaneGameCanvas_CommandAdapter(PlaneGameCanvas instance) {
this.instance = instance;
}
public void commandAction(Command command, Displayable displayable) {
instance.this_commandPerformed(command, displayable);
}
}
*/
class PlaneGameCanvas
extends Canvas
implements CommandListener, KeyRepeater, ActionListener
{
// save some calculated values for speed
private int m_maxX = getWidth() - 1;
private int m_maxY = getHeight() - 1;
private int m_imageWidth;
private int m_imageHeight;
// values we need for movement
private int m_x = m_maxX / 2;
private int m_y = m_maxY / 2;
// objects necessary for app
private AppExiter m_appExiter;
private KeyRepeat m_repeat = new KeyRepeat(this);
// private Constant cons = new Constant(); //in order to use self-defined constants
//actionUpDater,draw the screen every interval time
private static final ActionUpdater m_updater = new ActionUpdater(0, 10);
//Back ground mover
private final ImageScroller m_scroller;
boolean painting = false;
//*****MY PLANE******//
private MyPlane myplane;
private Image myplaneRed;
private Image myplaneExp;
//*****MY BULLET*****//
private Bullet[] mybullet = new Bullet[10];
private final BulletScroller b_scroller;
private boolean SendBulletTag;
//****ENEMY PLANES****//
private EnemyPlane[] ePlane = new EnemyPlane[5];
private final ePlaneScroller p_scroller;
//******ENEMY PLANE BULLET*****//
private Bullet[] eBullet = new Bullet[10];
//******ENEMY BOSS*******//
private EnemyBoss eBoss;
//******BOSS BULLET******//
private Bullet[] bossBullet = new Bullet[15];
private Image tempImage;
/**
* The constructor assigns the AppExiter, load the image, and initialies
* all the constants. It also adds the EXIT Command, and registers itself
* as a command listener.
*/
public PlaneGameCanvas(AppExiter exiter)
{
m_appExiter = exiter;
//**intiate my plane
myplane = new MyPlane(10, m_y);
try {
myplaneRed = Image.createImage("/myplane_red.PNG");
}
catch (Exception e) {
myplaneRed = null;
}
try {
myplaneExp = Image.createImage("/explosion.PNG");
}
catch (Exception e) {
myplaneExp = null;
}
//**intiate my plane's bullet array
for (int i = 0; i < mybullet.length; i++) {
mybullet[i] = new Bullet(0);
}
SendBulletTag = true;
//**initiate enemy plane array
for (int i = 0; i < ePlane.length; i++) {
ePlane[i] = new EnemyPlane();
}
//**initiate enemy boss
eBoss = new EnemyBoss(1, m_maxX, m_maxY / 2);
p_scroller = new ePlaneScroller(this, 50);
//**initiate enemy plane's bullet array
for (int i = 0; i < eBullet.length; i++) {
eBullet[i] = new Bullet(1);
}
//**initiat boss bullet array
for (int i = 0; i < bossBullet.length; i++) {
bossBullet[i] = new Bullet(2);
}
b_scroller = new BulletScroller(this, 50);
//** init back ground scroller
m_scroller = new ImageScroller(this, 200);
register(this);
//** add the EXIT command to the Canvas
addCommand(AppExiter.EXIT);
// register the Canavas as the listener for Commands, the action for
// the Commands will be handled in the commandAction() method
setCommandListener(this);
m_repeat.setSleepPeriod(20);
m_repeat.start();
}
protected void paint(Graphics g)
{
painting = true;
int x, y;
//*****clear the screen
g.setColor(0, 0, 0);
g.fillRect(0, 0, m_maxX + 1, m_maxY + 1);
//draw the background
m_scroller.paint(g);
//draw enemy planes
p_scroller.paint(g);
//draw bullets
b_scroller.paint(g);
// draw my plane
x = myplane.getXPos();
y = myplane.getYPos();
tempImage = myplane.getImage();
switch (myplane.getState()) {
case PlaneGameObject.ALIVE:
g.drawImage(tempImage, x, y,
Graphics.VCENTER | Graphics.HCENTER);
break;
case PlaneGameObject.HIT:
g.drawImage(myplaneRed, x, y,
Graphics.VCENTER | Graphics.HCENTER);
myplane.SetState(PlaneGameObject.ALIVE);
break;
case PlaneGameObject.BOMBING:
g.drawImage(myplaneExp, x, y,
Graphics.VCENTER | Graphics.HCENTER);
// Game Over
break;
}
painting = false;
}
protected void keyRepeated(int keyCode)
{
if (hasRepeatEvents()) {
moveMyPlane(getGameAction(keyCode));
m_repeat.stopRepeat(FIRE);
}
}
/**
* Called when a key is pressed.
*
* @param keyCode The key code of the key that was pressed.
*/
protected void keyPressed(int keyCode)
{
// if this MIDP implementation does not support repeated keys
// then start our own key repeatert
if (!hasRepeatEvents()) {
m_repeat.startRepeat(getGameAction(keyCode));
}
}
/**
* Called when a key is released.
*
* @param keyCode The key code of the key that was pressed.
*/
protected void keyReleased(int keyCode)
{
// if this MIDP implementation does not support repeated keys
// then stop our key repeater
if (!hasRepeatEvents()) {
m_repeat.stopRepeat(getGameAction(keyCode));
}
}
public boolean sendBullet(int a_x, int a_y, int tag)
{
int offset = myplane.getImageWidth() / 2;
switch (tag) {
case 0: //my plane Send bullet
for (int i = 0; i < mybullet.length; i++) { //find empty space
if (!mybullet[i].IsShow()) { //available space in array
mybullet[i].SetID(myplane.getBulletType()); //set id
mybullet[i].SetSpeed(10, 0);
mybullet[i].ShowTrue(); //take the space
mybullet[i].SetPos(a_x + offset, a_y);
return true; //!!break here if sent one bullet,very important
}
}
return false;
case 1: //enemy plane send bullet
for (int j = 0; j < eBullet.length; j++) {
if (!eBullet[j].IsShow()) {
eBullet[j].SetSpeed(3, 3);
eBullet[j].ShowTrue();
eBullet[j].SetPos(a_x, a_y);
return true;
}
} //for
return false;
case 2: //enemy boss send bullet
for (int i = 0; i < bossBullet.length; i++) {
if (!bossBullet[i].IsShow()) {
bossBullet[i].ShowTrue();
bossBullet[i].SetSpeed( -2, i / 3 - 2);
bossBullet[i].SetPos(a_x, a_y);
}
}
return true;
} //switch(tag)
return false;
} //sendBullet
public void moveMyPlane(int gameAction)
{
//change the data of my plane
int dx = 0, dy = 0;
int x_pos = myplane.getXPos();
int y_pos = myplane.getYPos();
int s = myplane.getSpeed();
switch (gameAction) {
case UP:
if (y_pos > 0) {
dy -= s;
}
if (SendBulletTag) {
if (sendBullet(x_pos, y_pos, 0)) {
SendBulletTag = false;
}
}
break;
case LEFT:
if (x_pos > 0) {
dx -= s;
}
break;
case DOWN:
if (y_pos < m_maxY) {
dy += s;
}
if (SendBulletTag) {
if (sendBullet(x_pos, y_pos, 0)) {
SendBulletTag = false;
}
}
break;
case RIGHT:
if (x_pos < m_maxX) {
dx += s;
}
break;
case FIRE:
if (SendBulletTag) {
if (sendBullet(x_pos, y_pos, 0)) {
SendBulletTag = false;
}
}
break;
}
myplane.ChangePos(dx, dy);
repaint();
}
//judge whether the two given objects collided
public boolean Collision(PlaneGameObject obj1, PlaneGameObject obj2)
{
int r1 = obj1.getRadius();
int r2 = obj2.getRadius();
int x1 = obj1.getXPos();
int x2 = obj2.getXPos();
int y1 = obj1.getYPos();
int y2 = obj2.getYPos();
int distance = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
if (distance < (r1 + r2) * (r1 + r2)) { //indicate that collided
return true; //return true
}
else {
return false;
}
} //Collision()
/**
* Called when a game action is to be performed.
*/
public void performAction()
{
repaint();
}
/**
* Register the DisplayObject with the action updater.
*
* @param obj The object to be registered.
*/
public static void register(ActionListener obj)
{
m_updater.register(obj);
}
public void commandAction(Command c, Displayable d)
{
if (c == AppExiter.EXIT) {
m_repeat.cancel();
m_repeat = null;
m_appExiter.exitApp();
}
}
//class BulletScroller controls the movement of my bullets by time
class BulletScroller
extends TimerTask
{
private final Timer m_updateTimer = new Timer();
private int ClockTag;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -