俄罗斯方块之四——方块子类
2010-08-23 22:32
127 查看
父类我们分析完了,现在来看看子类。父类里有两个抽象方法,以L型为例(其他类型同理,不仅7种类型)BlockL子类:
4.1首先以0所在的小方块为基础朝北,顺时针旋转。用数字标记的小方块相对位置不变。 (如图)
Code:
public override void Init(Direction dir)
{
Square s1 = new Square(), s2 = new Square(), s3 = new Square(), s4 = new Square();
if (dir == Direction.North)
{
/*
* 1
* 0
* 2 3
*/
s1.Location = this.Location;
Squares.Add(s1);
s2.Location = new Point(s1.Location.X, s1.Location.Y - Game.Side);
Squares.Add(s2);
s3.Location = new Point(s1.Location.X , s1.Location.Y+Game.Side);
Squares.Add(s3);
s4.Location = new Point(s1.Location.X + Game.Side, s1.Location.Y + Game.Side);
Squares.Add(s4);
}
else if (dir == Direction.East)
{
/*
* 2 0 1
* 3
*/
s1.Location = this.Location;
Squares.Add(s1);
s2.Location = new Point(s1.Location.X + Game.Side, s1.Location.Y);
Squares.Add(s2);
s3.Location = new Point(s1.Location.X - Game.Side, s1.Location.Y);
Squares.Add(s3);
s4.Location = new Point(s1.Location.X - Game.Side, s1.Location.Y + Game.Side);
Squares.Add(s4);
}
else if (dir == Direction.South)
{
/*
* 3 2
* 0
* 1
*/
s1.Location = this.Location;
Squares.Add(s1);
s2.Location = new Point(s1.Location.X, s1.Location.Y + Game.Side);
Squares.Add(s2);
s3.Location = new Point(s1.Location.X , s1.Location.Y - Game.Side);
Squares.Add(s3);
s4.Location = new Point(s1.Location.X - Game.Side, s1.Location.Y - Game.Side);
Squares.Add(s4);
}
else
{
/*
* 3
* 1 0 2
*/
s1.Location = this.Location;
Squares.Add(s1);
s2.Location = new Point(s1.Location.X - Game.Side, s1.Location.Y);
Squares.Add(s2);
s3.Location = new Point(s1.Location.X + Game.Side, s1.Location.Y );
Squares.Add(s3);
s4.Location = new Point(s1.Location.X + Game.Side, s1.Location.Y - Game.Side);
Squares.Add(s4);
}
}
4.2 旋转时首先记录旋转钱的位置和方向,然后判读能否旋转。不能旋转则回到原来的状态。旋转是以图示的0位置的小方块为中转动。参考代码如下:
顺时针旋转
Code:
public override void Ratate()
{
Direction oldDir = Direction;
Square s1 = Squares[0];
List<Square> oldSquares = new List<Square>();
foreach (Square s in Squares)
{
Square tempSquares = new Square();
tempSquares.Location = new Point(s.Location.X, s.Location.Y);
oldSquares.Add(tempSquares);
}
HideBlock();
if (Direction == Direction.North)
{
/*
* 1
* 0
* 2 3
*/
Direction = Direction.East;
Squares[1].Location = new Point(s1.Location.X + Game.Side, s1.Location.Y);
Squares[2].Location = new Point(s1.Location.X - Game.Side, s1.Location.Y);
Squares[3].Location = new Point(s1.Location.X - Game.Side, s1.Location.Y + Game.Side);
}
else if (Direction == Direction.East)
{
/*
* 2 0 1
* 3
*/
Direction = Direction.South;
Squares[1].Location = new Point(s1.Location.X, s1.Location.Y + Game.Side);
Squares[2].Location = new Point(s1.Location.X, s1.Location.Y - Game.Side);
Squares[3].Location = new Point(s1.Location.X - Game.Side, s1.Location.Y - Game.Side);
}
else if (Direction == Direction.South)
{
/*
* 3 2
* 0
* 1
*/
Direction = Direction.West;
Squares[1].Location = new Point(s1.Location.X - Game.Side, s1.Location.Y);
Squares[2].Location = new Point(s1.Location.X + Game.Side, s1.Location.Y);
Squares[3].Location = new Point(s1.Location.X + Game.Side, s1.Location.Y - Game.Side);
}
else
{
/*
* 3
* 1 0 2
*/
Direction = Direction.North;
Squares[1].Location = new Point(s1.Location.X, s1.Location.Y - Game.Side);
Squares[2].Location = new Point(s1.Location.X, s1.Location.Y + Game.Side);
Squares[3].Location = new Point(s1.Location.X + Game.Side, s1.Location.Y + Game.Side);
}
bool flag = true;
foreach (Square s in Squares)
flag &= Game.IsEmpty(s.Location.Y / Game.Side, s.Location.X / Game.Side);
if (!flag) {
Direction = oldDir;
for (int i = 0; i < oldSquares.Count; i++) Squares[i].Location = new Point(oldSquares[i].Location.X, oldSquares[i].Location.Y);
}
ShowBlock();
}
4.3 有些形状如正方形,旋转后都是一样,无方向可言。长棍子4个方向就只有2种状态,就只需要判断2个方向。到现在为止已经实现方块的移动、叠加、旋转。接下来就是当小方块满行消层的问题。
4.1首先以0所在的小方块为基础朝北,顺时针旋转。用数字标记的小方块相对位置不变。 (如图)
Code:
public override void Init(Direction dir)
{
Square s1 = new Square(), s2 = new Square(), s3 = new Square(), s4 = new Square();
if (dir == Direction.North)
{
/*
* 1
* 0
* 2 3
*/
s1.Location = this.Location;
Squares.Add(s1);
s2.Location = new Point(s1.Location.X, s1.Location.Y - Game.Side);
Squares.Add(s2);
s3.Location = new Point(s1.Location.X , s1.Location.Y+Game.Side);
Squares.Add(s3);
s4.Location = new Point(s1.Location.X + Game.Side, s1.Location.Y + Game.Side);
Squares.Add(s4);
}
else if (dir == Direction.East)
{
/*
* 2 0 1
* 3
*/
s1.Location = this.Location;
Squares.Add(s1);
s2.Location = new Point(s1.Location.X + Game.Side, s1.Location.Y);
Squares.Add(s2);
s3.Location = new Point(s1.Location.X - Game.Side, s1.Location.Y);
Squares.Add(s3);
s4.Location = new Point(s1.Location.X - Game.Side, s1.Location.Y + Game.Side);
Squares.Add(s4);
}
else if (dir == Direction.South)
{
/*
* 3 2
* 0
* 1
*/
s1.Location = this.Location;
Squares.Add(s1);
s2.Location = new Point(s1.Location.X, s1.Location.Y + Game.Side);
Squares.Add(s2);
s3.Location = new Point(s1.Location.X , s1.Location.Y - Game.Side);
Squares.Add(s3);
s4.Location = new Point(s1.Location.X - Game.Side, s1.Location.Y - Game.Side);
Squares.Add(s4);
}
else
{
/*
* 3
* 1 0 2
*/
s1.Location = this.Location;
Squares.Add(s1);
s2.Location = new Point(s1.Location.X - Game.Side, s1.Location.Y);
Squares.Add(s2);
s3.Location = new Point(s1.Location.X + Game.Side, s1.Location.Y );
Squares.Add(s3);
s4.Location = new Point(s1.Location.X + Game.Side, s1.Location.Y - Game.Side);
Squares.Add(s4);
}
}
4.2 旋转时首先记录旋转钱的位置和方向,然后判读能否旋转。不能旋转则回到原来的状态。旋转是以图示的0位置的小方块为中转动。参考代码如下:
顺时针旋转
Code:
public override void Ratate()
{
Direction oldDir = Direction;
Square s1 = Squares[0];
List<Square> oldSquares = new List<Square>();
foreach (Square s in Squares)
{
Square tempSquares = new Square();
tempSquares.Location = new Point(s.Location.X, s.Location.Y);
oldSquares.Add(tempSquares);
}
HideBlock();
if (Direction == Direction.North)
{
/*
* 1
* 0
* 2 3
*/
Direction = Direction.East;
Squares[1].Location = new Point(s1.Location.X + Game.Side, s1.Location.Y);
Squares[2].Location = new Point(s1.Location.X - Game.Side, s1.Location.Y);
Squares[3].Location = new Point(s1.Location.X - Game.Side, s1.Location.Y + Game.Side);
}
else if (Direction == Direction.East)
{
/*
* 2 0 1
* 3
*/
Direction = Direction.South;
Squares[1].Location = new Point(s1.Location.X, s1.Location.Y + Game.Side);
Squares[2].Location = new Point(s1.Location.X, s1.Location.Y - Game.Side);
Squares[3].Location = new Point(s1.Location.X - Game.Side, s1.Location.Y - Game.Side);
}
else if (Direction == Direction.South)
{
/*
* 3 2
* 0
* 1
*/
Direction = Direction.West;
Squares[1].Location = new Point(s1.Location.X - Game.Side, s1.Location.Y);
Squares[2].Location = new Point(s1.Location.X + Game.Side, s1.Location.Y);
Squares[3].Location = new Point(s1.Location.X + Game.Side, s1.Location.Y - Game.Side);
}
else
{
/*
* 3
* 1 0 2
*/
Direction = Direction.North;
Squares[1].Location = new Point(s1.Location.X, s1.Location.Y - Game.Side);
Squares[2].Location = new Point(s1.Location.X, s1.Location.Y + Game.Side);
Squares[3].Location = new Point(s1.Location.X + Game.Side, s1.Location.Y + Game.Side);
}
bool flag = true;
foreach (Square s in Squares)
flag &= Game.IsEmpty(s.Location.Y / Game.Side, s.Location.X / Game.Side);
if (!flag) {
Direction = oldDir;
for (int i = 0; i < oldSquares.Count; i++) Squares[i].Location = new Point(oldSquares[i].Location.X, oldSquares[i].Location.Y);
}
ShowBlock();
}
4.3 有些形状如正方形,旋转后都是一样,无方向可言。长棍子4个方向就只有2种状态,就只需要判断2个方向。到现在为止已经实现方块的移动、叠加、旋转。接下来就是当小方块满行消层的问题。
相关文章推荐
- 60行代码:Javascript 写的俄罗斯方块游戏
- (难度:40%)纯js的俄罗斯方块游戏(含源码)
- 俄罗斯方块
- shell写的俄罗斯方块游戏
- c++也能写俄罗斯方块
- 使用javascript制作俄罗斯方块游戏(1)
- 编程之美-俄罗斯方块游戏方法整理
- MFC-俄罗斯方块
- 教你看懂网上流传的60行JavaScript代码俄罗斯方块游戏
- Unity3D 自发型俄罗斯大方块。
- 俄罗斯方块游戏自动机
- OOP面向对象编程之俄罗斯方块项目实现过程
- 【转载】88行代码实现俄罗斯方块游戏(含讲解)
- 201604-2-俄罗斯方块
- J2ME游戏俄罗斯方块的设计与改良
- [java游戏开发快速入门]俄罗斯方块游戏开发开篇
- 以前我自己设计的“俄罗斯方块”,觉得挺有意思,今天贴出来
- 新手向!超详细!Java俄罗斯方块程序面向对象编程全记录
- csdn下载的java applet俄罗斯方块
- 用javascript写了个简单俄罗斯方块玩,发现IE8的速度最慢