?? blocksgame.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 + -