/**
* File: ErsBlocksGame.java
* User: 吴永坚
* Date: 2006.11.8
* Describe: 俄罗斯方块的 Java 实现
*/
package Tetris;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* 游戏主类,继承自JFrame类,负责游戏的全局控制。
* 内含
* 1, 一个GameCanvas画布类的实例引用,
* 2, 一个保存当前活动块(ErsBlock)实例的引用,
* 3, 一个保存当前控制面板(ControlPanel)实例的引用;
*/
public class ErsBlocksGame extends JFrame {
/**
* 每填满一行计多少分
*/
public final static int PER_LINE_SCORE = 100;
/**
* 积多少分以后能升级
*/
public final static int PER_LEVEL_SCORE = PER_LINE_SCORE * 20;
/**
* 最大级数是10级
*/
public final static int MAX_LEVEL = 10;
/**
* 默认级数是5
*/
public final static int DEFAULT_LEVEL = 5;
/**
* 默认慢速休息时间是200ms
*/
public final static int SLOW_SLEEP_TIME = 200;
/**
* 默认中速休息时间是100ms
*/
public final static int MIDDLE_SLEEP_TIME = 100;
/**
* 默认快速休息时间是0ms
*/
public final static int FAST_SLEEP_TIME = 0;
public int delLine1 = 0;
public int delLine2 = 0;
public int delLine3 = 0;
public int delLine4 = 0;
public boolean autoPlay = false;
private int showSleepTime;
private Thread thread;
private MyRand myRand = new MyRand(0.2, 0.2, 0.15, 0.15, 0.15, 0.075, 0.075);
private GameCanvas canvas;
private ErsBlock block;
private boolean playing = false;
private ControlPanel ctrlPanel;
private JMenuBar bar = new JMenuBar();
private JMenu
mGame = new JMenu("游戏"),
mControl = new JMenu("控制"),
mWindowStyle = new JMenu("风格"),
mInfo = new JMenu("作者"),
mSpeed = new JMenu("演示速度设置");
private JMenuItem
miNewGame = new JMenuItem("新游戏"),
miSetBlockColor = new JMenuItem("设置方块颜色"),
miSetBackColor = new JMenuItem("设置背景颜色"),
miTurnHarder = new JMenuItem("增加难度"),
miTurnEasier = new JMenuItem("降低难度"),
miExit = new JMenuItem("退出"),
miHplay = new JMenuItem("人工游戏"),
miCplay = new JMenuItem("电脑游戏"),
miPause = new JMenuItem("暂停游戏"),
miResume = new JMenuItem("恢复游戏"),
miStop = new JMenuItem("结束游戏"),
miSetFast = new JMenuItem("快速"),
miSetMiddle = new JMenuItem("中速"),
miSetSlow = new JMenuItem("慢速"),
miAuthor = new JMenuItem("关于智能俄罗斯");
private JCheckBoxMenuItem
miAsWindows = new JCheckBoxMenuItem("Windows"),
miAsMotif = new JCheckBoxMenuItem("Motif"),
miAsMetal = new JCheckBoxMenuItem("Metal", true);
private ErsSelectOpt selectOpt;
/**
* 主游戏类的构造函数
* @param title String,窗口标题
*/
public ErsBlocksGame(String title) {
super(title);
delLine1 = 0;
delLine2 = 0;
delLine3 = 0;
delLine4 = 0;
autoPlay = false;
setSize(315, 400);
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((scrSize.width - getSize().width) / 2,
(scrSize.height - getSize().height) / 2);
createMenu();
Container container = getContentPane();
container.setLayout(new BorderLayout());
//canvas = new GameCanvas(20, 12);
canvas = new GameCanvas(20, 10);
ctrlPanel = new ControlPanel(this);
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();
canvas.fanning();
}
/**
* 让游戏“复位”
*/
public void reset() {
delLine1 = 0;
delLine2 = 0;
delLine3 = 0;
delLine4 = 0;
showSleepTime = 200;
ctrlPanel.reset();
canvas.reset();
}
/**
* 判断游戏是否还在进行
* @return boolean, true-还在运行,false-已经停止
*/
public boolean isPlaying() {
return playing;
}
/**
* 得到当前活动的块
* @return ErsBlock, 当前活动块的引用
*/
public ErsBlock getCurBlock() {
return block;
}
/**
* 得到当前画布
* @return GameCanvas, 当前画布的引用
*/
public GameCanvas getCanvas() {
return canvas;
}
/**
* 电脑开始游戏
*/
public void computerPlayGame() {
autoPlay = true;
computerPlay();
ctrlPanel.setComputerPlayButtonEnabled(false);
ctrlPanel.setHumanPlayButtonEnabled(false);
miCplay.setEnabled(false);
miHplay.setEnabled(false);
ctrlPanel.requestFocus();
}
/**
* 电脑开始游戏
*/
public void humanPlayGame() {
autoPlay = false;
humanPlay();
ctrlPanel.setComputerPlayButtonEnabled(false);
ctrlPanel.setHumanPlayButtonEnabled(false);
miCplay.setEnabled(false);
miHplay.setEnabled(false);
ctrlPanel.requestFocus();
}
/**
* 游戏暂停
*/
public void pauseGame() {
if (block != null && autoPlay == false){
block.pauseMove();
}
else if(block != null && autoPlay == true){
thread.suspend();
}
ctrlPanel.setPauseButtonEnabled(false);
ctrlPanel.setResumeButtonEnabled(true);
miPause.setEnabled(false);
miResume.setEnabled(true);
}
/**
* 让暂停中的游戏继续
*/
public void resumeGame() {
if (block != null && autoPlay == false){
block.resumeMove();
}
else if(block != null && autoPlay == true){
thread.resume();
}
ctrlPanel.setPauseButtonEnabled(true);
ctrlPanel.setResumeButtonEnabled(false);
miPause.setEnabled(true);
miResume.setEnabled(false);
ctrlPanel.requestFocus();
}
/**
* 用户停止游戏
*/
public void stopGame() {
playing = false;
if (block != null) block.stopMove();
miCplay.setEnabled(true);
miHplay.setEnabled(true);
miPause.setEnabled(true);
miResume.setEnabled(false);
ctrlPanel.setComputerPlayButtonEnabled(true);
ctrlPanel.setHumanPlayButtonEnabled(true);
ctrlPanel.setPauseButtonEnabled(true);
ctrlPanel.setResumeButtonEnabled(false);
}
/**
* 得到当前游戏者设置的游戏难度
* @return int, 游戏难度1-MAX_LEVEL
*/
public int getLevel() {
return ctrlPanel.getLevel();
}
/**
* 让用户设置游戏难度
* @param level int, 游戏难度1-MAX_LEVEL
*/
public void setLevel(int level) {
if (level < 11 && level > 0) ctrlPanel.setLevel(level);
}
/**
* 得到游戏积分
* @return int, 积分。
*/
public int getScore() {
if (canvas != null) return canvas.getScore();
return 0;
}
/**
* 得到消去1行的总数
* @return int, 消去1行的总数。
*/
public int getDelLine1() {
return delLine1;
}
/**
* 得到消去2行的总数
* @return int, 消去2行的总数。
*/
public int getDelLine2() {
return delLine2;
}
/**
* 得到消去3行的总数
* @return int, 消去3行的总数。
*/
public int getDelLine3() {
return delLine3;
}
/**
* 得到消去4行的总数
* @return int, 消去4行的总数。
*/
public int getDelLine4() {
return delLine4;
}
/**
* 得到自上次升级以来的游戏积分,升级以后,此积分清零
* @return int, 积分。
*/
public int getScoreForLevelUpdate() {
if (canvas != null) return canvas.getScoreForLevelUpdate();
return 0;
}
/**
* 当分数累计到一定的数量时,升一次级
* @return boolean, ture-update successufl, false-update fail
*/
public boolean levelUpdate() {
int curLevel = getLevel();
if (curLevel < MAX_LEVEL) {
setLevel(curLevel + 1);
canvas.resetScoreForLevelUpdate();
return true;
}
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.valu
没有合适的资源?快使用搜索试试~ 我知道了~
野人与传教士过河问题的实现
共51个文件
class:39个
java:8个
png:2个
需积分: 9 17 下载量 166 浏览量
2011-07-22
16:25:13
上传
评论
收藏 31KB 7Z 举报
温馨提示
野人与传教士过河问题的实现 安装JDK、配置环境变量,打开Eclipse导入ch05项目,运行主类crossriver.CrossRiver即可启动演示程 序。
资源推荐
资源详情
资源评论
收起资源包目录
ch05.7z (51个子文件)
ch05
.project 380B
bin
Tetris
ErsBlocksGame$15.class 702B
ErsBlocksGame$4.class 1KB
ErsBlocksGame$16.class 700B
ControlPanel$7.class 1KB
ControlPanel$4.class 816B
ErsBlocksGame$10.class 733B
ErsBlocksGame$12.class 708B
ErsBlocksGame$13.class 705B
ControlPanel$2.class 819B
ControlPanel$ControlKeyListener.class 1KB
ErsBlocksGame$5.class 1KB
ErsBlocksGame$6.class 778B
about.png 423B
GameCanvas.class 3KB
ErsBlocksGame$19.class 1KB
ControlPanel$5.class 814B
ErsBlocksGame$9.class 730B
ControlPanel$1.class 822B
ErsBlocksGame$17.class 1KB
ErsBlocksGame$20.class 744B
ErsBlocksGame$2.class 786B
ErsBlocksGame$Game.class 4KB
ErsBlocksGame$11.class 722B
ErsSelectOpt.class 7KB
ControlPanel.class 5KB
AboutBox.class 4KB
AboutBox$WindowCloser.class 739B
ErsBlocksGame$3.class 767B
ErsBlocksGame$18.class 1KB
ErsBox.class 1KB
ControlPanel$TipPanel.class 2KB
ErsBlocksGame$7.class 777B
ControlPanel$6.class 805B
ControlPanel$3.class 815B
MyRand.class 1KB
ErsBlocksGame.class 12KB
ErsBlocksGame$1.class 736B
ErsBlocksGame$8.class 729B
ErsBlock.class 5KB
ErsBlocksGame$14.class 701B
src
Tetris
about.png 423B
ErsBox.java 1KB
ErsBlocksGame.java 16KB
AboutBox.java 4KB
MyRand.java 2KB
GameCanvas.java 4KB
ControlPanel.java 8KB
ErsBlock.java 8KB
ErsSelectOpt.java 11KB
.classpath 232B
共 51 条
- 1
资源评论
ChangeZ_
- 粉丝: 7
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功