这是java课最后做的课程设计,由于java是初学的,所以做的时候有参考一些技术大牛的博客,在此表示感谢。
发在这里跟大家交流学习一下。
如需要完整工程文件、说明文档以及可运行jar文件,下载地址:点击打开链接
RussianBlocksGame.java
package RussiaBlocksGame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
public class RussiaBlocksGame extends Jframe {
private static final long serialVersionUID = -7332245439279674749L;
public final static int PER_LINE_SCORE = 100;
public final static int PER_LEVEL_SCORE = PER_LINE_SCORE * 20;
public final static int MAX_LEVEL = 10;
public final static int DEFAULT_LEVEL = 2;
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(" 控制"),
mInfo = new JMenu("帮助");
private JMenuItem miNewGame = new JMenuItem("新游戏"),
miSetBlockColor = new JMenuItem("设置方块颜色..."),
miSetBackColor = new JMenuItem("设置背景颜色..."),
miTurnHarder = new JMenuItem("升高游戏难度"),
miTurnEasier = new JMenuItem("降低游戏难度"),
miExit = new JMenuItem("退出"),
miPlay = new JMenuItem("开始"),
miPause = new JMenuItem("暂停"),
miResume = new JMenuItem("恢复"),
miStop = new JMenuItem("终止游戏"),
miRule = new JMenuItem("游戏规则"),
miAuthor = new JMenuItem("关于本游戏");
private void creatMenu() {
bar.add(mGame);
bar.add(mControl);
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(miPlay);
miPlay.setEnabled(true);
mControl.add(miPause);
miPause.setEnabled(false);
mControl.add(miResume);
miResume.setEnabled(false);
mControl.add(miStop);
miStop.setEnabled(false);
mInfo.add(miRule);
mInfo.add(miAuthor);
setJMenuBar(bar);
miNewGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
stopGame();
reset();
setLevel(DEFAULT_LEVEL);
}
});
//设置方块颜色
miSetBlockColor.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Color newFrontColor =
JColorChooser.showDialog(RussiaBlocksGame.this, "设置方块颜色", canvas.getBlockColor());
if (newFrontColor != null) {
canvas.setBlockColor(newFrontColor);
}
}
});
//设置背景颜色
miSetBackColor.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Color newBackColor =
JColorChooser.showDialog(RussiaBlocksGame.this, "设置背景颜色", canvas.getBackgroundColor());
if (newBackColor != null) {
canvas.setBackgroundColor(newBackColor);
}
}
});
//定义菜单栏"关于"的功能,弹出确认框。
miAuthor.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "软件工程(4)班n3115005372n杨宇杰n©一切解释权归杨宇杰所有", "关于俄罗斯方块 - 2016", 1);
}
});
//游戏规则说明
miRule.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "由小方块组成的不同形状的板块陆续从屏幕上方落下来,n玩家通过调整板块的位置和方向,使它们在屏幕底部拼n出完整的一条或几条。这些完整的横条会随即消失,给新n落下来的板块腾出空间,与此同时,玩家得到分数奖励。n没有被消除掉的方块不断堆积起来,一旦堆到屏幕顶端,n玩家便告输,游戏结束。", "游戏规则", 1);
}
});
//增加难度
miTurnHarder.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int curLevel = getLevel();
if (!playing && curLevel < MAX_LEVEL) {
setLevel(curLevel + 1);
}
}
});
//减少难度
miTurnEasier.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int curLevel = getLevel();
if (!playing && curLevel > 1) {
setLevel(curLevel - 1);
}
}
});
//退出按钮动作响应
miExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
public RussiaBlocksGame(String title) {
super(title); //设置标题
setSize(500, 600); //设置窗口大小
setLocationRelativeTo(null); //设置窗口居中
creatMenu();
Container container = getContentPane(); //创建菜单栏
container.setLayout(new BorderLayout(6, 0)); //设置窗口的布局管理器
canvas = new GameCanvas(20, 15); //新建游戏画布
ctrlPanel = new ControlPanel(this); //新建控制面板
container.add(canvas, BorderLayout.CENTER); //左边加上画布
container.add(ctrlPanel, BorderLayout.EAST); //右边加上控制面板
//注册窗口事件。当点击关闭按钮时,结束游戏,系统退出。
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
stopGame();
System.exit(0);
}
});
//根据窗口大小,自动调节方格的尺寸
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent ce) {
canvas.adjust();
}
});
setVisible(true);
canvas.adjust();
}
public void reset() { //画布复位,控制面板复位
ctrlPanel.setPlayButtonEnable(true);
ctrlPanel.setPauseButtonEnable(false);
ctrlPanel.setPauseButtonLabel(true);
ctrlPanel.setStopButtonEnable(false);
ctrlPanel.setTurnLevelDownButtonEnable(true);
ctrlPanel.setTurnLevelUpButtonEnable(true);
miPlay.setEnabled(true);
miPause.setEnabled(false);
miResume.setEnabled(false);
miStop.setEnabled(false);
ctrlPanel.reset();
canvas.reset();
}
public boolean isPlaying() {
return playing;
}
public ErsBlock getCurBlock() {
return block;
}
public GameCanvas getCanvas() {
return canvas;
}
public void playGame() {
play();
ctrlPanel.setPlayButtonEnable(false);
ctrlPanel.setPauseButtonEnable(true);
ctrlPanel.setPauseButtonLabel(true);
ctrlPanel.setStopButtonEnable(true);
ctrlPanel.setTurnLevelDownButtonEnable(false);
ctrlPanel.setTurnLevelUpButtonEnable(false);
miStop.setEnabled(true);
miTurnHarder.setEnabled(false);
miTurnEasier.setEnabled(false);
ctrlPanel.requestFocus(); //设置焦点在控制面板上
}
public void pauseGame() {
if (block != null) {
block.pauseMove();
}
ctrlPanel.setPlayButtonEnable(false);
ctrlPanel.setPauseButtonLabel(false);
ctrlPanel.setStopButtonEnable(true);
miPlay.setEnabled(false);
miPause.setEnabled(false);
miResume.setEnabled(true);
miStop.setEnabled(true);
}
public void resumeGame() {
if (block != null) {
block.resumeMove();
}
ctrlPanel.setPlayButtonEnable(false);
ctrlPanel.setPauseButtonEnable(true);
ctrlPanel.setPauseButtonLabel(true);
miPause.setEnabled(true);
miResume.setEnabled(false);
ctrlPanel.requestFocus();
}
public void stopGame() {
playing = false;
if (block != null) {
block.stopMove();
}
ctrlPanel.setPlayButtonEnable(true);
ctrlPanel.setPauseButtonEnable(false);
ctrlPanel.setPauseButtonLabel(true);
ctrlPanel.setStopButtonEnable(false);
ctrlPanel.setTurnLevelDownButtonEnable(true);
ctrlPanel.setTurnLevelUpButtonEnable(true);
miPlay.setEnabled(true);
miPause.setEnabled(false);
miResume.setEnabled(false);
miStop.setEnabled(false);
miTurnHarder.setEnabled(true);
miTurnEasier.setEnabled(true);
reset();//重置画布和控制面板
}
public int getLevel() {
return ctrlPanel.getLevel();
}
public void setLevel(int level) {
if (level < 11 && level > 0) {
ctrlPanel.setLevel(level);
}
}
public int getScore() {
if (canvas != null) {
return canvas.getScore();
}
return 0;
}
public int getScoreForLevelUpdate() {
if (canvas != null) {
return canvas.getScoreForLevelUpdate();
}
return 0;
}
public boolean levelUpdate() {
int curLevel = getLevel();
if (curLevel < MAX_LEVEL) {
setLevel(curLevel + 1);
canvas.resetScoreForLevelUpdate();
return true;
}
return false;
}
private void play() {
reset();
playing = true;
Thread thread = new Thread(new Game());//启动游戏线程
thread.start();
}
private void reportGameOver() {
new gameOverDialog(this, "俄罗斯方块", "游戏结束,您的得分为" + canvas.getScore());
}
private class Game implements Runnable {
@Override
public void run() {
int col = (int) (Math.random() * (canvas.getCols() - 3));//随机生成方块出现的位置
int style = ErsBlock.STYLES[ (int) (Math.random() * 7)][(int) (Math.random() * 4)];//随机生成一种形状的方块
while (playing) {
if (block != null) { //第一次循环时,block为空
if (block.isAlive()) {
try {
Thread.currentThread();
Thread.sleep(500);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
continue;
}
}
checkFullLine(); //检查是否有全填满的行
if (isGameOver()) {
reportGameOver();
miPlay.setEnabled(true);
miPause.setEnabled(false);
miResume.setEnabled(false);
miStop.setEnabled(false);
ctrlPanel.setPlayButtonEnable(true);
ctrlPanel.setPauseButtonLabel(false);
ctrlPanel.setStopButtonEnable(false);
return;
}
block = new ErsBlock(style, -1, col, getLevel(), canvas);
block.start();
col = (int) (Math.random() * (canvas.getCols() - 3));
style = ErsBlock.STYLES[ (int) (Math.random() * 7)][(int) (Math.random() * 4)];
ctrlPanel.setTipStyle(style);
}
}
//检查画布中是否有全填满的行,如果有就删之
public void checkFullLine() {
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) {
row = i--;
canvas.removeLine(row);
}
}
}
//根据最顶行是否被占,判断游戏是否已经结束了
//@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;
}
}
@SuppressWarnings("serial")
private class gameOverDialog extends JDialog implements ActionListener {
private JButton againButton, exitButton;
private Border border = new EtchedBorder(EtchedBorder.RAISED, Color.white, new Color(148, 145, 140));
public gameOverDialog(Jframe parent, String title, String message) {
super(parent, title, true);
if (parent != null) {
setSize(240, 120);
this.setLocationRelativeTo(parent);
JPanel messagePanel = new JPanel();
messagePanel.add(new JLabel(message));
messagePanel.setBorder(border);
Container container = this.getContentPane();
container.setLayout(new GridLayout(2, 0, 0, 10));
container.add(messagePanel);
JPanel choosePanel = new JPanel();
choosePanel.setLayout(new GridLayout(0, 2, 4, 0));
container.add(choosePanel);
againButton = new JButton("再玩一局");
exitButton = new JButton("退出游戏");
choosePanel.add(new JPanel().add(againButton));
choosePanel.add(new JPanel().add(exitButton));
choosePanel.setBorder(border);
}
againButton.addActionListener(this);
exitButton.addActionListener(this);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == againButton) {
this.setVisible(false);
reset();
} else if (e.getSource() == exitButton) {
stopGame();
System.exit(0);
}
}
}
}
GameCanvas.java
package RussiaBlocksGame;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
public class GameCanvas extends JPanel {
private static final long serialVersionUID = 6732901391026089276L;
private Color backColor = Color.darkGray, frontColor = Color.WHITE;
private int rows, cols, score = 0, scoreForLevelUpdate = 0;
private ErsBox[][] boxes;
private int boxWidth, boxHeight;
public GameCanvas(int rows, int cols) {
this.rows = rows;
this.cols = cols;
boxes = new ErsBox[rows][cols];
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
boxes[i][j] = new ErsBox(false);
}
}
setBorder(new EtchedBorder(EtchedBorder.RAISED, Color.white, new Color(148, 145, 140)));
}
public GameCanvas(int rows, int cols,
Color backColor, Color frontColor) {
this(rows, cols);
this.backColor = backColor;
this.frontColor = frontColor;
}
public void setBackgroundColor(Color backColor) {
this.backColor = backColor;
}
public Color getBackgroundColor() {
return backColor;
}
public void setBlockColor(Color frontColor) {
this.frontColor = frontColor;
}
public Color getBlockColor() {
return frontColor;
}
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
public int getScore() {
return score;
}
public int getScoreForLevelUpdate() {
return scoreForLevelUpdate;
}
public void resetScoreForLevelUpdate() {
scoreForLevelUpdate -= RussiaBlocksGame.PER_LEVEL_SCORE;
}
public ErsBox getBox(int row, int col) {
if (row < 0 || row > boxes.length - 1 || col < 0 || col > boxes[0].length - 1) {
return null;
}
return (boxes[row][col]);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(frontColor);
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
g.setColor(boxes[i][j].isColorBox() ? frontColor : backColor);
g.fill3DRect(j * boxWidth, i * boxHeight,
boxWidth, boxHeight, true);
}
}
}
public void adjust() {
boxWidth = getSize().width / cols;
boxHeight = getSize().height / rows;
}
public synchronized void removeLine(int row) {
for (int i = row; i > 0; i--) {
for (int j = 0; j < cols; j++) {
boxes[i][j] = (ErsBox) boxes[i - 1][j].clone(); //将上一行的方块颜色克隆下来,
} //即消去一行方块
}
score += RussiaBlocksGame.PER_LEVEL_SCORE;
scoreForLevelUpdate += RussiaBlocksGame.PER_LEVEL_SCORE;
repaint();
}
public void reset() {
score = 0;
scoreForLevelUpdate = 0;
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
boxes[i][j].setColor(false);
}
}
repaint();
}
}
ControlPanel.java
package RussiaBlocksGame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Timer;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
class ControlPanel extends JPanel {
private static final long serialVersionUID = 3900659640646175724L;
private JTextField tfLevel = new JTextField("" + RussiaBlocksGame.DEFAULT_LEVEL),
tfScore = new JTextField(" 0"),
tfTime = new JTextField(" ");
private JButton btPlay = new JButton(" 开始"),
btPause = new JButton(" 暂停"),
btStop = new JButton("终止游戏"),
btTurnLevelUp = new JButton(" 增加难度"),
btTurnLevelDown = new JButton(" 降低难度");
private JPanel plTip = new JPanel(new BorderLayout());
private TipPanel plTipBlock = new TipPanel();
private JPanel plInfo = new JPanel(new GridLayout(4, 1));
private JPanel plButton = new JPanel(new GridLayout(6, 1));
private Timer timer;
private Border border = new EtchedBorder(EtchedBorder.RAISED, Color.white, new Color(148, 145, 140));
public ControlPanel(final RussiaBlocksGame game) {
setLayout(new GridLayout(3, 1, 0, 2));
plTip.add(new JLabel(" 下一个方块"), BorderLayout.NORTH); //添加组件
plTip.add(plTipBlock);
plTip.setBorder(border);
plInfo.add(new JLabel(" 难度系数"));
plInfo.add(tfLevel);
plInfo.add(new JLabel(" 得分"));
plInfo.add(tfScore);
plInfo.setBorder(border);
plButton.add(btPlay);
btPlay.setEnabled(true);
plButton.add(btPause);
btPause.setEnabled(false);
plButton.add(btStop);
btStop.setEnabled(false);
plButton.add(btTurnLevelUp);
plButton.add(btTurnLevelDown);
plButton.add(tfTime);
plButton.setBorder(border);
tfLevel.setEditable(false);
tfScore.setEditable(false);
tfTime.setEditable(false);
add(plTip);
add(plInfo);
add(plButton);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent ke) {
if (!game.isPlaying()) {
return;
}
ErsBlock block = game.getCurBlock();
switch (ke.getKeyCode()) {
case KeyEvent.VK_DOWN:
block.moveDown();
break;
case KeyEvent.VK_LEFT:
block.moveLeft();
break;
case KeyEvent.VK_RIGHT:
block.moveRight();
break;
case KeyEvent.VK_UP:
block.turnNext();
break;
default:
break;
}
}
});
btPlay.addActionListener(new ActionListener() { //开始游戏
@Override
public void actionPerformed(ActionEvent ae) {
game.playGame();
}
});
btPause.addActionListener(new ActionListener() { //暂停游戏
@Override
public void actionPerformed(ActionEvent ae) {
if (btPause.getText().equals(" 暂停")) {
game.pauseGame();
} else {
game.resumeGame();
}
}
});
btStop.addActionListener(new ActionListener() { //停止游戏
@Override
public void actionPerformed(ActionEvent ae) {
game.stopGame();
}
});
btTurnLevelUp.addActionListener(new ActionListener() { //升高难度
@Override
public void actionPerformed(ActionEvent ae) {
try {
int level = Integer.parseInt(tfLevel.getText());
if (level < RussiaBlocksGame.MAX_LEVEL) {
tfLevel.setText("" + (level + 1));
}
} catch (NumberFormatException e) {
}
requestFocus();
}
});
btTurnLevelDown.addActionListener(new ActionListener() { //降低游戏难度
@Override
public void actionPerformed(ActionEvent ae) {
try {
int level = Integer.parseInt(tfLevel.getText());
if (level > 1) {
tfLevel.setText("" + (level - 1));
}
} catch (NumberFormatException e) {
}
requestFocus();
}
});
addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent ce) {
plTipBlock.adjust();
}
});
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
DateFormat format = new SimpleDateFormat("时间:HH:mm:ss"); //系统获得时间
Date date = new Date();
tfTime.setText(format.format(date));
tfScore.setText("" + game.getScore());
int ScoreForLevelUpdate = //判断当前分数是否能升级
game.getScoreForLevelUpdate();
if (ScoreForLevelUpdate >= RussiaBlocksGame.PER_LEVEL_SCORE
&& ScoreForLevelUpdate > 0) {
game.levelUpdate();
}
}
});
timer.start();
}
public void setTipStyle(int style) {
plTipBlock.setStyle(style);
}
public int getLevel() {
int level = 0;
try {
level = Integer.parseInt(tfLevel.getText());
} catch (NumberFormatException e) {
}
return level;
}
public void setLevel(int level) {
if (level > 0 && level < 11) {
tfLevel.setText("" + level);
}
}
public void setPlayButtonEnable(boolean enable) {
btPlay.setEnabled(enable);
}
public void setPauseButtonEnable(boolean enable) {
btPause.setEnabled(enable);
}
public void setPauseButtonLabel(boolean pause) {
btPause.setText(pause ? " 暂停" : " 继续");
}
public void setStopButtonEnable(boolean enable) {
btStop.setEnabled(enable);
}
public void setTurnLevelUpButtonEnable(boolean enable) {
btTurnLevelUp.setEnabled(enable);
}
public void setTurnLevelDownButtonEnable(boolean enable) {
btTurnLevelDown.setEnabled(enable);
}
public void reset() {
tfScore.setText(" 0");
plTipBlock.setStyle(0);
}
public void adjust() {
plTipBlock.adjust();
}
public class TipPanel extends JPanel { //TipPanel用来显示下一个将要出现方块的形状
private static final long serialVersionUID = 5160553671436997616L;
private Color backColor = Color.darkGray, frontColor = Color.WHITE;
private ErsBox[][] boxes = new ErsBox[ErsBlock.BOXES_ROWS][ErsBlock.BOXES_COLS];
private int style, boxWidth, boxHeight;
private boolean isTiled = false;
public TipPanel() {
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
boxes[i][j] = new ErsBox(false);
}
}
}
public void setStyle(int style) {
this.style = style;
repaint();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (!isTiled) {
adjust();
}
int key = 0x8000;
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
Color color = ((key & style) != 0 ? frontColor : backColor);
g.setColor(color);
g.fill3DRect(j * boxWidth, i * boxHeight,
boxWidth, boxHeight, true);
key >>= 1;
}
}
}
public void adjust() {
boxWidth = getSize().width / ErsBlock.BOXES_COLS;
boxHeight = getSize().height / ErsBlock.BOXES_ROWS;
isTiled = true;
}
}
}
ErsBox.java
package RussiaBlocksGame;
import java.awt.Dimension;
public class ErsBox implements Cloneable {
private boolean isColor;
private Dimension size = new Dimension();
public ErsBox(boolean isColor) {
this.isColor = isColor;
}
public boolean isColorBox() {
return isColor;
}
public void setColor(boolean isColor) {
this.isColor = isColor;
}
public Dimension getSize() {
return size;
}
public void setSize(Dimension size) {
this.size = size;
}
@Override
public Object clone() {
Object cloned = null;
try {
cloned = super.clone();
} catch (Exception ex) {
ex.printStackTrace();
}
return cloned;
}
}
ErsBlock.java
package RussiaBlocksGame;
class ErsBlock extends Thread {
public final static int BOXES_ROWS = 4;
public final static int BOXES_COLS = 4;
public final static int LEVEL_FLATNESS_GENE = 3;
public final static int BETWEEN_LEVELS_DEGRESS_TIME = 50;
public final static int BLOCK_KIND_NUMBER = 7;
public final static int BLOCK_STATUS_NUMBER = 4;
public final static int[][] STYLES = { //共28种状态
{0x0f00, 0x4444, 0x0f00, 0x4444}, //长条型的四种状态
{0x04e0, 0x0464, 0x00e4, 0x04c4}, //T型的四种状态
{0x4620, 0x6c00, 0x4620, 0x6c00}, //反Z型的四种状态
{0x2640, 0xc600, 0x2640, 0xc600}, //Z型的四种状态
{0x6220, 0x1700, 0x2230, 0x0740}, //7型的四种状态
{0x6440, 0x0e20, 0x44c0, 0x8e00}, //反7型的四种状态
{0x0660, 0x0660, 0x0660, 0x0660}, //方块的四种状态
};
private GameCanvas canvas;
private ErsBox[][] boxes = new ErsBox[BOXES_ROWS][BOXES_COLS];
private int style, y, x, level;
private boolean pausing = false, moving = true;
public ErsBlock(int style, int y, int x, int level, GameCanvas canvas) {
this.style = style;
this.y = y;
this.x = x;
this.level = level;
this.canvas = canvas;
int key = 0x8000;
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
boolean isColor = ((style & key) != 0);
boxes[i][j] = new ErsBox(isColor);
key >>= 1;
}
}
display();
}
@Override
public void run() {
while (moving) {
try {
sleep(BETWEEN_LEVELS_DEGRESS_TIME
* (RussiaBlocksGame.MAX_LEVEL - level + LEVEL_FLATNESS_GENE));
} catch (InterruptedException ie) {
ie.printStackTrace();
}
//后边的moving是表示在等待的100毫秒间,moving没有被改变
if (!pausing) {
moving = (moveTo(y + 1, x) && moving);
}
}
}
public void moveLeft() {
moveTo(y, x - 1);
}
public void moveRight() {
moveTo(y, x + 1);
}
public void moveDown() {
moveTo(y + 1, x);
}
public void turnNext() {
for (int i = 0; i < BLOCK_KIND_NUMBER; i++) {
for (int j = 0; j < BLOCK_STATUS_NUMBER; j++) {
if (STYLES[i][j] == style) {
int newStyle = STYLES[i][(j + 1) % BLOCK_STATUS_NUMBER];
turnTo(newStyle);
return;
}
}
}
}
public void startMove() {
pausing = false;
moving = true;
}
public void pauseMove() {
pausing = true;
// moving = false;
}
public void resumeMove() {
pausing = false;
moving = true;
}
public void stopMove() {
pausing = false;
moving = false;
}
private void erase() {
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
if (boxes[i][j].isColorBox()) {
ErsBox box = canvas.getBox(i + y, j + x);
if (box == null) {
continue;
}
box.setColor(false);
}
}
}
}
private void display() {
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
if (boxes[i][j].isColorBox()) {
ErsBox box = canvas.getBox(i + y, j + x);
if (box == null) {
continue;
}
box.setColor(true);
}
}
}
}
public boolean isMoveAble(int newRow, int newCol) {
erase();
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
if (boxes[i][j].isColorBox()) {
ErsBox box = canvas.getBox(i + newRow, j + newCol);
if (box == null || (box.isColorBox())) {
display();
return false;
}
}
}
}
display();
return true;
}
private synchronized boolean moveTo(int newRow, int newCol) {
if (!isMoveAble(newRow, newCol) || !moving) {
return false;
}
erase();
y = newRow;
x = newCol;
display();
canvas.repaint();
return true;
}
private boolean isTurnAble(int newStyle) {
int key = 0x8000;
erase();
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
if ((newStyle & key) != 0) {
ErsBox box = canvas.getBox(i + y, j + x);
if (box == null || (box.isColorBox())) {
display();
return false;
}
}
key >>= 1;
}
}
display();
return true;
}
private boolean turnTo(int newStyle) {
if (!isTurnAble(newStyle) || !moving) {
return false;
}
erase();
int key = 0x8000;
for (int i = 0; i < boxes.length; i++) {
for (int j = 0; j < boxes[i].length; j++) {
boolean isColor = ((newStyle & key) != 0);
boxes[i][j].setColor(isColor);
key >>= 1;
}
}
style = newStyle;
display();
canvas.repaint();
return true;
}
}
Main.java
package RussiaBlocksGame;
public class Main {
public static void main(String[] args) {
new RussiaBlocksGame("俄罗斯方块:杨宇杰");
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



