RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:9:30-18:00
你可能遇到了下面的问题
关闭右侧工具栏
俄罗斯方块的接口实现
  • 作者:zhaozj
  • 发表时间:2020-12-23 11:03
  • 来源:未知

缺点很多,代码大量冗余:D,懒的修改了。主要的目的是研究一下接口。一不小心写成俄罗斯方块了。其实应该使用抽象类更好一些。

文件1:BlockInterface.cs

方块的接口。

用来描述方块的行为以及一些属性。

using System; using System.Drawing; namespace Blocks { /// /// 接口,提供方块类的一系列方法 /// public interface BlockInterface { /// /// 方块类型 /// int BlockType { get; } /// /// 坐标1 /// Point P1 { get; } /// /// 坐标2 /// Point P2 { get; } /// /// 坐标3 /// Point P3 { get; } /// /// 坐标4 /// Point P4 { get; } /// /// 绘制方块 /// void DrawSelf(); /// /// 擦除方块 /// void Refresh(); /// /// 判断是否可以移动 /// /// /// bool CanMove(int varType); /// /// 移动方块(左移、右移、下移) /// void Move(int varType); /// /// 判断是否可以旋转 /// /// /// bool CanCircle(); /// /// 旋转方块(四种不同状态) /// void Circle(); /// /// 方块下降到最低点,填充屏幕矩阵 /// void FillMatrix(); /// /// 是否游戏结束 /// /// bool isOver(); } }

文件2:Common.cs

一些系统的常量

using System; using System.Drawing; namespace Blocks { /// /// 系统公用常量类 /// public class Common { private Common() { } #region 绘制常量 /// /// 绘制屏幕面 /// public static Graphics DrawGraphics; /// /// 黑色画笔 /// public static Pen BlackPen; /// /// 白色画笔 /// public static Pen WhitePen; /// /// 红色画刷 /// public static Brush RedBrush = Brushes.Red; /// /// 白色画刷 /// public static Brush WhiteBrush = Brushes.White; /// /// 矩形宽度 /// public static int Width = 20; /// /// 矩形高度 /// public static int Height = 20; /// /// 屏幕矩阵 /// public static int[,] Matrix; /// /// 起始位置 /// public static Point StartPoint; public static BlockInterface Next; /// /// 初始化常量 /// public static void setCommon(Graphics g) { BlackPen = new Pen(Color.Black,2); WhitePen = new Pen(Color.White,2); Matrix = new int[15,20]; StartPoint = new Point(20,0); DrawGraphics = g; RandomBlock = new Random(); Next = Block.CreateBlock(); } #endregion #region 移动、状态常量 /// /// 左移 /// public const int Left = 1; /// /// 右移 /// public const int Right = 2; /// /// 下落 /// public const int Down = 3; /// /// 状态1 /// public const int StateOne = 1; /// /// 状态2 /// public const int StateTwo = 2; /// /// 状态3 /// public const int StateThree = 3; /// /// 状态4 /// public const int StateFour = 4; #endregion #region 图形常量 /// /// 直条 /// public const int BlockOne = 0; /// /// L /// public const int BlockTwo = 1; /// /// 反写的L /// public const int BlockThree = 2; /// /// 丁 /// public const int BlockFour = 3; /// /// 田 /// public const int BlockFive = 4; /// /// Z /// public const int BlockSix = 5; /// /// 反Z /// public const int BlockSeven = 6; #endregion #region 随机变量 /// /// 随机变量生成器 /// public static Random RandomBlock; #endregion #region 统计常量 /// /// 速度 /// public static int Speed = 0; /// /// 调整速度常量 /// public static int SpeedConst = 10; /// /// 得分 /// public static int Score = 0; #endregion } }