俄罗斯方块之一——Square类

俄罗斯方块的所有形状都是由若干小方块组成,一般情况下为4个小方块。所以我们创建一个小方块类Square类。有位置(Location),大小(Size),周围颜色(SurroundColor),中心颜色(CenterColor)4个属性,其中Size都一样。这里我们可以在构造方法里面直接赋值。
 

Code:
  1. #region ShowSquare 显示小方块   
  2. /// <summary>   
  3. /// 显示小方块   
  4. /// </summary>   
  5. /// <param name="g"></param>   
  6. public void ShowSquare(Graphics g)   
  7. {   
  8.    GraphicsPath path = new GraphicsPath();   
  9.    Rectangle rectSquare = new Rectangle(Location, Size);   
  10.    path.AddRectangle(rectSquare);   
  11.   
  12.    //填充过度颜色的画刷   
  13.    PathGradientBrush brush = new PathGradientBrush(path);   
  14.    brush.CenterColor = CenterColor;   
  15.    brush.SurroundColors = SurroundColor;   
  16.    g.FillPath(brush, path);   
  17. }   
  18.   
  19. public void ShowSquare(Graphics g,Color centerColor,Color[] surroundColors)   
  20. {   
  21.    GraphicsPath path = new GraphicsPath();   
  22.    Rectangle rectSquare = new Rectangle(Location, Size);   
  23.    path.AddRectangle(rectSquare);   
  24.   
  25.    //填充过度颜色的画刷   
  26.    PathGradientBrush brush = new PathGradientBrush(path);   
  27.    brush.CenterColor = centerColor;   
  28.    brush.SurroundColors = surroundColors;   
  29.    g.FillPath(brush, path);   
  30.  
  31. #endregion  


1.1 Square类的属性有了,那么它有什么方法呢?
1.1.1首先小方块要在游戏界面上显示,添加一个ShowSquare(Graphics g)方法传入画板对象。
1.1.2其次小方块作为形状的一部分是跟着一起移动的,那么移动后呢?我们这里可以采用移动后重新显示,再将原来位置的小方块重新绘制成游戏界面背景颜色,就解决了。所以这里还需要重新绘制小方块的方法。
 

Code:
  1. #region Hide 隐藏(就是画一个和背景颜色一样的对象替换)   
  2. /// <summary>   
  3. /// 隐藏(就是画一个和背景颜色一样的对象替换)   
  4. /// </summary>   
  5. /// <param name="g"></param>   
  6. public void HideSquares()   
  7. {   
  8.    //创建矩形   
  9.    Rectangle rectSquare = new Rectangle(Location, Size);   
  10.    SolidBrush brush = new SolidBrush(Game.BackColor);   
  11.    Game.picGraphics.FillRectangle(brush, rectSquare);   
  12.  
  13. #endregion  


1.2小方块是可以移动的,这里我们先不考虑,因为小方块是跟随大方块一起移动的,虽有我们只需要移动大方块的位置,小方块就一起移动。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 20
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值