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

📄 structrue.java

📁 俄罗斯方块的java源代码
💻 JAVA
字号:
/*
 * Created on 2004-11-15
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Softmedical
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;


/***                  Note
//because  different boxes  would have different factors to move or to reshape,
//we separate this from the base class ,and this changing things would
//be defined at it's derived class (IBox,TBox,LBox)..........
//interfaces here to implement statigies pattern.............
*/

/*
 *     Structrue is the base class of all common boxes
 *     some common fields and interfaces are defined here
 *  * 
 *  * */

public abstract class Structrue {
/*All box items(JButton) would be attached to conbtn*/
protected ActionBtn conbtn;
/* All boxes would be moved down a step by its timer at interval time */
protected Timer timer;
protected JButton[] btn=new JButton[]{
	new JButton(),		
	new JButton(),
	new JButton(),
	new JButton(),

	};

//stategies patterns.....here
protected DownFactor downfactor;
protected LeftFactor leftfactor;
protected RightFactor rightfactor;
protected ReshapeFactor reshapefactor; 

abstract  protected void configDown();
abstract  protected void configRight();
abstract  protected void configLeft();
abstract  protected void configReshape();
abstract  public  void reshape();

/* speed control
 * */

private void initTimer(){
 timer=new Timer(conbtn.getInterval(),new ActionListener(){
 	    int delay=0;
		public void actionPerformed(ActionEvent e){
		       moveDown();
		    if(!downfactor.canMoveDown()){
		     	if(delay==1)consume();
		     	delay++;
	  	  	  }
			}	
		});	
 timer.start();
  }


private void initConfig(){
	 configDown();
	 configLeft();
	 configRight();
	 configReshape();
	  }

private void initBtn(){
Random randomColor=new Random();
Color col=Color.WHITE;
for(int i=0;i<btn.length;i++){
btn[i].setSize(15,15);//the origin size of btn
switch(randomColor.nextInt(3)){
	case 0:
		col=Color.RED;
				break;
	case 1:
		col=Color.MAGENTA;
				break;
	case 2:
		col=Color.ORANGE;
				break;
		}
btn[i].setBackground(col);
  }	
}
private void attach(){
	 for(int i=0;i<btn.length;i++){
		conbtn.add(btn[i]);	
		 }

		}
private void initrialization(){
	initTimer();
	initBtn();
	initConfig();
	attach();
}

public Structrue(ActionBtn conbtn){
   this.conbtn=conbtn;
    initrialization();
  }




/*This interface would be used by RussianSYS to stop the motion of
 * boxes
 *  */
public Timer getTimer(){
return this.timer;	
}
/*setShape() and getShape() are common when we reshape the Box....*/
protected int shape;
protected void setShape(int shape){this.shape=shape;}
protected int getShape(){return this.shape;}

private void storeInfo(){//when box item has been consumed 
	//we store the infomation....to conbtn
	//called by consume fun...
   for(int i=0;i<btn.length;i++){
		int  row=btn[i].getY()/15,
		     col=btn[i].getX()/15;
		if(row>=0&&row<=39&&col>=0&&col<=19){
		conbtn.setFlag(row,col,true);
		conbtn.setBtn(row,col,btn[i]);}

		}

}

/*This flag show that it is time to level up*/
private boolean levelUp=false;
private void setLevelUpFlag(boolean flag){this.levelUp=flag;}

private boolean isFlash=false;
public void setFlashFlag(boolean flag){this.isFlash=flag;}
public boolean  isFlash(){return this.isFlash;}
private void toFlash(){////called by consume fun...
for(int i=0;i<=39;i++){
	if(conbtn.isLineFull(i))
	{       conbtn.FlashLine(i);
		    conbtn.setScore(conbtn.getScore()+200);
		    setFlashFlag(true);
		    if(conbtn.getScore()%1000==0){
		    	System.out.println("level 's up");
		    	conbtn.setLevel(conbtn.getLevel()+1);
		    	conbtn.speedUp();
		    	System.out.println("interval:"+conbtn.getInterval());
	 	    	setLevelUpFlag(true);
	 	       }
	        }

	  }
}

/*failFlag would be used to tell RussianSYS wheather 
	 	  * current game is over,this flag would be set by failChecker();
	 	  * 
	 	  * */
 private boolean failFlag=false;
 private void setFailFlag(boolean flag){
	 	 this.failFlag=flag;	
	 	 }
	 	 private void   failChecker(){//called by consume fun...
	 	 	boolean failFlag=false;
	 	 	int row=0,col=0;
	 	 	for(int i=0;i<btn.length;i++){
	 	 		row=btn[i].getY()/15;
	 	 		col=btn[i].getX()/15;
	 	 		if(row==0){setFailFlag(true);break;}
	 	 		
	 	 	}


	 	 }
	 	 
	 	 

/*sucFlag would be used to tell RussianSYS wheather 
	 	 * current game is suc,this flag would be set by sucChecker();
	 	 * 
	 	 * */
private boolean sucflag=false;
 private void setSucFlag(boolean flag){
	 	this.sucflag=flag;
	 	}
	 	private void sucChecker(){
	 	if(conbtn.getLevel()==10)sucflag=true;	

	 	}


   
/*consumedflag would be used by RussianSYS to decide wheather 
	 	 * it is time to produce a new boxes
	 	 * 
	 	 * */
private boolean consumedflag=false;
	
private void setComsumeFlag(boolean flag){
	 		this.consumedflag=flag;
	 		
	 	} 	
  private void consume(){
    timer.stop();
    storeInfo();
   	toFlash();
   	failChecker();
   	sucChecker();
   	this.setComsumeFlag(true);
   	}

//some common flags.....used by RussianSYS
public boolean isLevelUp(){return this.levelUp;}
public boolean isConsumed(){return this.consumedflag;}	
public boolean isSuccessful(){return this.sucflag;}	
public boolean isFail(){ return this.failFlag;} 	
/*destroy itself....
 * 
 */	
public void destroy(){
timer.stop();
for(int i=0;i<btn.length;i++){
	conbtn.remove(btn[i]);

  }	

}

//Belows are common motions of boxes :Move down ,Move left ,Move right,Reshape
public void moveDown(){
	if(downfactor.canMoveDown()){
	for(int i=0;i<btn.length;i++){
	btn[i].setLocation(btn[i].getX(),btn[i].getY()+15);	
	
	}	
}


}
public void moveLeft(){
	if(leftfactor.canMoveLeft()){
		for(int i=0;i<btn.length;i++){
			btn[i].setLocation(btn[i].getX()-15,btn[i].getY());	
			
			}	
			
	
	}



}
public void moveRight(){
	if(rightfactor.canMoveRight()){
	for(int i=0;i<btn.length;i++){
		btn[i].setLocation(btn[i].getX()+15,btn[i].getY());	
		
		}	
		
	}
}

}





⌨️ 快捷键说明

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