📄 gamecanvas.java
字号:
/*
* @(#)GameCanvas.java 1.0 04/07/01 @(#)
*
* Copyright 2004 NTT DoCoMo, Inc. All rights reserved.
*/
import java.util.Random;
import java.util.Vector;
import com.nttdocomo.ui.AudioPresenter;
import com.nttdocomo.ui.Canvas;
import com.nttdocomo.ui.Dialog;
import com.nttdocomo.ui.Display;
import com.nttdocomo.ui.Font;
import com.nttdocomo.ui.Graphics;
import com.nttdocomo.ui.Image;
import com.nttdocomo.ui.MediaListener;
import com.nttdocomo.ui.MediaPresenter;
/**
* GameCanvas<BR>
* The main part of the race game.
* <p>
* @version 1.0
* </p>
*/
public class GameCanvas extends Canvas implements Runnable,MediaListener {
/** Drawing interval */
private int sleepTime = 10;
/** Last stage number */
private static final int LAST_STAGE = 5;
/** Play time required for point get */
private static final long POINT_TIME = 1000;
/** Play time required for a stage clearance */
private static final long CLEAR_TIME = 16000;
/** Message drawing time */
private static final int MSG_DRAW_TIME = 2000;
/** State of the game */
private int gameStatus;
/** State of the game : playing */
private static int GAME_STATUS_PLAYING = 0;
/** State of the game : Failure */
private static int GAME_STATUS_FAILURE = 1;
/** State of the game : Stage clearance */
private static int GAME_STATUS_CLEAR = 2;
/** State of the game : Stage start */
private static int GAME_STATUS_READY = 3;
/** State of the game : Game over */
private static int GAME_STATUS_GAME_OVER = 4;
/** State of the game : Pause */
private static int GAME_STATUS_PAUSE = 5;
/** State of the game : Retry */
private static int GAME_STATUS_RETRY = 6;
/** State of the game : End */
private static int GAME_STATUS_END = 7;
/** State of the game : Check */
private static int GAME_STATUS_CONFIRM = 8;
/** Before being interrupted */
private int beforeGameStatus;
/** Stage number */
private int stage;
/** Stage play time */
private long stageTime;
/** Message drawing time */
private long msgDrawTime;
/** Number of message drawing */
private int msgDrawCount;
/** AudioPresenter for BGM performance */
private AudioPresenter bgm;
/** AudioPresenter for sound effects */
private AudioPresenter effect;
/** Size of a display */
private int dispSize[];
/** The position of the left end and the right end of the road */
private int[] course;
/** Width of the score drawing domain */
private int widthOfScoreArea;
/** Width of the score string */
private int scoreWidth;
/** Score drawing font */
private Font font;
/** Height of the score drawing font */
private int fontHeight;
/** The image which displays the stock number */
private Image stockimage;
/** Player */
private Player player;
/** Item */
private Vector item;
/** Goal */
private Goal goal;
/** Parent */
private CarRace parent = null;
/** Score */
private Score score = null;
/** Play time */
private long scoreTime;
/** Bonus point drawing font */
private Font bonusFont;
/** Random */
private Random rndm;
/** The graphic object for drawing a screen */
Graphics g;
/**
* Constructor.
* @param _parent Parent object
* @param _score Score object
*/
public GameCanvas(CarRace _parent, Score _score) {
this.parent = _parent;
this.score = _score;
/* Getting display size */
dispSize = new int[2];
dispSize[0] = this.getWidth();
dispSize[1] = this.getHeight();
/* Determination of a score drawing domain (1/4 of display size) */
widthOfScoreArea = dispSize[0] / 4;
font = Font.getDefaultFont();
scoreWidth = font.stringWidth("HI SCORE") + 6;
fontHeight = font.getHeight();
/* Determination of a road domain (right end -20 of the score drawing domain +20 to a display) */
course = new int[2];
course[0] = widthOfScoreArea + 20; /* left end */
course[1] = dispSize[0] - 20; /* right end */
/* Random */
rndm = new Random();
/* Generation of an item */
player = new Player(); /* Player */
item = new Vector(0); /* Item */
goal = new Goal(course[0]); /* Goal */
stockimage = MediaCollection.getImage(MediaCollection.IMG_PLAYER);
/* Getting bonus point drawing font */
bonusFont = Font.getFont(Font.SIZE_SMALL | Font.STYLE_BOLD);
/* Getting the graphic object for drawing a screen */
g = getGraphics();
/* Initialization of a sound */
bgm = AudioPresenter.getAudioPresenter();
bgm.setMediaListener(this);
bgm.setSound(MediaCollection.getSound(MediaCollection.SND_BGM));
effect = AudioPresenter.getAudioPresenter();
effect.setMediaListener(this);
effect.setSound(MediaCollection.getSound(MediaCollection.SND_GET_ITEM));
}
/*
* @see java.lang.Runnable#run()
*/
public void run() {
/* Initial setting */
reset();
/* Changes to game screen. */
Display.setCurrent(this);
/* Main loop */
while (GAME_STATUS_END != gameStatus) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
break;
}
control();
}
}
/**
* Initial setting of the game.
*/
private void reset() {
player.init(); /* Player */
resetItems(); /* Item */
goal.setEnabled(false); /* Goal */
score.reset(); /* Score */
stage = 1; /* Stage */
changeGameStatus(GAME_STATUS_READY); /* Status */
}
/**
* Elimination of all items.
*/
private void resetItems() {
item.removeAllElements();
item.trimToSize();
}
/**
* Distribution of the process.
*/
private void control() {
/* Playing */
if (GAME_STATUS_PLAYING == gameStatus) {
playing();
}
/* Failure */
else if (GAME_STATUS_FAILURE == gameStatus) {
crash();
}
/* Stage clearance */
else if (GAME_STATUS_CLEAR == gameStatus) {
clear();
}
/* Stage start */
else if (GAME_STATUS_READY == gameStatus) {
ready();
}
/* Game over */
else if (GAME_STATUS_GAME_OVER == gameStatus) {
gameOver();
}
/* Pause */
else if (GAME_STATUS_PAUSE == gameStatus) {
pause();
}
/* Retry */
else if (GAME_STATUS_RETRY == gameStatus) {
reset();
}
}
/**
* Control of each item in a race, and renewal of a game situation.
*/
private void playing() {
/* An item is generated */
createNewItem();
/*
* Starts buffering the contents of drawing.
* The contents of drawing to "unlock(true)" is buffered.
*/
g.lock();
/* Drawing the background */
drawBackground();
/* Drawing the goal point */
if (goal.isEnabled()) {
drawItem(goal);
}
/* Drawing the item */
AbstractItem temp;
for (int i = 0; i < item.size(); i++) {
temp = (AbstractItem)item.elementAt(i);
drawItem(temp);
}
/* Drawing the player */
drawItem(player);
/*
* Flashes the buffer.
*/
g.unlock(true);
/* Updating game situation */
updateGameStatus();
/* Moving the player */
refreshPlayer();
/* Moving items */
refreshItem();
/* Moving the goal point */
if (goal.isEnabled()) {
goal.move(stage);
}
}
/**
* Process when failure occurs
*/
private void crash() {
/* Starts buffering the contents of drawing */
g.lock();
/* Drawing the background */
drawBackground();
/* Drawing the goal point */
if (goal.isEnabled()) {
drawItem(goal);
}
/* Drawing items */
AbstractItem temp;
for (int i = 0; i < item.size(); i++) {
temp = (AbstractItem)item.elementAt(i);
drawItem(temp);
}
/* Drawing of a player (flame) */
msgDrawCount*=-1;
if (0 > msgDrawCount) {
g.setFlipMode(Graphics.FLIP_NONE);
} else {
g.setFlipMode(Graphics.FLIP_HORIZONTAL);
}
g.drawImage(player.getImage(), player.getPosX(), player.getPosY());
/* Buffering end */
g.unlock(true);
/* The next screen is displayed */
if (MSG_DRAW_TIME <= (System.currentTimeMillis() - msgDrawTime)) {
if (0 < player.getStock()) {
/* Preparing next stage */
changeGameStatus(GAME_STATUS_PLAYING); /* Game state */
player.reset(); /* Initialization of the player */
resetItems(); /* Elimination of all item */
/* The performance of a sound effect */
effect.stop();
bgm.play();
} else {
/* Game over */
score.save(); /* Preservation of the score */
changeGameStatus(GAME_STATUS_GAME_OVER); /* Game state */
/* The performance of the sound effect */
bgm.stop();
effect.stop();
effect.setSound(
MediaCollection.getSound(MediaCollection.SND_GAME_OVER)
);
effect.play();
}
}
}
/**
* Draws the message at the time of a stage clearance, and prepars next stage.
*/
private void clear() {
/* Buffering start */
g.lock();
/* Drawing the background */
drawBackground();
/* Drawing the goal point */
if (goal.isEnabled()) {
drawItem(goal);
}
/* Drawing the player */
drawItem(player);
/* Drawing the stage clear message */
drawMessage(MediaCollection.MSG_CLEAR);
/* Buffering end */
g.unlock(true);
/* Preparing the next contents of the display */
resetItems(); /* All elimination of an item */
if (goal.isEnabled()) { /* The goal is moved */
goal.move(stage);
}
/* Preparation of the next stage */
if (MSG_DRAW_TIME <= (System.currentTimeMillis() - msgDrawTime)) {
/* Last stage : end game */
if (LAST_STAGE == stage) {
changeGameStatus(GAME_STATUS_END);
parent.endGame();
return;
}
/* Next stage */
stage++;
changeGameStatus(GAME_STATUS_READY);
}
}
/**
* Initialize process at the time of stage start and drawing stage start screen are performed.
*/
private void ready() {
goal.setEnabled(false); /* Goal */
stageTime = 0; /* Start time */
scoreTime = 0; /* Play time */
player.reset(); /* Initialization of the player */
resetItems(); /* Eliminate all item */
/* Buffering start */
g.lock();
/* Drawing the background */
drawBackground();
/* Drawing the starting line */
g.setColor(Color.WHITE);
g.fillRect(course[0], player.getPosY() - 5, course[1] - course[0], 4);
/* Drawing the player */
drawItem(player);
/* Drawing the stage start message */
drawMessage(MediaCollection.MSG_READY);
/* Buffering end */
g.unlock(true);
}
/**
* Draws the game over message.
*/
private void gameOver() {
/* Buffering start */
g.lock();
/* Drawing the background */
drawBackground();
/* Drawing the game over message */
drawMessage(MediaCollection.MSG_GAME_OVER);
/* Buffering end */
g.unlock(true);
}
/**
* Draws a discontinuation message.
*/
private void pause() {
/* Buffering start */
g.lock();
/* Drawing the background */
drawBackground();
/* Drawing the goal */
if (goal.isEnabled()) {
drawItem(goal);
}
/* Drawing items */
AbstractItem temp;
for (int i = 0; i < item.size(); i++) {
temp = (AbstractItem)item.elementAt(i);
drawItem(temp);
}
/* Drawing the player */
g.drawImage(player.getImage(), player.getPosX(), player.getPosY());
/* Drawing a discontinuation message */
drawMessage(MediaCollection.MSG_PAUSE);
/* Buffering end */
g.unlock(true);
}
/**
* Displays a message.
* @param _messageId Message id
*/
private void drawMessage(int _messageId) {
Image img = MediaCollection.getImage(_messageId);
g.setFlipMode(Graphics.FLIP_NONE);
g.drawImage(img,
(dispSize[0] - img.getWidth()) / 2,
(dispSize[1] - img.getHeight()) / 2);
}
/**
* Generates new item/obstacle.
*/
private void createNewItem() {
AbstractItem temp;
/* Getting the number of items */
int cnt = getItemCount();
if (0 == cnt) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -