?? gamemanager.java
字號:
else
{
// After 4 seconds, prompt the user to return to main menu.
lastText = dict.getString(Dictionary.TEXT_GAME_QUIT_PROMPT);
}
drawShortMessage(g, color, winnerText,
(dict.getString(Dictionary.TEXT_GAME_YOU) + ": " + baseScore),
(dict.getString(Dictionary.TEXT_GAME_BLOCKS) + ": " +
blocksScore),
lastText);
}
else
{
// The game is still in progress.
drawGame(g);
}
}
private void drawBackground(Graphics g)
{
// The canvas may be bigger than the drawable game.
// We first erase the entire canvas with WHITE,
// and next draw a BLACK game board.
if ((gameWidth < canvas.getWidth()) ||
(gameHeight < canvas.getHeight()))
{
g.setColor(0xFFFFFF); // WHITE
g.fillRect(0, 0, canvas.getWidth(),
canvas.getHeight());
}
g.setColor(0x000000); // BLACK
g.fillRect(0, 0, gameWidth, gameHeight);
}
private void drawGame(Graphics g)
{
// Draw background
drawBackground(g);
// Draw bullets
Bullet b = (Bullet) (bullets.getFirst());
while (b != null)
{
b.draw(g);
b = (Bullet) bullets.getNext(b);
}
// Draw blocks
g.setColor(Base.COLOR);
for (int ix = 0; ix < blocks.size(); ix++)
{
Block block = (Block) blocks.elementAt(ix);
block.draw(g);
}
// At the bottom of the screen, either draw the blocks' score or
// draw the resume message after a pause.
g.setFont(GAME_FONT);
if (isPaused)
{
// draw the 'Push a key to resume' message after a pause
String str = dict.getString(Dictionary.TEXT_GAME_RESUME_PROMPT);
g.setColor(Base.COLOR);
g.drawString(str, gameWidth / 2, (gameHeight - 2),
(Graphics.BOTTOM | Graphics.HCENTER));
}
else
{
// draw the blocks' score
StringBuffer buf = new StringBuffer(((showLabelTicks > 0) ?
(dict.getString(Dictionary.TEXT_GAME_BLOCKS) + ": ") : ""));
buf.append(blocksScore);
g.setColor(Block.COLOR);
g.drawString(buf.toString(), (gameWidth - 2), (gameHeight - 2),
(Graphics.BOTTOM | Graphics.RIGHT));
}
// At the top of the screen:
// Draw the 'canon overheating gauge' + base's score and remaining
// lives in the upper left corner of the game screen.
// (Depending on showLabelTicks, longer or shorter versions
// of the strings are drawn.)
int yOffset = 2; // pixels
int xOffset = base.getWidth() + base.getCannonDimension() + yOffset;
int w = 0;
// draw 'cannon overheating' gauge (to limit fire rate)
if (useLimitedFiringRate)
{
// set gauge's height and width
int h = GAME_FONT.getHeight();
w = (2 * h) / 3;
int coolDownGaugeHeight = (h * base.getCannonCoolDownTicks()) /
Base.MAX_CANNON_COOLDOWN_TICKS;
int heatGaugeHeight = h * base.getCannonHeat() /
Base.MAX_CANNON_HEAT;
int xGauge = gameWidth - w - xOffset;
if (coolDownGaugeHeight > 0)
{
int color = 0x00FFFF; // cooling: CYAN
drawGauge(g, Base.COLOR, color, xGauge, yOffset, w, h,
coolDownGaugeHeight);
}
else
{
int color = Base.COLOR;
if (heatGaugeHeight > 0)
{
color = 0xFF55FF; // hot: MAGENTA(ish)
drawGauge(g, Base.COLOR, color, xGauge, yOffset, w, h,
heatGaugeHeight);
}
}
}
// draw base score/lives text
g.setColor(Base.COLOR);
StringBuffer buf = new StringBuffer((showLabelTicks > 0) ?
(dict.getString(Dictionary.TEXT_GAME_YOU) + ": ") : "");
buf.append(baseScore);
buf.append(" (");
buf.append(base.getLives());
buf.append((showLabelTicks > 0) ?
" " + dict.getString(Dictionary.TEXT_GAME_LIVES) : "");
buf.append(")");
g.setColor(Base.COLOR);
g.drawString(buf.toString(), xOffset, yOffset,
(Graphics.TOP | Graphics.LEFT));
// Draw base
base.draw(g);
}
private void drawGauge(Graphics g, int outerColor, int fillColor,
int x, int y, int w, int h, int gh)
{
// Draw the gauge so that it looks a bit like the
// ASCII art below. ('Y' is the base color (yellow) and
// 'f' is the fill color of the gauge.)
// YYYYYYYY
// f Y Y f
// f YffffffY f
// f YffffffY f
// YYYYYYYY
g.setColor(outerColor);
g.drawRect((x + 1), y, (w - 2), (h - 1));
g.setColor(fillColor);
g.drawLine(x, (y + 2), x, (y + h - 3));
g.drawLine((x + w), (y + 2), (x + w), (y + h - 3));
g.fillRect((x + 2), (y + 1 + h - gh), (w - 3), (gh - 2));
}
// Erase the entire canvas, and draw a short final message
// consisting of two lines of text. No attempt is made to fit the
// text onto the available canvas space, so the caller must
// use short text strings.
private void drawShortMessage(Graphics g, int color, String s1, String s2,
String s3, String s4)
{
drawBackground(g);
g.setColor(color);
g.setFont(GAME_FONT);
int fh = GAME_FONT.getHeight();
// yOffset needed to vertically center 4 lines of text
int yOffset = (gameHeight - (4 * fh)) / 2;
int xCenter = gameWidth / 2;
int anchor = (Graphics.TOP | Graphics.HCENTER);
g.drawString(s1, xCenter, yOffset, anchor);
g.drawString(s2, xCenter, yOffset + fh, anchor);
g.drawString(s3, xCenter, yOffset + (2 * fh), anchor);
if (s4 != null)
{
g.drawString(s4, xCenter, yOffset + (3 * fh), anchor);
}
}
void addBullet(Bullet b)
{
bullets.addFirst(b);
gameEffects.playBullet();
}
void doBlockPassed(Block block)
{
blocksScore += block.getPoints();
}
void closePressed()
{
// The GameManager handles the 'closePressed' event from one
// of two possible sources:
// - any softkey press in a NokiaCloseableCanvas (FullCanvas)
// indicates that 'close' was pressed
// - a CloseableCanvas (Canvas) uses its 'Back' command
// to indicate that close was pressed
// The 'closed' canvas is suspended but not destroyed.
// It can be resumed and shown on the display again later.
if (!isPaused)
{
pause();
}
midlet.gameManagerMainMenu(isGameOver);
}
public void keyPressed(int keyCode)
{
// When we return to an existing game screen from a 'Continue'
// in the MainMenu, the game remains paused though displayed.
// This gives the player time to put their fingers back
// onto the game keys before the game resumes.
// The first game key press resumes the game.
if (isPaused && canvas.isShown())
{
resume();
}
// The base handles game key presses with gameActionPressed;
// 'true' means 'game action pressed'.
base.gameActionPressed(canvas.getGameAction(keyCode), true);
}
public void keyReleased(int keyCode)
{
// The base handles game key releases with gameActionPressed.
// 'false' means 'game action released' (i.e. no longer pressed).
base.gameActionPressed(canvas.getGameAction(keyCode), false);
}
// Runnable
public void run()
{
Thread currentThread = Thread.currentThread();
try
{
// This ends when animationThread is set to null, or when
// it is subsequently set to a new thread; either way, the
// current thread should terminate.
while (currentThread == animationThread)
{
long startTime = System.currentTimeMillis();
if (!isPaused)
{
if (canvas.isShown())
{
// We shouldn't use a transition to the 'not shown'
// state to pause the game (see below), until we
// are first sure that we have been shown at
// some time.
if (!hasBeenShown)
{
hasBeenShown = true;
}
// We only tick if we are shown.
tick();
}
else
{
// The game canvas is no longer shown. We should
// pause the game since it is no longer visible.
// An example is when a system pop-up window like a
// low battery warning, incoming phone call, etc.
// occurs during the game.
// (The method keyPress is used to resume after
// such a pause.)
if (hasBeenShown)
{
pause();
}
}
}
// After each tick, we request a repaint.
// We do not request a repaint when we are not shown.
//
// (The repaint below also allows the paint method to print
// the "Press any key to resume" message after a pause
// caused by the "!isShown()" code just above,
// when the game canvas becomes shown again (e.g. after
// the system pop-up window disappears). The method keyPress
// causes the actual resume.)
if (canvas.isShown())
{
canvas.repaint(0, 0, gameWidth, gameHeight);
canvas.serviceRepaints();
}
long timeTaken = System.currentTimeMillis() - startTime;
if (timeTaken < MILLIS_PER_TICK)
{
synchronized(this)
{
wait(MILLIS_PER_TICK - timeTaken);
}
}
else
{
currentThread.yield();
}
}
}
catch (InterruptedException e)
{
}
}
public synchronized void start()
{
animationThread = new Thread(this);
animationThread.start();
gameEffects.resume();
}
public synchronized void stop()
{
animationThread = null;
gameEffects.pause();
}
public void pause()
{
synchronized(this)
{
isPaused = true;
}
gameEffects.pause();
}
public synchronized boolean isPaused()
{
return isPaused;
}
public void resume()
{
synchronized(this)
{
isPaused = false;
}
gameEffects.resume();
}
public Canvas getCanvas()
{
return canvas;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -