您的位置:首页 > 其它

俄罗斯方块游戏中游戏画布和预显画布的实现

2016-04-09 12:28 856 查看
俄罗斯方块游戏中游戏画布和预显画布的实现:
1, 建立网格类; 
2, 建立画布类; 
3, 创建出游戏画布和预显画布并显示。


一、建立网格类:

/**
*作     者: Helloway
*类     名:CGrid
*类说明:网格类
*/
public class CGrid {
private Color gridColor;//设置网格颜色
private Dimension gridSize;//设置网格大小
/**
* 构造方法
* @param gridColor
* @param gridsize
*/
public CGrid(Color gridColor,Dimension gridSize){
this.gridColor=gridColor;
this.gridSize=gridSize;
}
public Color getGridColor() {
return gridColor;
}
public void setGridColor(Color gridColor) {
this.gridColor = gridColor;
}
public Dimension getGridSize() {
return gridSize;
}
public void setGridSize(Dimension gridSize) {
this.gridSize = gridSize;
}
}


二、建立游戏画布类:

/**
*作     者: Helloway
*类     名:CShowPanel
*类说明:画板类
*/
public class CShowPanel extends JPanel{
private int rows; //画板里的网格行数
private int cols; //画板里的网格列数
private Color panelColor;//画板的颜色
private CGrid[][] arrayGrids;//画板数组
Dimension size;

public CShowPanel(int rows,int cols,Color panelColor) {
this.rows=rows;
this.cols=cols;
this.panelColor=panelColor;

arrayGrids=new CGrid[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arrayGrids[i][j]=new CGrid(panelColor, null);
}
}
}

/**
* 拿到这根画笔,把网格画在画布上
*/
public void paint(Graphics g) {
size=computeSize();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
g.setColor(arrayGrids[i][j].getGridColor());
g.fill3DRect(j*size.width, i*size.height, size.width, size.height, true);
}
}
}

/**
* 计算画布中网格的大小
*/
private Dimension computeSize() {
Dimension gridSize=new Dimension();
gridSize.width=this.getSize().width/cols;
gridSize.height=this.getSize().height/rows;
return gridSize;
}

/**
* 获取画布中的某一网格
*/
public CGrid getCGridPanel(int row,int col) {
if (row<0||row>rows||col<0||col>cols) {
return null;
}
return arrayGrids[row][col];
}

/**
* 方法说明:获得画布中的网格的行数
* @return the rows
*/
public int getRows() {
return rows;
}

public void setRows(int rows) {
this.rows = rows;
}

/**
* 方法说明:获得画布中的网格的列数
* @return the rows
*/
public int getCols() {
return cols;
}

public void setCols(int cols) {
this.cols = cols;
}

public Color getPanelColor() {
return panelColor;
}

public void setPanelColor(Color panelColor) {
this.panelColor = panelColor;
}
}


三、建立预显画板(注:下面蓝底字为新增设置预显面板程序代码)

/**
*作     者: Helloway
*类     名:CControlPanel
*类说明:
*信息控制主面板
*/
public class CControlPanel extends JPanel{
private InfoPanel infoPanel;
private ConButtonPanel conButtonPanel;
private TipPanel tipPanel;

public CControlPanel(){
tipPanel=new TipPanel();
infoPanel=new InfoPanel();
conButtonPanel=new ConButtonPanel();

this.setLayout(new GridLayout(3,1,0,4));
this.add(tipPanel);
this.add(infoPanel);
this.add(conButtonPanel);

this.setBorder(CGlobal.border);
}

}

/**
*作     者: Helloway
*类     名:TipPanel
*类说明:
*方块提示面板
*/
class TipPanel extends JPanel{
private JLabel tipLabel;
private CShowPanel tipBlock;

public TipPanel() {
tipLabel=new JLabel("下一个方块");
tipBlock=new CShowPanel(CGlobal.Block_Rows,CGlobal.Block_Cols,Color.DARK_GRAY);

setLayout(new BorderLayout());
this.add(tipLabel,BorderLayout.NORTH);
this.add(tipBlock,BorderLayout.CENTER);
this.setBorder(CGlobal.border);
}
}
/**
*作     者: Helloway
*类     名:InfoPanel
*类说明:
*信息显示面板
*/
class InfoPanel extends JPanel{
private JLabel lDiff,lScore;
private JTextField tfScore,tfLevel;
public InfoPanel() {
lDiff=new JLabel("难度等级");
lScore=new JLabel("得分");
tfLevel=new JTextField("5");
tfLevel.setEditable(false);
tfScore=new JTextField("0");
tfScore.setEditable(false);
this.setLayout(new GridLayout(4,1));
this.add(lDiff);this.add(tfLevel);
this.add(lScore);this.add(tfScore);
this.setBorder(CGlobal.border);
}
}

/**
*作     者: Helloway
*类     名:ConButtonPanel
*类说明:
*按钮控制面板
*/
class ConButtonPanel extends JPanel{
private JButton btPlay,btPause,btStop;
public ConButtonPanel(){
btPlay=new JButton("开始");
btPause=new JButton("暂停");
btStop=new JButton("结束");
this.setLayout(new GridLayout(3,1));
this.add(btPlay);
this.add(btPause);
this.add(btStop);
this.setBorder(CGlobal.border);
}
}


四、显示游戏画板和预显画板(注:下面蓝底字为新增程序代码)

/**
*作     者:Helloway
*类     名:TextMyGame
*类说明:用户创建窗口开始游戏及测试其他的类的正确性
*/
public class TestMyGame {
public static void main(String[] args) {
JFrame myFrame=new JFrame("我的俄罗斯方块");
CControlPanel myControlPanel = new CControlPanel();
CMessagePanel myMsgPanel= new CMessagePanel();
CShowPanel gamePanel=new CShowPanel(20, 12, Color.PINK);

CController myController = new CController();
CMenu myMenu=new CMenu();
myMenu.addActionListener(myController);
myFrame.setJMenuBar(myMenu.createMenuStruct());

Container con=myFrame.getContentPane();
con.add(myControlPanel,BorderLayout.EAST);
con.add(myMsgPanel,BorderLayout.SOUTH);
con.add(gamePanel,BorderLayout.CENTER);

myFrame.setSize(400, 500);
CGlobal.showCenter(myFrame);
myFrame.setVisible(true);

myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


具体界面效果展示如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  俄罗斯方块 界面