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

📄 gamecanvas.java

📁 手机版象棋游戏采用MIDP1.0开发
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.util.Vector;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class GameCanvas extends Canvas implements CommandListener {
	protected Game game;

	String color = "";

	protected int empty;// 屏幕右侧留的空间

	protected int x;// 棋盘输出的坐标

	private boolean myTurn = false;

	protected int cellWidth;// 每个棋格的边长

	protected int mapWidth, canvasW;// 棋盘的宽度和画布的宽度

	protected int a, b, c, d;// 这是画炮下面的那几个折线,没什么用

	protected int chessR;// 棋子的半径

	private int desknum = -1; // 桌子序号

	private int seatPos = -1; // 座位序号

	private boolean banker = false;

	protected int selectedX, selectedY;// 选择框在棋盘格局上的x,y位置

	protected static int i, j;

	protected int m, n, p;// 记住开始的selectedX,selectedY和point[selectedX][selectedY]

	protected String q;// 记住word[selectedX][selectedY]

	protected int guard, guard1, guard2, g, g1;// 标记FIRE被按了多少次,g是用来判断走直线时前后的棋子,中间是否有其他棋子的累加器

	protected static int g2, isRedWin, isWhiteWin;// g2表示该谁走了,后面那俩顾名思义了

	private Client client;

	protected Command exitCmd, start, ok;

	private int point[][];

	protected String[][] word;

	public GameCanvas() {
	};

	public void init() {
		// desknum = -1;
		// seatPos = -1;
		addCommand(start);
		addCommand(exitCmd);
		initWord();
		banker = false;
		myTurn = false;
	}

	public void reset() {
		initWord();
		addCommand(start);
		addCommand(exitCmd);
		banker = false;
		myTurn = false;
	}

	public GameCanvas(Game game, Client client)// 构造函数
	{
		this.game = game;
		this.client = client;

		empty = getWidth() / 6;
		x = empty * 1 / 3;
		canvasW = getWidth() - empty;
		mapWidth = canvasW - canvasW % 8;
		cellWidth = mapWidth / 8;
		a = cellWidth * 2 / 5;
		b = cellWidth / 8;
		c = cellWidth - a;
		d = cellWidth - b;
		chessR = cellWidth * 2 / 5;
		selectedX = 4;
		selectedY = 7;
		guard = 0;
		guard1 = selectedX;
		guard2 = selectedY;
		m = guard1;
		n = guard2;
		word = new String[10][9];
		g2 = 1;
		initWord();
		exitCmd = new Command("退出", Command.EXIT, 0);
		start = new Command("开始", Command.OK, 1);
		addCommand(start);
		addCommand(exitCmd);
		setCommandListener(this);
	}

	public void initWord() {
		point = new int[][] { { 1, 2, 3, 4, 5, 6, 7, 8, 9 },// 初始化INT数组
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 10, 0, 0, 0, 0, 0, 11, 0 },
				{ 12, 0, 13, 0, 14, 0, 15, 0, 16 },
				{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
				{ 28, 0, 29, 0, 30, 0, 31, 0, 32 },
				{ 0, 26, 0, 0, 0, 0, 0, 27, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
				{ 17, 18, 19, 20, 21, 22, 23, 24, 25 } };

		for (i = 0; i < 10; i++)// 初始化字符数组
		{
			for (j = 0; j < 9; j++) {
				if (i == 0) {
					if (j == 0) {
						word[i][j] = "车";
					}
					if (j == 1) {
						word[i][j] = "马";
					}
					if (j == 2) {
						word[i][j] = "相";
					}
					if (j == 3) {
						word[i][j] = "士";
					}
					if (j == 4) {
						word[i][j] = "帅";
					}
					if (j == 8) {
						word[i][j] = "车";
					}
					if (j == 7) {
						word[i][j] = "马";
					}
					if (j == 6) {
						word[i][j] = "相";
					}
					if (j == 5) {
						word[i][j] = "士";
					}
				}
				if (i == 1) {
					word[i][j] = "空";
				}
				if (i == 2) {
					if ((j != 1) & (j != 7)) {
						word[i][j] = "空";
					}
					if (j == 1) {
						word[i][j] = "炮";
					}
					if (j == 7) {
						word[i][j] = "炮";
					}
				}
				if (i == 3) {
					if (j % 2 == 0) {
						word[i][j] = "卒";
					}
					if (j % 2 == 1) {
						word[i][j] = "空";
					}
				}
				if (i == 4) {
					word[i][j] = "空";
				}
				if (i == 5) {
					word[i][j] = "空";
				}
				if (i == 6) {
					if (j % 2 == 0) {
						word[i][j] = "卒";
					}
					if (j % 2 == 1) {
						word[i][j] = "空";
					}
				}
				if (i == 7) {
					if ((j != 1) & (j != 7)) {
						word[i][j] = "空";
					}
					if (j == 1) {
						word[i][j] = "炮";
					}
					if (j == 7) {
						word[i][j] = "炮";
					}
				}
				if (i == 8) {
					word[i][j] = "空";
				}
				if (i == 9) {
					if (j == 0) {
						word[i][j] = "车";
					}
					if (j == 1) {
						word[i][j] = "马";
					}
					if (j == 2) {
						word[i][j] = "相";
					}
					if (j == 3) {
						word[i][j] = "士";
					}
					if (j == 4) {
						word[i][j] = "帅";
					}
					if (j == 8) {
						word[i][j] = "车";
					}
					if (j == 7) {
						word[i][j] = "马";
					}
					if (j == 6) {
						word[i][j] = "相";
					}
					if (j == 5) {
						word[i][j] = "士";
					}
				}

			}
		}
	}

	public int getDeskIndex() {
		return desknum;
	}

	public void setDeskIndex(int i) {
		desknum = i;
	}

	public void setSeatPos(int i) {
		seatPos = i;
	}

	public int getSeatPos() {
		return seatPos;
	}

	protected void paintMapa(Graphics g)// 画河的上半部分的棋盘
	{
		for (int q = 0; q < 4; q++) {
			for (int w = 0; w < 8; w++) {
				g.setColor(128, 128, 128);
				g.drawRect(x + w * cellWidth, x + q * cellWidth, cellWidth,
						cellWidth);
			}
		}
		g.setColor(128, 128, 128);
		g.drawLine(x + 3 * cellWidth, x, x + 5 * cellWidth, x + 2 * cellWidth);
		g.drawLine(x + 5 * cellWidth, x, x + 3 * cellWidth, x + 2 * cellWidth);

		// 画左上方的炮
		g.drawLine(x + d, x + cellWidth + c, x + d, x + cellWidth + d);// 左上竖
		g.drawLine(x + c, x + cellWidth + d, x + d, x + cellWidth + d);// 左上横

		g.drawLine(x + d + 2 * b, x + cellWidth + c, x + d + 2 * b, x
				+ cellWidth + d);// 右上竖
		g.drawLine(x + cellWidth + b, x + cellWidth + d, x + cellWidth + a, x
				+ cellWidth + d);// 右上横

		g.drawLine(x + d, x + 2 * cellWidth + b, x + d, x + 2 * cellWidth + a);// 左下竖
		g.drawLine(x + c, x + cellWidth + d + 2 * b, x + d, x + cellWidth + d
				+ 2 * b);// 左下横

		g.drawLine(x + d + 2 * b, x + 2 * cellWidth + b, x + d + 2 * b, x + 2
				* cellWidth + a);// 右下竖
		g.drawLine(x + cellWidth + b, x + cellWidth + d + 2 * b, x + cellWidth
				+ a, x + cellWidth + d + 2 * b);// 右下横

		// 画右上方的炮

		g.drawLine(x + d + 6 * cellWidth, x + cellWidth + c, x + d + 6
				* cellWidth, x + cellWidth + d);
		g.drawLine(x + c + 6 * cellWidth, x + cellWidth + d, x + d + 6
				* cellWidth, x + cellWidth + d);

		g.drawLine(x + d + 2 * b + 6 * cellWidth, x + cellWidth + c, x + d + 2
				* b + 6 * cellWidth, x + cellWidth + 13 + 9);
		g.drawLine(x + cellWidth + b + 6 * cellWidth, x + cellWidth + d, x
				+ cellWidth + a + 6 * cellWidth, x + cellWidth + d);

		g.drawLine(x + d + 6 * cellWidth, x + 2 * cellWidth + b, x + d + 6
				* cellWidth, x + 2 * cellWidth + a);
		g.drawLine(x + c + 6 * cellWidth, x + cellWidth + d + 2 * b, x + d + 6
				* cellWidth, x + cellWidth + d + 2 * b);

		g.drawLine(x + d + 2 * b + 6 * cellWidth, x + 2 * cellWidth + b, x + d
				+ 2 * b + 6 * cellWidth, x + 2 * cellWidth + a);
		g.drawLine(x + cellWidth + b + 6 * cellWidth,
				x + cellWidth + d + 2 * b, x + cellWidth + a + 6 * cellWidth, x
						+ cellWidth + d + 2 * b);
	}

	protected void paintMapb(Graphics g)// 画那条河--楚河,哈哈
	{
		g.setColor(128, 128, 128);
		g.drawRect(x, x + 4 * cellWidth, mapWidth, cellWidth);
		g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD,
				Font.SIZE_LARGE));
		g.drawString("楚河       汉界", getWidth() / 2, x + 4 * cellWidth
				+ cellWidth * 3 / 4, Graphics.HCENTER | Graphics.BASELINE);
	}

	protected void paintMapc(Graphics g)// 画河的下半部分的棋盘
	{
		for (int q = 0; q < 4; q++) {
			for (int w = 0; w < 8; w++) {
				g.setColor(128, 128, 128);
				g.drawRect(x + w * cellWidth, x + (q + 5) * cellWidth,
						cellWidth, cellWidth);
			}
		}
		g.setColor(128, 128, 128);
		g.drawLine(x + 3 * cellWidth, x + 7 * cellWidth, x + 5 * cellWidth, x
				+ 9 * cellWidth);
		g.drawLine(x + 5 * cellWidth, x + 7 * cellWidth, x + 3 * cellWidth, x
				+ 9 * cellWidth);

		// 画左上方的炮
		g.drawLine(x + d, x + 6 * cellWidth + c, x + d, x + 6 * cellWidth + d);// 左上竖
		g.drawLine(x + c, x + 6 * cellWidth + d, x + d, x + 6 * cellWidth + d);// 左上横

		g.drawLine(x + d + 2 * b, x + 6 * cellWidth + c, x + d + 2 * b, x + 6
				* cellWidth + d);// 右上竖
		g.drawLine(x + cellWidth + b, x + 6 * cellWidth + d, x + cellWidth + a,
				x + 6 * cellWidth + d);// 右上横

		g.drawLine(x + d, x + 7 * cellWidth + b, x + d, x + 7 * cellWidth + a);// 左下竖
		g.drawLine(x + c, x + 6 * cellWidth + d + 2 * b, x + d, x + 6
				* cellWidth + d + 2 * b);// 左下横

		g.drawLine(x + d + 2 * b, x + 7 * cellWidth + b, x + d + 2 * b, x + 7
				* cellWidth + a);// 右下竖
		g.drawLine(x + cellWidth + b, x + 6 * cellWidth + d + 2 * b, x
				+ cellWidth + a, x + 6 * cellWidth + d + 2 * b);// 右下横

		// 画右上方的炮

		g.drawLine(x + d + 6 * cellWidth, x + 6 * cellWidth + c, x + d + 6
				* cellWidth, x + 6 * cellWidth + d);
		g.drawLine(x + c + 6 * cellWidth, x + 6 * cellWidth + d, x + d + 6
				* cellWidth, x + 6 * cellWidth + d);

		g.drawLine(x + d + 2 * b + 6 * cellWidth, x + 6 * cellWidth + c, x + d
				+ 2 * b + 6 * cellWidth, x + 6 * cellWidth + d);
		g.drawLine(x + cellWidth + b + 6 * cellWidth, x + 6 * cellWidth + d, x
				+ cellWidth + a + 6 * cellWidth, x + 6 * cellWidth + d);

⌨️ 快捷键说明

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