📄 gamecanvas.java
字号:
/* Generating an obstacle */
item.addElement(new Obstacle(course[0], course[1]));
return;
}
/* Generating an item, if the number of items is not more than the maximum */
if (cnt < stage + 1) {
/* The drawing position of the last drawn item */
int posY = Integer.MAX_VALUE;
for (int i = 0; i < item.size(); i++) {
temp = (AbstractItem)item.elementAt(i);
if (temp.getPosY() < posY) {
posY = temp.getPosY();
}
}
AbstractItem newItem;
int space = player.getImage().getHeight() * 2;
if (posY > space) {
/*
* Determing the kind of object to generate.
*/
if ((Math.abs(rndm.nextInt()) % 24) > (stage * 4)) {
newItem = new Item(course[0], course[1]);
} else {
newItem = new Obstacle(course[0], course[1]);
}
item.addElement(newItem);
}
}
}
/**
* Gets the number of the effective items currently displayed on the screen.
* <br>Returns the number of the items of which the status is <code>AbstractItem.STATUS_NORMAL</code>
* @return The number of items
*/
private int getItemCount() {
AbstractItem temp;
int count = 0;
for (int i = 0; i < item.size(); i++) {
temp = (AbstractItem)item.elementAt(i);
if (AbstractItem.STATUS_NORMAL == temp.getStatus()) {
count++;
}
}
return count;
}
/**
* Updates game status.
*/
private void updateGameStatus() {
/* Course out? */
if (player.isCourseOut(course[0], course[1])) {
changeGameStatus(GAME_STATUS_FAILURE);
} else {
AbstractItem temp;
/* Was the player collided with? */
for (int i = 0; i < item.size(); i++) {
temp = (AbstractItem)item.elementAt(i);
/* Collided */
if (temp.isContact(player, g)) {
/* item */
if (AbstractItem.TYPE_ITEM == temp.getType()) {
/* The performance of a sound effect */
effect.stop();
effect.setSound(
MediaCollection.getSound(
MediaCollection.SND_GET_ITEM
)
);
effect.play();
/* Bonus point */
if (Item.ATTR_BONUS == ((Item)temp).getAttr()) {
score.add(((Item)temp).getBonus());
/* 1up */
} else if (Item.ATTR_1UP == ((Item)temp).getAttr()) {
player.oneUp();
}
}
/* Obstacle */
else {
/* Failure */
changeGameStatus(GAME_STATUS_FAILURE);
player.fail();
}
}
}
}
/* Checking the score rise and stage clearance */
if (GAME_STATUS_FAILURE != gameStatus) {
/* Renewing play time */
scoreTime+=sleepTime;
stageTime+=sleepTime;
/* Adding points */
if (POINT_TIME <= scoreTime) {
score.add();
scoreTime = 0;
}
/* Setting goal */
if ((!goal.isEnabled()) &&
(CLEAR_TIME <= stageTime)) {
goal.setEnabled(true);
}
/* Goal ? */
if (goal.isEnabled() && goal.isContact(player, g)) {
/* The performance of the sound effect */
bgm.stop();
effect.stop();
effect.setSound(
MediaCollection.getSound(MediaCollection.SND_CLEAR)
);
effect.play();
changeGameStatus(GAME_STATUS_CLEAR);
}
/* Failure */
} else {
/* The performance of the sound effect */
bgm.stop();
effect.stop();
effect.setSound(
MediaCollection.getSound(MediaCollection.SND_FIRE)
);
effect.play();
}
}
/**
* Movement of the player.
*/
private void refreshPlayer() {
/* The move direction
* [0]丗up
* [1]丗down
* [2]丗left
* [3]丗right
*/
boolean[] direction = {false, false, false, false};
/* Getting keypad status */
int i = getKeypadState();
/* up or 2 */
if ((0 != (1 << Display.KEY_UP & i)) ||
(0 != (1 << Display.KEY_2 & i))) {
direction[0] = true;
}
/* down or 8 */
if ((0 != (1 << Display.KEY_DOWN & i)) ||
(0 != (1 << Display.KEY_8 & i))) {
direction[1] = true;
}
/* left or 4 */
if ((0 != (1 << Display.KEY_LEFT & i)) ||
(0 != (1 << Display.KEY_4 & i))) {
direction[2] = true;
}
/* right or 6 */
if ((0 != (1 << Display.KEY_RIGHT & i)) ||
(0 != (1 << Display.KEY_6 & i))) {
direction[3] = true;
}
/* 1 : Upper left */
if (0 != (1 << Display.KEY_1 & i)) {
direction[0] = true;
direction[2] = true;
}
/* 3 : Upper right */
if (0 != (1 << Display.KEY_3 & i)) {
direction[0] = true;
direction[3] = true;
}
/* 9 : Lower right */
if (0 != (1 << Display.KEY_9 & i)) {
direction[1] = true;
direction[3] = true;
}
/* 7 : Lower left */
if (0 != (1 << Display.KEY_7 & i)) {
direction[1] = true;
direction[2] = true;
}
if (true == direction[0]) {
player.move(Player.DIRECTION_UP, stage);
}
if (true == direction[1]) {
player.move(Player.DIRECTION_DOWN, stage);
}
if (true == direction[2]) {
player.move(Player.DIRECTION_LEFT, stage);
}
if (true == direction[3]) {
player.move(Player.DIRECTION_RIGHT, stage);
}
}
/**
* Moves all items.
*/
private void refreshItem() {
AbstractItem temp;
for (int i = 0; i < item.size(); i++) {
temp = (AbstractItem)item.elementAt(i);
temp.move(stage);
/* Deleting item */
if (temp.getPosY() > dispSize[1]) {
item.removeElementAt(i);
item.trimToSize();
}
}
}
/**
* Draws an item.
* @param temp Item
* @see AbstractItem#contact()
*/
private void drawItem(AbstractItem temp) {
/*
* Score up item
*/
if ((AbstractItem.STATUS_CONTACT == temp.getStatus()) &&
(AbstractItem.TYPE_ITEM == temp.getType()) &&
(Item.ATTR_BONUS == ((Item)temp).getAttr())) {
String bonus = "+" + ((Item)temp).getBonus();
int width = bonusFont.stringWidth(bonus);
int posX = course[0];
int posY = temp.getPosY() + temp.getImage().getHeight() / 2;
if (temp.getPosX() != course[0]) {
posX = course[1] - width;
}
/* drawing a bonus point */
g.setFont(bonusFont);
g.setColor(Color.BLACK);
g.drawString(bonus, posX, posY);
}
/*
* etc
*/
else {
/* Reversing right-and-left */
if ((AbstractItem.TYPE_OBSTACLE == temp.getType()) &&
(temp.getPosX() != course[0])) {
g.setFlipMode(Graphics.FLIP_HORIZONTAL);
} else {
g.setFlipMode(Graphics.FLIP_NONE);
}
g.drawImage(temp.getImage(), temp.getPosX(), temp.getPosY());
/* Saving color */
temp.setPixels(
g.getPixels(temp.getPosX(),
temp.getPosY(),
temp.getImage().getWidth(),
temp.getImage().getHeight(),
null,
0)
);
}
}
/**
* Draws the background.
*/
private void drawBackground() {
/* drawing score */
drawScore();
/* Painting between the road and the score domain. */
g.setColor(Color.LIME);
g.fillRect(widthOfScoreArea, 0, course[0], dispSize[1]);
/* Painting the road. */
g.setColor(Color.SILVER);
g.fillRect(course[0], 0, course[1] - course[0], dispSize[1]);
/* Painting the right side of the road. */
g.setColor(Color.LIME);
g.fillRect(course[1], 0, dispSize[0] - course[1], dispSize[1]);
}
/**
* Draws the score.
*/
private void drawScore() {
String str;
/* Setting font */
g.setFont(font);
/* Paint over the score domain */
g.setColor(Color.BLACK);
g.fillRect(0, 0, widthOfScoreArea, dispSize[1]);
/* Drawing the Hi Score */
g.setColor(Color.WHITE);
g.drawString("HI SCORE", 5, fontHeight);
str = String.valueOf(score.getHighScore());
g.drawString(str, scoreWidth - font.stringWidth(str), fontHeight * 2);
/* Drawing the present score */
str = String.valueOf(score.getScore());
g.setColor(Color.WHITE);
g.drawString("SCORE", 5, fontHeight * 4);
g.drawString(str, scoreWidth - font.stringWidth(str), fontHeight * 5);
/* Drawing the stage number */
g.setColor(Color.WHITE);
g.drawString("STAGE " + stage, 5, dispSize[0] / 2);
/* Drawing the number of stocks of the car */
g.setColor(Color.WHITE);
g.drawImage(stockimage,
5,
dispSize[1] - stockimage.getHeight() - 10);
g.drawString(" X " + player.getStock(),
5 + stockimage.getWidth(),
dispSize[1] - stockimage.getHeight() / 2 - 5);
}
/**
* Sets up a soft key label according to a game state.
* @param _newStatus Game state
*/
private void changeGameStatus(int _newStatus) {
gameStatus = _newStatus;
/* Playing */
if (GAME_STATUS_PLAYING == gameStatus) {
setSoftLabel(SOFT_KEY_1, "Return");
setSoftLabel(SOFT_KEY_2, "Pause");
}
/* Failure */
else if (GAME_STATUS_FAILURE == gameStatus) {
setSoftLabel(SOFT_KEY_1, "Return");
setSoftLabel(SOFT_KEY_2, "");
msgDrawTime = System.currentTimeMillis();
msgDrawCount = 1;
}
/* Stage clearance */
else if (GAME_STATUS_CLEAR == gameStatus) {
setSoftLabel(SOFT_KEY_1, "Return");
setSoftLabel(SOFT_KEY_2, "");
msgDrawTime = System.currentTimeMillis();
}
/* Stage start */
else if (GAME_STATUS_READY == gameStatus) {
setSoftLabel(SOFT_KEY_1, "Return");
setSoftLabel(SOFT_KEY_2, "Start");
}
/* Game over */
else if (GAME_STATUS_GAME_OVER == gameStatus) {
setSoftLabel(SOFT_KEY_1, "Return");
setSoftLabel(SOFT_KEY_2, "Retry");
}
/* Pause */
else if (GAME_STATUS_PAUSE == gameStatus) {
setSoftLabel(SOFT_KEY_1, "Return");
setSoftLabel(SOFT_KEY_2, "Resume");
}
}
/**
* The method called when an event occurs
* @param _type The kind of event
* @param _param Parameter
*/
public void processEvent(int _type, int _param) {
if (Display.KEY_PRESSED_EVENT == _type) {
if (Display.KEY_SOFT1 == _param) {
/*
* Soft key 1
*/
if (GAME_STATUS_PAUSE != gameStatus) {
beforeGameStatus = gameStatus;
}
changeGameStatus(GAME_STATUS_CONFIRM);
bgm.stop();
effect.stop();
Dialog dialog = new Dialog(Dialog.DIALOG_YESNO, "Check");
dialog.setText("Are you sure you want to return to the title screen?\n\n");
int ans = dialog.show();
if (Dialog.BUTTON_YES == ans) {
changeGameStatus(GAME_STATUS_END);
parent.endGame();
} else {
resume(beforeGameStatus);
}
} else if (Display.KEY_SOFT2 == _param) {
/*
* Soft key 2
*/
if (GAME_STATUS_PLAYING == gameStatus) {
bgm.stop();
effect.stop();
beforeGameStatus = gameStatus;
changeGameStatus(GAME_STATUS_PAUSE);
} else if (GAME_STATUS_READY == gameStatus) {
bgm.play();
effect.stop();
changeGameStatus(GAME_STATUS_PLAYING);
} else if (GAME_STATUS_GAME_OVER == gameStatus) {
bgm.play();
effect.stop();
changeGameStatus(GAME_STATUS_RETRY);
} else if (GAME_STATUS_PAUSE == gameStatus) {
changeGameStatus(beforeGameStatus);
if (GAME_STATUS_PLAYING == gameStatus) {
bgm.play();
}
}
}
} else if (Display.RESUME_VM_EVENT == _type) {
resume(gameStatus);
}
}
/**
* A game is resumed.
* @param _gameStatus The game state before discontinuation
*/
private void resume(int _gameStatus) {
if (GAME_STATUS_PLAYING == _gameStatus ||
GAME_STATUS_FAILURE == _gameStatus ||
GAME_STATUS_CLEAR == _gameStatus)
{
beforeGameStatus = _gameStatus;
changeGameStatus(GAME_STATUS_PAUSE);
}
else if (GAME_STATUS_PAUSE == _gameStatus) {
beforeGameStatus = GAME_STATUS_PLAYING;
changeGameStatus(GAME_STATUS_PAUSE);
}
else {
bgm.play();
changeGameStatus(_gameStatus);
}
}
/*
* @see com.nttdocomo.ui.Frame#paint(com.nttdocomo.ui.Graphics)
*/
public void paint(Graphics _g) {
}
/**
* The notice from a media presenter
* @param source Generating origin of a notice
* @param type The kind of event
* @param param The parameter of an event
*/
public void mediaAction(MediaPresenter source, int type, int param) {
/*
* Specifying the media presenter whom the event generated
*/
if (source.equals(bgm)) {
/*
* Specifying the event type
*/
if (AudioPresenter.AUDIO_COMPLETE == type) {
bgm.play();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -