制作单机俄罗斯游戏心得(三)
到目前为止,Piece对象也创建好了,所以“物资”齐全,该准备登上游戏的舞台———MainFrame中了,要想让物快在舞台上表演,得有”演员“在”舞台"上啊.所以,在MainFrame中提供了表示当前正在运动的Piece对象的引用currentPiece和下一个方块的引用nextPiece
private Piece currentPiece;
private Piece nextPiece;
注意了当前大方块的运动是在GamePanel中运行的,(需要提出的是,游戏界面包括两个部分,一是方块的堆积界面GamePanel,一是工具界面。整个游戏界面用GameFrame来类实现),在MainFrame中提供了gamePanel(private GamePanel gamePanel;)
在继承JPanel中的GamePanel类中提供了实现了paint方法,把当前的大方块画到界面上。但是怎么在游戏界面MainFrame里得到当前运动的大方块呢?方法就是在GamePanel里提供一个MainFrame的引用mainFrame,(MainFrame mainFrame;),
并提供一个构造器来初始化mainFrame。
public GamePanel(MainFrame mainFrame) {
this.mainFrame = mainFrame;
}
为MainFrame类提供了一个获得当前下降大方块的方法
public Piece getCurrentPiece(){
return this.currentPiece;
}
public void paint(Graphics g) {
//画背景
g.drawImage(this.background,0, 0,
this.getWidth(),this.getHeight(),null);
//画当前运动的方块
/**
* 由于方块是在游戏界面gamepanel里动的,所以在这里定义了话piece的方法
*/
Piece currentPiece = this.mainFrame.getCurrentPiece();
ImageUtil.paintPiece(g, currentPiece);
}
在这里我们将GamePanel看做是一个二维数组,每一个Square都是二维数组的一个元素,然后再paint中将这些小方块画到GamePanel中。当大方块进行下降的时候,GamePanel中就根据这个二维数组进行重画。注意二维数组中的Square元素是只含有位置而不含有图片的Square对象,为此需要在Square类里提供一个构造器
/*
* 初始化方块的位置和图片。用来作为gamePanel存放有图片的square二维数组的元素
*/
public Square(int beginX, int beginY) {
this.image = image;
this.beginX = beginX;
this.beginY = beginY;
}
当一个Piece对象完成下降时,我们需要将这个Piece里面有的带有图片的Square记录到MainFrame中。没完成一个下降就把Square加入二维数组中。下面是初始化界面的二维数组
/**
* 游戏的界面看做是个二维数组组成的平面
* 当一个piece对象完成下降是,需要将这个piece里面所有的square对象添加到mainframe中
* 也就是gamePanel中。在mainframe类里提供一个二维数组来存放一个square对象
* 每个完成下降的piece都将自己所有的square对象存放到一个二维数组中
* 注意只是初始化这个二维数组,绘制二维数组在gamePanel里绘制
*/
private void initSquares() {
int xSize = this.gamePanel.getWidth()/Piece.SQUARE_BORDER;
int ySize = this.gamePanel.getHeight()/Piece.SQUARE_BORDER;
this.squares = new Square[xSize][ySize];
for(int i = 0; i < this.squares.length; i++) {
for (int j = 0; j < this.squares[i].length; j++) {
this.squares[i][j] = new Square(Piece.SQUARE_BORDER * i,
Piece.SQUARE_BORDER * j);
}
}
}
此方法在ManFrame里调用,以便在运行程序时就可以将二维数组初始化,然后再GamePanel里画出来。在这里MainFrame类里又提供了
private Square[][] squares;和
//得到游戏gamepanel的二维数组
public Square[][] getSquares() {
// TODO Auto-generated method stub
return this.square
}
这样在GamePanel类里就可以画好在GamePanel界面了(在paint里画)代码如下
public void paint(Graphics g) {
//画背景
g.drawImage(this.background,0, 0,
this.getWidth(),this.getHeight(),null);
//画当前运动的方块
/**
* 由于方块是在游戏界面gamepanel里动的,所以在这里定义了话piece的方法
*/
Piece currentPiece = this.mainFrame.getCurrentPiece();
ImageUtil.paintPiece(g, currentPiece);
//
Square[][] squares = this.mainFrame.getSquares();
if(squares == null) return ;
//绘制用来存放二维数组的代码,
for(int i=0;i<squares.length;i++) {
for(int j=0;j<squares[i].length;j++) {
Square s = squares[i][j];
if( s!=null) {
g.drawImage(s.getImage(), s.getBeginX(), s.getBeginY(),this);
/*
* g.drawImage(null, s.getBeginX(), s.getBeginY(),this);
* 二维数组代码是用来存放带有图片的square的,可以发现把s.getImage()改成null
* 也可以,因为本身画二维数组的square就没有图片
*/
}
}
}
到此位置就完成了GamePanel的设计其完整的类如下
public class GamePanel extends JPanel {
MainFrame mainFrame;
/**
* 得到背景图卡
*/
private Image background =
ImageUtil.getImage("images/background.jpg");
public GamePanel(MainFrame mainFrame) {
this.mainFrame = mainFrame;
}
public void paint(Graphics g) {
//画背景
g.drawImage(this.background,0, 0,
this.getWidth(),this.getHeight(),null);
//画当前运动的方块
/**
* 由于方块是在游戏界面gamepanel里动的,所以在这里定义了话piece的方法
*/
Piece currentPiece = this.mainFrame.getCurrentPiece();
ImageUtil.paintPiece(g, currentPiece);
//
Square[][] squares = this.mainFrame.getSquares();
if(squares == null) return ;
//绘制用来存放二维数组的代码,
for(int i=0;i<squares.length;i++) {
for(int j=0;j<squares[i].length;j++) {
Square s = squares[i][j];
if( s!=null) {
g.drawImage(s.getImage(), s.getBeginX(), s.getBeginY(),this);
/*
* g.drawImage(null, s.getBeginX(), s.getBeginY(),this);
* 二维数组代码是用来存放带有图片的square的,可以发现把s.getImage()改成null
* 也可以,因为本身画二维数组的square就没有图片
*/
}
}
}
}
}
相关文章
- Humble Bundle联合EA游戏大促销
- unity3d游戏场景制作[通俗易懂]
- java游戏开发入门教程_JAVA制作游戏入门教程
- elevator saga游戏下载_win10玩沙滩ZERO特别卡
- 华为模拟器ensp怎么安装_华为游戏模拟器电脑版
- 卡牌类游戏游戏大厅——下篇
- 探索 PS4 游戏世界的 Linux 之旅(ps4linux)
- MacOS上的游戏乐趣:高速下载新乐趣(macos游戏下载)
- 《精灵宝可梦Go》的开发商Niantic正在制作一款变形金刚游戏
- Linux文化:玩转方块游戏(linux中文方块)
- PacVim:一个学习 vim 命令的命令行游戏
- 通过编写互动游戏学习 Bash
- #新闻拍一拍# 数据显示目前全球游戏玩家数量已超过 30 亿
- 天堂2建立MSSQL服务端,助力游戏体验创新突破(天堂2服务端mssql)
- 技术答题游戏再升级借助Redis技术(答题游戏 结合redis)
- 游戏人文件夹程序ver3.0
- 微信游戏打飞机游戏制作(java模拟微信打飞机游戏)
- PHP网页游戏学习之Xnova(ogame)源码解读(二)
- PHP网页游戏学习之Xnova(ogame)源码解读(十三)