序言
声明一下:本人是java初学者,所以这个游戏的代码大部分是由我买的<<疯狂java实战演义 >>面的, 当然也有我自己的心血在里面.当初最初自己按照书上的思路做这个游戏(没有看作者的代码),走了不少的弯路.
做完这个游戏,收获颇多,感觉有必要总结一下自己的学习心得和分享一下自己的一点儿经验.前面已经提到自己是个初学者,如果有不当之处和理解错误的地方.在所难免,真诚地希望看到此文的朋友给以指点和帮助,再此先谢过.(仅供互相交流)
整个游戏总共花费了我两个多星期的时间,"坎坷"之路很多.中间兴奋过:仅仅因为自己实现一个功能或者理解了作者设计的思路和方案.有时又充满了迷茫,郁闷甚至打击:特别是那个空指针的错误自己始终找不到改正的方法.
但不管怎么说自己是作出来了,呵呵,小有成就感(尽管90%的代码来源与书上)
闲言少叙.下面开始说我的总结吧
首先大家想必都玩过或者知道俄罗斯方块的游戏,该游戏的过程也就是几个不同形状的方块在游戏界面中实现下降, 左移, 右移,变化, 加速下降的过程(注意游戏里那个田字格大方块不能发生变化)
根据面向对象的思维方式,每个下降的物快(后面成为大方块)都应该抽象成一个对象,需要注意个是:下降的大方块是由四个小方块图片组成的,比如按个田字格的大方块,就是由四个小方块组成。这里把组成大方块的小方块图片也抽象为一个类Square.如下所示
public class Square {
/**
* 初始化带有图片的方块的位置和该方块的图片,
* @param image
* @param beginX
* @param beginY
*/
public Square(Image image, int beginX, int beginY) {
this.image = image;
this.beginX = beginX;
this.beginY = beginY;
}
//方块的图片
private Image image;
//方块开始出现的的横坐标
private int beginX;
//方块开始出现的纵坐标
private int beginY;
}
大方块对象包括多个小方块,而这些小方块的不同位置使得大方块可产生不同的变化。每一个大方块都有多种变化,下面新建一个Piece表示大方块
public class Piece {
/*
* 得到装着小方块的容器,进而通过迭代得到容器的各个小方块
*/
public List<Square> getSquares() {
return squares;
}
public void setSquares(List<Square> squares) {
this.squares = squares;
}
//该打方块所包含的小方块
private List<Square> squares;
//该大方快的变化
protected List<List<Square>> changes = new ArrayList<List<Square>>();
//当前变化的索引(在changes集合中的索引
protected int currentIndex;
//每个小块的边长
public final static int SQUARE_BORDER = 16;
}
真正的游戏中有七种大方块,所以就需要创建七种大方块,在这里把Piece类作为父类,当创建某一个大方块时就需要新建类来继承Piece。在构造器里设置各个小方块的位置。例如我们下面创建一个正方形的大方块类
public class Piece0 extends Piece {
public Piece0(Image image) {
//创建各个小方块,一个集合为一种变化
List<Square> squares = new ArrayList<Square>();
squares.add(new Square(image, 0, 0));
squares.add(new Square(image, 0, image.getHeight(null)));
squares.add(new Square(image, image.getWidth(null), 0));
squares.add(new Square(image, image.getWidth(null), image.getHeight(null)));
//加入到变化中
super.changes.add(squares);
//把唯一的一个变化添加到Piece对象中
super.setSquares(squares);
}
}
由于正方形方块是没有变化的。因此在Piece对象中changes集合中只有一个元素。表示改大方块只有一种变化。在这里需要为Piece类提供一个setSquares(List<Square> square)方法,
public void setSquares(List<Square> squares) {
this.squares = squares;
}
类似的接下来创建剩下的六种大方块
public class Piece1 extends Piece {
public Piece1(Image image) {
// 第一种变化
List<Square> squares0 = new ArrayList<Square>();
squares0.add(new Square(image, image.getWidth(null), image
.getHeight(null)));
squares0.add(new Square(image, 0, image.getHeight(null) * 2));
squares0.add(new Square(image, image.getWidth(null), image
.getHeight(null) * 2));
squares0.add(new Square(image, image.getWidth(null) * 2, image
.getHeight(null) * 2));
// 第二种变化
List<Square> squares1 = new ArrayList<Square>();
squares1.add(new Square(image, 0, 0));
squares1.add(new Square(image, 0, image.getHeight(null)));
squares1.add(new Square(image, image.getWidth(null), image
.getHeight(null)));
squares1.add(new Square(image, 0, image.getHeight(null) * 2));
// 第三种变化
List<Square> squares2 = new ArrayList<Square>();
squares2.add(new Square(image, 0, 0));
squares2.add(new Square(image, image.getWidth(null), 0));
squares2.add(new Square(image, image.getWidth(null) * 2, 0));
squares2.add(new Square(image, image.getWidth(null), image
.getHeight(null)));
// 第四种变化
List<Square> squares3 = new ArrayList<Square>();
squares3.add(new Square(image, image.getWidth(null), image
.getHeight(null)));
squares3.add(new Square(image, image.getWidth(null) * 2, 0));
squares3.add(new Square(image, image.getWidth(null) * 2, image
.getHeight(null)));
squares3.add(new Square(image, image.getWidth(null) * 2, image
.getHeight(null) * 2));
super.changes.add(squares0);
super.changes.add(squares1);
super.changes.add(squares2);
super.changes.add(squares3);
}
}
这是Picece1第一种变化这是Piece1的第二种变化这是Piece1第三种变化,这是Piece1的第四种变化
所以类似的可以创建剩下的五种大方块的的的类(图片就不贴了)
public class Piece2 extends Piece {
public Piece2(Image image) {
//第一种变化
List<Square> squares0 = new ArrayList<Square>();
squares0.add(new Square(image, 0, 0));
squares0.add(new Square(image, image.getWidth(null), 0));
squares0.add(new Square(image, image.getWidth(null), image.getHeight(null)));
squares0.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)));
//第二种变化
List<Square> squares1 = new ArrayList<Square>();
squares1.add(new Square(image, image.getWidth(null)*2, 0));
squares1.add(new Square(image, image.getWidth(null), image.getHeight(null)));
squares1.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)));
squares1.add(new Square(image, image.getWidth(null), image.getHeight(null)*2));
super.changes.add(squares0);
super.changes.add(squares1);
}
}
public class Piece3 extends Piece {
public Piece3(Image image) {
//第一种变化
List<Square> squares0 = new ArrayList<Square>();
squares0.add(new Square(image, image.getWidth(null), 0));
squares0.add(new Square(image, image.getWidth(null)*2, 0));
squares0.add(new Square(image, 0, image.getHeight(null)));
squares0.add(new Square(image, image.getWidth(null), image.getHeight(null)));
// 第二种变化
List<Square> squares1 = new ArrayList<Square>();
squares1.add(new Square(image, 0, 0));
squares1.add(new Square(image, 0, image.getHeight(null)));
squares1.add(new Square(image, image.getWidth(null), image.getHeight(null)));
squares1.add(new Square(image, image.getWidth(null), image.getHeight(null)*2));
super.changes.add(squares0);
super.changes.add(squares1);
}
}
public class Piece4 extends Piece {
public Piece4(Image image) {
//第一种变化
List<Square> squares0 = new ArrayList<Square>();
squares0.add(new Square(image, 0, image.getHeight(null)));
squares0.add(new Square(image, 0, image.getHeight(null)*2));
squares0.add(new Square(image, image.getWidth(null), image.getHeight(null)*2));
squares0.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)*2));
//第二种变化
List<Square> squares1 = new ArrayList<Square>();
squares1.add(new Square(image, 0, 0));
squares1.add(new Square(image, image.getWidth(null), 0));
squares1.add(new Square(image, 0, image.getHeight(null)));
squares1.add(new Square(image, 0, image.getHeight(null)*2));
//第三种变化
List<Square> squares2 = new ArrayList<Square>();
squares2.add(new Square(image, 0, 0));
squares2.add(new Square(image, image.getWidth(null), 0));
squares2.add(new Square(image, image.getWidth(null)*2, 0));
squares2.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)));
//第四种变化
List<Square> squares3 = new ArrayList<Square>();
squares3.add(new Square(image, image.getWidth(null)*2, 0));
squares3.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)));
squares3.add(new Square(image, image.getWidth(null), image.getHeight(null)*2));
squares3.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)*2));
super.changes.add(squares0);
super.changes.add(squares1);
super.changes.add(squares2);
super.changes.add(squares3);
}
}
public class Piece5 extends Piece {
public Piece5(Image image) {
//第一种变化
List<Square> squares0 = new ArrayList<Square>();
squares0.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)));
squares0.add(new Square(image, 0, image.getHeight(null)*2));
squares0.add(new Square(image, image.getWidth(null), image.getHeight(null)*2));
squares0.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)*2));
//第二种变化
List<Square> squares1 = new ArrayList<Square>();
squares1.add(new Square(image, image.getWidth(null), 0));
squares1.add(new Square(image, image.getWidth(null)*2, 0));
squares1.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)));
squares1.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)*2));
//第三种变化
List<Square> squares2 = new ArrayList<Square>();
squares2.add(new Square(image, 0, 0));
squares2.add(new Square(image, image.getWidth(null), 0));
squares2.add(new Square(image, image.getWidth(null)*2, 0));
squares2.add(new Square(image, 0, image.getHeight(null)));
//第四种变化
List<Square> squares3 = new ArrayList<Square>();
squares3.add(new Square(image, 0, 0));
squares3.add(new Square(image, 0, image.getHeight(null)));
squares3.add(new Square(image, 0, image.getHeight(null)*2));
squares3.add(new Square(image, image.getWidth(null), image.getHeight(null)*2));
super.changes.add(squares0);
super.changes.add(squares1);
super.changes.add(squares2);
super.changes.add(squares3);
}
}
public class Piece6 extends Piece {
public Piece6(Image image) {
//第一种变化
List<Square> squares0 = new ArrayList<Square>();
squares0.add(new Square(image, 0, image.getHeight(null)));
squares0.add(new Square(image, image.getWidth(null), image.getHeight(null)));
squares0.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)));
squares0.add(new Square(image, image.getWidth(null)*3, image.getHeight(null)));
//第二种变化
List<Square> squares1 = new ArrayList<Square>();
squares1.add(new Square(image, image.getWidth(null), 0));
squares1.add(new Square(image, image.getWidth(null), image.getHeight(null)));
squares1.add(new Square(image, image.getWidth(null), image.getHeight(null)*2));
squares1.add(new Square(image, image.getWidth(null), image.getHeight(null)*3));
super.changes.add(squares0);
super.changes.add(squares1);
}
}