📄 gamescreen.java
字号:
/*
* Siemens AG
* Mobile Radio Terminals
* Munich, Germany
* .AUTHOR Michael Becker CT SE 2 / Sam Nova (THQ)
* .PACKAGE GameAPI_Demo
* .STATUS DRAFT
* .CHANGE_CONTROL
* Version | Date | Changed by | Reason for Change
* 1.0 21.05.01 M.Becker file created.
* 1.1 26.06.01 Jan Eichholz Math.randomInt removed
*/
package GameAPI_Demo;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import com.siemens.mp.game.*;
class GameScreen extends MyScreen
{
private static final int HERO_X = 10;
private static final int HERO_FRAMES[] = { 0,1,2,1,-1 };
private int heroFrameIndex = 0;
private int heroFrameDelay = 0;
private static final int MAX_SHOTS = 10;
private static final int MAX_ALIENS = 3;
private static final int MAX_EXPLOS = 5;
public boolean bRunning, gameOver=false;
private boolean readyToPaint = false;
private static final int GAME_SCREEN_WIDTH = 96;
private static final int GAME_SCREEN_HEIGHT = 56;
private Image gameScreenImage;
private ExtendedImage gameScreen = null;
private GraphicObjectManager gfxManager;
private byte [] background;
private byte [] heroPixels, heroMask;
private byte [] shotPixels, shotMask;
private byte [] exploPixels, exploMask;
private Sprite heroSpr;
private Sprite [] shotsSpr;
private int [] shotsX;
private int [] shotsY;
private boolean [] shotsActive;
private int shotDelay = 0;
private byte [] aliensPixels, aliensMask;
private int [] aliensX, aliensY, aliensFrame, aliensType, aliensFrameDelay;
private boolean [] aliensActive;
private Sprite [] aliensSpr;
private int aliensDelay = 10;
private static final int ALIENS_ANIM_DELAY = 5;
private static final int EXPLO_ANIM_DELAY = 3;
private int [] exploX, exploY, exploFrame, exploFrameDelay;
private Sprite [] exploSpr;
private int state, nFrameCounter;
private boolean bFirePressed = false, bUpPressed = false, bDownPressed = false;
private static final int PRESSED = 1;
private static final int RELEASED = 0;
private static final int REPEATED = 1;
private int currentKey, currentKeyState = RELEASED;
private int heroY;
public GameScreen()
{
// new gameScreen
try
{
gfxManager= new GraphicObjectManager();
gameScreenImage = Image.createImage(GAME_SCREEN_WIDTH, GAME_SCREEN_HEIGHT);
gameScreen = new ExtendedImage(gameScreenImage);
gameScreen.clear((byte)0);
background = ReadByteArray("res\\game.bin", 672, 0);
heroPixels = ReadByteArray("res\\sship.bin", 78, 0);
heroMask = ReadByteArray("res\\sship.mask", 78, 0);
heroSpr = new Sprite(heroPixels,0, 16, 13, heroMask,0, 3);
heroY = 20;
heroSpr.setPosition(HERO_X, heroY);
// Load the SHOT
shotPixels = ReadByteArray("res\\shot.bin", 3, 0);
shotMask = ReadByteArray("res\\shot.mask", 3, 0);
// Load the byte arrays for the aliens
aliensPixels = ReadByteArray("res\\aliens.bin", 312, 0);
aliensMask = ReadByteArray("res\\aliens.mask", 312, 0);
// Load the byte arrays for the aliens
exploPixels = ReadByteArray("res\\explo.bin", 130, 0);
exploMask = ReadByteArray("res\\explo.mask", 130, 0);
shotsSpr = new Sprite[MAX_SHOTS];
shotsX = new int[MAX_SHOTS];
shotsY = new int[MAX_SHOTS];
shotsActive = new boolean[MAX_SHOTS];
for (int i = 0; i < MAX_SHOTS; i++)
{
shotsSpr[i] = new Sprite(shotPixels,0, 8,3, shotMask,0, 1);
shotsSpr[i].setVisible(false);
shotsSpr[i].setCollisionRectangle(0,0,6,3);
gfxManager.addObject(shotsSpr[i]);
}
aliensX = new int[MAX_ALIENS];
aliensY = new int[MAX_ALIENS];
aliensFrame = new int[MAX_ALIENS];
aliensType = new int[MAX_ALIENS];
aliensActive = new boolean[MAX_ALIENS];
aliensFrameDelay = new int[MAX_ALIENS];
aliensSpr = new Sprite[MAX_ALIENS];
for (int i = 0; i < MAX_ALIENS; i++)
{
aliensSpr[i] = new Sprite(aliensPixels,0, 16,13, aliensMask,0, 12);
aliensSpr[i].setVisible(false);
gfxManager.addObject(aliensSpr[i]);
aliensActive[i] = false;
}
exploX = new int[MAX_EXPLOS];
exploY = new int[MAX_EXPLOS];
exploFrame = new int[MAX_EXPLOS];
exploFrameDelay = new int[MAX_EXPLOS];
exploSpr = new Sprite[MAX_EXPLOS];
for (int i = 0; i < MAX_EXPLOS; i++)
{
exploSpr[i] = new Sprite(exploPixels,0, 16,13, exploMask,0, 5);
exploSpr[i].setVisible(false);
gfxManager.addObject(exploSpr[i]);
}
gfxManager.addObject(heroSpr);
}catch(Exception e)
{
System.out.println("Exception: "+e);
}
System.gc();
readyToPaint = true;
}
public void Dispose()
{
gameScreen = null;
// We should remove all objects from the gfx manager here
gfxManager = null;
}
protected void keyPressed(int keyCode)
{
// System.out.println("Pressed " + keyCode);
currentKey = keyCode;
switch(getGameAction(keyCode))
{
case Canvas.FIRE : currentKey = Canvas.FIRE; break;
case Canvas.UP : currentKey = Canvas.UP ; break;
case Canvas.DOWN : currentKey = Canvas.DOWN; break;
}
currentKeyState = PRESSED;
if (currentKey == Canvas.FIRE)
bRunning = false;
}
protected void keyReleased(int keyCode)
{
//System.out.println("Released " + keyCode);
currentKey = keyCode;
switch(getGameAction(keyCode))
{
case Canvas.FIRE : currentKey = Canvas.FIRE; break;
case Canvas.UP : currentKey = Canvas.UP ; break;
case Canvas.DOWN : currentKey = Canvas.DOWN; break;
}
currentKeyState = RELEASED;
}
protected void keyRepeated(int keyCode)
{
// System.out.println("Repeat " + keyCode);
// currentKey = getGameAction(keyCode);
// if (currentKey == 0)
// currentKey = keyCode;
//currentKeyState = REPEATED;
}
private void addShot()
{
// Go through all the shots and find a none active
for (int i = 0; i < MAX_SHOTS; i++)
if (shotsActive[i] == false)
{
shotDelay = 15;
shotsActive[i] = true;
shotsX[i] = HERO_X + 11;
shotsY[i] = heroY + 6;
shotsSpr[i].setVisible(true);
// Lets vibrate a bit
Vibrator.triggerVibrator(100);
return;
}
}
private void addExplo(int x, int y)
{
// Go through all the shots and find a none active
for (int i = 0; i < MAX_EXPLOS; i++)
if (exploSpr[i].getVisible() == false)
{
exploX[i] = x;
exploY[i] = y;
exploFrame[i] = 0;
exploFrameDelay[i] = EXPLO_ANIM_DELAY;
exploSpr[i].setVisible(true);
exploSpr[i].setFrame(0);
// Lets vibrate a bit
Vibrator.triggerVibrator(250);
return;
}
}
private boolean addAlien()
{
for (int i = 0; i < MAX_ALIENS; i++)
if (aliensActive[i] == false)
{
aliensActive[i] = true;
aliensX[i] = 96;
aliensY[i] = Math.abs(GameAPI_Demo.rand.nextInt()) % (GAME_SCREEN_HEIGHT - 13);
aliensType[i] = Math.abs(GameAPI_Demo.rand.nextInt()) % 4;
aliensFrame[i] = 0;
aliensFrameDelay[i] = ALIENS_ANIM_DELAY ;
aliensSpr[i].setVisible(true);
aliensSpr[i].setFrame((aliensType[i] * 3) + aliensFrame[i]);
// Set the correct collision rectangle for this type
switch(aliensType[i])
{
case 0 : aliensSpr[i].setCollisionRectangle(3,2,12,8); break;
case 1 : aliensSpr[i].setCollisionRectangle(1,4,12,5); break;
case 2 : aliensSpr[i].setCollisionRectangle(3,3,8,6); break;
case 3 : aliensSpr[i].setCollisionRectangle(3,3,11,5); break;
}
return true;
}
return false;
}
private void updateSprite()
{
boolean fire = false, up = false, down = false;
if (currentKeyState != RELEASED)
{
switch(currentKey)
{
case Canvas.KEY_NUM2 : up = true; break;
case Canvas.UP: up = true; break;
case Canvas.KEY_NUM8 : down = true; break;
case Canvas.DOWN: down = true; break;
case Canvas.KEY_NUM5 : fire = true; break;
}
}
if (up && heroY > 0)
heroY--;
if (down && heroY < GAME_SCREEN_HEIGHT - 13)
heroY++;
if (shotDelay != 0)
shotDelay--;
if (shotDelay == 0 && fire)
addShot();
// Handle shots
for (int i = 0; i < MAX_SHOTS; i++)
{
if (shotsActive[i])
{
shotsX[i]+=2;
if (shotsX[i] >= 96)
{
shotsActive[i] = false;
shotsSpr[i].setVisible(false);
}
//System.out.println("Shot " + i + " @ " + shotsX[i] + "," + shotsY[i]);
shotsSpr[i].setPosition(shotsX[i], shotsY[i]);
}
}
// Handle aliens
if (aliensDelay == 0)
{
// Lets see if we can activate a alien
if (addAlien())
aliensDelay = 24;
}
else
{
aliensDelay--;
}
for (int i = 0; i < MAX_ALIENS; i++)
{
if (aliensActive[i])
{
aliensX[i]--;
if (aliensX[i] == -16)
{
aliensActive[i] = false;
aliensSpr[i].setVisible(false);
}
else
{
aliensSpr[i].setPosition(aliensX[i], aliensY[i]);
if (aliensFrameDelay[i] > 0)
aliensFrameDelay[i]--;
else
{
aliensFrameDelay[i] = ALIENS_ANIM_DELAY;
aliensFrame[i]++;
if (aliensFrame[i] == 3)
aliensFrame[i] = 0;
aliensSpr[i].setFrame((aliensType[i] * 3) + aliensFrame[i]);
}
}
//check for collision hero with alien
if (heroSpr.isCollidingWith(aliensSpr[i]))
{
// YES...
// Lets just disable both..
aliensSpr[i].setVisible(false);
aliensActive[i] = false;
heroSpr.setVisible(false);
gameOver = true; //GameOver
addExplo(HERO_X, heroY);
}
}
}
// Check collision between shots and the aliens
for (int s = 0; s < MAX_SHOTS; s++)
{
if (shotsActive[s])
{
// Okay, shot is active.. now go through all aliens
for (int a = 0; a < MAX_ALIENS; a++)
{
if (aliensActive[a])
{
// if (shotsSpr[s].isCollidingWith(aliensSpr[a]))
if (aliensSpr[a].isCollidingWithPos(shotsX[s] + 5, shotsY[s] + 1))
{
// YES...
// Lets just disable both..
aliensSpr[a].setVisible(false);
aliensActive[a] = false;
shotsSpr[s].setVisible(false);
shotsActive[s] = false;
addExplo(aliensX[a], aliensY[a]);
}
}
}
}
}
// Handle explosions
for (int i = 0; i < MAX_EXPLOS; i++)
{
if (exploSpr[i].getVisible())
{
if (exploFrameDelay[i] > 0)
exploFrameDelay[i]--;
else
{
exploFrameDelay[i] = EXPLO_ANIM_DELAY;
exploFrame[i]++;
if (exploFrame[i] == 5)
exploSpr[i].setVisible(false);
exploSpr[i].setFrame(exploFrame[i]);
}
exploSpr[i].setPosition(exploX[i], exploY[i]);
}
}
// Handle hero
heroSpr.setPosition(HERO_X, heroY);
if (heroFrameDelay == 0)
{
heroFrameDelay = 5;
heroFrameIndex++;
if (HERO_FRAMES[heroFrameIndex] == -1)
heroFrameIndex = 0;
heroSpr.setFrame(HERO_FRAMES[heroFrameIndex]);
}
else
heroFrameDelay--;
}
/**
* Method declaration
*
*
* @param g
*
* @see
*/
public void paint(Graphics g)
{
//no normal painting used!
//paint all Sprites (no repaint necessary!)
if (gameScreen != null && readyToPaint)
{
gameScreen.blitToScreen(0,0);
}
}
public int getReturnValue()
{
return 0;
}
public void run()
{
bRunning = true;
nFrameCounter = 0;
while (bRunning)
{
try
{
if (gameOver)
{
//GameOver
gameScreen.getImage().getGraphics().drawString("Game Over!", 20, 20, Graphics.LEFT | Graphics.TOP);
gameScreen.blitToScreen(0,13);
Thread.sleep(4000);
bRunning=false;
}
Thread.yield();
//Thread.sleep(20);
}
catch (Exception exc)
{
}
//move the sprites
nFrameCounter++;
updateSprite();
//paint all Sprites (no repaint necessary!)
try
{
gameScreen.setPixels(background, 0,0, GAME_SCREEN_WIDTH, GAME_SCREEN_HEIGHT);
gfxManager.paint(gameScreen, 0, 0);
gameScreen.blitToScreen(0,13);
}catch(Exception e)
{
System.out.println("Exception: "+e);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -