`
jinvasshole
  • 浏览: 770988 次
文章分类
社区版块
存档分类
最新评论

俄罗斯方块之一——Square类

 
阅读更多

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

Code:
  1. #regionShowSquare显示小方块
  2. ///<summary>
  3. ///显示小方块
  4. ///</summary>
  5. ///<paramname="g"></param>
  6. publicvoidShowSquare(Graphicsg)
  7. {
  8. GraphicsPathpath=newGraphicsPath();
  9. RectanglerectSquare=newRectangle(Location,Size);
  10. path.AddRectangle(rectSquare);
  11. //填充过度颜色的画刷
  12. PathGradientBrushbrush=newPathGradientBrush(path);
  13. brush.CenterColor=CenterColor;
  14. brush.SurroundColors=SurroundColor;
  15. g.FillPath(brush,path);
  16. }
  17. publicvoidShowSquare(Graphicsg,ColorcenterColor,Color[]surroundColors)
  18. {
  19. GraphicsPathpath=newGraphicsPath();
  20. RectanglerectSquare=newRectangle(Location,Size);
  21. path.AddRectangle(rectSquare);
  22. //填充过度颜色的画刷
  23. PathGradientBrushbrush=newPathGradientBrush(path);
  24. brush.CenterColor=centerColor;
  25. brush.SurroundColors=surroundColors;
  26. g.FillPath(brush,path);
  27. }
  28. #endregion


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

Code:
  1. #regionHide隐藏(就是画一个和背景颜色一样的对象替换)
  2. ///<summary>
  3. ///隐藏(就是画一个和背景颜色一样的对象替换)
  4. ///</summary>
  5. ///<paramname="g"></param>
  6. publicvoidHideSquares()
  7. {
  8. //创建矩形
  9. RectanglerectSquare=newRectangle(Location,Size);
  10. SolidBrushbrush=newSolidBrush(Game.BackColor);
  11. Game.picGraphics.FillRectangle(brush,rectSquare);
  12. }
  13. #endregion


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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics