📄 ersblocksgame.java
字号:
return false;
}
/**
* 电脑智能玩游戏开始
*/
private void computerPlay() {
reset();
playing = true;
thread = new Thread(new Game());
thread.start();
}
/**
* 人工玩游戏开始
*/
private void humanPlay() {
reset();
playing = true;
thread = new Thread(new Game());
thread.start();
}
/**
* 报告游戏结束了
*/
private void reportGameOver() {
String msg = "游戏结束\n一共消去行数为: ";
msg += String.valueOf(delLine1+delLine2*2+delLine3*3+delLine4*4);
msg += "\n";
JOptionPane.showMessageDialog(this, msg);
}
/**
* 建立并设置窗口菜单
*/
private void createMenu() {
bar.add(mGame);
bar.add(mControl);
bar.add(mWindowStyle);
bar.add(mInfo);
mGame.add(miNewGame);
mGame.addSeparator();
mGame.add(miSetBlockColor);
mGame.add(miSetBackColor);
mGame.addSeparator();
mGame.add(miTurnHarder);
mGame.add(miTurnEasier);
mGame.addSeparator();
mGame.add(miExit);
mControl.add(miCplay);
mControl.add(miHplay);
mControl.add(miPause);
mControl.add(miResume);
mControl.add(miStop);
mControl.add(mSpeed);
mSpeed.add(miSetFast);
mSpeed.add(miSetMiddle);
mSpeed.add(miSetSlow);
mWindowStyle.add(miAsWindows);
mWindowStyle.add(miAsMotif);
mWindowStyle.add(miAsMetal);
mInfo.add(miAuthor);
setJMenuBar(bar);
miPause.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_P, KeyEvent.CTRL_MASK));
miResume.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
miNewGame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
stopGame();
reset();
setLevel(DEFAULT_LEVEL);
}
});
miSetBlockColor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Color newFrontColor =
JColorChooser.showDialog(ErsBlocksGame.this,
"设置方块颜色", canvas.getBlockColor());
if (newFrontColor != null)
canvas.setBlockColor(newFrontColor);
}
});
miSetBackColor.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Color newBackColor =
JColorChooser.showDialog(ErsBlocksGame.this,
"设置背景颜色", canvas.getBackgroundColor());
if (newBackColor != null)
canvas.setBackgroundColor(newBackColor);
}
});
miTurnHarder.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int curLevel = getLevel();
if (curLevel < MAX_LEVEL) setLevel(curLevel + 1);
}
});
miTurnEasier.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int curLevel = getLevel();
if (curLevel > 1) setLevel(curLevel - 1);
}
});
miSetFast.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
showSleepTime = FAST_SLEEP_TIME;
}
});
miSetMiddle.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
showSleepTime = MIDDLE_SLEEP_TIME;
}
});
miSetSlow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
showSleepTime = SLOW_SLEEP_TIME;
}
});
miExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
miCplay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
computerPlayGame();
}
});
miHplay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
humanPlayGame();
}
});
miPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
pauseGame();
}
});
miResume.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
resumeGame();
}
});
miStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
stopGame();
}
});
miAsWindows.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String plaf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
setWindowStyle(plaf);
canvas.fanning();
ctrlPanel.fanning();
miAsWindows.setState(true);
miAsMetal.setState(false);
miAsMotif.setState(false);
}
});
miAsMotif.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String plaf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
setWindowStyle(plaf);
canvas.fanning();
ctrlPanel.fanning();
miAsWindows.setState(false);
miAsMetal.setState(false);
miAsMotif.setState(true);
}
});
miAsMetal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String plaf = "javax.swing.plaf.metal.MetalLookAndFeel";
setWindowStyle(plaf);
canvas.fanning();
ctrlPanel.fanning();
miAsWindows.setState(false);
miAsMetal.setState(true);
miAsMotif.setState(false);
}
});
miAuthor.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
AboutBox about = new AboutBox();
}
});
}
/**
* 根据字串设置窗口外观
* @param plaf String, 窗口外观的描述
*/
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() * (canvas.getCols() - 3));
int curType = myRand.getRandom();//(int)(Math.random()*7);
int nextType;
int style = ErsBlock.STYLES[curType][(int)(Math.random()*4)];
selectOpt = new ErsSelectOpt();
while (playing) {
if (block != null) { //第一次循环时,block为空
if (block.isAlive()) {
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
continue;
}
}
checkFullLine(); //检查是否有全填满的行
if (isGameOver()) { //检查游戏是否应该结束了
miHplay.setEnabled(true);
miCplay.setEnabled(true);
miPause.setEnabled(true);
miResume.setEnabled(false);
ctrlPanel.setComputerPlayButtonEnabled(true);
ctrlPanel.setHumanPlayButtonEnabled(true);
ctrlPanel.setPauseButtonEnabled(true);
ctrlPanel.setResumeButtonEnabled(false);
reportGameOver();
return;
}
nextType = myRand.getRandom();//(int)(Math.random()*7);
block = new ErsBlock(style, -1, col, getLevel(), curType, nextType, selectOpt, canvas);
block.setAutoPlay(autoPlay);
if(autoPlay == true){
block.setShowSleepTime(showSleepTime);
}
col = (int) (Math.random() * (canvas.getCols() - 3));
style = ErsBlock.STYLES[nextType][(int)(Math.random()*4)];
ctrlPanel.setTipStyle(style);
block.start();
curType = nextType;
}
}
/**
* 检查画布中是否有全填满的行,如果有就删除之
*/
public void checkFullLine() {
int delLines = 0;
for (int i = 0; i < canvas.getRows(); i++) {
int row = -1;
boolean fullLineColorBox = true;
for (int j = 0; j < canvas.getCols(); j++) {
if (!canvas.getBox(i, j).isColorBox()) {
fullLineColorBox = false;
break;
}
}
if (fullLineColorBox) {
delLines++;
row = i--;
canvas.removeLine(row);
}
}
switch(delLines){
case 0:
break;
case 1:
delLine1++;
break;
case 2:
delLine2++;
break;
case 3:
delLine3++;
break;
case 4:
delLine4++;
break;
default:
break;
}
}
/**
* 根据最顶行是否被占,判断游戏是否已经结束了。
* @return boolean, true-游戏结束了,false-游戏未结束
*/
private boolean isGameOver() {
for (int i = 0; i < canvas.getCols(); i++) {
ErsBox box = canvas.getBox(0, i);
if (box.isColorBox()) return true;
}
return false;
}
}
/**
* 程序入口函数
* @param args String[], 附带的命令行参数
*/
public static void main(String[] args) {
new ErsBlocksGame("智能俄罗斯方块");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -