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

📄 ersblocksgame.java

📁 java实现的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		bar.add(mGame);
		bar.add(mControl);
		bar.add(mWindowStyle);
		//bar.add(mInfo);
		bar.add(mGuanyu);
		
        //添加mGame的子菜单
		mGame.add(miNewGame);
		mGame.addSeparator();
		mGame.add(miSetBlockColor);
		mGame.add(miSetBackColor);
		mGame.addSeparator();
		mGame.add(miTurnHarder);
		mGame.add(miTurnEasier);
		mGame.addSeparator();
		mGame.add(miExit);
        
        //添加mControl的子菜单
		mControl.add(miPlay);
		mControl.add(miPause);
		mControl.add(miResume);
		mControl.add(miStop);
		miPause.setEnabled(false);
		miResume.setEnabled(false);
		miStop.setEnabled(false);
        
         //添加mWindowStyle的子菜单
		mWindowStyle.add(miAsWindows);
		mWindowStyle.add(miAsMotif);
		mWindowStyle.add(miAsMetal);
        
         //添加mInfo的子菜单
		//mInfo.add(miAuthor);
	//	mInfo.add(miSourceInfo);
	     mGuanyu.add(mguanyu);
	     mGuanyu.add(minfo);
         
         //将菜单栏添加到JFrame中
		setJMenuBar(bar);
        mGame.setMnemonic(67);
        miNewGame.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.ALT_MASK));
        miSetBlockColor.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, KeyEvent.ALT_MASK));
        miSetBackColor.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.ALT_MASK));
        miTurnHarder.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U, KeyEvent.ALT_MASK));
        miTurnEasier.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.ALT_MASK));
        miExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E, KeyEvent.ALT_MASK));
        miPlay.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K, KeyEvent.ALT_MASK));
		miPause.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, KeyEvent.ALT_MASK));
		miStop.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.ALT_MASK));
		miResume.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, KeyEvent.ALT_MASK));
		mguanyu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G, KeyEvent.ALT_MASK));
		
        //添加新游戏的监听器
		miNewGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				stopGame();//先停止游戏
				reset();  //重新还原状态(包括控制面板和画布面板两部分)
				setLevel(DEFAULT_LEVEL);//设置级别为初始值为5
			}
		});
		//用JColorChooser设置活动的方块的色
		miSetBlockColor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
			newFrontColor =
				        JColorChooser.showDialog(ErsBlocksGame.this,
				                "Set color for block", canvas.getBlockColor());
				if (newFrontColor != null)
					canvas.setBlockColor(newFrontColor);
					
			
			}
		});
		//用JColorChooser设置背景色
		miSetBackColor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				Color newBackColor =
				        JColorChooser.showDialog(ErsBlocksGame.this,
				                "Set color for block", canvas.getBackgroundColor());
				if (newBackColor != null)
					canvas.setBackgroundColor(newBackColor);
					
			}
		});
		//设置更高的级别
		miTurnHarder.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				int curLevel = getLevel();//得到级别
				if (curLevel < MAX_LEVEL) setLevel(curLevel + 1);//设置级别
			}
		});
		//设置较底的级别
		miTurnEasier.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				int curLevel = getLevel();
				if (curLevel > 1) setLevel(curLevel - 1);
			}
		});
		//退出
		miExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				System.exit(0);
			}
		});
		//开始游戏
		miPlay.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				playGame();
			}
		});
		//暂停游戏
		miPause.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				pauseGame();
			}
		});
		//唤醒游戏
		miResume.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				resumeGame();
			}
		});
		//停止游戏
		miStop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				stopGame();
			}
		});
	
		miAsWindows.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				String plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
				setWindowStyle(plaf);
			//	canvas.fanning();
			//	ctrlPanel.fanning();
				miAsWindows.setState(true);
				miAsMetal.setState(false);
				miAsMotif.setState(false);
			}
		});
		miAsMotif.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				String plaf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
				setWindowStyle(plaf);
			//	canvas.fanning();//??????????????
			//	ctrlPanel.fanning();//?????????????????
				miAsWindows.setState(false);
				miAsMetal.setState(false);
				miAsMotif.setState(true);
			}
		});
		miAsMetal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				String plaf = "javax.swing.plaf.metal.MetalLookAndFeel";
				setWindowStyle(plaf);
			//	canvas.fanning();
			//	ctrlPanel.fanning();
				miAsWindows.setState(false);
				miAsMetal.setState(true);
				miAsMotif.setState(false);
			}
		});
		mguanyu.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae) {
	        	aboutdialog();
	        
	        	
	        }});
	   minfo.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae) {
	        	aboutinfo();
	        
	        	
	        }});
}
    //关于对话框
private void aboutdialog()
{
	new AboutDialog(this,"关于对话框",newFrontColor);
}

private void aboutinfo()
{
	new Aboutinfo(this,"简介对话框");
}

	/**
	 * 根据字串设置窗口外观
	 * @param plaf String, 窗口外观的描述
	 */
	private void setWindowStyle(String plaf) 
	{
		try {
			UIManager.setLookAndFeel(plaf);
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception e) {
		}
	}

	/**
	 * 一轮游戏过程,实现了Runnable接口
	 * 一轮游戏是一个大循环,在这个循环中,每隔100毫秒,
	 * 检查游戏中的当前块是否已经到底了,如果没有,
	 * 就继续等待。如果到底了,就看有没有全填满的行,
	 * 如果有就删除它,并为游戏者加分,同时随机产生一个
	 * 新的当前块,让它自动下落。
	 * 当新产生一个块时,先检查画布最顶上的一行是否已经
	 * 被占了,如果是,可以判断Game Over了。
	 */
	private class Game implements Runnable 
	{
		public void run()
		 {
			int col = (int) (Math.random() * (canvas.getCols() - 3)),//?????????????-3的意思
			    style = ErsBlock.STYLES[(int) (Math.random() * 7)][(int) (Math.random() * 4)];

			while (playing) 
			{
				//playing在play中已经设为真
				if (block != null) 
				{    //第一次循环时,block为空,块类的对象
					if (block.isAlive()) //isAlive()?????????????????
					{
						try 
						{
							Thread.currentThread().sleep(100);
						} catch (InterruptedException ie) 
						{
							ie.printStackTrace();
						}
						continue;
					}
				}
                checkFullLine();        //检查是否有全填满的行
        
				if (isGameOver()) 
				{     //检查游戏是否应该结束了
					miPlay.setEnabled(true);
					miPause.setEnabled(true);
					miResume.setEnabled(false);
					ctrlPanel.setPlayButtonEnable(true);
					ctrlPanel.setPauseButtonLabel(true);

					reportGameOver();
					return;
				}
                //块类对象初始化,块类继承自Thread,因此有start()方法,启动它后,直接调用run()方法
				block = new ErsBlock(style, -1, col, getLevel(), canvas);//-1代表画布外面的行
				block.start();

				col = (int) (Math.random() * (canvas.getCols() - 3));
			    style = ErsBlock.STYLES[(int) (Math.random() * 7)][(int) (Math.random() * 4)];
                //给控制面板的预显窗口传递style
				ctrlPanel.setTipStyle(style);
			}
		}

		/**
		 * 检查画布中是否有全填满的行,如果有就删除之
		 */
		public void checkFullLine() 
		{
			for(int i=0;i<canvas.getRows();i++)
			{//从最后一行开始检测是否有满行如有的话,销掉.
				boolean fullLineColorBox = true;
				for (int j = 0; j < canvas.getCols(); j++) 
				{
					if (!canvas.getBox(i, j).isColorBox())
					 {
						fullLineColorBox = false;
						break;
					 }
				}
				if (fullLineColorBox)
				 {
					canvas.removeLine(i--);
			     }
			}
		}

		/**
		 * 根据最顶行是否被占,判断游戏是否已经结束了。
		 * @return boolean, true-游戏结束了,false-游戏未结束
		 */
		private boolean isGameOver() 
		{
			for (int i = 0; i < canvas.getCols(); i++)
			 {
				ErsBox box = canvas.getBox(0, i);
				if (box.isColorBox()) //如果最上面的一行有方块,即游戏该结束了.
				         return true;
		     }
			return false;
		}
	}
	
    /**
	 * 程序入口函数
	 * @param args String[], 附带的命令行参数
	 */
		public static void main(String[] args) 
	{
		new ErsBlocksGame("俄罗斯方块");
	}
}	

 







⌨️ 快捷键说明

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