首页>代码>java swing编写俄罗斯方块小游戏源代码下载>/russia/src/russia/game/square/RussiaGame.java
package russia.game.square;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.*;

/**
 * 游戏界面类 思路 当方块落到底部时用二维数组储存
 * 
 * @author lyj
 * 
 */
public class RussiaGame extends JPanel implements Serializable, KeyListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 数组的行数和列数 初始话界面的大小为 22行 12列
	 */
	private static final int ROW_SIZE = 22, COLUMN_SIZE = 12;
	/**
	 * 界面宽度
	 */
	private static final int WIDTH_SIZE = 344;

	/**
	 * 界面高度
	 */
	private static final int HEIGHT_SIZE = 375;
	/**
	 * 储存格子的数组
	 */
	private static int[][] shapeMap = new int[ROW_SIZE][COLUMN_SIZE];

	int[][] random;// 用来装载随机形状的二维数组
	/**
	 * 用来记载移动的坐标
	 */
	private int move_x = 5, move_y = -2;
	/**
	 * 方块大小
	 */
	private static final int SQUARE_SIZE = 20;
	/**
	 * 方块矩阵的大小
	 */
	private static final int MATRIX_SIZE = 5;

	/**
	 * 分数
	 */
	private int scores = 0;

	/**
	 * 判断游戏是否结束的标志
	 */
	private boolean canPlay = true;
	/**
	 * 休眠时间
	 */
	private int SLEEP_TIME = 500;
	/**
	 * 暂停游戏
	 */
	private boolean SLEEPGAME = true;
	/**
	 * ENTER 建的值用来标记暂停事件
	 */
	static final int VK_ENTER = 10;
	/**
	 * 下一个方块
	 * 
	 */

	static int[][] next;
	/**
	 * 用来装载当期方块和下一个方块
	 */
	private LinkedList<int[][]> shapeList = new LinkedList<int[][]>();

	/**
	 * 构造函数
	 */
	public RussiaGame() {
		randomShape();
		initShape();
		javax.swing.Timer time = new Timer(SLEEP_TIME, new GameLitsner());
		time.start();
		setBackground(Color.DARK_GRAY);
		setSize(new Dimension(WIDTH_SIZE, HEIGHT_SIZE));
	}

	private void initShape() {
		shapeMap = FileHelpers.read(ROW_SIZE, COLUMN_SIZE);
	}
	

	/**
	 * 判断是否能旋转 如果下一个图形所在的坐标没有越界和没有
	 * 
	 * @return true转换 false 不转换
	 */
	private boolean canTurnChange(int[][] newRandom) {
		boolean canTurn = true;
		if (move_y > 0)
			for (int i = MATRIX_SIZE - 1; i >= 0; i--) {
				for (int j = 0; j < MATRIX_SIZE; j++) {
					if (newRandom[i][j] == 1) {
						if (shapeMap[move_y + i][move_x + j] == 1
								|| shapeMap[move_y + i][move_x + j] == 2) {
							canTurn = false;
							break;
						}

					}
				}
			}
		if (move_x == -1 || move_y == 18 || move_x == 8) {
			canTurn = false;
		}
		if (random == Shapes.SHAPES[5] || random == Shapes.SHAPES[7]) {
			if (move_y >= -1 && shapeMap[move_y + 1][move_x + 3] == 1) {
				canTurn = false;
			}

			if (move_x == 0 || move_x == 3 || move_y == 3) {
				canTurn = false;
			}
		}
		return canTurn;
	}

	/**
	 * 消行
	 * 
	 * @return
	 */
	private int[][] delteLine() {
		int count = 0;
		for (int i = ROW_SIZE - 2; i >= 1; i--) {
			if (canDelete(shapeMap[i])) {
				scores += 10;
				for (int j = 1; j <= 10; j++) {
					shapeMap[i][j] = 0;
					count++;
				}
			}
			if (count >= 1) {
				int[] temp = shapeMap[i];
				shapeMap[i] = shapeMap[i - 1];
				shapeMap[i - 1] = temp;
			}
		}
		return shapeMap;
	}

	protected boolean canDelete(int[] line) {

		for (int i = 1, size = 10; i <= size; i++) {
			if (line[i] == 0) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 重新开始游戏
	 */
	private void restart() {

		for (int i = 0; i <= 20; i++) {
			for (int j = 1; j <= 10; j++) {
				shapeMap[i][j] = 0;
			}
		}
		canPlay = true;

	}

	/**
	 * 判断游戏是否结束
	 * 
	 * @return
	 */
	private boolean gameOver() {
		int[] top = shapeMap[1];// 获得顶部
		for (int i = 1; i < COLUMN_SIZE - 1; i++) {
			if (top[i] == 1) {
				return true;
			}
		}

		return false;
	}

	/**
	 * 盘能是否能下落
	 * 
	 * @return
	 */
	private boolean canDown() {
		boolean canDown = true;
		int x = move_x == -1 ? move_x + 2 : move_x + 1;
		int len = shapeLength();
		if (move_y >= 0 && move_x < 12)
			for (int i = MATRIX_SIZE - 1; i >= 0; i--) {
				for (int j = 0; j < MATRIX_SIZE; j++) {
					if (random[i][j] == 1) {
						if (shapeMap[move_y + i + 1][move_x + j] == 1) {
							canDown = false;
							break;
						}

					}
				}
			}

		if (move_y >= 0 && x < 12 && shapeMap[move_y + len][x] == 2) {
			canDown = false;
		}

		return canDown;

	}

	/**
	 * 判断是否能左移
	 * 
	 * @return
	 */
	private boolean canLeft() {
		boolean canLeft = true;
		if(move_y>=0)
		for (int i = MATRIX_SIZE - 1; i >= 0; i--) {
			for (int j = 0; j < MATRIX_SIZE; j++) {
				if (random[i][j] == 1) {
					if (shapeMap[move_y + i][move_x + j - 1] == 1
							|| shapeMap[move_y + i][move_x + j - 1] == 2) {
						canLeft = false;
						break;
					}
				}
			}
		}

		return canLeft;
	}

	/**
	 * 获得方块的长度
	 */
	private int shapeLength() {
		for (int leng = 5; leng >= 1; leng--) {
			int[] number = random[leng - 1];
			for (int index = 0; index < 5; index++) {
				if (number[index] == 1) {
					return leng;
				}
			}
		}

		return -1;

	}

	/**
	 * 画当前方块
	 * 
	 * @param gs
	 */
	private void paintCurr(java.awt.Graphics gs, int[][] randoms) {
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (randoms[i][j] == 1) {
					gs.setColor(Color.blue);
					gs.fill3DRect(j * SQUARE_SIZE + move_x * SQUARE_SIZE, i
							* SQUARE_SIZE + move_y * SQUARE_SIZE, SQUARE_SIZE,
							SQUARE_SIZE, true);

				}
			}
		}
	}

	private void paintNext(java.awt.Graphics gs) {
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				if (next[i][j] == 1) {
					gs.drawRect(j * SQUARE_SIZE + 250, i * SQUARE_SIZE,
							SQUARE_SIZE, SQUARE_SIZE);

				}
			}
		}
	}

	@Override
	// 画组建的方法
	protected void paintComponent(java.awt.Graphics g) {
		super.paintComponent(g);
		Image bc = new ImageIcon("333.jpg").getImage();
		g.drawImage(bc, 0, 0, getWidth(), getHeight(), this);
		g.setColor(Color.red);
		g.drawString("分数" + scores, 250, 150);
		g.drawString("F5 清屏重新开始  ", 250, 190);
		g.drawString("ENTER 暂停\\开始游戏", 250, 230);
		g.drawString("ESC 退出并保存", 250, 270);

		Color green = new Color(140, 140, 140);
		g.setColor(green);

		paintNext(g);
		// 绘制当前方块
		paintCurr(g, random);
		// 画已经落下的方块
		for (int j = 0; j < ROW_SIZE; j++) {
			for (int i = 0; i < COLUMN_SIZE; i++) {
				if (shapeMap[j][i] == 1) {
					g.setColor(green);
					g.fill3DRect(i * SQUARE_SIZE, j * SQUARE_SIZE, SQUARE_SIZE,
							SQUARE_SIZE, true);
				}
				if (shapeMap[j][i] == 2) {
					g.setColor(Color.black);
					g.drawRect(i * SQUARE_SIZE, j * SQUARE_SIZE, SQUARE_SIZE,
							SQUARE_SIZE);

				}

			}
		}

	};

	/**
	 * 生成随机形状
	 * 
	 * @return
	 */
	private void randomShape() {
		Random ran = new Random();
		if (shapeList.size() == 2) {
			shapeList.removeFirst();
			shapeList.addLast(Shapes.SHAPES[ran.nextInt(8)]);
		} else {

			shapeList.addFirst(Shapes.SHAPES[ran.nextInt(8)]);
			shapeList.addLast(Shapes.SHAPES[ran.nextInt(8)]);
		}
		random = shapeList.getFirst();
		next = shapeList.getLast();

	}

	
	/**
	 * 画墙壁的方法
	 */
	private void clearMap() {
		shapeMap = new int[ROW_SIZE][COLUMN_SIZE];

		for (int i = 0; i < ROW_SIZE; i++) {
			shapeMap[i][0] = 2;
			shapeMap[i][COLUMN_SIZE - 1] = 2;
		}
		// 画底部的格子
		for (int j = 0; j < COLUMN_SIZE; j++) {
			shapeMap[ROW_SIZE - 1][j] = 2;
		}
	}

	private void turnLef() {
		System.out.println(SQUARE_SIZE*(move_x+3));
		if (move_y>=0&&canLeft()) {
			move_x--;
			shapeMap = delteLine();
			repaint();
		}

	}

	/**
	 * 判断是否能右移
	 * 
	 * @return
	 */
	private boolean canRight() {
		boolean canRight = true;
		if(move_y>=0)
		for (int i = MATRIX_SIZE - 1; i >= 0; i--) {
			for (int j = 0; j < MATRIX_SIZE; j++) {
				if (random[i][j] == 1) {
					if (shapeMap[move_y + i][move_x + j + 1] == 1
							|| shapeMap[move_y + i][move_x + j + 1] == 2) {
						canRight = false;
						break;
					}

				}
			}
		}

		return canRight;
	}

	private void turnRight() {
		if (move_y>=0&&canRight()) {
			move_x++;
			shapeMap = delteLine();
			repaint();
		}

	}

	private void turnUp() {

		int[][] newRandom = xuanzhuan();
		if (canTurnChange(newRandom)) {
			if (random == Shapes.SHAPES[5]) {
				newRandom = Shapes.SHAPES[7];

			} else if (random == Shapes.SHAPES[7]) {

				newRandom = Shapes.SHAPES[5];
			}

			random = newRandom;
		}

		shapeMap = delteLine();
		repaint();
	}

	private void turnDown() {
		if (canDown())
			move_y++;
		shapeMap = delteLine();
		repaint();

	}

	/**
	 * 旋转矩阵
	 * 
	 * @return
	 */
	public int[][] xuanzhuan() {
		int[][] newRandom = new int[random.length][random[0].length];
		if (random == Shapes.SHAPES[2]) {
			return random;
		}
		for (int i = 0; i < random.length; i++) {
			for (int j = 0; j < random[0].length; j++) {
				newRandom[j][random[0].length - i - 1] = random[i][j];
			}
		}

		return newRandom;
	}

	class GameLitsner implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (SLEEPGAME)
				if (gameOver() == false) {
					if (canDown()) {
						move_y++;
						shapeMap = delteLine();
						repaint();
					} else {
						shapeMap = delteLine();
						addPoint();
						move_x = 5;
						move_y = -5;
						randomShape();
						repaint();

					}
				}

				else
					canPlay = false;

		}

	}

	private void addPoint() {
		int len = shapeLength();
		int index = len == 3 ? 3 : 4;
		for (int i = 0; i < 5; i++, index--) {
			for (int j = 0; j < 5; j++) {
				if (random[i][j] == 1 && move_y + len - index >= 0) {
					shapeMap[move_y + len - index][move_x + j] = 1;
				}
			}
		}

	}

	public void keyPressed(KeyEvent key) {

		if (canPlay && SLEEPGAME)
			switch (key.getKeyCode()) {
			// 改变形状
			case KeyEvent.VK_UP:
			case KeyEvent.VK_W:
				turnUp();
				break;
			// 下落
			case KeyEvent.VK_DOWN:
			case KeyEvent.VK_S:
				turnDown();
				break;
			// 左移
			case KeyEvent.VK_LEFT:
			case KeyEvent.VK_A:
				turnLef();
				break;
			// 右移
			case KeyEvent.VK_RIGHT:
			case KeyEvent.VK_D:
				turnRight();
				break;
			case KeyEvent.VK_ESCAPE:
				int isSave = JOptionPane.showConfirmDialog(null, "是否保存游戏");
				if (isSave == 0) {
					FileHelpers.wirte(shapeMap, ROW_SIZE, COLUMN_SIZE);
				} else {
					clearMap();
					FileHelpers.wirte(shapeMap, ROW_SIZE, COLUMN_SIZE);
				}
				System.exit(1000);

				break;

			}
		if (key.getKeyCode() == KeyEvent.VK_F5) {

			restart();

		}
		if (key.getKeyCode() == VK_ENTER) {
			SLEEPGAME = SLEEPGAME == true ? false : true;

		} else if (canPlay == false) {
			JOptionPane.showMessageDialog(null, "Game Over!");
			scores = 0;
		}
	}

	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

}
最近下载更多
ClydeSon  LV5 2023年12月27日
艾卡西亚没有暴雨  LV1 2021年6月9日
我叫杜有青  LV1 2021年3月5日
lyh3568263744  LV2 2020年12月19日
Care269031877  LV4 2020年12月18日
bhdzhhd  LV1 2020年12月2日
zzwllkw  LV1 2020年11月29日
xzx666  LV1 2020年11月16日
hhhhhjqjq  LV1 2020年7月1日
konodion  LV1 2020年6月28日
最近浏览更多
zr20050503  LV2 6月27日
李龙生 1月31日
暂无贡献等级
ClydeSon  LV5 2023年12月27日
1112WHQ  LV7 2023年11月3日
ggl163163eel  LV2 2023年10月19日
huangzy  LV12 2023年6月11日
小安同学  LV7 2023年5月11日
XVXIAOPING  LV1 2022年12月21日
522881297  LV1 2022年12月19日
tree1112666  LV1 2022年8月19日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友