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

📄 mainframe.java

📁 比较好的用java实现的俄罗斯方块 写的比较详细 功能基本完善
💻 JAVA
📖 第 1 页 / 共 5 页
字号:

		final JSeparator separator_2 = new JSeparator();
		separator_2.setBounds(12, 145, 255, 39);
		add(separator_2);

		button_obstacleColor = new JButton();
		button_obstacleColor.setBounds(135, 10, 96, 23);
		separator_2.add(button_obstacleColor);

		button_obstacleColor.setFont(new Font("宋体", Font.PLAIN, 12));
		button_obstacleColor.setFocusable(false);
		button_obstacleColor.setText("设置颜色");

		checkBox_colorfulObstacle.setText("关闭彩色障碍物");
		checkBox_colorfulObstacle.setBounds(5, 10, 122, 23);
		separator_2.add(checkBox_colorfulObstacle);

		checkBox_colorfulObstacle.setSelected(true);
		checkBox_colorfulObstacle.setFont(new Font("宋体", Font.PLAIN, 12));

		final JSeparator separator_3 = new JSeparator();
		separator_3.setBounds(12, 185, 255, 33);
		add(separator_3);

		final JLabel label_7 = new JLabel();
		label_7.setBounds(10, 10, 48, 15);
		separator_3.add(label_7);
		label_7.setFont(new Font("宋体", Font.PLAIN, 12));
		label_7.setFocusable(false);
		label_7.setText("随机生成");

		textField_obstacleNum.setFont(new Font("宋体", Font.PLAIN, 12));
		textField_obstacleNum.setBounds(65, 10, 32, 15);
		separator_3.add(textField_obstacleNum);
		textField_obstacleNum.setText("30");

		final JLabel label_2 = new JLabel();
		label_2.setBounds(100, 10, 18, 15);
		separator_3.add(label_2);
		label_2.setText("个");
		label_2.setFont(new Font("宋体", Font.PLAIN, 12));

		textField_lineNum.setBounds(120, 10, 24, 15);
		textField_lineNum.setFont(new Font("宋体", Font.PLAIN, 12));
		separator_3.add(textField_lineNum);
		//

		textField_lineNum.setText("0");

		final JLabel label_8 = new JLabel();
		label_8.setBounds(150, 10, 66, 15);
		separator_3.add(label_8);
		label_8.setFont(new Font("宋体", Font.PLAIN, 12));
		label_8.setFocusable(false);
		label_8.setText("行障碍物");

		final JSeparator separator_4 = new JSeparator();
		separator_4.setBounds(12, 220, 255, 33);
		add(separator_4);

		textField_stayTime.setFont(new Font("宋体", Font.PLAIN, 12));
		textField_stayTime.setBounds(110, 14, 42, 15);
		separator_4.add(textField_stayTime);
		textField_stayTime.setText("300");

		final JLabel label_1 = new JLabel();
		label_1.setBounds(160, 14, 24, 15);
		separator_4.add(label_1);
		label_1.setFont(new Font("宋体", Font.PLAIN, 12));
		label_1.setFocusable(false);
		label_1.setText("毫秒");

		button_fullLineColor = new JButton();
		button_fullLineColor.setBounds(195, 10, 60, 23);
		separator_4.add(button_fullLineColor);

		button_fullLineColor.setFont(new Font("宋体", Font.PLAIN, 12));
		button_fullLineColor.setFocusable(false);
		button_fullLineColor.setText("颜色");

		final JLabel label = new JLabel();
		label.setBounds(10, 14, 120, 15);
		separator_4.add(label);
		label.setFont(new Font("宋体", Font.PLAIN, 12));
		label.setFocusable(false);
		label.setText("满行的效果时间:");

		final JSeparator separator_5 = new JSeparator();
		separator_5.setBounds(15, 260, 250, 69);
		add(separator_5);

		stopGameButton.setText("停止游戏");

		stopGameButton.setBounds(10, 10, 101, 23);
		separator_5.add(stopGameButton);
		stopGameButton.setFont(new Font("宋体", Font.PLAIN, 12));
		stopGameButton.setFocusable(false);

		pauseButton.setBounds(135, 10, 103, 23);
		separator_5.add(pauseButton);
		pauseButton.setText("暂停/继续");
		pauseButton.setFont(new Font("宋体", Font.PLAIN, 12));
		pauseButton.setFocusable(false);

		newGameButton.setFont(new Font("宋体", Font.PLAIN, 12));
		newGameButton.setBounds(65, 40, 110, 23);
		separator_5.add(newGameButton);
		newGameButton.setFocusable(false);
		newGameButton.setText("开始新游戏");
		// button_obstacleColor.setVisible(false);
		// button_shapeColor.setVisible(false);

		final JLabel label_logo = czbkIcon == null ? new JLabel() : new JLabel(
				czbkIcon);
		label_logo.setBounds(140, 10, 125, 50);
		add(label_logo);

		final JSeparator separator_6 = new JSeparator();
		separator_6.setBounds(12, 35, 119, 33);
		add(separator_6);

		buttonBackgroundColor = new JButton();
		buttonBackgroundColor.setBounds(5, 5, 110, 23);
		separator_6.add(buttonBackgroundColor);
		buttonBackgroundColor.setFont(new Font("宋体", Font.PLAIN, 12));
		buttonBackgroundColor.setFocusable(false);
		buttonBackgroundColor.setText("设置背景颜色");

		button_default = new JButton();
		button_default.setFont(new Font("宋体", Font.PLAIN, 12));
		button_default.setText("恢复默认设置");
		button_default.setBounds(17, 5, 110, 23);
		button_default.setFocusable(false);
		add(button_default);
	}

	public JFrame getFrame() {
		return frame;
	}

	public void setFrame(JFrame frame) {
		this.frame = frame;
	}

	public int getObstacleNum() {
		try {
			return Integer.parseInt(textField_obstacleNum.getText());
		} catch (NumberFormatException e) {
			return 0;
		}
	}

	public int getStayTime() {
		try {
			return Integer.parseInt(textField_stayTime.getText());
		} catch (NumberFormatException e) {
			return 0;
		}
	}

	public JButton getNewGameButton() {
		return newGameButton;
	}

	public JButton getStopGameButton() {
		return stopGameButton;
	}

	public int getLineNum() {
		try {
			return Integer.parseInt(textField_lineNum.getText());
		} catch (NumberFormatException e) {
			return 0;
		}
	}

	public void setTextField_lineNum(JTextField textField_lineNum) {
		this.textField_lineNum = textField_lineNum;
	}

	public JCheckBox getCheckBox_colorfulShape() {
		return checkBox_colorfulShape;
	}

	public JCheckBox getCheckBox_drawGridding() {
		return checkBox_drawGridding;
	}

	public JTextField getTextField_stayTime() {
		return textField_stayTime;
	}

	public JTextField getTextField_obstacleNum() {
		return textField_obstacleNum;
	}

	public JTextField getTextField_lineNum() {
		return textField_lineNum;
	}

	public boolean isDrawGridding() {

		return checkBox_drawGridding.isSelected();
	}

	public boolean isColorfulShape() {
		return checkBox_colorfulShape.isSelected();
	}

	public boolean isColorfulObstacle() {
		return checkBox_colorfulObstacle.isSelected();
	}

	public ImageIcon getCzbkIcon() {
		return czbkIcon;
	}

	public void setCzbkIcon(ImageIcon czbkIcon) {
		this.czbkIcon = czbkIcon;
	}

	public JButton getPauseButton() {
		return pauseButton;
	}

	public JCheckBox getCheckBox_colorfulObstacle() {
		return checkBox_colorfulObstacle;
	}

	public JButton getButton_obstacleColor() {
		return button_obstacleColor;
	}

	public JButton getButton_griddingColor() {
		return button_griddingColor;
	}

	public JButton getButton_shapeColor() {
		return button_shapeColor;
	}

	public JButton getButton_fullLineColor() {
		return button_fullLineColor;
	}

	public JButton getButtonBackgroundColor() {
		return buttonBackgroundColor;
	}

	public JButton getButton_default() {
		return button_default;
	}

}
/**
 * 自己用的一个GroundListener, 处理不可消的障碍物, 增加趣味性
 * 
 * @version 1.0, 01/01/08
 * 
 * @author 汤阳光
 * 
 */

class MyGroundListener extends GroundAdapter {

	int deletedLineCount = 0;

	@Override
	public void fullLineDeleted(Ground ground, int deletedLineCount) {

		this.deletedLineCount += deletedLineCount;
		// TODO Auto-generated method stub
		if ((deletedLineCount %= 10) == 9 || deletedLineCount > 2)
			for (int y = 0; y < Global.HEIGHT; y++)
				for (int x = 0; x < Global.WIDTH; x++)
					if (ground.isStubbornObstacle(x, y))
						ground.addObstacle(x, y);
	}

}
/**
 * 控制器<BR>
 * 控制Ground, Snake, Food<BR>
 * 负责游戏的逻辑<BR>
 * 处理按键事件<BR>
 * <BR>
 * 
 * @version 1.0, 01/01/08
 * 
 * @author 汤阳光
 * 
 */
 class Controller extends KeyAdapter implements ShapeListener,
		GroundListener {

	protected Set<GameListener> listeners = new HashSet<GameListener>();
	/**
	 * 图形工厂
	 * 
	 */
	protected ShapeFactory shapeFactory;

	protected Shape shape;

	protected Ground ground;

	protected GamePanel gamePanel;

	protected JLabel gameInfoLabel;

	/**
	 * 当前的游戏状态
	 */
	protected boolean playing;

	/**
	 * 
	 * @param shapeFactory
	 * @param ground
	 * @param gamePanel
	 */
	public Controller(ShapeFactory shapeFactory, Ground ground,
			GamePanel gamePanel) {
		super();
		this.shapeFactory = shapeFactory;
		this.ground = ground;
		this.gamePanel = gamePanel;
	}

	/**
	 * 多接受一个 JTextComponent, 可以给在这个组件上显示提示信息
	 * 
	 * @param shapeFactory
	 * @param ground
	 * @param gamePanel
	 * @param gameInfoLabel
	 */
	public Controller(ShapeFactory shapeFactory, Ground ground,
			GamePanel gamePanel, JLabel gameInfoLabel) {

		this(shapeFactory, ground, gamePanel);
		this.setGameInfoLabel(gameInfoLabel);
	}

	/**
	 * 处理键盘按键 <BR>
	 * LEFT: 向左移动<BR>
	 * RIGHT:向右移动<BR>
	 * DOWN: 向下移动<BR>
	 * UP: 变形<BR>
	 * PAGE UP: 加快速度<BR>
	 * PAGE DOWN: 减慢速度<BR>
	 * Y: 重新开始游戏<BR>
	 * ENTER: 暂停/继续
	 */
	@Override
	public void keyPressed(KeyEvent e) {

		if (e.getKeyCode() != KeyEvent.VK_Y && !playing)
			return;

		switch (e.getKeyCode()) {
		/**
		 * 方向左
		 */
		case KeyEvent.VK_LEFT:
			if (isPausingGame()) {
				this.continueGame();
			}
			shape.setSwift(false);

			if (isPlaying() && ground.isMoveable(shape, Shape.LEFT))
				shape.moveLeft();
			break;
		/**
		 * 方向右
		 */
		case KeyEvent.VK_RIGHT:
			if (isPausingGame()) {
				this.continueGame();
			}
			shape.setSwift(false);

			if (isPlaying() && ground.isMoveable(shape, Shape.RIGHT))
				shape.moveRight();
			break;
		/**
		 * 方向上
		 */
		case KeyEvent.VK_UP:

			if (isPlaying()) {
				if (!shape.isPause()) {
					if (ground.isMoveable(shape, Shape.ROTATE)) {
						shape.setSwift(false);
						shape.rotate();
					}
				} else {
					if (ground.isMoveable(shape, Shape.UP))
						shape.moveUp();
					else {
						shape.die();
						shape = shapeFactory.getShape(this);
					}
				}
			}

			break;
		/**
		 * 方向下
		 */
		case KeyEvent.VK_DOWN:
			if (isPausingGame()) {
				this.continueGame();
			}
			if (isPlaying() && isShapeMoveDownable(shape))
				shape.moveDown();
			break;
		/**
		 * PAGE UP
		 */
		case KeyEvent.VK_PAGE_UP:
			shape.speedUp();
			break;
		/**
		 * PAGE DOWN
		 */
		case KeyEvent.VK_PAGE_DOWN:
			shape.speedDown();
			break;
		/**
		 * 反引号,换一个图形
		 */
		case KeyEvent.VK_BACK_QUOTE:
			if (isPlaying()) {
				shape.die();
				shape = shapeFactory.getShape(this);
			}
			break;
		case KeyEvent.VK_ENTER:
			if (isPausingGame())
				this.continueGame();
			else
				this.pauseGame();
			break;
		case KeyEvent.VK_Y:
			if (!isPlaying())
				newGame();
			break;
		case KeyEvent.VK_SPACE:

			if (isPlaying() && !isPausingGame())
				shape.setSwift(true);
			break;
		}
		/**
		 * 重新显示
		 */
		gamePanel.redisplay(ground, shape);
		if (gameInfoLabel != null)
			gameInfoLabel.setText(this.getNewInfo());
	}

	/**
	 * 询问一下图形是否可以下落,如果不能下落了,就会让图形变成障碍物<BR>
	 * 这个方法是同步的
	 */
	public synchronized boolean isShapeMoveDownable(Shape s) {

		if (shape == null)
			return true;
		if (!playing || shape != s)
			return false;

		if (ground.isMoveable(shape, Shape.DOWN))
			return true;

		shape.die();
		ground.accept(shape);
		if (playing && !ground.isFull()) {
			shape = shapeFactory.getShape(this);
		}
		gamePanel.redisplay(ground, shape);
		if (gameInfoLabel != null)
			gameInfoLabel.setText(this.getNewInfo());

		return false;
	}

	/**
	 * 处理图形触发的 shapeMovedDown (图形下落) 事件<BR>
	 * 将会重新显示
	 */
	public void shapeMovedDown(Shape s) {
		// TODO Auto-generated method stub
		if (playing && ground != null && shape != null)
			gamePanel.redisplay(ground, shape);
	}

	/**
	 * 开始一个新游戏
	 */
	public void newGame() {
		playing = true;
		ground.init();
		ground.addGroundListener(this);

		Global.CURRENT_SPEED = Global.DEFAULT_SPEED;
		shape = shapeFactory.getShape(this);

		if (playing)
			gamePanel.redisplay(ground, shape);

		if (gameInfoLabel != null)
			gameInfoLabel.setText(this.getNewInfo());

		for (GameListener l : listeners)
			l.gameStart();
	}

	/**
	 * 停止当前游戏
	 */
	public void stopGame() {
		if (shape == null)
			return;
		playing = false;
		for (GameListener l : listeners)
			l.gameOver();
	}

	/**
	 * 暂停游戏
	 */

⌨️ 快捷键说明

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