Silverlight:手把手教你写俄罗斯方块(三)

  上一节中我们已经写好了构建俄罗斯方块的基类,接下来右键单击项目选择“添加->类”,创建Block_XX.cs这个文件,我们把7个形状的俄罗斯方块的类写在里面。由于在基类中我们已经实现了大部分功能,所以在子类中,我们只用设置方块的具体形状和颜色就可以了,先看“Z”,反“Z”,“L”,反“L”,“T”这5种方块。

        #region Z形方块
        /// <summary>
        /// Z形方块
        /// </summary>
        public class Block_Z : Block
        {
            public Block_Z(int x, int y)
                : base(x, y)
            {
                point[0].X = -1;
                point[0].Y = 0;
                point[1].X = 0;
                point[1].Y = 0;
                point[2].X = 0;
                point[2].Y = -1;
                point[3].X = 1;
                point[3].Y = -1;
                c = Color.FromArgb(255, 26, 255, 21);
            }
        }
        #endregion

        #region 反Z形方块
        /// <summary>
        /// 反Z形方块
        /// </summary>
        public class Block_Z2 : Block
        {
            public Block_Z2(int x, int y)
                : base(x, y)
            {
                point[0].X = -1;
                point[0].Y = -1;
                point[1].X = 0;
                point[1].Y = 0;
                point[2].X = 0;
                point[2].Y = -1;
                point[3].X = 1;
                point[3].Y = 0;
                c = Color.FromArgb(255, 252, 255, 26);
            }
        }
        #endregion

        #region L形方块
        /// <summary>
        /// L形方块
        /// </summary>
        public class Block_L : Block
        {
            public Block_L(int x, int y)
                : base(x, y)
            {
                point[0].X = -1;
                point[0].Y = 0;
                point[1].X = -1;
                point[1].Y = -1;
                point[2].X = 0;
                point[2].Y = 0;
                point[3].X = 1;
                point[3].Y = 0;
                c = Color.FromArgb(255, 15, 191, 158);
            }
        }
        #endregion

        #region 反L形方块
        /// <summary>
        /// 反L形方块
        /// </summary>
        public class Block_L2 : Block
        {
            public Block_L2(int x, int y)
                : base(x, y)
            {
                point[0].X = -1;
                point[0].Y = 0;
                point[1].X = 0;
                point[1].Y = 0;
                point[2].X = 1;
                point[2].Y = 0;
                point[3].X = 1;
                point[3].Y = -1;
                c = Color.FromArgb(255, 26, 44, 255);
            }
        }
        #endregion

        #region T形方块
        /// <summary>
        /// T形方块
        /// </summary>
        public class Block_T : Block
        {
            public Block_T(int x, int y)
                : base(x, y)
            {
                point[0].X = -1;
                point[0].Y = 0;
                point[1].X = 0;
                point[1].Y = 0;
                point[2].X = 0;
                point[2].Y = -1;
                point[3].X = 1;
                point[3].Y = 0;
                c = Color.FromArgb(255, 28, 255, 26);
            }
        }
        #endregion

代码几乎一样,在构造函数中,先是实现父类的构造函数,然后给4个小方块的坐标赋值,这个是方块出现的起始形状,当然您可以自己定义形状,只需在直角坐标系中画出这4个点的位置,取得它们的坐标,值得注意的是俄罗斯方块的移动和旋转是以坐标定义为原点(0,0)的小方块为参考对象的。

  接下来我们画“I”型方块,我们认为他的变形是有2种状态,横着和竖着,不需要旋转,所以我们重载了基类的变形方法,具体代码如下:

#region 棍子方块
    /// <summary>
    /// 棍子方块
    /// </summary>
    public class Block_I : Block
    {
        private bool isShuli;        //棍子独有属性
        public Block_I(int x, int y)
            : base(x, y)
        {
            point[0].X = -1;
            point[0].Y = 0;
            point[1].X = 0;
            point[1].Y = 0;
            point[2].X = 1;
            point[2].Y = 0;
            point[3].X = 2;
            point[3].Y = 0;
            c = Color.FromArgb(255, 255, 44, 26);
            isShuli = false;
        }

        public override void Change()
        {
            if (!isShuli)
            {
                point[0].X = 0;
                point[0].Y = 1;
                point[1].X = 0;
                point[1].Y = 0;
                point[2].X = 0;
                point[2].Y = -1;
                point[3].X = 0;
                point[3].Y = -2;
                isShuli = true;
            }
            else
            {
                point[0].X = -1;
                point[0].Y = 0;
                point[1].X = 0;
                point[1].Y = 0;
                point[2].X = 1;
                point[2].Y = 0;
                point[3].X = 2;
                point[3].Y = 0;
                isShuli = false;
            }
        }

        public override Point[] GetChangedPoint()
        {
            Point[] newPoint = new Point[4];
            if (!isShuli)
            {
                newPoint[0].X = posX + 0;
                newPoint[0].Y = posY - 1;
                newPoint[1].X = posX + 0;
                newPoint[1].Y = posY - 0;
                newPoint[2].X = posX + 0;
                newPoint[2].Y = posY - -1;
                newPoint[3].X = posX + 0;
                newPoint[3].Y = posY - -2;
            }
            else
            {
                newPoint[0].X = posX + -1;
                newPoint[0].Y = posY - 0;
                newPoint[1].X = posX + 0;
                newPoint[1].Y = posY - 0;
                newPoint[2].X = posX + 1;
                newPoint[2].Y = posY - 0;
                newPoint[3].X = posX + 2;
                newPoint[3].Y = posY - 0;
            }
            return newPoint;
        }
    }
    #endregion

前面说过,我们把方块内部的坐标转变成在画板中的坐标是通过posX+小方块x坐标,posY-小方块y坐标实现的,您可以定义任意的变形后的形状,只要确保GetChangedPoint和Change是一致的。

  接下来是“正方形”方块:

#region 正方形方块
        /// <summary>
        /// 正方形方块
        /// </summary>
        public class Block_O : Block
        {
            public Block_O(int x, int y)
                : base(x, y)
            {
                point[0].X = 0;
                point[0].Y = 0;
                point[1].X = 0;
                point[1].Y = -1;
                point[2].X = 1;
                point[2].Y = 0;
                point[3].X = 1;
                point[3].Y = -1;
                c = Color.FromArgb(255, 61, 26, 255);
            }

            public override void Change()
            {
                return;
            }

            public override Point[] GetChangedPoint()
            {
                Point[] newPoint = new Point[4];
                for (int i = 0; i < 4; i++)
                {
                    newPoint[i] = this[i];
                }
                return newPoint;
            }
        }
        #endregion

由于“正方形”没有变形,所以我们直接return,在GetChangedPoint方法中我们直接返回原始小方块的坐标。

  之所以这样处理,是因为在后面的逻辑操作中,我们并不用关心方块的具体形状,对于任意的方块,我们都可以先调用GetChangedPoint取得变形后的坐标,判断能否变形,如果能,则再调用Change方法。

  下一节中,我将介绍俄罗斯方块游戏的核心-逻辑操作类,敬请关注。

posted @ 2011-05-20 23:57  疯狂的戴夫  阅读(659)  评论(3编辑  收藏  举报