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