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

📄 windowgrid.java

📁 进入cmd 执行java WindowGrid进入游戏界面
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/******************************************************************
 *  超级俄罗斯方块 -- 主窗格
 *版本: 1.0
 *作者:金永哲
 *最后修改日期:2005-9-18
 *
 *说明:游戏的运行的组织单位, 小方块显示的面板, 
 *控制一切方格, 方格组元素的运行, 用户接口, 可接受键盘事件
 ******************************************************************/

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;


public class WindowGrid extends JPanel
{		 
	private int ROWS = 20;                   //面板中可容纳的最大行数
	private int COLS = 10;                   //面板中可容纳的最大列数
	private static final int blockWidth = 30;//小方块宽度, 高度与其相同
	private Color blockColor;                //产生的小方块颜色
	private BlockGroup blockGroupTmp = null; //组织下一次会出现的方块组,显示在信息面板中
	private BlockGroup blockGroup = null;    //方块组
	private String[] gifNames = {            //图片名
		"gif\\a.gif", "gif\\b.gif",	"gif\\c.gif", "gif\\d.gif", "gif\\e.gif",};
	private int gifIndex = 0;
	
	private Block table[][] = null;      //保存小方格的矩阵	
	private int nextType;                //下次出现的小方块的类型
	private int nextSubType;             //下次出现的小方块的子类型
	
	TNewTimer timer = new TNewTimer(500); //控制方块自动下移的时钟 
	boolean dead = true;                  //为真时表示游戏结束
	JEditorPane edtScore = null;          //显示分数的文本框
	JEditorPane edtSpeed = null;          //显示速度的文本框
	TNewTimer   tmrPassed = null;          //信息面板计时的时钟
	JPanel      palNextType = null;       //信息面板中显示下一次图形的容器
	
	//表示单元格位置的类
	private class Cell{
		public int row;
		public int col;
		public Cell(){};
		public Cell(int r, int c){row = r; col = c;}
		public boolean equals(Cell c)
		{
			if(this.row == c.row && this.col == c.col)
				return true;
			else 
				return false;
		}
	}
	
	//构造函数======================================================
	public WindowGrid()
	{		
		this.setFocusable(true);
		timer.addNewTimerListener(new timerListener() );								
		this.addKeyListener(new panel_KeyListener() );
		this.setLayout(null);
		this.addMouseListener(new MouseAdapter(){
			public void mouseClicked(MouseEvent event){
				WindowGrid.this.requestFocus();
			}	
		} );
	}
	
	//开始运行=====================================================
	public void start()
	{
		//设置小方块参数		 	
		blockColor = Color.GREEN;
		ROWS = this.getSize().height / blockWidth;				
		COLS = this.getSize().width  / blockWidth;		 		
		
		//清除屏幕小方块
		if(table != null)
		{
			for(int row=0; row<ROWS; row++)
				for(int col=0; col<COLS; col++)
				{
					if(table[row][col] != null)
					{
						table[row][col].setVisible(false);
						this.remove(table[row][col]);
					}			
				}	
			this.repaint();
		}
		if(blockGroup != null)
		{
			for(int i=0; i<blockGroup.getCount();i++)
			{
				try{
				blockGroup.get(i).setVisible(false);
				this.remove(blockGroup.get(i));			
				}
				catch(BlockException e){
				System.out.println(e.getMessage() )	;
				}												
			}
			this.repaint();
		}
		
		//置空方格矩阵		
		table = new Block[ROWS][COLS];
		for(int i=0; i<ROWS; i++)
			for(int j=0; j<COLS; j++)
				table[i][j] = null;
		
		generateNextType();
		blockGroup = null;
		timer.setInterval(500);
		dead = false;
		timer.start();	 			
	}
	
	//暂停运行=====================================================
	public void pause()
	{
		timer.stop();							
	}
	
	//游戏停止=====================================================
	public void stop()
	{
		timer.stop();
	}
	
	//继续运行=====================================================
	public void continueRun()
	{
		timer.start();
	}
	
	//产生下一次出现的方格组类型,存放在成员变量nextType,nextSubType
	public void generateNextType()
	{		
		Random generator = new Random();
		nextType = generator.nextInt(5);
		if(nextType == 0)
			nextSubType = generator.nextInt(4);
		else if(nextType == 1 || nextType == 2)
			nextSubType = generator.nextInt(2);
		else if(nextType == 3)
			nextSubType = 0;
		else if(nextType == 4)
			nextSubType = generator.nextInt(4);
		
				
		//在信息面板中显示下一次出现的方块组形状
		if(palNextType != null)
		{
			try{
				if(blockGroupTmp != null)
				{
					for(int i=0; i<blockGroupTmp.getCount(); i++ )
					{
						blockGroupTmp.get(i).setVisible(false);
						palNextType.remove(blockGroupTmp.get(i));
					}
				}
				Block[] blocks = new Block[4];
				gifIndex = (int)(Math.random()*5);
				for(int i=0; i<4; i++)
				{
					blocks[i] = new Block(blockWidth, blockColor);						
					//第一次产生图片索引, generateBlocks()处都使用此图形
					ImageIcon imageIcon = new ImageIcon(gifNames[gifIndex]);
					blocks[i].setImage(imageIcon.getImage());	 		
					palNextType.add(blocks[i]);
				}
				
				blockGroupTmp = new BlockGroup(0,
						 	    0, blocks, nextType, nextSubType);		
			}
			catch(BlockException e){
				System.out.println(e.getMessage() + "WindowGrid:159");
			}			
		}	
	}
	
	//产生小方块组=================================================
	public Block[] generateBlocks(int count) throws BlockException
	{
		if(count == 0)
			return null;
			
		Block[] blocks = new Block[count];
		for(int i=0; i<count; i++)
		{
			blocks[i] = new Block(blockWidth, blockColor);	 					
			//此处gifIndex图形索引由generateNextType产生一次
			ImageIcon imageIcon = new ImageIcon(gifNames[gifIndex]);
			blocks[i].setImage(imageIcon.getImage());
			this.add(blocks[i]);
		}
		return blocks;
	}
	
 
	//判断给定单元格指针是否为空, =====================================
	public boolean isCellEmpty(Cell cell) throws BlockException 
	{
		if(cell.row < 0 || cell.row >= ROWS || cell.col < 0 || cell.col >= COLS)
			throw new BlockException(BETable.getMessage(6) + "WindowGrid.119" );
		
		if(table[cell.row][cell.col] == null)
			return true;
		return false;				
	}
	
	
	//判断一个小方块是否可以到达指定单元格============================
	public boolean blockCanTo(Cell cell)
	{
		//cell在矩阵表格外部
		if(cell.row < 0 || cell.row >= ROWS || cell.col < 0 || cell.col >= COLS)
		{
			//列数超过最大 最小范围, 行数超过最大范围
			if(cell.row >= ROWS || cell.col < 0 || cell.col >= COLS)
				return false;			
			else
				return true;		
		}
		//cell在矩阵表格内部
		else
		{
			try{
				//cell单元格无小方块
				if(isCellEmpty(cell))
					return true;
				else
					return false;																			
			}
			catch(BlockException e){
				System.out.println(e.getMessage() );
			}
			
		}
		return true;								 
	}
	
	//判断当前方格组bg是否可以向way的方向移动一个位置
	//way: DOWN, LEFT, RIGHT
	public boolean groupCanTo(String way, int step) throws BlockException
	{
		if(blockGroup == null)
			throw new BlockException(BETable.getMessage(10) + "WindowGrid:162" );
		if(step < 0)
			throw new BlockException(BETable.getMessage(12) + "WindowGrid:164");
						
		//判断方块组是否可以向下移动
		way = way.trim();		
		boolean canTo = true;	 
		Cell cell     = null;		
		for(int i=0; i<blockGroup.getCount(); i++)				
		{			 			 
			cell  = getCell(blockGroup.get(i));			
			if(way.equals("DOWN"))
				cell.row+= step;									
			else if(way.equals("LEFT") )
				cell.col-= step;
			else if(way.equals("RIGHT") )
				cell.col+= step;
			else
				throw new BlockException(BETable.getMessage(9) + "WindowGrid:180");				
			if(!blockCanTo(cell))	
			{			 			
				canTo = false;				 
				break;
			}	 
		} 	
		
		return canTo;			
	}
	
	//判断当前方格组是否可以变形到另一种样式=====================
	public boolean groupCanTranslate() throws 
									CloneNotSupportedException,
									BlockException
	{		
		boolean can = true;
		BlockGroup bg = (BlockGroup)blockGroup.clone();
		bg.translate();
		for(int i=0; i<bg.getCount(); i++)
		{
			Block b = bg.get(i);
			if(!blockCanTo(getCell(b) ) )
				can = false;
		}
		return can;
	 
	}
		  
	//得到小方块的单元格位置========================================
	public Cell getCell(Block block) throws BlockException
	{
		if(block == null)
			throw new BlockException(BETable.getMessage(11) + "WindowGrid:212 ");	
				
		Point pt = block.getLocation();
		Cell cell = new Cell();
		cell.row = pt.y / blockWidth;
		cell.col = pt.x / blockWidth;
		return cell;
	}
	
	//得到指定单元格的矩形区======================================

⌨️ 快捷键说明

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