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

📄 tetriscanvas.java

📁 J2ME的源码!我以前学习J2ME的源码哈!非常适合初学者
💻 JAVA
字号:
package TestDemo05;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import java.util.*;
import java.lang.Math;

public class TetrisCanvas extends Canvas implements Runnable {

	private Thread Blocker = null;

	private Random generator;

	private int FutureBlockType, BlockType, LastType, LastX, LastY, BlockX,
			BlockY;

	private int BlockLines, BlockScore;

	private int BlockSpeed, CurSpeed;

	private static final int COLOR_GRAY = 0x00eeeeee;

	private static final int COLOR_RED = 0x00ff0000;

	private static final int COLOR_BLACK = 0x00000000;

	private static final int COLOR_WHITE = 0x00ffffff;

	private static final int COLOR_BLUE = 0x000000ff;

	private static final int COLOR_LIGHT_BLUE = 0x0089a5d1;

	private static final int COLOR_DARK_GRAY = 0x00808080;

	private static final int COLOR_BACKGROUND = COLOR_LIGHT_BLUE;

	private static final int BLOCK_SIZE = 7;

	private static final int CANVAS_SIZE_WIDTH = 12;

	private static final int CANVAS_SIZE_HEIGHT = 22;

	private static final int CANVAS_OFFSET_X = 5;

	private static final int CANVAS_OFFSET_Y = 7;

	/**
	 * 
	 * The paint status.
	 * 
	 */

	boolean ISCLEAR = false;

	boolean ISDOWN = false;

	boolean ISDEL = false;

	/**
	 * 
	 * the blockinformation matrix.
	 * 
	 */

	int BlockInfo[][] = { { 1, 0, 1, 1, 1, 2, 1, 3, 0xff0000, 2 },

	{ 0, 1, 1, 1, 2, 1, 3, 1, 0xff0000, 4 },

	{ 0, 0, 0, 1, 1, 1, 1, 2, 0x0000ff, 2 },

	{ 0, 1, 1, 0, 1, 1, 2, 0, 0x0000ff, 3 },

	{ 0, 1, 0, 2, 1, 0, 1, 1, 0x00ff00, 2 },

	{ 0, 0, 1, 0, 1, 1, 2, 1, 0x00ff00, 3 },

	{ 0, 0, 0, 1, 1, 0, 1, 1, 0xffff00, 2 },

	{ 0, 1, 1, 0, 1, 1, 1, 2, 0x00ffff, 2 },

	{ 0, 1, 1, 0, 1, 1, 2, 1, 0x00ffff, 3 },

	{ 1, 0, 1, 1, 1, 2, 2, 1, 0x00ffff, 3 },

	{ 0, 1, 1, 1, 1, 2, 2, 1, 0x00ffff, 3 },

	{ 0, 1, 0, 2, 1, 1, 2, 1, 0xff00ff, 3 },

	{ 0, 0, 1, 0, 1, 1, 1, 2, 0xff00ff, 3 },

	{ 0, 1, 1, 1, 2, 0, 2, 1, 0xff00ff, 3 },

	{ 1, 0, 1, 1, 1, 2, 2, 2, 0xff00ff, 3 },

	{ 0, 0, 0, 1, 1, 1, 2, 1, 0xffffff, 3 },

	{ 1, 0, 1, 1, 1, 2, 2, 0, 0xffffff, 3 },

	{ 0, 1, 1, 1, 2, 1, 2, 2, 0xffffff, 3 },

	{ 0, 2, 1, 0, 1, 1, 1, 2, 0xffffff, 3 },

	};

	// Gridmatrix 中只存储颜色信息

	int Gridmatrix[][] = new int[CANVAS_SIZE_HEIGHT][CANVAS_SIZE_WIDTH];

	/**
	 * 
	 * Initialize theapplet. Resize and load images.
	 * 
	 */

	public void init() {

		BlockType = Math.abs(generator.nextInt() % 19);

		FutureBlockType = Math.abs(generator.nextInt() % 19);

		LastType = BlockType;

		BlockLines = 0;

		BlockScore = 0;

		BlockSpeed = 1;

		CurSpeed = BlockSpeed;

		BlockX = 4;
		LastX = BlockX;

		BlockY = 0;
		LastY = BlockY;

		// 初始化Gridmatrix矩阵,内容为带边框的主绘图区。

		for (int i = 0; i < CANVAS_SIZE_HEIGHT; i++)

			for (int j = 0; j < CANVAS_SIZE_WIDTH; j++)

				Gridmatrix[i][j] = 0;

		for (int i = 0; i < CANVAS_SIZE_WIDTH; i++)

			Gridmatrix[CANVAS_SIZE_HEIGHT - 1][i] = COLOR_DARK_GRAY;

		for (int i = 0; i < CANVAS_SIZE_HEIGHT; i++) {

			Gridmatrix[i][0] = COLOR_DARK_GRAY;

			Gridmatrix[i][11] = COLOR_DARK_GRAY;

		}

	}

	/** Creates a new instanceof TetrisCanvas */

	public TetrisCanvas() {

		generator = new Random(System.currentTimeMillis());

		init();

		Blocker = new Thread(this);

		Blocker.start();

	}

	private void draw3DBlock(Graphics g, int c, int x, int y, int width,
			int height) {

		int color = g.getColor();

		g.setColor(COLOR_WHITE);

		g.drawRect(x, y, width, height);

		g.setColor(c);

		g.fillRect(x + 1, y + 1, width - 2, height - 2);

		g.setColor(COLOR_BLACK);

		g.drawLine(x + width - 1, y, x + width - 1, y + height - 1);

		g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);

		g.setColor(color);

	}

	public static boolean drawText(Graphics g, String str, int x, int y,
			int anchor, int color, int size) {

		Font f_old, f_new;

		int c_old;

		try {

			f_old = g.getFont();

			f_new = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, size);

			g.setFont(f_new);

			c_old = g.getColor();

			g.setColor(color);

			g.drawString(str, x, y, anchor);

			g.setColor(c_old);

			g.setFont(f_old);

			return true;

		} catch (Exception ex) {

			return false;

		}

	}

	protected void paint(Graphics g) {

		// 画背景

		try {

			Image image_Splash = Image.createImage("/bg.png");

			g.drawImage(image_Splash, 0, 0, Graphics.TOP | Graphics.LEFT);

		}

		catch (Exception ex) {

		}

		// 画下一个要出现的方块

		drawText(g, "下一个", 91, 5, Graphics.TOP | Graphics.LEFT, COLOR_BLUE,
				Font.SIZE_SMALL);

		g.setColor(COLOR_GRAY);

		g.drawRoundRect(91, 18, 26, 30, 2, 2);

		g.setColor(COLOR_DARK_GRAY);

		g.fillRoundRect(92, 19, 24, 28, 2, 2);

		for (int i = 0; i <= 3; i++)

			draw3DBlock(g, BlockInfo[FutureBlockType][8],

			93 + BlockInfo[FutureBlockType][i * 2 + 1] * BLOCK_SIZE,

			20 + BlockInfo[FutureBlockType][i * 2] * BLOCK_SIZE,

			BLOCK_SIZE, BLOCK_SIZE);

		drawText(g, "速度:" + String.valueOf(CurSpeed), 91, 60, Graphics.TOP
				| Graphics.LEFT, COLOR_BLUE, Font.SIZE_SMALL);

		drawText(g, "行数:" + String.valueOf(BlockLines), 91, 75, Graphics.TOP
				| Graphics.LEFT, COLOR_BLUE, Font.SIZE_SMALL);

		drawText(g, "成绩:", 91, 90, Graphics.TOP | Graphics.LEFT, COLOR_BLUE,
				Font.SIZE_SMALL);

		g.setColor(COLOR_GRAY);

		g.drawRoundRect(91, 105, 26, 20, 2, 2);

		g.setColor(COLOR_DARK_GRAY);

		g.fillRoundRect(92, 106, 24, 18, 2, 2);

		drawText(g, String.valueOf(BlockScore), 93, 107, Graphics.TOP
				| Graphics.LEFT, COLOR_WHITE, Font.SIZE_MEDIUM);

		// 画当前战况

		for (int i = 0; i < CANVAS_SIZE_HEIGHT - 1; i++)

			for (int j = 1; j < CANVAS_SIZE_WIDTH - 1; j++)

				if (Gridmatrix[i][j] != 0)

					draw3DBlock(g, Gridmatrix[i][j], CANVAS_OFFSET_X + j
							* BLOCK_SIZE,

					CANVAS_OFFSET_Y + i * BLOCK_SIZE,

					BLOCK_SIZE, BLOCK_SIZE);

		if (!ISDOWN) {

			// 画上新的方块

			LastX = BlockX;
			LastY = BlockY;
			LastType = BlockType;

			for (int i = 0; i <= 3; i++)

				draw3DBlock(g, BlockInfo[BlockType][8],

				CANVAS_OFFSET_X + BlockX * BLOCK_SIZE
						+ BlockInfo[BlockType][i * 2 + 1] * BLOCK_SIZE,

				CANVAS_OFFSET_Y + BlockY * BLOCK_SIZE
						+ BlockInfo[BlockType][i * 2] * BLOCK_SIZE,

				BLOCK_SIZE, BLOCK_SIZE);

		}

	}

	private boolean feasible() {

		for (int i = 0; i <= 3; i++)

			if (Gridmatrix[BlockY + BlockInfo[BlockType][i * 2]][BlockX
					+ BlockInfo[BlockType][i * 2 + 1]] != 0)

				return false;

		return true;

	}

	private void delline() {

		for (int i = 0; i <= 3; i++)

			Gridmatrix[BlockY + BlockInfo[BlockType][i * 2]][BlockX
					+ BlockInfo[BlockType][i * 2 + 1]] = BlockInfo[BlockType][8];

		int temp = 4;

		boolean CanSkip = false;

		int i = CANVAS_SIZE_HEIGHT - 2;

		while ((temp > 0) && (i >= 1)) {

			CanSkip = false;

			label1: for (int j = 1; j <= CANVAS_SIZE_WIDTH - 2; j++) {

				if (Gridmatrix[i][j] == 0) {
					CanSkip = true;
					i--;
					break label1;
				}

			}

			if (!CanSkip) {

				temp--;

				for (int k = i; k >= 1; k--)

					for (int l = 1; l <= CANVAS_SIZE_WIDTH - 2; l++)

						Gridmatrix[k][l] = Gridmatrix[k - 1][l];

				BlockLines++;

				BlockScore += 200;

				if ((BlockScore % 2000) < 200)
					CurSpeed++;

			}

		}

	}

	public void run() {

		while (Blocker != null) {

			if (!ISDOWN) {

				BlockY++;

				if (!feasible()) {

					ISDOWN = true;
					BlockY--;
					delline();

					try {
						Thread.sleep(400);
					} catch (InterruptedException e) {
					}

				}

				else {

					repaint();

					try {
						Thread.sleep(950 - 100 * (int) BlockSpeed);
					} catch (InterruptedException e) {
					}

				}

			}

			else {
				BlockScore += 50;

				if ((BlockScore % 2000) < 50)
					CurSpeed++;

				ISDOWN = false;

				repaint();

				BlockSpeed = CurSpeed;

				BlockType = FutureBlockType;

				FutureBlockType = Math.abs(generator.nextInt() % 19);

				BlockX = 4;
				LastX = BlockX;

				BlockY = 0;
				LastY = BlockY;

				if (!feasible()) {
					init();
				}

			}

		}

		Blocker = null;

	}

	protected void keyPressed(int keyCode) {

		// 处理按下键盘的事件,这是Canvas的实例方法

		switch (getGameAction(keyCode)) {// 将按键的值转化成方向常量

		case Canvas.UP:// 向上

			break;

		case Canvas.DOWN:// 向下

			BlockY++;

			if (!feasible())
				BlockY--;

			repaint();

			BlockSpeed = 9;

			// Blocker.run();

			break;

		case Canvas.LEFT:// 向左

			BlockX--;

			if (!feasible())
				BlockX++;

			break;

		case Canvas.RIGHT:// 向右

			BlockX++;

			if (!feasible())
				BlockX--;

			break;

		case Canvas.FIRE:

			int tempBlockType = BlockType;

			if (BlockType == 1)
				BlockType = -1;

			else if (BlockType == 3)
				BlockType = 1;

			else if (BlockType == 5)
				BlockType = 3;

			else if (BlockType == 6)
				BlockType = 5;

			else if (BlockType == 10)
				BlockType = 6;

			else if (BlockType == 14)
				BlockType = 10;

			else if (BlockType == 18)
				BlockType = 14;

			BlockType++;

			if (!feasible())
				BlockType = tempBlockType;

			break;

		default:

			break;

		}

		repaint();
		return;

	}

}

⌨️ 快捷键说明

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