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

📄 blocksgame.java

📁 java源代码
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.applet.*;

public class BlocksGame extends JFrame {
	public final static int PER_LINE_SCORE = 100;
	public final static int MAX_LEVEL = 10;
	public final static int DEFAULT_LEVEL = 6;

	private GameTable table;
	private Block block;
	private boolean playing = false;
  private ControlPanel ctrlPanel;  
  
  private JMenuBar bar=new JMenuBar();
  private JMenu mGame=new JMenu("Set Color");
  private JMenuItem SetBlockColor=new JMenuItem("set block color");
  private JMenuItem SetBackColor=new JMenuItem("set background color");

	public BlocksGame() {
  	this.setSize(350, 391);
		
		this.createMenu();
		this.setJMenuBar(bar);
		
		Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((scrSize.width - getSize().width) / 2,
		        (scrSize.height - getSize().height) / 2);
		        
		      

    this.getContentPane().setLayout(new BorderLayout());
    
    table = new GameTable(25, 22);
  	ctrlPanel = new ControlPanel(this);
  
    this.getContentPane().add(table,BorderLayout.CENTER);
    this.getContentPane().add(ctrlPanel,BorderLayout.EAST);
    this.setTitle("俄罗斯方块");
    this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
	//	Container container = getContentPane();
	//	container.setLayout(new BorderLayout(6, 0));

	//	container.add(canvas, BorderLayout.CENTER);
	//	container.add(ctrlPanel, BorderLayout.EAST);
       
	/*	addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				stopGame();
				System.exit(0);
			}
		});*/
	 
/*		addComponentListener(new ComponentAdapter() {
			public void componentResized(ComponentEvent ce) {
				canvas.fanning();
			}
		});*/
    // show();
       setVisible(true);
  	table.fanning();
  	
}

public void createMenu(){
     bar.add(mGame);
     mGame.add(SetBlockColor);
     mGame.addSeparator();
     mGame.add(SetBackColor);
     
     SetBlockColor.addActionListener(new ActionListener(){
      public void  actionPerformed(ActionEvent e){
      Color FrontColor=
      JColorChooser.showDialog(BlocksGame.this,"set color for block",table.getBlockColor());
      if(FrontColor!=null){
       table.setBlockColor(FrontColor);
      }
      }
     });
     SetBackColor.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent e){
       Color BackColor=
       JColorChooser.showDialog(BlocksGame.this,"set color for table",table.getBackgroundColor());
       if(BackColor!=null){
        table.setBackgroundColor(BackColor);
       }
      }
     });
         }

// 让游戏“复位”

	public void reset() {
		ctrlPanel.reset();
		table.reset();
	} 
// 判断游戏是否还在进行
	public boolean isPlaying() {
		return playing;
	}

// 得到当前活动的块
	public Block getCurBlock() {
		return block;
	}
	
	public GameTable gettable() {
		return table;
	}

// 开始游戏
	
	public void playGame() {
		play();
		ctrlPanel.setPlayButtonEnable(false);

		ctrlPanel.requestFocus();
	}

//游戏暂停

	public void pauseGame() {
  	
		if(table.getScore()>=100){
		if (block != null) {
     block.pauseMove();
     ctrlPanel.setPauseButtonLabel(false);
    JOptionPane.showMessageDialog(this,"恭喜你!");
    }
    }
	}

// 让暂停中的游戏继续

	public void resumeGame() {
		if (block != null) 
    block.resumeMove();
		ctrlPanel.setPauseButtonLabel(true);
		ctrlPanel.requestFocus();
	}

// 用户停止游戏

	public void stopGame() {
		playing = false;
		if (block != null) block.stopMove();
		ctrlPanel.setPlayButtonEnable(true);
		ctrlPanel.setPauseButtonLabel(true);
	}

	public int getLevel() {
		return ctrlPanel.getLevel();
	}

	public void setLevel(int level) {
		if (level < 11 && level > 0) ctrlPanel.setLevel(level);
	}

	public int getScore() {
		if (table != null)
    return table.getScore();
		return 0;
	}


/*	public int getScoreForLevelUpdate() {
		if (table != null) 
    return table.getScoreForLevelUpdate();
		return 0;
	}*/
 

// 当分数累计到一定的数量时,升一次级
/*	public boolean levelUpdate() {
		int curLevel = getLevel();
		if (curLevel < MAX_LEVEL) {
		setLevel(curLevel + 1);
			table.resetScoreForLevelUpdate();
			return true;
		}
		return false;
	}*/


// 游戏开始

	private void play() {
	reset();
		playing = true;
		Thread thread = new Thread(new Game());
		thread.start();
	}

//当你达到一定的分数时,弹出一消息框
 /*  private void xiaoxi(){
   if(table.getScore()>=100){
     JOptionPane.showMessageDialog(this,"恭喜你!");
   }
   }*/
   
// 报告游戏结束了
	private void reportGameOver() {
		
   		if(table.getScore()>=100){
      //JOptionPane.showMessageDialog(this,"你还行,给你个奖励!");
        JOptionPane.showOptionDialog(this,"你还行,给你个惊喜!得分为:"+getScore(),"Option Dialog",
        JOptionPane.DEFAULT_OPTION,
        JOptionPane.PLAIN_MESSAGE,null,new Object[]{"Button1","Button2","Button3"},"Button2"); 
                                                                
       }
		else{
        JOptionPane.showMessageDialog(this, "哈哈!你太笨了!得分为:"+getScore());
    }
	}

//设置窗口外观
	 private void setWindowStyle(String plaf) {
		try {
			UIManager.setLookAndFeel(plaf);
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception e) {
		}
	}

	/**
	 * 一轮游戏过程,实现了Runnable接口
	 * 一轮游戏是一个大循环,在这个循环中,每隔100毫秒,
	 * 检查游戏中的当前块是否已经到底了,如果没有,
	 * 就继续等待。如果到底了,就看有没有全填满的行,
	 * 如果有就删除它,并为游戏者加分,同时随机产生一个
	 * 新的当前块,让它自动下落。
	 * 当新产生一个块时,先检查画布最顶上的一行是否已经
	 * 被占了,如果是,可以判断Game Over了。
	 */
	private class Game implements Runnable {
		public void run() {
			int col = (int)(Math.random()*(table.getCols()-3)),
			        style=Block.STYLES[(int)(Math.random()*7)][(int)(Math.random()*4)];

			while (playing) {
				if (block != null) {    //第一次循环时,block为空
					if (block.isAlive()) {
						try {
							Thread.currentThread().sleep(100);
						} catch (InterruptedException ie) {
							ie.printStackTrace();
						}
						continue;
					}
				}

				checkFullLine();        //检查是否有全填满的行

				if (isGameOver()) {     //检查游戏是否应该结束了
			
					ctrlPanel.setPlayButtonEnable(true);
					ctrlPanel.setPauseButtonLabel(true);

					reportGameOver();
					return;
				}

				block = new Block(style, -1, col, getLevel(), table);
				block.start();

				col = (int) (Math.random() * (table.getCols() - 3));
				style =Block.STYLES[(int) (Math.random() * 7)][(int) (Math.random() * 4)];

				ctrlPanel.setTipStyle(style);
			}
		}

/*
检查画布中是否有全填满的行,如果有就删除
*/
		public void checkFullLine() {
			for (int i = 0; i < table.getRows(); i++) {
				int row = -1;
				boolean fullLineColorBox = true;
				for (int j = 0; j < table.getCols(); j++) {
					if (!table.getBox(i, j).isColorBox()) {
						fullLineColorBox = false;
						break;
					}
				}
				if (fullLineColorBox) {
					row = i--;
					table.removeLine(row);
				}
			}
		}

	
//根据最顶行是否被占,判断游戏是否已经结束了。
		 
		private boolean isGameOver() {
			for (int i = 0; i < table.getCols(); i++) {
				ErsBox box = table.getBox(0, i);
				if (box.isColorBox()) return true;
			}
			return false;
		}
	}
		
	public static void main(String[] args) {
		new BlocksGame();
	}
}

⌨️ 快捷键说明

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