俄罗斯方块之四——方块子类

父类我们分析完了,现在来看看子类。父类里有两个抽象方法,以L型为例(其他类型同理,不仅7种类型)BlockL子类:
4.1首先以0所在的小方块为基础朝北,顺时针旋转。用数字标记的小方块相对位置不变。 (如图)

 

Code:
  1. public override void Init(Direction dir)   
  2. {   
  3.     Square s1 = new Square(), s2 = new Square(), s3 = new Square(), s4 = new Square();   
  4.     if (dir == Direction.North)   
  5.     {   
  6.         /*  
  7.          *  1  
  8.          *  0  
  9.          *  2 3  
  10.          */  
  11.         s1.Location = this.Location;   
  12.         Squares.Add(s1);   
  13.         s2.Location = new Point(s1.Location.X, s1.Location.Y - Game.Side);   
  14.         Squares.Add(s2);   
  15.         s3.Location = new Point(s1.Location.X , s1.Location.Y+Game.Side);   
  16.         Squares.Add(s3);   
  17.         s4.Location = new Point(s1.Location.X + Game.Side, s1.Location.Y + Game.Side);   
  18.         Squares.Add(s4);   
  19.     }   
  20.     else if (dir == Direction.East)   
  21.     {   
  22.         /*   
  23.          *  2 0 1  
  24.          *  3  
  25.          */  
  26.         s1.Location = this.Location;   
  27.         Squares.Add(s1);   
  28.         s2.Location = new Point(s1.Location.X + Game.Side, s1.Location.Y);   
  29.         Squares.Add(s2);   
  30.         s3.Location = new Point(s1.Location.X - Game.Side, s1.Location.Y);   
  31.         Squares.Add(s3);   
  32.         s4.Location = new Point(s1.Location.X - Game.Side, s1.Location.Y + Game.Side);   
  33.         Squares.Add(s4);   
  34.     }   
  35.     else if (dir == Direction.South)   
  36.     {   
  37.         /*  
  38.          *  3 2  
  39.          *    0  
  40.          *    1  
  41.          */  
  42.         s1.Location = this.Location;   
  43.         Squares.Add(s1);   
  44.         s2.Location = new Point(s1.Location.X, s1.Location.Y + Game.Side);   
  45.         Squares.Add(s2);   
  46.         s3.Location = new Point(s1.Location.X , s1.Location.Y - Game.Side);   
  47.         Squares.Add(s3);   
  48.         s4.Location = new Point(s1.Location.X - Game.Side, s1.Location.Y - Game.Side);   
  49.         Squares.Add(s4);   
  50.     }   
  51.     else  
  52.     {   
  53.         /*    
  54.          *      3  
  55.          *  1 0 2   
  56.          */  
  57.         s1.Location = this.Location;   
  58.         Squares.Add(s1);   
  59.         s2.Location = new Point(s1.Location.X - Game.Side, s1.Location.Y);   
  60.         Squares.Add(s2);   
  61.         s3.Location = new Point(s1.Location.X + Game.Side, s1.Location.Y );   
  62.         Squares.Add(s3);   
  63.         s4.Location = new Point(s1.Location.X + Game.Side, s1.Location.Y - Game.Side);   
  64.         Squares.Add(s4);   
  65.     }   
  66. }  

4.2 旋转时首先记录旋转钱的位置和方向,然后判读能否旋转。不能旋转则回到原来的状态。旋转是以图示的0位置的小方块为中转动。参考代码如下:
顺时针旋转
 

Code:
  1. public override void Ratate()   
  2. {   
  3.     Direction oldDir = Direction;   
  4.     Square s1 = Squares[0];   
  5.     List<Square> oldSquares = new List<Square>();   
  6.     foreach (Square s in Squares)   
  7.     {   
  8.         Square tempSquares = new Square();   
  9.         tempSquares.Location = new Point(s.Location.X, s.Location.Y);   
  10.         oldSquares.Add(tempSquares);   
  11.     }   
  12.     HideBlock();   
  13.   
  14.     if (Direction == Direction.North)   
  15.     {   
  16.         /*  
  17.          *  1  
  18.          *  0  
  19.          *  2 3  
  20.          */  
  21.         Direction = Direction.East;   
  22.         Squares[1].Location = new Point(s1.Location.X + Game.Side, s1.Location.Y);   
  23.         Squares[2].Location = new Point(s1.Location.X - Game.Side, s1.Location.Y);   
  24.         Squares[3].Location = new Point(s1.Location.X - Game.Side, s1.Location.Y + Game.Side);   
  25.     }   
  26.     else if (Direction == Direction.East)   
  27.     {   
  28.         /*   
  29.          *  2 0 1  
  30.          *  3  
  31.          */  
  32.         Direction = Direction.South;   
  33.         Squares[1].Location = new Point(s1.Location.X, s1.Location.Y + Game.Side);   
  34.         Squares[2].Location = new Point(s1.Location.X, s1.Location.Y - Game.Side);   
  35.         Squares[3].Location = new Point(s1.Location.X - Game.Side, s1.Location.Y - Game.Side);   
  36.     }   
  37.     else if (Direction == Direction.South)   
  38.     {   
  39.         /*  
  40.          *  3 2  
  41.          *    0  
  42.          *    1  
  43.          */  
  44.         Direction = Direction.West;   
  45.         Squares[1].Location = new Point(s1.Location.X - Game.Side, s1.Location.Y);   
  46.         Squares[2].Location = new Point(s1.Location.X + Game.Side, s1.Location.Y);   
  47.         Squares[3].Location = new Point(s1.Location.X + Game.Side, s1.Location.Y - Game.Side);   
  48.     }   
  49.     else  
  50.     {   
  51.         /*    
  52.          *      3  
  53.          *  1 0 2   
  54.          */  
  55.         Direction = Direction.North;   
  56.         Squares[1].Location = new Point(s1.Location.X, s1.Location.Y - Game.Side);   
  57.         Squares[2].Location = new Point(s1.Location.X, s1.Location.Y + Game.Side);   
  58.         Squares[3].Location = new Point(s1.Location.X + Game.Side, s1.Location.Y + Game.Side);   
  59.     }   
  60.   
  61.     bool flag = true;   
  62.     foreach (Square s in Squares)   
  63.         flag &= Game.IsEmpty(s.Location.Y / Game.Side, s.Location.X / Game.Side);   
  64.   
  65.     if (!flag) {   
  66.         Direction = oldDir;   
  67.         for (int i = 0; i < oldSquares.Count; i++) Squares[i].Location = new Point(oldSquares[i].Location.X, oldSquares[i].Location.Y);   
  68.     }   
  69.     ShowBlock();   
  70. }  

 

4.3 有些形状如正方形,旋转后都是一样,无方向可言。长棍子4个方向就只有2种状态,就只需要判断2个方向。到现在为止已经实现方块的移动、叠加、旋转。接下来就是当小方块满行消层的问题。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值