Java手机游戏编程之MIDP图形设计篇(5)
5、GameScreen.java
GameScreen使用了一个低级应用编程接口Canvas屏幕,和Image、Graphics类来绘制游戏面板、棋子,以及游戏的最终结果状态。要获取更详细的信息,请参阅各种绘画方法和drawCircle、drawCross、drawPiece、drawPlayerCursor、drawBoard等方法。这个屏幕使用MIDlet的quit回调方法来指示游戏结束。
此屏幕可适应各种可用显示性能(高、宽、色彩等)。此外还要注意到可以使用四向导航键,也可以使用双向导航键来移动光标。
它使用了封装了主游戏程序逻辑的Game类。
package example.tictactoe;
import java.util.Random;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
class GameScreen extends Canvas implements CommandListener {
private static final int BLACK = 0x00000000;
private static final int WH99vE = 0x00FFFFFF;
private static final int RED = 0x00FF0000;
private static final int BLUE = 0x000000FF;
private static final int NO_MOVE = -1;
private final TicTacToeMIDlet midlet;
private final Game game;
private final Command exitCommand;
private final Command newGameCommand;
private final Random random = new Random();
private int screenWidth, screenHeight;
private int boardCellSize, boardSize, boardTop, boardLeft;
private boolean playerIsCircle;
private boolean computerIsCircle;
private int preCursorPosition, cursorPosition;
private int computerMove = NO_MOVE;
private int playerMove = NO_MOVE;
private int computerGamesWonTally = 0;
private int playerGamesWonTally = 0;
private boolean isRestart;
public GameScreen(TicTacToeMIDlet midlet, boolean playerIsCircle) {
this.midlet = midlet;
this.playerIsCircle = playerIsCircle;
computerIsCircle = !playerIsCircle;
game = new Game(random);
initializeBoard();
// configure Screen commands
exitCommand = new Command("Exit", Command.EX99v, 1);
newGameCommand = new Command("New", Command.SCREEN, 2);
addCommand(exitCommand);
addCommand(newGameCommand);
setCommandListener(this);
// begin the game play initialize();
}
// Initialize the Game and Game screen. Also used for game restarts.
private void initialize() {
game.initialize();
preCursorPosition = cursorPosition = 0;
playerMove = NO_MOVE;
boolean computerFirst = ((random.nextInt() & 1) == 0);
if (computerFirst) {
computerMove = game.makeComputerMove();
}
else
{
computerMove = NO_MOVE;
}
isRestart = true;
repaint();
}
public void paint(Graphics g) {
if (game.isGameOver()) {
paintGameOver(g);
}
else {
paintGame(g);
}
}
private void paintGame(Graphics g) {
if (isRestart) {
// clean the canvas
g.setColor(WH99vE);
g.fillRect(0, 0, screenWidth, screenHeight);
drawBoard(g);
isRestart = false;
}
drawCursor(g);
if (playerMove != NO_MOVE) {
drawPiece(g, playerIsCircle, playerMove);
}
if (computerMove != NO_MOVE) {
drawPiece(g, computerIsCircle, computerMove);
}
}
private void paintGameOver(Graphics g)
{
String statusMsg = null;
if(game.isComputerWinner()) {
statusMsg = "I win !";
computerGamesWonTally++;
}
else if (game.isPlayerWinner()) {
statusMsg = "You win";
playerGamesWonTally++;
}
else {
statusMsg = "Stalemate";
}
String tallyMsg = "You:" + playerGamesWonTally + " Me:" + computerGamesWonTally;
Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
int strHeight = font.getHeight();
int statusMsgWidth = font.stringWidth(statusMsg);
int tallyMsgWidth = font.stringWidth(tallyMsg);
int strWidth = tallyMsgWidth;
if (statusMsgWidth > tallyMsgWidth)
{
strWidth = statusMsgWidth;
}
// Get the
{
x, y
}
position for painting the strings. int x = (screenWidth - strWidth) / 2;
x = x < 0 ? 0 : x;
int y = (screenHeight - 2 * strHeight) / 2;
y = y < 0 ? 0 : y;
// clean the canvas
g.setColor(WH99vE);
g.fillRect(0, 0, screenWidth, screenHeight);
// paint the strings' text
g.setColor(BLACK);
g.drawString(statusMsg, x, y, (Graphics.TOP | Graphics.LEFT));
g.drawString(tallyMsg, x, (y + 1 + strHeight), (Graphics.TOP | Graphics.LEFT));
}
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
midlet.quit();
}
else if (c == newGameCommand) {
initialize();
}
}
private void initializeBoard() {
screenWidth = getWidth();
screenHeight = getHeight();
if (screenWidth > screenHeight) {
boardCellSize = (screenHeight - 2) / 3;
boardLeft = (screenWidth - (boardCellSize * 3)) / 2;
boardTop = 1;
}
else {
boardCellSize = (screenWidth - 2) / 3;
boardLeft = 1;
boardTop = (screenHeight - boardCellSize * 3) / 2;
}
}
protected void keyPressed(int keyCode) {
// can't continue playing until the player restarts
if (game.isGameOver()) {
return;
}
int gameAction = getGameAction(keyCode);
switch (gameAction) {
case FIRE: doPlayerMove();
break;
case RIGHT: doMoveCursor(1, 0);
break;
case DOWN: doMoveCursor(0, 1);
break;
case LEFT: doMoveCursor(-1, 0);
break;
case UP: doMoveCursor(0, -1);
break;
default: break;
}
}
private void doPlayerMove() {
if (game.isFree(cursorPosition)) {
// player move game.
makePlayerMove(cursorPosition);
playerMove = cursorPosition;
// computer move
if (!game.isGameOver()) {
computerMove = game.makeComputerMove();
}
repaint();
}
}
private void doMoveCursor(int dx, int dy) {
int newCursorPosition = cursorPosition + dx + 3 * dy;
if ((newCursorPosition >= 0) && (newCursorPosition < 9))
{
preCursorPosition = cursorPosition;
cursorPosition = newCursor本文:http://www.qqread.com/j2ee/w226182.html
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
相关专题
- 手机病毒揭密 (3567篇文章)
- 游戏开发 (2064篇文章)
- 网络游戏攻略 (4276篇文章)
- Java环境安装配置 (5571篇文章)
- Java编程开发手册 (8196篇文章)
- 手机游戏开发 (65篇文章)
- 游戏策划 (287篇文章)
- 程序设计 (225篇文章)
- 图形图像 (91篇文章)
- 手机解决方案 (72篇文章)
- J2EE Server下的第一个EJB程序(图文进阶篇 (509次浏览)
- J2EE实战留言本(升级应用篇) (329次浏览)
- 如何开发基于JBoss的J2EE应用 (110次浏览)
- J2ME Mobile 3D入门教程(三)碰撞初步 (107次浏览)
- 使用JSR-238更快地全球化您的应用程序 (106次浏览)
- J2ME游戏中的图片处理 (105次浏览)
- GT4 开发:从头开始进行相互的身份验证 (104次浏览)
- 使用Java NIO提高服务端程序的性能 (103次浏览)
- J2ME RPG游戏边学边做(一) (70次浏览)
- J2ME中通过Http协议传输图片 (67次浏览)



