俄罗斯方块的所有形状都是由若干小方块组成,一般情况下为4个小方块。所以我们创建一个小方块类Square类。有位置(Location),大小(Size),周围颜色(SurroundColor),中心颜色(CenterColor)4个属性,其中Size都一样。这里我们可以在构造方法里面直接赋值。
- #regionShowSquare显示小方块
- ///<summary>
- ///显示小方块
- ///</summary>
- ///<paramname="g"></param>
- publicvoidShowSquare(Graphicsg)
- {
- GraphicsPathpath=newGraphicsPath();
- RectanglerectSquare=newRectangle(Location,Size);
- path.AddRectangle(rectSquare);
- //填充过度颜色的画刷
- PathGradientBrushbrush=newPathGradientBrush(path);
- brush.CenterColor=CenterColor;
- brush.SurroundColors=SurroundColor;
- g.FillPath(brush,path);
- }
- publicvoidShowSquare(Graphicsg,ColorcenterColor,Color[]surroundColors)
- {
- GraphicsPathpath=newGraphicsPath();
- RectanglerectSquare=newRectangle(Location,Size);
- path.AddRectangle(rectSquare);
- //填充过度颜色的画刷
- PathGradientBrushbrush=newPathGradientBrush(path);
- brush.CenterColor=centerColor;
- brush.SurroundColors=surroundColors;
- g.FillPath(brush,path);
- }
- #endregion
1.1 Square类的属性有了,那么它有什么方法呢?
1.1.1首先小方块要在游戏界面上显示,添加一个ShowSquare(Graphics g)方法传入画板对象。
1.1.2其次小方块作为形状的一部分是跟着一起移动的,那么移动后呢?我们这里可以采用移动后重新显示,再将原来位置的小方块重新绘制成游戏界面背景颜色,就解决了。所以这里还需要重新绘制小方块的方法。
- #regionHide隐藏(就是画一个和背景颜色一样的对象替换)
- ///<summary>
- ///隐藏(就是画一个和背景颜色一样的对象替换)
- ///</summary>
- ///<paramname="g"></param>
- publicvoidHideSquares()
- {
- //创建矩形
- RectanglerectSquare=newRectangle(Location,Size);
- SolidBrushbrush=newSolidBrush(Game.BackColor);
- Game.picGraphics.FillRectangle(brush,rectSquare);
- }
- #endregion
1.2小方块是可以移动的,这里我们先不考虑,因为小方块是跟随大方块一起移动的,虽有我们只需要移动大方块的位置,小方块就一起移动。