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

📄 test.java

📁 俄罗斯方块的java源代码
💻 JAVA
字号:
import java.awt.Color;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.*;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

/*
 * 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
 */

class Game extends JFrame {
	    private JButton startBtn=new  JButton("开始游戏");
	    private JTextArea text=new JTextArea("游戏说明:  (毕竟红尘 04.11.22 )\n" 
                                             +"**************************************\n\n"
	    		                             +"** W 键 变形                       **\n\n"
	    		                             +"** A 键 左移                        **\n\n"
											 +"** D 键 右移                         **\n\n"
											 +"** S 键 加速                          **\n\n"
											 +"** R 键 重新开始游戏                   **\n\n"
											 +"**鼠标单击可暂停游戏或者继续开始游戏\n\n"
											 +"*************************************");
	    
	    
	    private JLabel []info=new JLabel[]{
	        	new JLabel("*****   分数   ******"),	
				new JLabel("*****   等级   ******"),
				new JLabel("0 分"),
				new JLabel("1 级"),
				new JLabel("****************************")
				
			  };
	    private  ActionBtn  conbtn=new ActionBtn();
	    private Structrue currentbox;
	    private Thread gameThread;
	    private KeyListener keylistener=new KeyListener(){
		 	  public void keyPressed(KeyEvent e){
		 	  	if(e.getKeyCode()==KeyEvent.VK_A)
		 	  		currentbox.moveLeft();
		 	  		if(e.getKeyCode()==KeyEvent.VK_D)
				 	  	currentbox.moveRight();
		 	  			if(e.getKeyCode()==KeyEvent.VK_W)
		 	  				currentbox.reshape();
		 	  				if(e.getKeyCode()==KeyEvent.VK_S)
		 	  			 	  	currentbox.moveDown();
		 	  			 if(e.getKeyCode()==KeyEvent.VK_R){
		 	  			 reStartGame();	
		 	  			 }
		 	  
		 	            }
		 	           
		 	  			 public void keyTyped(KeyEvent e){ }
		 			 	 public void keyReleased(KeyEvent e) {
		 					
		 				 }
		 	  };
		 
		
	 
	    
 public Game(){
	 	configUI();
	 	initTask();
	 	
	
	  }
 

 
 private void refreshScoreLabel(){
 this.getContentPane().remove(info[2]);
 info[2]=new JLabel(String.valueOf(conbtn.getScore())+"分");
 info[2].setBounds(370+40,350+30,200,30);
 info[2].setBackground(Color.ORANGE);
info[2].setForeground(Color.red);
this.getContentPane().add(info[2]);
this.repaint();
 
 }
 private void refreshLevelLabel(){
 	 this.getContentPane().remove(info[3]);
 	info[3]=new JLabel(String.valueOf(conbtn.getLevel())+"级");
 	info[3].setBounds(370+40,350+30*3+20,200,30);
 	info[3].setBackground(Color.ORANGE);
 	info[3].setForeground(Color.red);
 	this.getContentPane().add(info[3]);
 	this.repaint();

 }
 private void configUI(){
	 	 this.setSize(600,750);
		 this.setBackground(Color.ORANGE);
		 this.setVisible(true);
		 Container con=this.getContentPane();
		 con.setBackground(Color.ORANGE);
		 con.setLayout(null);
		 this.setResizable(false);
		 startBtn.setBackground(Color.ORANGE);
	     startBtn.setForeground(Color.RED);
	     startBtn.setBounds(370,500,100,30);
	     con.add(startBtn);
	     con.add(conbtn);
	     //..//
	     text.setBackground(Color.ORANGE);
	     text.setForeground(Color.RED);
	     text.setBounds(370,50,250,300);
	     text.setFocusable(false);
	     text.setEditable(false);
	     con.add(text);
	     //..//
	     
	     info[0].setBounds(370,350,200,30);
	     info[2].setBounds(370+40,350+30,200,30);
	     info[1].setBounds(370,350+30*2+20,200,30);
	     info[3].setBounds(370+40,350+30*3+20,200,30);
	     for(int i=0;i<info.length;i++){
	     	info[i].setBackground(Color.ORANGE);
	     	info[i].setForeground(Color.red);
	     	con.add(info[i]);
	     	info[i].setFocusable(false);
		}
	     info[4].setForeground(Color.BLUE);
	     info[4].setBounds(370,600,200,30); 
	     conbtn.gameFailApearanceShower();
	    
	  }
private void initTask(){
	 	startBtn.addActionListener(new ActionListener(){
	 		boolean firstStart=true;
	 	public void actionPerformed(ActionEvent e){
	 		
	 		if(firstStart){
	 		
	 		startGame();
	 		startBtn.setText("重新开始");
	 		repaint();
	 		firstStart=false;
	 		}else reStartGame();
	 		
	 		 }	
	 	 
	 	});
	 	startBtn.addKeyListener(keylistener);
	 	this.addMouseListener(new MouseListener(){
	 	boolean stopFlag=false;
	 	public void mouseExited(MouseEvent e){}	
	 	public void mouseEntered(MouseEvent e){
	 		setCursor(Cursor.HAND_CURSOR);
	 		 	}
	 	public void mouseClicked(MouseEvent e){
	 		if(stopFlag){
	 			continueGame();
	 	  } else haltGame();
	 		
	 		stopFlag=!stopFlag;
	 	 		
	 	}
		public void mousePressed(MouseEvent arg0) {}
				
		public void mouseReleased(MouseEvent arg0) {}
			
	 	});
	 	
	 	 this.addWindowListener(new WindowAdapter(){
		 	public void windowClosing(WindowEvent e){
		 		 System.exit(0);
		 	}
		   });	
	 	
	 	 }

	  private void createGameThread(){
	    	gameThread=new Thread(new Runnable(){
	    	public void run(){
	    	      startSYS();
	    	      }	
	    	    	
	    	});
	    	
	      }
	 private boolean haltflag=false;
	 private void setHaltFlag(boolean flag){this.haltflag=flag;}
	 private boolean getHaltFlag(){return this.haltflag;}
	  
	 private void startSYS(){
	 currentbox=new BoxFactory().Produce(conbtn);
	   while(true){
	   	 if(!haltflag){
	 	   if(currentbox.isConsumed()){
	 	   	 if(currentbox.isFail())
	        { conbtn.gameFailApearanceShower();
	          System.out.println("Fail..............Game over...............");
	        	      break;}
	 	     if(currentbox.isSuccessful()){
	 	     	 conbtn.gameSuccessfulApearanceShower();
	 	         System.out.println("Successful..............Game over...............");
	 	     	 	     	break;}
	 	        if(currentbox.isLevelUp())this.refreshLevelLabel();
	 	        if(currentbox.isFlash())this.refreshScoreLabel();
	 	       
	 	        
	 	       
	 	           	 currentbox=new BoxFactory().Produce(conbtn);
	 	      	    
	 	         }
	           }
	         }
	       }  
	     
/*some extensable interfaces......*/
	 /*
	  * int leftKey,
	  *     rightKey,
	  *     reshapeKey,
	  *     downKey;
	  * public void setLeftKey(int key){this.leftKey=key;}
	  * public void setRightKey(int key){this.rightKey=key;}
	  * public void setReshapeKey(int key){this.reshapeKey=key;}
	  * public void setDownKey(int key){this.downKey=key;}
	  * 
	  * public void randomColor(){}
	  * 
	  * 
	  * 
	  * 
	  * 
	  * */
	 
	 
	 
//common interfaces of game
	 public void startGame(){
	 	//game start music playing...
	    this.createGameThread();
	        conbtn.reset();
	       	gameThread.start();
	       	
	} 

	 
	  public void haltGame(){
//	  game halt music playing...
	  	if(currentbox!=null){
	  		currentbox.getTimer().stop();
	  	    this.setHaltFlag(true);	
	  	  System.out.println("System halting....");
	  	}
	   }
	  public void continueGame(){
//game continue music playing.....
	  	if(currentbox!=null){
	  		currentbox.getTimer().restart();
	  	    this.setHaltFlag(false);
	  	    System.out.println("System continuing....");
	  	    }
	      }
	  public void reStartGame(){
	  	//reset music playing...
	  	//source.play()....
	  	currentbox.destroy();
	  	currentbox.getTimer().stop();
	  	gameThread.stop();
	  	gameThread=null;
	  	conbtn.reset();
	  	this.refreshLevelLabel();
	  	this.refreshScoreLabel();
	  	this.repaint();
	  	startGame();
		
	  	
	   } 
 
}



public class Test {

	public static void main(String[] args) {
			
		Game game=new Game();
		
		
	}

}

⌨️ 快捷键说明

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