package russia;
/www.quzhuanpan.com 转载请告知/
/*
控制面板类
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class ControlPanel extends JPanel
{
private TipBlockPanel tipBlockPanel;
private JPanel tipPanel,InfoPanel,buttonPanel;
private final JTextField levelField,scoreField;
private JButton playButton,pauseButton,stopButton,
turnHarderButton,turnEasilyButton;
private EtchedBorder border=new EtchedBorder(EtchedBorder.RAISED,Color.WHITE, new Color(148, 145, 140)) ;
private RussiaBlocksGame game;
private Timer timer;
/www.quzhuanpan.com 转载请告知/
public ControlPanel(final RussiaBlocksGame game)
{
this.game = game;
/*
*图形界面部分
*/
setLayout(new GridLayout(3,1,0,4));
tipBlockPanel = new TipBlockPanel();
tipPanel = new JPanel( new BorderLayout() );
tipPanel.add( new JLabel(“Next Block:”) , BorderLayout.NORTH );
tipPanel.add( tipBlockPanel , BorderLayout.CENTER );
tipPanel.setBorder(border);
InfoPanel = new JPanel( new GridLayout(4,1,0,0) );
levelField = new JTextField(“”+RussiaBlocksGame.DEFAULT_LEVEL);
levelField.setEditable(false);
scoreField = new JTextField(“0”);
scoreField.setEditable(false);
InfoPanel.add(new JLabel(“Level:”));
InfoPanel.add(levelField);
InfoPanel.add(new JLabel(“Score:”));
InfoPanel.add(scoreField);
InfoPanel.setBorder(border);
/www.quzhuanpan.com 转载请告知/
buttonPanel = new JPanel(new GridLayout(5,1,0,0));
playButton = new JButton(“Play”);
pauseButton = new JButton(“Pause”);
stopButton = new JButton(“Stop”);
turnHarderButton = new JButton(“Turn harder”);
turnEasilyButton = new JButton(“Turn easily”);
buttonPanel.add(playButton);
buttonPanel.add(pauseButton);
buttonPanel.add(stopButton);
buttonPanel.add(turnHarderButton);
buttonPanel.add(turnEasilyButton);
buttonPanel.setBorder(border);
addKeyListener(new ControlKeyListener());//添加
add(tipPanel);
add(InfoPanel);
add(buttonPanel);
/*
*添加事件监听器
*/
playButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
game.playGame();
requestFocus();//让ControlPanel重新获得焦点以响应键盘事件
}
});
/*www.quzhuanpan.com 转载请告知*/
pauseButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(pauseButton.getText().equals(“Pause”))
game.pauseGame();
else
game.resumeGame();
requestFocus();//让ControlPanel重新获得焦点以响应键盘事件
}
}
);
stopButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
game.stopGame();
requestFocus();//让ControlPanel重新获得焦点以响应键盘事件
}
});
turnHarderButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
int level = 0;
try{
level = Integer.parseInt(levelField.getText());
setLevel(level + 1);
}catch(NumberFormatException e)
{
e.printStackTrace();
}
requestFocus();//让ControlPanel重新获得焦点以响应键盘事件
}
});
turnEasilyButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
int level = 0;
try{
level = Integer.parseInt(levelField.getText());
setLevel(level – 1);
}catch(NumberFormatException e)
{
e.printStackTrace();
}
requestFocus();//让ControlPanel重新获得焦点以响应键盘事件
}
});
/*
* 时间驱动程序,每格500毫秒对level,score值进行更新
*/
timer = new Timer(500,
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
scoreField.setText(“”+game.getScore());
game.levelUpdate();
}
}
);
timer.start();
}
/*
设置预显方块的样式
*/
public void setBlockStyle(int style)
{
tipBlockPanel.setStyle(style);
tipBlockPanel.repaint();
}
/*
重置,将所有数据恢复到最初值
*/
public void reset()
{
levelField.setText(“”+RussiaBlocksGame.DEFAULT_LEVEL);
scoreField.setText(“0”);
setPlayButtonEnabled(true);
setPauseButtonLabel(true);
tipBlockPanel.setStyle(0);
}
/*
*设置playButton是否可用
*/
public void setPlayButtonEnabled(boolean enable)
{
playButton.setEnabled(enable);
}
/*
*设置pauseButton的文本
*/
public void setPauseButtonLabel(boolean pause)
{
pauseButton.setText( pause ? “Pause” : “Rusume” );
}
/*
*设置方块的大小,改变窗体大小时调用可自动调整方块到合适的尺寸
*/
public void fanning()
{
tipBlockPanel.fanning();
}
/*
*根据level文本域的值返回当前的级别
*/
public int getLevel()
{
int level = 0;
try
{
level=Integer.parseInt(levelField.getText());
}catch(NumberFormatException e)
{
e.printStackTrace();
}
return level;
}
/*
设置level文本域的值
*/
public void setLevel(int level)
{
if(level > 0 && level <= RussiaBlocksGame.MAX_LEVEL)
levelField.setText(“” + level);
}
/*
内部类 为预显方块的显示区域
*/
private class TipBlockPanel extends JPanel
{
private Color bgColor = Color.darkGray,
blockColor = Color.lightGray;
private RussiaBox [][]boxes = new RussiaBox[RussiaBlock.ROWS][RussiaBlock.COLS];
private int boxWidth, boxHeight,style;
private boolean isTiled = false;
/*
* 构造函数
*/
public TipBlockPanel()
{
for(int i = 0; i < boxes.length; i ++)
for(int j = 0; j < boxes[i].length; j ++)
{
boxes[i][j]=new RussiaBox(false);
}
style = 0x0000;
}
/*
* 构造函数
*/
public TipBlockPanel(Color bgColor, Color blockColor)
{
this();
this.bgColor = bgColor;
this.blockColor = blockColor;
}
/*
* 设置方块的风格
*/
public void setStyle(int style)
{
this.style = style;
repaint();
}
/*
* 绘制预显方块
*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int key = 0x8000;
if(!isTiled)
fanning();
for(int i = 0; i < boxes.length; i ++)
for(int j = 0; j
{
Color color = (style & key) != 0 ? blockColor : bgColor;
g.setColor(color);
g.fill3DRect(j * boxWidth, i * boxHeight, boxWidth, boxHeight, true);
key >>=1;
}
}
/*
*设置方块的大小,改变窗体大小时调用可自动调整方块到合适的尺寸
*/
public void fanning()
{
boxWidth = getSize().width / RussiaBlock.COLS;
boxHeight = getSize().height /RussiaBlock.ROWS;
isTiled=true;
}
}
/*
*内部类 键盘键听器,响应键盘事件
*/
class ControlKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent ke)
{
if (!game.isPlaying()) return;
RussiaBlock 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;
case KeyEvent.VK_SPACE://一键到底
while(block.moveDown())
{
}
break;
default:
break;
}
}
}
}
package russia;
/www.tengxunyun.me 转载请告知/
/*
游戏中方块显示的画布类
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class GameCanvas extends JPanel
{
private RussiaBox [][]boxes;
private int rows = 20 , cols = 12;
private static GameCanvas canvas=null;
private int boxWidth, boxHeight;//默认为零需要调用fanning函数设置
private Color blockColor = Color.RED, bgColor = new Color(0,204,204);
private EtchedBorder border=new EtchedBorder(EtchedBorder.RAISED,Color.WHITE, new Color(148, 145, 140)) ;
/*
*采用单件模式,构造函数私有
*/
private GameCanvas()
{
boxes = new RussiaBoxrows;
for(int i = 0; i < boxes.length; i ++)
for(int j = 0; j
boxes[i][j] = new RussiaBox(false);
setBorder(border);
}
/*
*获得GameCanvas实例
*/
public static GameCanvas getCanvasInstance()
{
if(canvas == null)
canvas = new GameCanvas();
return canvas;
}
/*
*设置画布的背景色
*/
public void setBgColor(Color bgColor)
{
this.bgColor = bgColor;
}
/*
获得画布的背景色
*/
public Color getBgColor()
{
return bgColor;
}
/*
*设置方块的颜色
*/
public void setBlockColor(Color blockColor)
{
this.blockColor = blockColor;
}
/*
*方块的颜色
*/
public Color getBlockColor()
{
return blockColor;
}
/*
*设置画布中方块的行数
*/
public void setRows(int rows)
{
this.rows = rows;
}
/*
*得到画布中方块的行数
*/
public int getRows()
{
return rows;
}
/*
*设置画布中方块的列数
*/
public void setCols(int cols)
{
this.cols = cols;
}
/*
*得到画布中方块的列数
*/
public int getCols()
{
return cols;
}
/*
*得到row行,col列的方格
*/
public RussiaBox getBox(int row, int col)
{
if(row >= 0 && row < rows && col >= 0 && col < cols)
return boxes[row][col];
else
return null;
}
/*
*在画布中绘制方块
*/
public void paintComponent(Graphics g)
{
super.paintComponent(g);
fanning();
for(int i = 0; i < boxes.length; i ++)
for(int j = 0; j < boxes[i].length; j ++)
{
Color color = boxes[i][j].isColorBox() ? blockColor : bgColor;
g.setColor(color);
g.fill3DRect(j * boxWidth, i * boxHeight , boxWidth , boxHeight , true);
}
}
/*
*清除第row行
*/
public void removeLine(int row)
{
for(int i = row; i > 0; i –)
for(int j = 0; j < cols; j ++)
{
boxes[i][j] = (RussiaBox)boxes[i-1][j].clone();
}
}
/*
*重置 为初始时的状态
*/
public void reset()
{
for(int i = 0; i < boxes.length; i++)
for(int j = 0 ;j < boxes[i].length; j++)
{
boxes[i][j].setColor(false);
}
repaint();
}
/*
根据窗体的大小自动调整方格的大小
*/
public void fanning()
{
boxWidth = getSize().width / cols;
boxHeight = getSize().height / rows;
}
}
package russia;
/*
方块类
*/
public class RussiaBlock extends Thread
{
private int style,y,x,level;
private boolean moving,pausing;
private RussiaBox boxes[][];
private GameCanvas canvas;
public final static int ROWS = 4;
public final static int COLS = 4;
public final static int BLOCK_KIND_NUMBER = 7;
public final static int BLOCK_STATUS_NUMBER = 4;
public final static int BETWEEN_LEVELS_TIME = 50;
public final static int LEVEL_FLATNESS_GENE = 3;
/*
*方块的所有风格及其不同的状态
*/
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}, // 方块的四种状态
};
/*
*构造函数
*/
/www.tengxunyun.me 转载请告知/
public RussiaBlock(int y,int x,int level,int style)
{
this.y = y;
this.x = x;
this.level = level;
moving = true;
pausing = false;
this.style = style;
canvas = GameCanvas.getCanvasInstance();
boxes = new RussiaBoxROWS;
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 RussiaBox(isColor);
key >>= 1;
}
display();
}
/*
*线程的 run方法控制放块的下落及下落速度
*/
public void run()
{
while(moving)
{
try
{
sleep( BETWEEN_LEVELS_TIME * (RussiaBlocksGame.MAX_LEVEL – level + LEVEL_FLATNESS_GENE) );
if(!pausing)
moving = ( moveTo(y + 1,x) && moving );
}catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
/*
*暂停移动
*/
public void pauseMove()
{
pausing = true;
}
/*
*从暂停状态恢复
*/
public void resumeMove()
{
pausing = false;
}
/*
*停止移动
*/
public void stopMove()
{
moving = false;
}
/*
*向左移一格
*/
public void moveLeft()
{
moveTo(y , x – 1);
}
/*
*向右移一格
*/
public void moveRight()
{
moveTo(y , x + 1);
}
/*
*向下移一格,返回与其他几个不同,为了一键到底
*/
public boolean moveDown()
{
if(moveTo(y + 1, x))
return true;
else
return false;
}
/*
*移到newRow,newCol位置
*/
public synchronized boolean moveTo(int newRow, int newCol)
{
//erase();//必须在判断前进行擦除,否则isMoveable将产生错误行为
if(!moving || !isMoveable(newRow,newCol))
{
display();
return false;
}
y = newRow;
x = newCol;
display();
canvas.repaint();
return true;
}
/*
*判断能否移到newRow,newCol位置
*/
private 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() )
{
RussiaBox box = canvas.getBox(newRow + i, newCol + j);
if(box == null || box.isColorBox())
return false;
}
}
return true;
}
/*
*通过旋转变为下一种状态
*/
public void turnNext()
{
int newStyle = 0;
for(int i = 0; i < STYLES.length; i ++)
for(int j = 0 ;j < STYLES[i].length; j++)
{
if(style == STYLES[i][j])
{
newStyle = STYLES[i][(j + 1)%BLOCK_STATUS_NUMBER];
break;
}
}
turnTo(newStyle);
}
/*
*通过旋转变能否变为newStyle状态
*/
private synchronized boolean turnTo(int newStyle)
{
//erase();//擦除之后在判断isTurnNextAble
if(!moving || !isTurnable(newStyle))
{
display();
return false;
}
style=newStyle;
int key = 0x8000;
for(int i = 0; i < boxes.length; i ++)
for(int j = 0 ;j < boxes[i].length; j++)
{
boolean isColor = ((key & style) != 0 );
boxes[i][j].setColor(isColor);
key >>=1;
}
display();
canvas.repaint();
return true;
}
/*
*判断通过旋转能否变为下一种状态
*/
private boolean isTurnable(int newStyle)
{
erase();
int key = 0x8000;
for(int i = 0; i< boxes.length; i++)
for(int j=0; j
{
if((key & newStyle) != 0)
{
RussiaBox box = canvas.getBox(y + i, x + j);
if(box == null || box.isColorBox())
return false;
}
key >>= 1;
}
return true;
}
/*
*擦除当前方块(只是设置isColor属性,颜色并没有清除,为了判断能否移动之用)
*/
/www.tengxunyun.me 转载请告知/
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() )
{
RussiaBox box = canvas.getBox( y + i, x + j);
if(box != null)
box.setColor(false);
}
}
}
/*
*显示当前方块(其实只是设置Color属性,在调用repaint方法时才真正显示)
*/
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())
{
RussiaBox box = canvas.getBox( y + i, x + j);
if(box != null)
box.setColor( true );
}
}
}
}
package russia;
/*
主游戏类
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RussiaBlocksGame extends JFrame
{
public final static int PER_LINE_SCORE = 100;//消去一行得分
public final static int PER_LEVEL_SCORE = PER_LINE_SCORE*20;//升一级需要的分数
public final static int DEFAULT_LEVEL = 5;//默认级别
public final static int MAX_LEVEL = 10;//最高级别
private int score=0,curLevelScore = 0;//总分和本级得分
private GameCanvas canvas;
private ControlPanel controlPanel;
private RussiaBlock block;
private int style = 0;
boolean playing = false;
private JMenuBar bar;
private JMenu gameMenu,controlMenu,windowStyleMenu,informationMenu;
private JMenuItem newGameItem,setBlockColorItem,setBgColorItem,
turnHardItem,turnEasyItem,exitItem;
private JMenuItem playItem,pauseItem,resumeItem,stopItem;
private JRadioButtonMenuItem windowsRadioItem,motifRadioItem,metalRadioItem;
private JMenuItem authorItem,helpItem;
private ButtonGroup buttonGroup;
/*
构造函数
*/
public RussiaBlocksGame(String title)
{
super(title);
setSize(300,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 = GameCanvas.getCanvasInstance();
controlPanel = new ControlPanel(this);
container.add(canvas,BorderLayout.CENTER);
container.add(controlPanel,BorderLayout.EAST);
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
stopGame();
System.exit(0);
}
}
);
addComponentListener(
new ComponentAdapter()
{
public void componentResized(ComponentEvent event)
{
canvas.fanning();
}
}
);
canvas.fanning();
setVisible(true);
}
/*
判断游戏是否正在进行
*/
public boolean isPlaying()
{
return playing;
}
/*
* 开始游戏并设置按钮和菜单项的可用性
*/
public void playGame()
{
play();
controlPanel.setPlayButtonEnabled(false);
playItem.setEnabled(false);
}
/*
* 暂停游戏
*/
public void pauseGame()
{
if(block != null) block.pauseMove();
controlPanel.setPauseButtonLabel(false);
pauseItem.setEnabled(false);
resumeItem.setEnabled(true);
}
/*
* 从暂停中恢复游戏
*/
public void resumeGame()
{
if(block != null) block.resumeMove();
controlPanel.setPauseButtonLabel(true);
pauseItem.setEnabled(true);
resumeItem.setEnabled(false);
}
/*
* 停止游戏
*/
public void stopGame()
{
if(block != null) block.stopMove();
playing = false;
controlPanel.setPlayButtonEnabled(true);
controlPanel.setPauseButtonLabel(true);
playItem.setEnabled(true);
}
/*
* 得到当前级别
*/
public int getLevel()
{
return controlPanel.getLevel();
}
/*
* 设置当前级别,并更新控制面板的显示
*/
public void setLevel(int level)
{
if(level>0&&level<11)
controlPanel.setLevel(level);
}
/*
* 得到当前总分数
*/
public int getScore()
{
if(canvas != null)
return score;
return 0;
}
/*
* 得到本级得分
*/
public int getCurLevelScore()
{
if(canvas != null)
return curLevelScore;
return 0;
}
/*
* 更新等级
*/
public void levelUpdate()
{
int curLevel = getLevel();
if(curLevel < MAX_LEVEL && curLevelScore >= PER_LEVEL_SCORE)
{
setLevel(curLevel + 1);
curLevelScore -= PER_LEVEL_SCORE;
}
}
/*
* 获得当前得方块
*/
public RussiaBlock getCurBlock() {
return block;
}
/*
* 开始游戏
*/
private void play()
{
playing=true;
Thread thread = new Thread(new Game());
thread.start();
reset();
}
/*
* 重置
*/
private void reset()
{
controlPanel.reset();
canvas.reset();
score = 0;
curLevelScore = 0;
}
/*
* 宣告游戏结束
*/
private void reportGameOver()
{
JOptionPane.showMessageDialog(this,”Game over!”);
}
/*
* 创建菜单
*/
private void createMenu()
{
gameMenu = new JMenu(“Game”);
newGameItem = new JMenuItem(“New Game”);
setBlockColorItem = new JMenuItem(“Set Block Color…”);
setBgColorItem = new JMenuItem(“Set BackGround Color…”);
turnHardItem = new JMenuItem(“Turn Harder”);
turnEasyItem = new JMenuItem(“Turn Easily”);
exitItem = new JMenuItem(“Exit”);
gameMenu.add(newGameItem);
gameMenu.add(setBlockColorItem);
gameMenu.add(setBgColorItem);
gameMenu.add(turnHardItem);
gameMenu.add(turnEasyItem);
gameMenu.add(exitItem);
/*www.panmum.com 转载请告知*/
controlMenu = new JMenu(“Control”);
playItem = new JMenuItem(“Play”);
pauseItem = new JMenuItem(“Pause”);
resumeItem = new JMenuItem(“Resume”);
stopItem = new JMenuItem(“Stop”);
controlMenu.add(playItem);
controlMenu.add(pauseItem);
controlMenu.add(resumeItem);
controlMenu.add(stopItem);
windowStyleMenu = new JMenu(“WindowStyle”);
buttonGroup = new ButtonGroup();
windowsRadioItem = new JRadioButtonMenuItem(“Windows”);
motifRadioItem = new JRadioButtonMenuItem(“Motif”);
metalRadioItem = new JRadioButtonMenuItem(“Mentel”,true);
windowStyleMenu.add(windowsRadioItem);
buttonGroup.add(windowsRadioItem);
windowStyleMenu.add(motifRadioItem);
buttonGroup.add(motifRadioItem);
windowStyleMenu.add(metalRadioItem);
buttonGroup.add(metalRadioItem);
informationMenu = new JMenu(“Information”);
authorItem = new JMenuItem(“Author:Fuliang”);
helpItem = new JMenuItem(“Help”);
informationMenu.add(authorItem);
informationMenu.add(helpItem);
bar = new JMenuBar();
bar.add(gameMenu);
bar.add(controlMenu);
bar.add(windowStyleMenu);
bar.add(informationMenu);
addActionListenerToMenu();
setJMenuBar(bar);
}
/*
添加菜单响应
*/
private void addActionListenerToMenu()
{
newGameItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
stopGame();
reset();
setLevel(DEFAULT_LEVEL);
}
});
setBlockColorItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Color newBlockColor =
JColorChooser.showDialog(RussiaBlocksGame.this,
“Set color for block”, canvas.getBlockColor());
if (newBlockColor != null)
canvas.setBlockColor(newBlockColor);
}
});
setBgColorItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Color newBgColor =
JColorChooser.showDialog(RussiaBlocksGame.this,”Set color for block”,
canvas.getBgColor());
if (newBgColor != null)
canvas.setBgColor(newBgColor);
}
});
/www.oksousou.com 转载请告知/
turnHardItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int curLevel = getLevel();
if (curLevel < MAX_LEVEL) setLevel(curLevel + 1);
}
});
turnEasyItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int curLevel = getLevel();
if (curLevel > 1) setLevel(curLevel – 1);
}
});
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
playItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
playGame();
}
});
pauseItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
pauseGame();
}
});
resumeItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
resumeGame();
}
});
stopItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
stopGame();
}
});
windowsRadioItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String plaf = “com.sun.java.swing.plaf.windows.WindowsLookAndFeel”;
setWindowStyle(plaf);
canvas.fanning();
controlPanel.fanning();
}
});
motifRadioItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String plaf = “com.sun.java.swing.plaf.motif.MotifLookAndFeel”;
setWindowStyle(plaf);
canvas.fanning();
controlPanel.fanning();;
}
});
metalRadioItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String plaf = “javax.swing.plaf.metal.MetalLookAndFeel”;
setWindowStyle(plaf);
canvas.fanning();
controlPanel.fanning();
}
});
}
/*
* 设定窗口风格
*/
private void setWindowStyle(String plaf)
{
try {
UIManager.setLookAndFeel(plaf);
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
e.printStackTrace();
}
}
/www.quzhuanpan.com 转载请告知/
private class Game implements Runnable
{
/*
(non-Javadoc)
@see java.lang.Runnable#run()
游戏线程的run函数
*/
public void run()
{
int col=(int)(Math.random()*(canvas.getCols()-3));
style=RussiaBlock.STYLES(int)(Math.random()*RussiaBlock.BLOCK_KIND_NUMBER);
while (playing) {
if (block != null) { //第一次循环时,block为空
if (block.isAlive()) {
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
continue;
}
}
checkFullLine();
if (isGameOver()) { //检查游戏是否应该结束了
playItem.setEnabled(true);
pauseItem.setEnabled(true);
resumeItem.setEnabled(false);
controlPanel.setPlayButtonEnabled(true);
controlPanel.setPauseButtonLabel(true);
reportGameOver();
return;
}
block = new RussiaBlock(-1, col, getLevel(),style);
block.start();
col=(int)(Math.random()*(canvas.getCols()-3));
style=RussiaBlock.STYLES(int)(Math.random()*RussiaBlock.BLOCK_KIND_NUMBER);
controlPanel.setBlockStyle(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) {
curLevelScore += PER_LINE_SCORE;
score += PER_LINE_SCORE;
row = i–;
canvas.removeLine(row);
}
}
}
/*
判断游戏是否结束
*/
private boolean isGameOver() {
for (int i = 0; i < canvas.getCols(); i++) {
RussiaBox box = canvas.getBox(0, i);
if (box.isColorBox()) return true;
}
return false;
}
}
public static void main(String[] args)
{
new RussiaBlocksGame(“Russia Blocks Game”);
}
}
package russia;
/*
*虚拟的单个方格类,控制方格的颜色
*/
public class RussiaBox implements Cloneable
{
private boolean isColor;
public RussiaBox(boolean isColor)
{
this.isColor = isColor;
}
/*
*设置颜色
*/
public void setColor(boolean isColor)
{
this.isColor=isColor;
}
/*
*返回颜色
*/
/www.oksousou.com 转载请告知/
public boolean isColorBox()
{
return isColor;
}
/*
@see java.lang.Object#clone()
*/
public Object clone()
{
Object o = null;
try
{
o=super.clone();
}catch(CloneNotSupportedException e)
{
e.printStackTrace();
}
return o;
}
}
今天的文章俄罗斯方块英文JAVA版下载_俄罗斯方块java源代码完美版分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/26073.html