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

📄 block.java

📁 用JAVA实现的经典小游戏——俄罗斯方块的源代码!~
💻 JAVA
字号:
import java.lang.*;
import java.awt.*;
import javax.swing.*;
public class Block {
	int random;
    boolean dead;
    boolean over;
    int game[][];
    int count;
    int score;
    Sound sound;
    NewPanel panel;
    boolean go;
    int diff=1;
    int rotDirection;
	int block[][][]={
		              {{0,1,0,0},{0,1,0,0},{0,1,0,0},{0,1,0,0}},//长方块
		              {{0,0,0,0},{0,1,1,0},{0,1,1,0},{0,0,0,0}},//正方块
		              {{0,1,0,0},{0,1,0,0},{0,1,1,0},{0,0,0,0}},//正L块
		              {{0,0,1,0},{0,0,1,0},{0,1,1,0},{0,0,0,0}},//反L块 
		              {{0,0,0,0},{0,1,0,0},{1,1,1,0},{0,0,0,0}},//T行块
		              {{0,0,0,0},{0,1,1,0},{0,0,1,1},{0,0,0,0}},//正Z块
		              {{0,0,0,0},{0,1,1,0},{1,1,0,0},{0,0,0,0}},//反Z块
		            } ;
	int X;
	int Y;
	int currentBlock[][];
	
	public Block(NewPanel panel)
	{
		
		this.panel=panel;
		game=new int[18][11];
		currentBlock=new int[4][4];
		sound=new Sound();
		
	}
	
	public void initGame()
	{
		for(int i=0;i<18;i++)
		{
			for(int j=0;j<11;j++)
			{
				game[i][j]=0;
			}
		}
		
		for(int i=18-diff+1;i<18;i++)
		{
			for(int j=0;j<11;j++)
			{
				//game[i][j]=0;
				if(i%2==0 && j%2 == 0 )
				{
					game[i][j]=2;
				}
				
				if(i%2!=0 && j%2!=0)
				{
					game[i][j]=2;
				}
			}
		}
		this.rotDirection=1;
		random=(int)(Math.random()*7);
		dead=false;
		over=false;
		go=false;
		
	}
	
	public void creatBlock()
	{  
	    
	    int [][]temp=new int[4][4];
	    
	    X=0;
	    Y=3;
	    
	    
		
		for(int i=0; i<4;i++)
		{
			for(int j=0;j<4;j++)
			{
				
				if(game[i+X][j+Y]==2&&block[random][i][j]==1){dead=true;break;}
				else
				{
					//game[i+X][j+Y]=block[random][i][j];
				   temp[i][j]=block[random][i][j];
				}
			
			}
		}
		if(!dead)
		{
			currentBlock=temp;
		 for(int i=0; i<4;i++)
		  {
			for(int j=0;j<4;j++)
			{
			   game[i+X][j+Y]=block[random][i][j];
			}
		  }
			
		}
		else
		{
		   go=false;	
		}
		random=(int)(Math.random()*7);
	
		
		
		
		
		
	}
	
	

	
	public boolean isRight()
	{
		for(int i=0;i<18;i++)
		{
			for(int j=0;j<11;j++)
			{
		    	if((game[i][j]==1&&j==10)||(game[i][j]==1&&game[i][j+1]==2))//非常容易发生越界
				{
					return false;
				}
				
				
			}
		}
		return true;
	}
	
	public boolean isLeft()
	{
		for(int i=0;i<18;i++)
		{
			for(int j=0;j<11;j++)
			{
				if((game[i][j]==1&&j==0)||(game[i][j]==1&&game[i][j-1]==2))
				{
					return false;
				}
			
			}
		}
		return true;
	}
	
	public boolean isDown()
	{
		for(int i=0;i<18;i++)
		{
			for(int j=0;j<11;j++)
			{
				if((game[i][j]==1&&i==17)||(game[i][j]==1&&game[i+1][j]==2))
				{
					
					over=true;
					return false;
				}
				
			}
		}
		return true;
	}
	
	public void changeState()
	{
		for(int i=0;i<18;i++)
		{
			for(int j=0;j<11;j++)
			{
				if(game[i][j]==1)
				game[i][j]=2;
			}
		}
		
	}
	
	public void moveRight()
	{
	    int temp=0;
	    
		if(isRight())
		{
			for(int i=0;i<18;i++)
			{
				for(int j=10;j>=0;j--)
				{
					if(game[i][j]==1)
					{
						temp=game[i][j];
						game[i][j]=0;
						game[i][j+1]=temp;
					}
				}
			}
			Y++;
		}
		
	
		
	}
	
	public void moveLeft()
	{
		int temp=0;
		if(isLeft())
		{
			for(int i=0;i<18;i++)
			{
				for(int j=0;j<11;j++)
				{
					if(game[i][j]==1)
					{
						temp=game[i][j];
						game[i][j]=0;
						game[i][j-1]=temp;
					}
				}
			}
			Y--;
			if(Y<0)Y=0;
		}
		
	}
	

	public void moveDown()
	{
		int temp=0;
		
		if(isDown())
		{
			for(int i=17;i>=0;i--)
			{
				for(int j=0;j<11;j++)
				{
					if(game[i][j]==1)
					{
						temp=game[i][j];
						game[i][j]=0;
						game[i+1][j]=temp;
					}
				}
			}
			X++;
		}
		
		else
		{
			this.changeState();
			this.clearLine();
			if(!dead)
			{
				this.creatBlock();
			}
			
			else
			{
				go=false;
			}
		    
		   // mp.imageLbl.setIcon(image0);
		}
		
	}
	
	public boolean isXuanZhuan()
	{
	   
	   boolean flag=true;
	   int temp[][]=new int[4][4];
	   for(int i=0;i<4;i++)
	   {
	   	for(int j=0;j<4;j++)
	   	{
	   		
	   		
	   		if(this.rotDirection==1)
	   		{
	   			temp[j][3-i]=currentBlock[i][j];
	   		}
	   		
	   		if(this.rotDirection==2)
	   		{
	   			temp[3-j][i]=currentBlock[i][j];
	   		}
	   		
	   	}
	   }
	  // System.out.println(X+"   "+Y);
	   
	 // System.out.println(block.X+"  "+block.Y);
	   for(int i=0;i<4;i++)
	   {
	   	for(int j=0;j<4;j++)
	   	{
	   		if(Y+j<0 || Y+j>=11 || X+i>=18)
	   		{
	   			if(temp[i][j]==1)
	   			{
	   				flag=false;
	   			//	System.out.println("haha");
	   				break;
	   			}
	   		
	   		}
	   		else
	   		{
	   			if(temp[i][j]==1 && game[X+i][Y+j]==2)
	   			{
	   				flag=false;
	   				break;
	   			}
	   		}
	   		
	   		
	   		
	   	}
	   }
	   
	   if(flag)
	   {
	   	for(int i=0;i<4;i++)
	    {
	   	for(int j=0;j<4;j++)
	   	{
	   		
	   		currentBlock[i][j]=temp[i][j];
	   	}
	   }
	   }
	   
	   return flag;
	   
}

   public void XuanZhuan()
   {
   	 if(isXuanZhuan())
   	 {
   	 	
   	 	for(int i=0;i<4;i++)
   	   {
   	 	for(int j=0;j<4;j++)
   	 	{
   	 		if(Y+j>=0 && Y+j<11)
   	 		{
   	 			if(game[X+i][Y+j]==1)
   	 		 {
   	 			game[X+i][Y+j]=0;
   	 		 }
   	 		if(currentBlock[i][j]==1)
   	 		{
   	 			game[X+i][Y+j]=1;
   	 		}
   	 		}
   	 		

   	 	}
   	   }
   	 }
   	 
   	
   } 
   
   public int isClear()
   {
   	 boolean flag=true;
   	 boolean flag2=true;
   	 int init=0;
   	 for(int i=17;i>=1;i--)
   	  {
   	  	flag=true;
   	  	for(int j=0;j<11;j++)
   	  	{
   	  		if(game[i][j]!=2)
   	  		{
   	  			flag=false;
   	  		
   	  			
   	  		}
   	  	 }
   	  	 if(flag) count++;
   	  	 if(count==1&&flag2)
   	  	 {
   	  	 	init=i;
   	  	 	flag2=false;
   	  	 
   	  	 }
   	  	 
   	  }
   	  if(flag2)
   	  return -1;
   	  else
   	  return init;
   	 
   	  
   	  	
   	 }
   
   public void clearLine()
   {
   	 
   	 int init=isClear();
   	 
   	// System.out.println(init+" ,,,,,  "+count);
   	 if(init!=-1&&init!=0)
   	 {
   	 	sound.play2();
   	 	for(int i=init;i>init-count;i--)
   	 {
   	 	for(int j=0;j<11;j++)
   	 	{
   	 		game[i][j]=0;
   	 	}
   	 }
   	 
   	 for(int i=init-count;i>=0;i--)
   	 {
   	 	for(int j=0;j<11;j++)
   	 	{
   	 		if(game[i][j]==2)
   	 		{
   	 			int temp=game[i][j];
   	 			game[i][j]=0;
   	 			game[i+count][j]=temp;
   	 		}
   	 	}
   	 }
   	 }
   	 
   	 
   	 score=score+count;
   	 
   	 //panel.scoreLbl.setText(String.valueOf(score));
   	 init=0;
   	 count=0;
   	 
   	 
   }
   
   public void speedGo()
   {
   	 while(!over && !dead)
   	 {
   	    moveDown();
   	 }
   	 over=false;
   }
		
		
		
		
	
	public void print(int a[][])
	{
		for(int i=0;i<18;i++)
		{
			for(int j=0;j<11;j++)
			{
				System.out.print(a[i][j]);
			}
			System.out.println();
		}
	}
	

	

}
	
		
	
		
	
	
	
	
		                 
	                   
		
	
	
	
	
	                

⌨️ 快捷键说明

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