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

📄 action.java

📁 J2SE版的俄罗斯方块。SWING界面.有分数记录功能
💻 JAVA
字号:

public class Action 
{
	private Box box;
	private BackGround bg;
	public Action(Box box,BackGround bg)
	{
		
	this.box=box;	
	this.bg=bg;
	}
	//获得填充方块的有效宽度
	public void delay(int ms)
	{
		try{   
		    Thread.currentThread();
			Thread.sleep(ms);   
		    }     
		  catch(InterruptedException   e)   {}   
	}
	
	public int getw(int Type,int Sata)
	{
	   for(int x=3;x>=0;x--)
	   {
		   for(int y=0;y<4;y++)
			   if(box.box[Type][Sata][y][x]==1)return x;
	   }
	   return -1;
		   
		
	}
	public int geth(int Type,int Sata,Box box)
	//获得高度
	{
		for(int y=0;y<4;y++)
			for(int x=0;x<4;x++)
				if(box.box[Type][Sata][y][x]==1)return y;
		return -1;
	}
	public boolean down()
	{

		int temp;
		int top[][]=new int[4][4];
		for(int y=0;y<4;y++)	
		for(int x=0;x<4;x++)
			{
			temp=box.x+x;
			if(temp>=10)temp=10;	
			top[y][x]=bg.ground[box.y+y][temp];
			}
		//获得原来数据
		for(int y=box.y;y<4+box.y;y++)
			for(int x=box.x;x<4+box.x;x++)
			{
				temp=x;
				if(temp>=10)temp=10;
				if((box.box[box.Type][box.Stat][y-box.y][x-box.x]==1)&&(bg.ground[y+1][temp]==3||bg.ground[y+1][temp]==2))
					return false;
				//下沉结束返回
			}
					
		for(int y=0;y<4;y++)	
			for(int x=0;x<4;x++)
				{
				temp=box.x+x;
				if(temp>=10)temp=10;
				 if(top[y][x]==5)
				 {
				 bg.ground[box.y+y][temp]=4;
				 continue;
				 }
				bg.ground[box.y+y][temp]=top[y][x];
				
				}
		for(int y=box.y;y<4+box.y;y++)
			for(int x=box.x;x<4+box.x;x++)
			{
				temp=x;
				if(temp>=10)temp=10;
				if(bg.ground[y+1][temp]==4&&box.box[box.Type][box.Stat][y-box.y][x-box.x]==1)
				
				bg.ground[y+1][temp]=5;
			}
		//////
		box.y++;

		return true;
			
	}
    public boolean left()
    {
    	int temp;
    	int top[][]=new int[4][4];
		for(int y=0;y<4;y++)	
		for(int x=0;x<4;x++)
			{
			if(box.x+x>11)break;	
			top[y][x]=bg.ground[box.y+y][box.x+x];
			}
		for(int y=box.y;y<4+box.y;y++)
			for(int x=box.x;x<4+box.x;x++)
			{
				if((box.box[box.Type][box.Stat][y-box.y][x-box.x]==1)&&(bg.ground[y][x-1]==3||bg.ground[y][x-1]==2))
					return false;
				//左移结束返回
			}
		for(int y=0;y<4;y++)	
			for(int x=0;x<4;x++)
				{
				if(box.x+x>11)break;
				if(top[y][x]==5)
				{
				bg.ground[box.y+y][box.x+x]=4;
				continue;
				}
				bg.ground[box.y+y][box.x+x]=top[y][x];
				
				}
		for(int y=box.y;y<4+box.y;y++)
			for(int x=box.x;x<4+box.x;x++)
			{
				temp=x-1;
				if(temp>11)temp=11;
				if(bg.ground[y][temp]==4&&box.box[box.Type][box.Stat][y-box.y][x-box.x]==1)
				
				bg.ground[y][temp]=5;
			}
		box.x--;
		return true;
    }
	public boolean right()
	{
		int top[][]=new int[4][4];
		int temp;
		for(int y=0;y<4;y++)	
		for(int x=0;x<4;x++)
			{
			temp=box.x+x;
			if(temp>=10)temp=10;	
			top[y][x]=bg.ground[box.y+y][temp];
			}
		for(int y=box.y;y<4+box.y;y++)
			for(int x=box.x+3;x>=box.x;x--)
			{
				temp=x+1;
				//if(temp>=9)temp=9;
				if((box.box[box.Type][box.Stat][y-box.y][x-box.x]==1)&&(bg.ground[y][temp]==3||bg.ground[y][temp]==2))
					return false;
				//右移结束返回
			}
		for(int y=0;y<4;y++)	
			for(int x=0;x<4;x++)
				{
				temp=box.x+x;
				if(temp>=10)temp=10;
				if(top[y][x]==5)
				{
				bg.ground[box.y+y][temp]=4;
				continue;
				}
				bg.ground[box.y+y][temp]=top[y][x];
				
				}
		for(int y=box.y;y<4+box.y;y++)
			for(int x=box.x;x<4+box.x;x++)
			{
				temp=x+1;
				if(temp>=10)temp=10;
				if(bg.ground[y][temp]==4&&box.box[box.Type][box.Stat][y-box.y][x-box.x]==1)
				
				bg.ground[y][temp]=5;
			}
		box.x++;
		return true;
	}
    public boolean changStat()
    {
    	if(box.Stat==box.StatMax-1)
    		box.Stat=0;
    	else box.Stat++;
    	if(box.x+getw(box.Type,box.Stat)>10)
		{
			if(box.Stat==0)box.Stat=0;
			else box.Stat--;
			return false;
		}
    		
    	int top[][]=new int[4][4];
		for(int y=0;y<4;y++)	
		for(int x=0;x<4;x++)
			{
				top[y][x]=bg.ground[box.y+y][box.x+x];
			}
		
		for(int y=box.y;y<4+box.y;y++)
			for(int x=box.x;x<4+box.x;x++)
			{
				if((box.box[box.Type][box.Stat][y-box.y][x-box.x]==1)&&(bg.ground[y][x]==3||bg.ground[y][x]==2))
				{
					
					if(box.Stat==0)box.Stat=0;
					else box.Stat--;
					return false;
				}
				//不可改变结束返回
			}
		for(int y=0;y<4;y++)	
			for(int x=0;x<4;x++)
				{
				if(top[y][x]==5)
				{
				bg.ground[box.y+y][box.x+x]=4;
				continue;
				}
				bg.ground[box.y+y][box.x+x]=top[y][x];
				
				}
		for(int y=box.y;y<4+box.y;y++)
			for(int x=box.x;x<4+box.x;x++)
			{
				if(bg.ground[y][x]==4&&box.box[box.Type][box.Stat][y-box.y][x-box.x]==1)
				
				bg.ground[y][x]=5;
			}
		return true;
    	
    	
    }
    public void nextbox()
    {
    	int type=0;//0-6
    	int stat=0;//0-3
    	type=(int) (Math.random()*7);
    	stat=(int) (Math.random()*4);
    	box.x=5;
    	box.y=0;
    	box.nextType=type;
    	box.nextStat=stat;
    	
    }
    public void newbox()
    {
    	box.Type=box.nextType;
    	box.Stat=box.nextStat;
    	box.x=5;
    	box.y=0;
    	nextbox();
		
    }
    public void clear()
    {
	    
    	int line=0;
    	DataIO IO=new DataIO(bg,box);
    	for(int y=3;y<26;y++)
    	{
    		for(int x=1;x<11;x++)
    		{


    			if(bg.ground[y][x]!=3)
    				{

    				break;
    				}
    			if(x==10)
    			{
    				line++;
    				IO.Del(y);
    			}
    			
    			//	
    	   }
    		
    		
    		
    	}
    	IO.addscore(line);
    }
    
}

⌨️ 快捷键说明

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