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

📄 map.java

📁 一个锻炼记忆力的小游戏
💻 JAVA
字号:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Random;

import javax.microedition.lcdui.Image;

public class Map {
	GamePlay gy;

	Image age1Image[], temp[], fanpai[];

	byte age1Byte[], age[], arry[][], beijing[][];

	byte age2[];

	public final byte CLASS_ONE = 1;

	public final byte CLASS_TWO = 2;

	public final byte CLASS_THREE = 3;

	public int n, sum1, sum; // 控制一唯数组传到2唯数组

	public int pass = 0;

	public int nowWidth, nowHeight, multiplication;

	public Map(GamePlay gp) {
		// TODO 自动生成构造函数存根
		this.gy = gp;

		age1Image = new Image[16]; // 保存翻牌的图片
		age1Byte = new byte[14]; // 保存图片数组的下标

		// 14张不同图片的索引
		for (byte k = 0; k <= 13; k++) {
			age1Byte[k] = k;
		}

		// 14张图片初始
		try {
			for (int i = 0; i < 15; i++) {
				age1Image[i] = Image.createImage("/images/" + i + ".png");
				// temp[i] = age1[i];

			}

		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		// age1Image[15] = null;
	}

	public void init(int level, int addWidth, int addHeight) {
		nowWidth = GamePlay.WIDTH + addWidth;
		nowHeight = GamePlay.HEIGHT + addHeight;
		multiplication = nowWidth * nowHeight;

		switch (level) {
		// 第一关
		case CLASS_ONE:
			age2 = new byte[multiplication]; // 保存打乱的图片索引
			age = new byte[multiplication]; // 保存复制出来的翻牌的图片
			// temp = new Image[14]; // 翻牌图片的索引
			arry = new byte[nowWidth][nowHeight]; // 保存复制出来的翻牌的图片
			beijing = new byte[nowWidth][nowHeight];// 保存最后显示在屏幕上的图片

			break;

		// 第2关
		case CLASS_TWO:
			age2 = new byte[multiplication]; // 保存打乱的图片索引
			age = new byte[multiplication]; // 保存复制出来的翻牌的图片
			// temp = new Image[14]; // 翻牌图片的索引
			arry = new byte[nowWidth][nowHeight]; // 保存复制出来的翻牌的图片
			beijing = new byte[nowWidth][nowHeight];// 保存最后显示在屏幕上的图片
			break;

		// 第三关
		case CLASS_THREE:
			age2 = new byte[multiplication]; // 保存打乱的图片索引
			age = new byte[multiplication]; // 保存复制出来的翻牌的图片
			// temp = new Image[14]; // 翻牌图片的索引
			arry = new byte[nowWidth][nowHeight]; // 保存复制出来的翻牌的图片
			beijing = new byte[nowWidth][nowHeight];// 保存最后显示在屏幕上的图片
			break;

		}
		// 盖牌
		n = 0;
		sum1 = 1;
		sum = 0;

		for (int i = 0; i <= nowWidth - 1; i++) {
			for (int j = 0; j <= nowHeight - 1; j++) {
				beijing[i][j] = 14;
			}
		}
		ArryTwo();
	}

	// 随机产生成对的图片
	public void random() {
		Random rnd = new Random();
		for (int j = 0; j <= multiplication - 2; j = j + 2) {
			age[j] = (byte) age1Byte[(rnd.nextInt() >>> 1) % 13];
			age[j + 1] = age[j];
		}
	}

	// 显示在屏幕上的图片
	public final void readMap() {

		for (int i = 0; i <= nowWidth - 1; i++) {
			for (int j = 0; j <= nowHeight - 1; j++) {
				if (beijing[i][j] != 15) {
					gy.gm.gameGraphics.drawImage(age1Image[beijing[i][j]],
							i * 24, j * 24 + 48, 0);
				}
			}
		}
	}

	// 打乱一唯数组
	public void ArryOne(int number) {
		while (number > 0) {
			number = number - 1;
			for (int i = 0; i <= multiplication - 1; i++) {
				sum = n;
				n = n + 2;
				if (sum1 == 1) {
					age2[i] = age[sum];
					if (sum == multiplication - 2) {
						sum1 = 0;
						n = 0;
						sum = 0;
					}
				} else if (sum1 == 0) {
					age2[i] = age[sum + 1];
					if (i == multiplication - 1) {
						sum1 = 1;
						n = 0;
						sum = 0;
					}
				}
			}
			for (int n = 0; n < multiplication; n++) {
				age[n] = age2[n];
			}

		}

	}

	// 将一唯数组打乱传递到二唯数组上
	public void ArryTwo() {
		random();
		ArryOne(3);
		int n = 0;
		for (int i = 0; i < nowWidth; i++)
			for (int j = 0; j < nowHeight; j++)
			// {
			// int sum = n;
			// n = n + 2;
			// if (sum1 == 1) {
			// arry[i][j] = age2[sum];
			// if (sum == multiplication - 2) {
			// sum1 = 0;
			// n = 0;
			// sum = 0;
			// }
			// } else if (sum1 == 0) {
			// arry[i][j] = age2[sum + 1];
			// if (sum == multiplication - 1) {
			// sum1 = 1;
			// n = 0;
			// sum = 0;
			// }
			// }
			// }
			{
				arry[i][j] = age2[n];
				n = n + 1;
			}
	}

	public void SaveArry(DataOutputStream dos) {

		try {
			dos.writeInt(nowWidth);
			dos.writeInt(nowHeight);
			dos.writeInt(multiplication);

			for (int i = 0; i < nowWidth; i++)
				for (int j = 0; j < nowHeight; j++) {
					dos.writeByte(arry[i][j]);
					dos.writeByte(beijing[i][j]);
				}
		} catch (IOException e1) {
			// TODO 自动生成 catch 块
			e1.printStackTrace();
		}

	}

	public void ReadArry(DataInputStream dis) {

		try {
			nowWidth = dis.readInt();
			nowHeight = dis.readInt();
			multiplication = dis.readInt();
			for (int i = 0; i < nowWidth; i++)
				for (int j = 0; j < nowHeight; j++) {
					arry[i][j] = dis.readByte();
					beijing[i][j] = dis.readByte();
				}
		} catch (IOException e1) {
			// TODO 自动生成 catch 块
			e1.printStackTrace();
		}

	}
}

⌨️ 快捷键说明

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