这篇主要是提供代码,其中相对应的比较值得思考的代码加以注释。
主页面:(主页面:panel、button组成)
主程序:(1.label之间可以跨线程;2.利用GameController控制器,来显示方块、背景等;3.实现ProcessDialogKey来得到键盘控制)
using System; using System.Drawing; using System.Windows.Forms; namespace 俄罗斯方块 { public partial class MainForm : Form, Interface.IUIControl { Controller.GameController controller; public MainForm() { InitializeComponent(); Label.CheckForIllegalCrossThreadCalls = false; this.gamePanel.Size = 俄罗斯方块.Util.CommonHelper.InitEnvironment(this.gamePanel.Width, this.gamePanel.Height); } /// <summary> /// 开始游戏按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnStart_Click(object sender, EventArgs e) { String s = ""; Graphics g = Graphics.FromHwnd(this.gamePanel.Handle); controller = new Controller.GameController(g, this); controller.NewGame(); } public void GameOver() { label1.Text = "游戏结束.."; } protected override bool ProcessDialogKey(Keys keyData) { controller.ProcessDialogKey(keyData); return base.ProcessDialogKey(keyData); } } }
帮助类:
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 俄罗斯方块.Util { /// <summary> /// 游戏帮助类 本类提供程序的共享变量及经常会调用到的方法. /// </summary> class CommonHelper { #region 01 游戏面板的行数 默认18行 +static int RowCount private static int rowCount=18; public static int RowCount { get { return CommonHelper.rowCount; } set { CommonHelper.rowCount = value; } } #endregion #region 02 游戏面板的列数 默认11行 + static int ColCount private static int colCount = 11; public static int ColCount { get { return CommonHelper.colCount; } set { CommonHelper.colCount = value; } } #endregion #region 03 游戏面板小方格的边长 + static int SquLength private static int squLength; public static int SquLength { get { return CommonHelper.squLength; } } #endregion #region 04 游戏面板的线条颜色 默认黑色 +static Color GroundLineColor private static Color groundLineColor = Color.Black; public static Color GroundLineColor { get { return CommonHelper.groundLineColor; } set { CommonHelper.groundLineColor = value; } } #endregion #region 05 游戏面板的背景颜色 默认天蓝色 +static Color GroundColor private static Color groundColor = Color.SkyBlue; public static Color GroundColor { get { return CommonHelper.groundColor; } set { CommonHelper.groundColor = value; } } #endregion private static int midCol; public static int MidCol { get { return CommonHelper.midCol; } } public static object lockKey = new object(); /// <summary> /// 根据游戏面板的宽高初始化游戏环境 /// </summary> /// <param name="width">游戏面板的宽度</param> /// <param name="height">游戏面板的高度</param> public static Size InitEnvironment(int width,int height) { //01. 根据面板的宽度与高度求出小方格的边长. int w = width / colCount; int h = height / rowCount; CommonHelper.squLength = w > h ? h : w; //02. 设置中间列 CommonHelper.midCol = colCount / 2; Size size = new Size(); size.Width = squLength * colCount+1; size.Height = squLength * rowCount+1; return size; } } }
实体类
1.方格类
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 俄罗斯方块.Entities { struct Block { private Color blockColor; public Color BlockColor { get { return blockColor; } set { blockColor = value; } } private BlockType blockType; internal BlockType BlockType { get { return blockType; } set { blockType = value; } } public Block(Color blockColor,BlockType blockType) { this.blockColor = blockColor; this.blockType = blockType; } } }
2.单元格的位置
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 俄罗斯方块.Entities { /// <summary> /// 单元格的位置 /// </summary> struct CellPosition { #region 01. 行坐标 + int RowIndex private int rowIndex; public int RowIndex { get { return rowIndex; } set { rowIndex = value; } } #endregion #region 02. 列坐标 +int ColIndex private int colIndex; public int ColIndex { get { return colIndex; } set { colIndex = value; } } #endregion #region 03. 构造函数 初始化结构体对象 + CellPosition(int rowIndex, int colIndex) /// <summary> /// 构造函数 初始化结构体对象, /// </summary> /// <param name="rowIndex"></param> /// <param name="colIndex"></param> public CellPosition(int rowIndex, int colIndex) { this.rowIndex = rowIndex; this.colIndex = colIndex; } #endregion } }
3.背景类
using System.Drawing; using 俄罗斯方块.Util; namespace 俄罗斯方块.Entities { /// <summary> /// 背景类. /// </summary> class Ground { Block[,] blocks = new Block[CommonHelper.RowCount, CommonHelper.ColCount]; //初始化二维数组 public void InitBlcoks() { for (int i = 0; i < blocks.GetLength(0); i++) { for (int j = 0; j < blocks.GetLength(1); j++) { blocks[i, j] = new Block(); } } blocks[10, 2].BlockType = BlockType.Obstacle; blocks[14, 8].BlockType = BlockType.Obstacle; } /// <summary> /// 背景自己绘制自己. /// </summary> /// <param name="g">画家对象</param> public void DrawMe(Graphics g) { Pen pen = new Pen(CommonHelper.GroundLineColor); SolidBrush brush = new SolidBrush(CommonHelper.GroundColor); //外层循环控制行数 lock (CommonHelper.lockKey) { for (int rowIndex = 0; rowIndex < CommonHelper.RowCount; rowIndex++) { //内存循环控制每一行的列数 for (int colIndex = 0; colIndex < CommonHelper.ColCount; colIndex++) { g.DrawRectangle(pen, new Rectangle(colIndex * CommonHelper.SquLength, rowIndex * CommonHelper.SquLength, CommonHelper.SquLength, CommonHelper.SquLength)); if (this.blocks[rowIndex, colIndex].BlockType == BlockType.Blank) g.FillRectangle(brush, new Rectangle(colIndex * CommonHelper.SquLength + 1, rowIndex * CommonHelper.SquLength + 1, CommonHelper.SquLength - 1, CommonHelper.SquLength - 1)); else if (this.blocks[rowIndex, colIndex].BlockType == BlockType.Obstacle) g.FillRectangle(Brushes.Tomato, new Rectangle(colIndex * CommonHelper.SquLength + 1, rowIndex * CommonHelper.SquLength + 1, CommonHelper.SquLength - 1, CommonHelper.SquLength - 1)); } } } } //判断制定的形状能否移动 public bool IsMoveable(Shape shape, Direction dir) { CellPosition position; foreach (ShapeUnit unit in shape.units) { position = unit.Position; switch (dir) { case Direction.Up: break; case Direction.Down: position.RowIndex++; break; case Direction.Left: position.ColIndex--; break; case Direction.Right: position.ColIndex++; break; } if (position.RowIndex >= CommonHelper.RowCount || position.ColIndex < 0 || position.ColIndex >= CommonHelper.ColCount || blocks[position.RowIndex, position.ColIndex].BlockType == BlockType.Obstacle ) return false; } return true; } //将形状变成障碍物 public void KillAShape(Shape shape) { shape.KillMe(); foreach (ShapeUnit unit in shape.units) { blocks[unit.Position.RowIndex, unit.Position.ColIndex].BlockType = BlockType.Obstacle; } } //判断游戏是否结束. public bool IsGameOver(Shape shape) { foreach (ShapeUnit unit in shape.units) { if (unit.Position.RowIndex == 0) return true; } return false; } /// <summary> /// 判断制定的形状能否变形. /// </summary> /// <param name="shape"></param> /// <param name="shapeDir"></param> /// <returns></returns> public bool IsRotateable(Shape shape) { bool isRotateable = true; //1. 预变形. switch (shape.shapeDir) { case Direction.Up: shape.RotateRight(); shape.shapeDir = Direction.Right; break; case Direction.Down: shape.RotateLeft(); shape.shapeDir = Direction.Left; break; case Direction.Left: shape.RotateUp(); shape.shapeDir = Direction.Up; break; case Direction.Right: shape.RotateDown(); shape.shapeDir = Direction.Down; break; } //2. 判断预变形后 每个形状单元的位置 是否遇到障碍物或者越界. foreach (ShapeUnit u in shape.units) { if (u.Position.ColIndex < 0 || u.Position.ColIndex >= CommonHelper.ColCount || u.Position.RowIndex < 0 || u.Position.RowIndex >= CommonHelper.RowCount || blocks[u.Position.RowIndex, u.Position.ColIndex].BlockType == BlockType.Obstacle) { isRotateable = false; } } //3. 还原变形 switch (shape.shapeDir) { case Direction.Up: shape.RotateLeft(); shape.shapeDir = Direction.Left; break; case Direction.Down: shape.RotateRight(); shape.shapeDir = Direction.Right; break; case Direction.Left: shape.RotateDown(); shape.shapeDir = Direction.Down; break; case Direction.Right: shape.RotateUp(); shape.shapeDir = Direction.Up; break; } return isRotateable; } #region 检查并删除满行 void CheckAndDeleteFullRow() /// <summary> /// 检查并删除满行 /// </summary> public void CheckAndDeleteFullRow() { //从最后一行往前遍历 检查. for (int rowIndex = this.blocks.GetLength(0) - 1; rowIndex >= 0; rowIndex--) { bool isFullRow = true; for (int colIndex = 0; colIndex < this.blocks.GetLength(1); colIndex++) { if (blocks[rowIndex, colIndex].BlockType == BlockType.Blank) { isFullRow = false; break; } } if (isFullRow) { //删除当前循环到的行. //从当前行往上遍历 DeleteFullRow(rowIndex++); } } } #endregion private void DeleteFullRow(int deleteLineNumber) { for (int rowNum = deleteLineNumber; rowNum > 0; rowNum--) { for (int colIndex = 0; colIndex < this.blocks.GetLength(1); colIndex++) { blocks[rowNum, colIndex].BlockType = blocks[rowNum - 1, colIndex].BlockType; } } } } }
4.方格父类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Threading; using 俄罗斯方块.Util; namespace 俄罗斯方块.Entities { /// <summary> /// 形状父类. /// </summary> abstract class Shape { /// <summary> /// 形状单元数组 4个长度 因为每个形状都是由4个形状单元组成. /// </summary> public ShapeUnit[] units = new ShapeUnit[4]; /// <summary> /// 形状原点.九宫格的中心. /// </summary> protected CellPosition shapePosition = new CellPosition(); /// <summary> /// 形状当前的位置 /// </summary> public Direction shapeDir = Direction.Up; //构造函数 初始化形状单元位置. public Shape() { this.InitPosition(); } //初始化形状单元的位置 抽象方法 每个子类的形状单元都不一样 protected abstract void InitPosition(); //形状自己绘制. public void DrawMe(Graphics g) { lock (CommonHelper.lockKey) { foreach (ShapeUnit unit in units) { unit.DrawMe(g); } } } /// <summary> /// 形状擦除自己 /// </summary> /// <param name="g"></param> public void WipeMe(Graphics g) { lock (CommonHelper.lockKey) { foreach (ShapeUnit unit in this.units) { unit.WipeMe(g); } } } //形状向下移动 public void MoveDown(Graphics g) { //1. 擦出当前形状 this.WipeMe(g); //2. 移动当前形状的位置 rowIndex+1 for (int i = 0; i < this.units.Length; i++) { units[i].Position = new CellPosition() { RowIndex = units[i].Position.RowIndex + 1, ColIndex = units[i].Position.ColIndex }; } //改变形状原点的位置 this.shapePosition.RowIndex++; //3. 根据新位置重新绘制形状. this.DrawMe(g); } //*****************************形状下落线程************************begin**************************** Interface.IShapeMoveable shapeListener = null; Thread shapeMoveThread = null; bool isAlive = true; public void AddShapeListener(Interface.IShapeMoveable shapeListener) { this.shapeListener = shapeListener; shapeMoveThread = new Thread(MoveDownAuto); shapeMoveThread.IsBackground = true; shapeMoveThread.Start(); } public void MoveDownAuto() { while (isAlive) { Thread.Sleep(450); this.shapeListener.ShapeMove(this); } } public void KillMe() { this.isAlive = false; } #region 向左移动 +void MoveToLeft(Graphics g) public void MoveToLeft(Graphics g) { this.WipeMe(g); for (int i = 0; i < units.Length; i++) { units[i].Position = new CellPosition() { RowIndex = units[i].Position.RowIndex, ColIndex = units[i].Position.ColIndex - 1 }; } this.shapePosition.ColIndex--; this.DrawMe(g); } #endregion #region 向右移动+void MoveToRight(Graphics g) public void MoveToRight(Graphics g) { this.WipeMe(g); for (int i = 0; i < units.Length; i++) { units[i].Position = new CellPosition() { RowIndex = units[i].Position.RowIndex, ColIndex = units[i].Position.ColIndex + 1 }; } this.shapePosition.ColIndex++; this.DrawMe(g); } #endregion #region 向下移动+void MoveToDown(Graphics g) public void MoveToDown(Graphics g) { this.WipeMe(g); for (int i = 0; i < units.Length; i++) { units[i].Position = new CellPosition() { RowIndex = units[i].Position.RowIndex + 1, ColIndex = units[i].Position.ColIndex }; } this.shapePosition.RowIndex++; this.DrawMe(g); } #endregion //*****************************形状下落线程************************end**************************** public abstract void RotateUp(); public abstract void RotateDown(); public abstract void RotateLeft(); public abstract void RotateRight(); } }
5.方格形状属性类
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using 俄罗斯方块.Util; namespace 俄罗斯方块.Entities { /// <summary> /// 形状单元. /// </summary> struct ShapeUnit { //1. 位置 private CellPosition position; public CellPosition Position { get { return position; } set { position = value; } } //2. 颜色 private Color unitColor; public Color UnitColor { get { return unitColor; } set { unitColor = value; } } #region 构造函数. public ShapeUnit(int rowIndex, int colIndex, Color color) { CellPosition position = new CellPosition(); position.RowIndex = rowIndex; position.ColIndex = colIndex; this.position = position; this.unitColor = color; } public ShapeUnit(CellPosition position, Color color) { this.position = position; this.unitColor = color; } #endregion /// <summary> /// 形状单元自己画自己 /// </summary> /// <param name="g">画家对象</param> public void DrawMe(Graphics g) { lock (CommonHelper.lockKey) { g.FillRectangle(Brushes.White, new Rectangle(this.Position.ColIndex * CommonHelper.SquLength + 1, this.position.RowIndex * CommonHelper.SquLength + 1, CommonHelper.SquLength - 1, CommonHelper.SquLength - 1)); } } /// <summary> /// 形状单元自己擦除自己. /// </summary> /// <param name="g"></param> public void WipeMe(Graphics g) { lock (CommonHelper.lockKey) { g.FillRectangle(Brushes.SkyBlue, new Rectangle(this.Position.ColIndex * CommonHelper.SquLength + 1, this.position.RowIndex * CommonHelper.SquLength + 1, CommonHelper.SquLength - 1, CommonHelper.SquLength - 1)); } } } }
6.方格形状工厂
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 俄罗斯方块.Entities { /// <summary> /// 形状工厂-负责随机生成1个形状 /// </summary> class ShapeFactory { Random r = new Random(); public Shape GetAShape() { ShapeType shapeType = (ShapeType)r.Next(1, 7); Shape shape = null; switch (shapeType) { case ShapeType.ZLeft: shape = new ShapeZLeft(); break; case ShapeType.ZRight: shape = new ShapeZRight(); break; case ShapeType.LLeft: shape = new ShapeLLeft(); break; case ShapeType.LRight: shape = new ShapeLRight(); break; case ShapeType.Squ: shape = new ShapeSqu(); break; case ShapeType.Line: shape = new ShapeLine(); break; case ShapeType.T: shape = new ShapeT(); break; } return shape; } } }
7.7个形状子类
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using 俄罗斯方块.Util; namespace 俄罗斯方块.Entities { class ShapeLine : Shape { protected override void InitPosition() { //设置形状原点 base.shapePosition = new CellPosition(1, CommonHelper.MidCol); base.units[0].Position = new CellPosition(base.shapePosition.RowIndex - 1, base.shapePosition.ColIndex); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex + 2, base.shapePosition.ColIndex); } public override void RotateUp() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex - 1, base.shapePosition.ColIndex); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex + 2, base.shapePosition.ColIndex); } public override void RotateDown() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex - 1, base.shapePosition.ColIndex); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex + 2, base.shapePosition.ColIndex); } public override void RotateLeft() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex - 1); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex + 1); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex + 2); } public override void RotateRight() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex - 1); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex + 1); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex + 2); } } }
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using 俄罗斯方块.Util; namespace 俄罗斯方块.Entities { class ShapeLLeft:Shape { protected override void InitPosition() { base.shapePosition = new CellPosition(1,CommonHelper.MidCol); base.units[0].Position = new CellPosition(base.shapePosition.RowIndex - 1, base.shapePosition.ColIndex); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex-1); } public override void RotateUp() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex - 1, base.shapePosition.ColIndex); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex - 1); } public override void RotateDown() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex - 1, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex-1, base.shapePosition.ColIndex +1); } public override void RotateLeft() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex-1); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex+1); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex+ 1, base.shapePosition.ColIndex + 1); } public override void RotateRight() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex+1); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex+1, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex-1); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex - 1); } } }
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using 俄罗斯方块.Util; namespace 俄罗斯方块.Entities { class ShapeLRight : Shape { protected override void InitPosition() { //设置原点 base.shapePosition = new CellPosition(1, Util.CommonHelper.MidCol); //设置形状单元的位置. base.units[0].Position = new CellPosition(base.shapePosition.RowIndex - 1, base.shapePosition.ColIndex); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex+1); } public override void RotateUp() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex - 1, base.shapePosition.ColIndex); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex + 1); } //向下旋转 public override void RotateDown() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex+1, base.shapePosition.ColIndex); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex-1, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex - 1, base.shapePosition.ColIndex - 1); } public override void RotateLeft() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex-1); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex+1, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex +1, base.shapePosition.ColIndex+1); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex + 1); } //向右旋转. public override void RotateRight() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex + 1); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex-1); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex+1, base.shapePosition.ColIndex -1); } } }
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using 俄罗斯方块.Util; namespace 俄罗斯方块.Entities { class ShapeSqu : Shape { protected override void InitPosition() { base.units[0].Position = new CellPosition(0, CommonHelper.MidCol); base.units[1].Position = new CellPosition(0, CommonHelper.MidCol + 1); base.units[2].Position = new CellPosition(1, CommonHelper.MidCol); base.units[3].Position = new CellPosition(1, CommonHelper.MidCol + 1); } public override void RotateUp() { } public override void RotateDown() { } public override void RotateLeft() { } public override void RotateRight() { } } }
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using 俄罗斯方块.Util; namespace 俄罗斯方块.Entities { class ShapeT : Shape { protected override void InitPosition() { base.shapePosition = new CellPosition(1, CommonHelper.MidCol); base.units[0].Position = new CellPosition(base.shapePosition.RowIndex - 1, base.shapePosition.ColIndex); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex - 1); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex + 1); } public override void RotateUp() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex - 1, base.shapePosition.ColIndex); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex - 1); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex + 1); } public override void RotateDown() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex+1, base.shapePosition.ColIndex); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex+1); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex-1); } public override void RotateLeft() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex-1); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex+1, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex-1, base.shapePosition.ColIndex); } public override void RotateRight() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex+1); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex-1, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex+1, base.shapePosition.ColIndex); } } }
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using 俄罗斯方块.Util; namespace 俄罗斯方块.Entities { class ShapeZLeft:Shape { protected override void InitPosition() { base.shapePosition = new CellPosition(1,CommonHelper.MidCol); base.units[0].Position = new CellPosition(base.shapePosition.RowIndex-1,base.shapePosition.ColIndex-1); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex-1); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex,base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex+1,base.shapePosition.ColIndex); } public override void RotateUp() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex - 1, base.shapePosition.ColIndex - 1); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex - 1); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex); } public override void RotateDown() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex - 1, base.shapePosition.ColIndex - 1); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex - 1); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex); } public override void RotateLeft() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex - 1); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex + 1); } public override void RotateRight() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex+1, base.shapePosition.ColIndex - 1); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex+1, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex+1); } } }
using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using 俄罗斯方块.Util; namespace 俄罗斯方块.Entities { class ShapeZRight : Shape { protected override void InitPosition() { base.shapePosition = new CellPosition(1, CommonHelper.MidCol); base.units[0].Position = new CellPosition(base.shapePosition.RowIndex - 1, base.shapePosition.ColIndex); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex - 1); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex - 1); } public override void RotateUp() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex - 1, base.shapePosition.ColIndex); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex - 1); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex - 1); } public override void RotateDown() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex - 1, base.shapePosition.ColIndex); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex - 1); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex - 1); } public override void RotateLeft() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex - 1); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex + 1); } public override void RotateRight() { base.units[0].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex-1); base.units[1].Position = new CellPosition(base.shapePosition.RowIndex, base.shapePosition.ColIndex); base.units[2].Position = new CellPosition(base.shapePosition.RowIndex+1, base.shapePosition.ColIndex); base.units[3].Position = new CellPosition(base.shapePosition.RowIndex + 1, base.shapePosition.ColIndex + 1); } } }
8.枚举类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 俄罗斯方块.Entities { /// <summary> /// 背景 /// </summary> enum BlockType { /// <summary> /// 普通背景 /// </summary> Blank=0, /// <summary> /// 障碍物背景 /// </summary> Obstacle=1 } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 俄罗斯方块.Entities {
//用来判断方块的移动方向,想搞明白,画图最好 enum Direction { Up = 0, Down = 1, Left = 2, Right = 3 } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 俄罗斯方块.Entities { /// <summary> /// 形状类型枚举. /// </summary> enum ShapeType { ZLeft = 0, ZRight = 1, LLeft = 2, LRight = 3, Squ = 4, Line = 5, T = 6 } }
接口类
1.给GameController来判断能否给定
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using 俄罗斯方块.Entities; namespace 俄罗斯方块.Interface { interface IShapeMoveable { void ShapeMove(Shape shape); bool IsMoveable(Shape shape); } }
2.给主函数实现,是否结束游戏
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 俄罗斯方块.Interface { interface IUIControl { void GameOver(); } }
属性类(不明白不明白呀)
using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // 有关程序集的常规信息通过以下 // 特性集控制。更改这些特性值可修改 // 与程序集关联的信息。 [assembly: AssemblyTitle("俄罗斯方块")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("俄罗斯方块")] [assembly: AssemblyCopyright("Copyright © 2013")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] // 将 ComVisible 设置为 false 使此程序集中的类型 // 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型, // 则将该类型上的 ComVisible 特性设置为 true。 [assembly: ComVisible(false)] // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID [assembly: Guid("0abdc7fb-4094-446b-af33-9dccb1f227e6")] // 程序集的版本信息由下面四个值组成: // // 主版本 // 次版本 // 生成号 // 修订号 // // 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, // 方法是按如下所示使用“*”: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")]