📄 gamescreen.java
字号:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import java.util.Vector;
public class GameScreen extends Canvas
{
private Vector actors;
private Ship playerShip;
private static GameScreen theGameScreen; // the one and only
public GameScreen()
{
theGameScreen = this;
// This code now uses a Vector instead of an array.
actors = new Vector();
playerShip = new Ship(false, 64, 64);
actors.addElement(playerShip);
// Now construct an enemy fighter.
actors.addElement(new Ship(true, 30, 30));
}
/**
* @return The player's Ship object.
*/
public Ship getPlayerShip()
{
return playerShip;
}
/**
* A static method to return the one and only instance of the GameScreen
* object.
*/
public final static GameScreen getGameScreen()
{
return theGameScreen;
}
/**
* Canvas paint method used to draw all the Actors on the screen.
* @param graphics The graphics context upon which to draw the Actors.
*/
protected void paint(Graphics graphics)
{
graphics.setColor(0);
graphics.fillRect(0, 0, getWidth(), getHeight());
for (int i = 0; i < actors.size(); i++)
((Actor)actors.elementAt(i)).render(graphics);
}
/**
* Cycle all the Actors using fixed timing of 100 milliseconds.
*/
public void cycle()
{
for (int i = 0; i < actors.size(); i++)
((Actor) actors.elementAt(i)).cycle(100);
}
/**
* Check whether the passed in Actor collides with any other Actor in the
* GameScreen.
* @param actorToCheck The Actor to check collisions against.
* @return True if actorToCheck is in a collision state.
*/
public boolean checkCollision(Actor actorToCheck)
{
if (actorToCheck instanceof Bullet) return false;
// did this ship hit another?
for (int j = 0; j < actors.size(); j++)
{
Actor another = (Actor)actors.elementAt(j);
if (another != actorToCheck && !(another instanceof Bullet) &&
Tools.isIntersectingRect(actorToCheck.getX(),
actorToCheck.getY(),
actorToCheck.getWidth(),
actorToCheck.getHeight(),
another.getX(), another.getY(),
another.getWidth(),
another.getHeight()))
return true;
}
return false;
}
/**
* Removes an Actor from the GameScreen.
* @param a The Actor to remove.
*/
public void removeActor(Actor a)
{
actors.removeElement(a);
}
/**
* Adds an Actor to the GameScreen.
* @param a The Actor to add.
*/
public void addActor(Actor a)
{
actors.addElement(a);
}
/**
* React to keys pressed by the user.
* @param keyCode The code of the key the players pressed.
*/
protected void keyPressed(int keyCode)
{
int action = getGameAction(keyCode);
if (action == RIGHT)
playerShip.setNegPeakSpin();
if (action == LEFT)
playerShip.setPeakSpin();
if (action == UP)
playerShip.setFiring(true);
}
/**
* React to key being released. For this example the code stops the spin.
* @param keyCode The code for the key that was released.
*/
protected void keyReleased(int keyCode)
{
int action = getGameAction(keyCode);
if (action == RIGHT)
playerShip.setSpin(0);
if (action == LEFT)
playerShip.setSpin(0);
if (action == UP)
playerShip.setFiring(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -