⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamescreen.java

📁 j2me手机程序eclipse开发基础》源代码
💻 JAVA
字号:
/*
 * 创建日期 2005-6-28
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package game;

import java.util.Random;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * @author Administrator
 *
 * TODO 要更改此生成的类型注释的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class GameScreen extends Canvas implements CommandListener {
	private static final int BLACK = 0x00000000;
	private static final int WHITE = 0x00FFFFFF;
	private static final int RED = 0x00FF0000;
	private static final int BLUE = 0x000000FF;
	private static final int NO_MOVE = -1;
	private final TriChessmanMIDlet 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 playerIsCat;
	private boolean computerIsCat;
	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(TriChessmanMIDlet midlet, boolean playerIsCat) {
		this.midlet = midlet;
		this.playerIsCat = playerIsCat;
		computerIsCat = !playerIsCat;
		
		// 新建Game对象
		game = new Game(random);
		
		// 初始化网格
		initializeBoard();

		// 添加屏幕命令
		exitCommand = new Command("退出", Command.EXIT, 1);
		newGameCommand = new Command("开始", Command.SCREEN, 2);
		addCommand(exitCommand);
		addCommand(newGameCommand);
		
		// 侦听命令按键
		setCommandListener(this);

		// 初始化屏幕显示
		initialize();
	}

	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) {
			// 以白色清空画布
			g.setColor(WHITE);
			g.fillRect(0, 0, screenWidth, screenHeight);
			
			// 绘制网格
			drawBoard(g);
			isRestart = false;
		}
		
		// 绘制光标
		drawCursor(g);
		
		// 绘制选手行棋位置
		if (playerMove != NO_MOVE) 
			drawPiece(g, playerIsCat, playerMove);
		
		// 绘制计算机行棋位置
		if (computerMove != NO_MOVE) 
			drawPiece(g, computerIsCat, computerMove);
	}

	private void paintGameOver(Graphics g){
		// 状态信息
		String statusMsg = null;
		
		// 计算机胜
		if (game.isComputerWinner()) {
			statusMsg = "你输了!";
			computerGamesWonTally++;
		}
		
		// 选手胜 
		else if (game.isPlayerWinner()) {
			statusMsg = "祝贺!你赢了!";
			playerGamesWonTally++;
		}
		
		// 平局
		else {
			statusMsg = "双方握手言和!";
			computerGamesWonTally++;
			playerGamesWonTally++;		
		}
		String tallyMsg = "战绩:人vs机 =" + playerGamesWonTally + ":" + 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;

		// 计算字符绘制位置 
		int x = (screenWidth - strWidth) / 2;
		x = x < 0 ? 0 : x;
		int y = (screenHeight - 2 * strHeight) / 2;
		y = y < 0 ? 0 : y;
		
		// 白色清空画布 
		g.setColor(WHITE);
		g.fillRect(0, 0, screenWidth, screenHeight);

		// 黑色显示信息
		g.setColor(BLACK);
		g.drawString(statusMsg, x, y, (Graphics.TOP | Graphics.LEFT));
		g.drawString(tallyMsg, x, (y + 1 + strHeight), (Graphics.TOP | Graphics.LEFT));
	}

	/* (非 Javadoc)
	 * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
	 */
	public void commandAction(Command arg0, Displayable arg1) {
		if (arg0 == exitCommand) {
			try {
				// 退出
				midlet.quit();
			} catch (MIDletStateChangeException e) {
				// TODO 自动生成 catch 块
				e.printStackTrace();
			}
		}
		else if (arg0 == 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) {
		// 游戏结束,不响应按键
		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)) {
			// 选手在该位置行棋
			game.makePlayerMove(cursorPosition);
			playerMove = cursorPosition;
			
			// 如果游戏没有结束,则计算机随即行棋
			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 = newCursorPosition;
			
			// 重绘
			repaint();
		}
	}

	private void drawPiece(Graphics g, boolean isCat, int pos) {
		// 计算绘制位置
		int x = ((pos % 3) * boardCellSize) + 3;
		int y = ((pos / 3) * boardCellSize) + 3;
		
		// 绘制图片
		if (isCat)
			drawCat(g, x, y);
		else 
			drawRabbit(g, x, y);
	}

	private void drawCat(Graphics g, int x, int y) {
		Image image = null;
		try 
		{
			// 装载图象
			image = Image.createImage("/cat.png");
		}
		catch (Exception e)	{}

		// 在指定位置绘制图象
		g.drawImage(image, x + 1, y + 1, 0);
	}
	
	private void drawRabbit(Graphics g, int x, int y) {
		Image image = null;
		try 
		{
			// 装载图象
			image = Image.createImage("/rabbit.png");
		}
		catch (Exception e)	{}

		// 在指定位置绘制图象
		g.drawImage(image, x + 1, y + 1, 0);
	}
	
	private void drawCursor(Graphics g) {
		// 检测光标移动的前一位置是否为空闲网格
		if (game.isFree(preCursorPosition))
		{
			// 以白线擦除黑线绘制的光标
			g.setColor(WHITE);
			g.drawRect(((preCursorPosition % 3) * boardCellSize) + 2 + boardLeft, ((preCursorPosition/3) * boardCellSize) + 2 + boardTop, boardCellSize - 3, boardCellSize - 3);
		}
		else
		{
			// 检测光标移动的前一位置是否为选手占用
			if (game.isPlayer(preCursorPosition))
			{
				// 根据选手所选图案进行重绘
				if (playerIsCat)
					drawCat(g, ((preCursorPosition % 3) * boardCellSize) + 2 + boardLeft, ((preCursorPosition/3) * boardCellSize) + 2 + boardTop);
				else
					drawRabbit(g, ((preCursorPosition % 3) * boardCellSize) + 2 + boardLeft, ((preCursorPosition/3) * boardCellSize) + 2 + boardTop);	
			}
			
			// 检测光标移动的前一位置是否为选手占用
			if (game.isComputer(preCursorPosition))
			{
				// 根据选手所选图案进行重绘
				if (playerIsCat)
					drawRabbit(g, ((preCursorPosition % 3) * boardCellSize) + 2 + boardLeft, ((preCursorPosition/3) * boardCellSize) + 2 + boardTop);	
				else
					drawCat(g, ((preCursorPosition % 3) * boardCellSize) + 2 + boardLeft, ((preCursorPosition/3) * boardCellSize) + 2 + boardTop);
			}
		}

		// 在光标当前所处位置以黑线绘制光标框
		g.setColor(BLACK);
		g.drawRect(((cursorPosition % 3) * boardCellSize) + 2 + boardLeft, ((cursorPosition/3) * boardCellSize) + 2 + boardTop, boardCellSize - 3, boardCellSize - 3);		
	}

	private void drawBoard(Graphics g) {
		// 白色清空画布
		g.setColor(WHITE);
		g.fillRect(0, 0, screenWidth, screenHeight);

		//	黑色绘制网格 
		g.setColor(BLACK);
		for (int i = 0;	i < 4;i++) {
			g.fillRect(boardLeft, boardCellSize * i + boardTop, (boardCellSize * 3) + 2, 2);
			g.fillRect(boardCellSize * i + boardLeft, boardTop, 2, boardCellSize * 3);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -