父类我们分析完了,现在来看看子类。父类里有两个抽象方法,以L型为例(其他类型同理,不仅7种类型)BlockL子类:
4.1首先以0所在的小方块为基础朝北,顺时针旋转。用数字标记的小方块相对位置不变。 (如图)
- 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位置的小方块为中转动。参考代码如下:
顺时针旋转
- 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个方向。到现在为止已经实现方块的移动、叠加、旋转。接下来就是当小方块满行消层的问题。