C#俄罗斯方块小游戏程序设计与简单实现
C#俄罗斯方块小游戏程序设计与简单实现
相信90后或者80后都玩过这款小游戏,一直想干一票,琢磨一下,但又不太懂,于是网上搜集修改就有了以下效果!bug较多,多多包涵!
1.效果展示
2.实现方法
参考https://blog.csdn.net/qian_f/article/details/19758671 感谢博主分享,我在这里也没修改啥,有时间修复几个bug
2.1对象分析
把每个砖块当成一个对象。每个砖块都有共同的行为,就是可以左移、 右移、下移和变形。既然这是他们共同的行为(方法),那么可以定义一个虚基类Brick,然后在该基类中声明这些行为。当然,砖块在做这些行为前需要知道能不能进行这些行为,比如说到了左边界就不能左移;到了下边界就不能下移;周围空间不够大,就不能变形等等。因此该基类还需要声明一些虚函数:CanTransform() CanLeftMove() CanRightMove() CanDropMove()等。
2.2继承实现
继承定义的基类,每种砖块根据自身的形状具体实现相应函数。据说在标准的俄罗斯方块中,一共有七种形状。本练习项目中定义的方块和变形方式(绕着中心点顺时针旋转,途中颜色较深的点就是中心点)如下:
根据上图就可以知道,表示砖块最好的方法就是用二维数组了。对于砖块而言,这个二维数组就是它的变形范围,数组中的数字为0,代表砖块在该区域中无显示,为1代表有显示。在实现CanTransform() CanLeftMove() CanRightMove() CanDropMove()这四个函数时,要尤其小心,这边是最容易出错的地方。
2.3画布处理
完成砖块下面就要进行画布的处理了。可以想象一下,把画布分成多个方格,也就相当于二维数组了,然后把砖块所对应的二维数组按指定的位置放到代表画布的二维数组中。在显示的时候就可以根据值为1的方格来获取位置并进行绘图了。所以,该项目中定义了一个名为Canvas的类,核心功能是用于获取这个二维数组的值,其中包含根据砖块设置数组的值、行满(一行里所有的值都为1)之后消除、超出高度后返回失败等。
2.4绘图
真正的绘图操作。根据二维数组的值绘制显示,并响应方向键操作。
3.代码实现
Block基类
- public abstract class Block
- {
- protected int _curChangeTimes; //变化次数
- public int _needRows; //行数
- public int _needColumns; //列数
- public int[,] _range; //变化范围
- public Point _center; //中心点 相对于必要区域
- public Point _Pos; //中心点位置,相对于画布
- /// <summary>
- /// 能否变形 ,能变形的条件为在方块的变形范围内不能有其它的方块
- /// </summary>
- /// <param name="arr"></param>
- /// <param name="rows"></param>
- /// <param name="cloumns"></param>
- /// <returns></returns>
- public abstract bool CanChange(int[,] arr ,int rows,int cloumns);
- /// <summary>
- /// 变形
- /// </summary>
- public abstract void Change();
- /// <summary>
- /// 能否左移动
- /// </summary>
- /// <param name="arr"></param>
- /// <param name="rows"></param>
- /// <param name="columns"></param>
- /// <returns></returns>
- public abstract bool CanLeftMove(int[,] arr,int rows,int columns);
- /// <summary>
- /// 左移
- /// </summary>
- public void LeftMove()
- {
- _Pos.Y -= ;
- }
- /// <summary>
- /// 能否右移
- /// </summary>
- /// <param name="arr"></param>
- /// <param name="rows"></param>
- /// <param name="columns"></param>
- /// <returns></returns>
- public abstract bool CanRightMove(int[,] arr, int rows, int columns);
- /// <summary>
- /// 右侧移动
- /// </summary>
- public void RightMove()
- {
- _Pos.Y += ;
- }
- /// <summary>
- /// 能否下移
- /// </summary>
- /// <param name="arr"></param>
- /// <param name="rows"></param>
- /// <param name="columns"></param>
- /// <returns></returns>
- public abstract bool CanDownMove(int[,] arr, int rows, int columns);
- /// <summary>
- /// 下侧移动
- /// </summary>
- public void DownMove()
- {
- _Pos.X += ;
- }
- /// <summary>
- /// 随机生成一个可以通过变形得到的形状
- /// </summary>
- public void RandomShape()
- {
- Random random = new Random();
- this._curChangeTimes = random.Next();
- this.Change();
- }
- /// <summary>
- /// 设置中心点相对于画布的位置
- /// </summary>
- /// <param name="x">横向位置</param>
- /// <param name="y">纵向位置</param>
- public void SetCenterPos(int x, int y)
- {
- this._Pos = new Point(x, y);
- }
- /// <summary>
- /// 获取方块出现时中心点的Y轴坐标
- /// </summary>
- /// <returns></returns>
- public abstract int Appear();
- }
Block1类
- class Block1:Block
- {
- public Block1()
- {
- this._curChangeTimes = ;
- this._needRows = ;
- this._needColumns = ;
- _range = new int[, ]{
- {,},
- {,}
- };
- this._center = new Point(,);
- this._Pos = new Point();
- }
- public override bool CanChange(int[,] arr, int rows, int cloumns)
- {
- return false;
- }
- public override void Change()
- {
- return;
- }
- public override bool CanLeftMove(int[,] arr, int rows, int columns)
- {
- if (_Pos.X < )
- {
- if (_Pos.Y == || arr[_Pos.X + , _Pos.Y - ] == )
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- if (_Pos.Y == || arr[_Pos.X, _Pos.Y - ] == || arr[_Pos.X + , _Pos.Y - ] == )
- return false;
- else
- return true;
- }
- public override bool CanRightMove(int[,] arr, int rows, int columns)
- {
- if (_Pos.X < )
- {
- if (_Pos.Y == columns - || arr[_Pos.X + , _Pos.Y + ] == )
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- if (_Pos.Y == columns - || arr[_Pos.X, _Pos.Y + ] == || arr[_Pos.X + , _Pos.Y + ] == )
- return false;
- else
- return true;
- }
- public override bool CanDownMove(int[,] arr, int rows, int columns)
- {
- if (_Pos.X < rows - && arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y + ] == )
- return true;
- return false;
- }
- public override int Appear()
- {
- return -;
- }
- }
Block2类
- class Block2:Block
- {
- public Block2()
- {
- this._curChangeTimes = ;
- this._needRows = ;
- this._needColumns = ;
- this._range = new int[, ]{{,,,,},
- {,,,,},
- {,,,,},
- {,,,,},
- {,,,,}};
- this._center = new Point(, );
- }
- public override bool CanChange(int[,] arr, int rows, int cloumns)
- {
- bool result = false;
- if (_Pos.X - >= && _Pos.X + <= rows - && _Pos.Y - >= && _Pos.Y + <= cloumns - )
- {
- switch (_curChangeTimes)
- {
- case :
- arr[_Pos.X, _Pos.Y - ] = ;
- arr[_Pos.X, _Pos.Y] = ;
- arr[_Pos.X, _Pos.Y + ] = ;
- arr[_Pos.X, _Pos.Y + ] = ;
- break;
- case :
- arr[_Pos.X - , _Pos.Y] = ;
- arr[_Pos.X, _Pos.Y] = ;
- arr[_Pos.X + , _Pos.Y] = ;
- arr[_Pos.X + , _Pos.Y] = ;
- break;
- case :
- arr[_Pos.X, _Pos.Y - ] = ;
- arr[_Pos.X, _Pos.Y - ] = ;
- arr[_Pos.X, _Pos.Y] = ;
- arr[_Pos.X, _Pos.Y + ] = ;
- break;
- case :
- arr[_Pos.X - , _Pos.Y] = ;
- arr[_Pos.X - , _Pos.Y] = ;
- arr[_Pos.X, _Pos.Y] = ;
- arr[_Pos.X + , _Pos.Y] = ;
- break;
- default:
- break;
- }
- bool temp = true;
- for (int i = -; i < ; i++)
- {
- for (int j = -; j < ; j++)
- {
- if (arr[_Pos.X + i, _Pos.Y + j] == )
- {
- temp = false;
- goto break2;
- }
- }
- }
- break2:
- result = temp;
- switch (_curChangeTimes)
- {
- case :
- arr[_Pos.X, _Pos.Y - ] = ;
- arr[_Pos.X, _Pos.Y] = ;
- arr[_Pos.X, _Pos.Y + ] = ;
- arr[_Pos.X, _Pos.Y + ] = ;
- break;
- case :
- arr[_Pos.X - , _Pos.Y] = ;
- arr[_Pos.X, _Pos.Y] = ;
- arr[_Pos.X + , _Pos.Y] = ;
- arr[_Pos.X + , _Pos.Y] = ;
- break;
- case :
- arr[_Pos.X, _Pos.Y - ] = ;
- arr[_Pos.X, _Pos.Y - ] = ;
- arr[_Pos.X, _Pos.Y] = ;
- arr[_Pos.X, _Pos.Y + ] = ;
- break;
- case :
- arr[_Pos.X - , _Pos.Y] = ;
- arr[_Pos.X - , _Pos.Y] = ;
- arr[_Pos.X, _Pos.Y] = ;
- arr[_Pos.X + , _Pos.Y] = ;
- break;
- default:
- break;
- }
- }
- return result;
- }
- public override void Change()
- {
- switch (_curChangeTimes)
- {
- case :
- _range = new int[, ]{{,,,,},
- {,,,,},
- {,,,,},
- {,,,,},
- {,,,,}};
- _curChangeTimes = ;
- break;
- case :
- _range = new int[, ]{{,,,,},
- {,,,,},
- {,,,,},
- {,,,,},
- {,,,,}};
- _curChangeTimes = ;
- break;
- case :
- _range = new int[, ]{{,,,,},
- {,,,,},
- {,,,,},
- {,,,,},
- {,,,,}};
- _curChangeTimes = ;
- break;
- case :
- _range = new int[, ]{{,,,,},
- {,,,,},
- {,,,,},
- {,,,,},
- {,,,,}};
- _curChangeTimes = ;
- break;
- default:
- break;
- }
- }
- public override bool CanLeftMove(int[,] arr, int rows, int columns)
- {
- bool result = false;
- switch (_curChangeTimes)
- {
- case :
- if (_Pos.Y > )
- {
- if (arr[_Pos.X, _Pos.Y - ] == )
- {
- result = true;
- }
- else
- {
- result = false;
- }
- }
- break;
- case :
- if (_Pos.Y > )
- {
- bool temp = true;
- for (int i = -; i < ; i++)
- {
- if (_Pos.X + i < )
- {
- continue;
- }
- else
- {
- if (arr[_Pos.X + i, _Pos.Y - ] == )
- {
- temp = false;
- break;
- }
- }
- }
- result = temp;
- }
- break;
- case :
- if (_Pos.Y > )
- {
- if (arr[_Pos.X, _Pos.Y - ] == )
- {
- result = true;
- }
- else
- {
- result = false;
- }
- }
- break;
- case :
- if (_Pos.Y > )
- {
- bool temp = true;
- for (int i = -; i < ; i++)
- {
- if (_Pos.X + i < )
- {
- continue;
- }
- else
- {
- if (arr[_Pos.X + i, _Pos.Y - ] == )
- {
- temp = false;
- break;
- }
- }
- }
- result = temp;
- }
- break;
- default:
- break;
- }
- return result;
- }
- public override bool CanRightMove(int[,] arr, int rows, int columns)
- {
- bool result = false;
- switch (_curChangeTimes)
- {
- case :
- if (_Pos.Y < columns - )
- {
- if (arr[_Pos.X, _Pos.Y + ] == )
- {
- result = true;
- }
- else
- {
- result = false;
- }
- }
- break;
- case :
- if (_Pos.Y < columns - )
- {
- bool temp = true;
- for (int i = -; i < ; i++)
- {
- if (_Pos.X + i < )
- {
- continue;
- }
- else
- {
- if (arr[_Pos.X + i, _Pos.Y + ] == )
- {
- temp = false;
- break;
- }
- }
- }
- result = temp;
- }
- break;
- case :
- if (_Pos.Y < columns - )
- {
- if (arr[_Pos.X, _Pos.Y + ] == )
- {
- result = true;
- }
- else
- {
- result = false;
- }
- }
- break;
- case :
- if (_Pos.Y < columns - )
- {
- bool temp = true;
- for (int i = -; i < ; i++)
- {
- if (_Pos.X + i < )
- {
- continue;
- }
- else
- {
- if (arr[_Pos.X + i, _Pos.Y + ] == )
- {
- temp = false;
- break;
- }
- }
- }
- result = temp;
- }
- break;
- default:
- break;
- }
- return result;
- }
- public override bool CanDownMove(int[,] arr, int rows, int columns)
- {
- bool result = false;
- switch (_curChangeTimes)
- {
- case :
- if (_Pos.X != rows - )
- {
- if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y - ] == )
- {
- result = true;
- }
- else
- {
- result = false;
- }
- }
- break;
- case :
- if (_Pos.X != rows - )
- {
- if (arr[_Pos.X + , _Pos.Y] == )
- {
- result = true;
- }
- else
- {
- result = false;
- }
- }
- break;
- case :
- if (_Pos.X != rows - )
- {
- if (arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y + ] == )
- {
- result = true;
- }
- else
- {
- result = false;
- }
- }
- break;
- case :
- if (_Pos.X != rows - )
- {
- if (arr[_Pos.X + , _Pos.Y] == )
- {
- result = true;
- }
- else
- {
- result = false;
- }
- }
- break;
- default:
- break;
- }
- return result;
- }
- public override int Appear()
- {
- int result = ;
- switch (_curChangeTimes)
- {
- case :
- result = ;
- break;
- case :
- result = -;
- break;
- case :
- result = ;
- break;
- case :
- result = -;
- break;
- default:
- break;
- }
- return result;
- }
- }
Block3
- ///////////////////////////////////////////////////////////
- // Class : Block3.cs
- // CLRVersion : 4.0.30319.42000
- // NameSpace : BenNHTetris
- // Created on : 2018/5/31 11:41:56
- // Original author : JIYONGFEI
- // JiYF笨男孩博客 : https://www.cnblogs.com/JiYF/
- ///////////////////////////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- namespace BenNHTetris
- {
- class Block3:Block
- {
- public Block3()
- {
- _curChangeTimes = ;
- _needRows = ;
- _needColumns = ;
- _range = new int[, ]{{,,},
- {,,},
- {,,}};
- _center = new Point(, );
- }
- public override bool CanChange(int[,] arr, int rows, int cloumns)
- {
- bool result = true;
- if (_Pos.X - >= && _Pos.X + <= rows - && _Pos.Y - >= && _Pos.Y + <= cloumns - )
- {
- for (int i = -; i < ; i++)
- {
- for (int j = -; j < ; j++)
- {
- switch (_curChangeTimes)
- {
- case :
- if (i == - && j == || i == && j == - || i == && j == || i == && j == )
- {
- continue;
- }
- break;
- case :
- if (i == - && j == || i == && j == || i == && j == || i == && j == )
- {
- continue;
- }
- break;
- case :
- if (i == && j == - || i == && j == || i == && j == || i == && j == )
- {
- continue;
- }
- break;
- case :
- if (i == - && j == || i == && j == - || i == && j == || i == && j == )
- {
- continue;
- }
- break;
- default:
- break;
- }
- if (arr[_Pos.X + i, _Pos.Y + j] == )
- {
- result = false;
- }
- }
- }
- }
- return result;
- }
- public override void Change()
- {
- switch (_curChangeTimes)
- {
- case :
- _range = new int[, ]{{,,},
- {,,},
- {,,}};
- _curChangeTimes = ;
- break;
- case :
- _range = new int[, ]{{,,},
- {,,},
- {,,}};
- _curChangeTimes = ;
- break;
- case :
- _range = new int[, ]{{,,},
- {,,},
- {,,}};
- _curChangeTimes = ;
- break;
- case :
- _range = new int[, ]{{,,},
- {,,},
- {,,}};
- _curChangeTimes = ;
- break;
- default:
- break;
- }
- }
- public override bool CanLeftMove(int[,] arr, int rows, int columns)
- {
- bool result = false;
- switch (_curChangeTimes)
- {
- case :
- if (_Pos.Y - != )
- {
- if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y - ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X - , _Pos.Y - ] == && arr[_Pos.X, _Pos.Y - ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y != )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- else if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X - , _Pos.Y - ] == && arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y - != )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y - != )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- else if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X - , _Pos.Y - ] == && arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- }
- break;
- default:
- break;
- }
- return result;
- }
- public override bool CanRightMove(int[,] arr, int rows, int columns)
- {
- bool result = false;
- switch (_curChangeTimes)
- {
- case :
- if (_Pos.Y + < columns - )
- {
- if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y + ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X - , _Pos.Y + ] == && arr[_Pos.X, _Pos.Y + ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y + < columns - )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- else if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X - , _Pos.Y + ] == && arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y + < columns - )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y < columns - )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- else if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X - , _Pos.Y + ] == && arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- }
- break;
- default:
- break;
- }
- return result;
- }
- public override bool CanDownMove(int[,] arr, int rows, int columns)
- {
- bool result = false;
- switch (_curChangeTimes)
- {
- case :
- if (_Pos.X != rows - )
- {
- if (arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- break;
- case :
- if (_Pos.X + != rows - )
- {
- if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- break;
- case :
- if (_Pos.X + != rows - )
- {
- if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- break;
- case :
- if (_Pos.X + != rows - )
- {
- if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- break;
- default:
- break;
- }
- return result;
- }
- public override int Appear()
- {
- int result = ;
- switch (_curChangeTimes)
- {
- case :
- result = ;
- break;
- case :
- result = -;
- break;
- case :
- result = -;
- break;
- case :
- result = -;
- break;
- default:
- break;
- }
- return result;
- }
- }
- }
Block4
- ///////////////////////////////////////////////////////////
- // Class : Class1.cs
- // CLRVersion : 4.0.30319.42000
- // NameSpace : BenNHTetris
- // Created on : 2018/5/31 11:42:01
- // Original author : JIYONGFEI
- // JiYF笨男孩博客 : https://www.cnblogs.com/JiYF/
- ///////////////////////////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- namespace BenNHTetris
- {
- class Block4:Block
- {
- public Block4()
- {
- _curChangeTimes= ;
- _needRows = ;
- _needColumns = ;
- _range = new int[, ]{{,,},
- {,,},
- {,,}};
- _center = new Point(, );
- }
- public override bool CanChange(int[,] arr, int rows, int cloumns)
- {
- bool result = true;
- if (_Pos.X - >= && _Pos.X + <= rows - && _Pos.Y - >= && _Pos.Y + <= cloumns - )
- {
- for (int i = -; i < ; i++)
- {
- for (int j = -; j < ; j++)
- {
- switch (_curChangeTimes)
- {
- case :
- if (i == && j == - || i == && j == || i == && j == || i == && j == )
- continue;
- break;
- case :
- if (i == - && j == || i == && j == || i == && j == - || i == && j == -)
- continue;
- break;
- case :
- if (i == - && j == - || i == - && j == || i == && j == || i == && j == )
- continue;
- break;
- case :
- if (i == - && j == || i == && j == || i == && j == || i == && j == )
- continue;
- break;
- default:
- break;
- }
- if (arr[_Pos.X + i, _Pos.Y + j] == )
- {
- result = false;
- goto break1;
- }
- }
- }
- }
- break1: return result;
- }
- public override void Change()
- {
- switch (_curChangeTimes)
- {
- case :
- _range = new int[, ]{{,,},
- {,,},
- {,,}};
- _curChangeTimes = ;
- break;
- case :
- _range = new int[, ]{{,,},
- {,,},
- {,,}};
- _curChangeTimes = ;
- break;
- case :
- _range = new int[, ]{{,,},
- {,,},
- {,,}};
- _curChangeTimes = ;
- break;
- case :
- _range = new int[, ]{{,,},
- {,,},
- {,,}};
- _curChangeTimes = ;
- break;
- default:
- break;
- }
- }
- public override bool CanLeftMove(int[,] arr, int rows, int columns)
- {
- bool result = false;
- switch (_curChangeTimes)
- {
- case :
- if (_Pos.Y - > )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y - > )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- else if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X - , _Pos.Y - ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y - > )
- {
- if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y - ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X - , _Pos.Y - ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y > )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- else if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X - , _Pos.Y] == )
- result = true;
- }
- }
- break;
- default:
- break;
- }
- return result;
- }
- public override bool CanRightMove(int[,] arr, int rows, int columns)
- {
- bool result = false;
- switch (_curChangeTimes)
- {
- case :
- if (_Pos.Y + < columns - )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y < columns - )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y] == )
- result = true;
- }
- else if (_Pos.X == )
- {
- if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X, _Pos.Y + ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X - , _Pos.Y + ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y + < columns - )
- {
- if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y + ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X - , _Pos.Y + ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y + < columns - )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- else if (_Pos.X == )
- {
- if (arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X, _Pos.Y + ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X - , _Pos.Y + ] == )
- result = true;
- }
- }
- break;
- default:
- break;
- }
- return result;
- }
- public override bool CanDownMove(int[,] arr, int rows, int columns)
- {
- bool result = false;
- switch (_curChangeTimes)
- {
- case :
- if (_Pos.X + < rows - )
- {
- if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- break;
- case :
- if (_Pos.X + < rows - )
- {
- if (arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y] == )
- result = true;
- }
- break;
- case :
- if (_Pos.X < rows - )
- {
- if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X, _Pos.Y - ] == )
- result = true;
- }
- break;
- case :
- if (_Pos.X + < rows - )
- {
- if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- break;
- default:
- break;
- }
- return result;
- }
- public override int Appear()
- {
- int result = ;
- switch (_curChangeTimes)
- {
- case :
- result = -;
- break;
- case :
- result = -;
- break;
- case :
- result = ;
- break;
- case :
- result = -;
- break;
- default:
- break;
- }
- return result;
- }
- }
- }
Block5
- ///////////////////////////////////////////////////////////
- // Class : Block5.cs
- // CLRVersion : 4.0.30319.42000
- // NameSpace : BenNHTetris
- // Created on : 2018/5/31 11:42:29
- // Original author : JIYONGFEI
- // JiYF笨男孩博客 : https://www.cnblogs.com/JiYF/
- ///////////////////////////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- namespace BenNHTetris
- {
- class Block5:Block
- {
- public Block5()
- {
- _curChangeTimes = ;
- _needRows = ;
- _needColumns = ;
- _range = new int[, ]{{,,},
- {,,},
- {,,}};
- _center = new Point(, );
- }
- public override bool CanChange(int[,] arr, int rows, int cloumns)
- {
- bool result = true;
- if (_Pos.X - >= && _Pos.X + <= rows - && _Pos.Y - >= && _Pos.Y + <= cloumns - )
- {
- for (int i = -; i < ; i++)
- {
- for (int j = -; j < ; j++)
- {
- switch (_curChangeTimes)
- {
- case :
- if (i == && j == || i == && j == || i == && j == - || i == && j == )
- continue;
- break;
- case :
- if (i == - && j == - || i == && j == - || i == && j == || i == && j == )
- continue;
- break;
- case :
- if (i == - && j == || i == - && j == || i == && j == - || i == && j == )
- continue;
- break;
- case :
- if (i == - && j == || i == && j == || i == && j == || i == && j == )
- continue;
- break;
- default:
- break;
- }
- if (arr[_Pos.X + i, _Pos.Y + j] == )
- {
- result = false;
- goto break1;
- }
- }
- }
- }
- break1: return result;
- }
- public override void Change()
- {
- switch (_curChangeTimes)
- {
- case :
- _range = new int[, ]{{,,},
- {,,},
- {,,}};
- _curChangeTimes = ;
- break;
- case :
- _range = new int[, ]{{,,},
- {,,},
- {,,}};
- _curChangeTimes = ;
- break;
- case :
- _range = new int[, ]{{,,},
- {,,},
- {,,}};
- _curChangeTimes = ;
- break;
- case :
- _range = new int[, ]{{,,},
- {,,},
- {,,}};
- _curChangeTimes = ;
- break;
- default:
- break;
- }
- }
- public override bool CanLeftMove(int[,] arr, int rows, int columns)
- {
- bool result = false;
- switch (_curChangeTimes)
- {
- case :
- if (_Pos.Y - > )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y - > )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- else if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X - , _Pos.Y - ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y - > )
- {
- if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y - ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X - , _Pos.Y - ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y > )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y] == )
- result = true;
- }
- else if (_Pos.X == )
- {
- if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X, _Pos.Y - ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X - , _Pos.Y - ] == )
- result = true;
- }
- }
- break;
- default:
- break;
- }
- return result;
- }
- public override bool CanRightMove(int[,] arr, int rows, int columns)
- {
- bool result = false;
- switch (_curChangeTimes)
- {
- case :
- if (_Pos.Y + < columns - )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y < columns - )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- else if (_Pos.X == )
- {
- if (arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X, _Pos.Y + ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X - , _Pos.Y] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y + < columns - )
- {
- if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y + ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X - , _Pos.Y + ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y + < columns - )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- else if (_Pos.X == )
- {
- if (arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X, _Pos.Y + ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X - , _Pos.Y + ] == )
- result = true;
- }
- }
- break;
- default:
- break;
- }
- return result;
- }
- public override bool CanDownMove(int[,] arr, int rows, int columns)
- {
- bool result = false;
- switch (_curChangeTimes)
- {
- case :
- if (_Pos.X + < rows - )
- {
- if (arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- break;
- case :
- if (_Pos.X + < rows - )
- {
- if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- break;
- case :
- if (_Pos.X < rows - )
- {
- if (arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X, _Pos.Y + ] == )
- result = true;
- }
- break;
- case :
- if (_Pos.X + < rows - )
- {
- if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- break;
- default:
- break;
- }
- return result;
- }
- public override int Appear()
- {
- int result = ;
- switch (_curChangeTimes)
- {
- case :
- result = -;
- break;
- case :
- result = -;
- break;
- case :
- result = ;
- break;
- case :
- result = -;
- break;
- default:
- break;
- }
- return result;
- }
- }
- }
Block6
- ///////////////////////////////////////////////////////////
- // Class : Block6.cs
- // CLRVersion : 4.0.30319.42000
- // NameSpace : BenNHTetris
- // Created on : 2018/5/31 11:42:37
- // Original author : JIYONGFEI
- // JiYF笨男孩博客 : https://www.cnblogs.com/JiYF/
- ///////////////////////////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- namespace BenNHTetris
- {
- class Block6:Block
- {
- public Block6()
- {
- _curChangeTimes = ;
- _needRows = ;
- _needColumns = ;
- _range = new int[, ]{{,,,,},
- {,,,,},
- {,,,,},
- {,,,,},
- {,,,,}};
- _center = new Point(, );
- }
- public override bool CanChange(int[,] arr, int rows, int cloumns)
- {
- if (_Pos.X - >= && _Pos.X + <= rows - && _Pos.Y - >= && _Pos.Y + <= cloumns - )
- {
- bool result = true;
- switch (_curChangeTimes)
- {
- case :
- for (int i = -; i < ; i++)
- {
- for (int j = -; j < ; j++)
- {
- if (i == - && j == || i == && j == || i == && j == || i == && j == )
- continue;
- if (arr[_Pos.X + i, _Pos.Y + j] == )
- {
- result = false;
- goto break1;
- }
- }
- }
- break;
- case :
- for (int i = -; i < ; i++)
- {
- for (int j = -; j < ; j++)
- {
- if (i == && j == || i == && j == || i == && j == || i == && j == )
- continue;
- if (arr[_Pos.X + i, _Pos.Y + j] == )
- {
- result = false;
- goto break1;
- }
- }
- }
- break;
- case :
- for (int i = -; i < ; i++)
- {
- for (int j = -; j < ; j++)
- {
- if (i == && j == - || i == && j == - || i == && j == || i == && j == )
- continue;
- if (arr[_Pos.X + i, _Pos.Y + j] == )
- {
- result = false;
- goto break1;
- }
- }
- }
- break;
- case :
- for (int i = -; i < ; i++)
- {
- for (int j = -; j < ; j++)
- {
- if (i == - && j == || i == - && j == || i == && j == - || i == && j == )
- continue;
- if (arr[_Pos.X + i, _Pos.Y + j] == )
- {
- result = false;
- goto break1;
- }
- }
- }
- break;
- default:
- break;
- }
- break1: return result;
- }
- else
- {
- return false;
- }
- }
- public override void Change()
- {
- switch (_curChangeTimes)
- {
- case :
- _range = new int[, ]{{,,,,},
- {,,,,},
- {,,,,},
- {,,,,},
- {,,,,}};
- _curChangeTimes = ;
- break;
- case :
- _range = new int[, ]{{,,,,},
- {,,,,},
- {,,,,},
- {,,,,},
- {,,,,}};
- _curChangeTimes = ;
- break;
- case :
- _range = new int[, ]{{,,,,},
- {,,,,},
- {,,,,},
- {,,,,},
- {,,,,}};
- _curChangeTimes = ;
- break;
- case :
- _range = new int[, ]{{,,,,},
- {,,,,},
- {,,,,},
- {,,,,},
- {,,,,}};
- _curChangeTimes = ;
- break;
- default:
- break;
- }
- }
- public override bool CanLeftMove(int[,] arr, int rows, int columns)
- {
- bool result = false;
- switch (_curChangeTimes)
- {
- case :
- if (_Pos.Y > )
- {
- if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y - ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X - , _Pos.Y - ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y > )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- else if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X, _Pos.Y - ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y > )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y - ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X, _Pos.Y - ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y > )
- {
- if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y - ] == )
- result = true;
- }
- else if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X - , _Pos.Y - ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X - , _Pos.Y - ] == && arr[_Pos.X - , _Pos.Y - ] == )
- result = true;
- }
- }
- break;
- default:
- break;
- }
- return result;
- }
- public override bool CanRightMove(int[,] arr, int rows, int columns)
- {
- bool result = false;
- switch (_curChangeTimes)
- {
- case :
- if (_Pos.Y + < columns - )
- {
- if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y + ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X - , _Pos.Y + ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y + < columns - )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- else if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X, _Pos.Y + ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y < columns - )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X, _Pos.Y + ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.Y < columns - )
- {
- if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y + ] == )
- result = true;
- }
- else if (_Pos.X == )
- {
- if (arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X - , _Pos.Y + ] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X - , _Pos.Y + ] == && arr[_Pos.X - , _Pos.Y + ] == )
- result = true;
- }
- }
- break;
- default:
- break;
- }
- return result;
- }
- public override bool CanDownMove(int[,] arr, int rows, int columns)
- {
- bool result = false;
- switch (_curChangeTimes)
- {
- case :
- if (_Pos.X < rows - )
- {
- if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- break;
- case :
- if (_Pos.X + < rows - )
- {
- if (_Pos.X == -)
- {
- if (arr[_Pos.X + , _Pos.Y] == )
- result = true;
- }
- else
- {
- if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y + ] == )
- result = true;
- }
- }
- break;
- case :
- if (_Pos.X + < rows - )
- {
- if (arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y] == )
- result = true;
- }
- break;
- case :
- if (_Pos.X < rows - )
- {
- if (arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y] == )
- result = true;
- }
- break;
- default:
- break;
- }
- return result;
- }
- public override int Appear()
- {
- int result = ;
- switch (_curChangeTimes)
- {
- case :
- result = ;
- break;
- case :
- result = -;
- break;
- case :
- result = -;
- break;
- case :
- result = -;
- break;
- default:
- break;
- }
- return result;
- }
- }
- }
Block7
///////////////////////////////////////////////////////////
// Class : Block7.cs
// CLRVersion : 4.0.30319.42000
// NameSpace : BenNHTetris
// Created on : 2018/5/31 11:42:44
// Original author : JIYONGFEI
// JiYF笨男孩博客 : https://www.cnblogs.com/JiYF/
///////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text; namespace BenNHTetris
{
class Block7:Block
{
public Block7()
{
_curChangeTimes = ;
_needRows = ;
_needColumns = ;
_range = new int[, ]{{,,,,},
{,,,,},
{,,,,},
{,,,,},
{,,,,}};
_center = new Point(, );
}
public override bool CanChange(int[,] arr, int rows, int cloumns)
{
if (_Pos.X - >= && _Pos.X + <= rows - && _Pos.Y - >= && _Pos.Y + <= cloumns - )
{
bool result = true;
switch (_curChangeTimes)
{
case :
for (int i = -; i < ; i++)
{
for (int j = -; j < ; j++)
{
if (i == - && j == || i == && j == - || i == && j == - || i == && j == )
continue;
if (arr[_Pos.X + i, _Pos.Y + j] == )
{
result = false;
goto break1;
}
}
}
break;
case :
for (int i = -; i < ; i++)
{
for (int j = -; j < ; j++)
{
if (i == - && j == || i == - && j == || i == && j == || i == && j == )
continue;
if (arr[_Pos.X + i, _Pos.Y + j] == )
{
result = false;
goto break1;
}
}
}
break;
case :
for (int i = -; i < ; i++)
{
for (int j = -; j < ; j++)
{
if (i == && j == || i == && j == || i == && j == || i == && j == )
continue;
if (arr[_Pos.X + i, _Pos.Y + j] == )
{
result = false;
goto break1;
}
}
}
break;
case :
for (int i = -; i < ; i++)
{
for (int j = -; j < ; j++)
{
if (i == && j == - || i == && j == || i == && j == || i == && j == )
continue;
if (arr[_Pos.X + i, _Pos.Y + j] == )
{
result = false;
goto break1;
}
}
}
break;
default:
break;
}
break1: return result;
}
else
{
return false;
}
} public override void Change()
{
switch (_curChangeTimes)
{
case :
_range = new int[, ]{{,,,,},
{,,,,},
{,,,,},
{,,,,},
{,,,,}};
_curChangeTimes = ;
break;
case :
_range = new int[, ]{{,,,,},
{,,,,},
{,,,,},
{,,,,},
{,,,,}};
_curChangeTimes = ;
break;
case :
_range = new int[, ]{{,,,,},
{,,,,},
{,,,,},
{,,,,},
{,,,,}};
_curChangeTimes = ;
break;
case :
_range = new int[, ]{{,,,,},
{,,,,},
{,,,,},
{,,,,},
{,,,,}};
_curChangeTimes = ;
break;
default:
break;
}
} public override bool CanLeftMove(int[,] arr, int rows, int columns)
{
bool result = false;
switch (_curChangeTimes)
{
case :
if (_Pos.Y - > )
{
if (_Pos.X == )
{
if (arr[_Pos.X, _Pos.Y - ] == )
result = true;
}
else
{
if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X - , _Pos.Y - ] == )
result = true;
}
}
break;
case :
if (_Pos.Y > )
{
if (_Pos.X == )
{
if (arr[_Pos.X, _Pos.Y - ] == )
result = true;
}
else if (_Pos.X == )
{
if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X - , _Pos.Y - ] == )
result = true;
}
else
{
if (arr[_Pos.X, _Pos.Y - ] == && arr[_Pos.X - , _Pos.Y - ] == && arr[_Pos.X - , _Pos.Y - ] == )
result = true;
}
}
break;
case :
if (_Pos.Y > )
{
if (_Pos.X == -)
{
if (arr[_Pos.X + , _Pos.Y - ] == )
result = true;
}
else
{
if (arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X, _Pos.Y - ] == )
result = true;
}
}
break;
case :
if (_Pos.Y > )
{
if (_Pos.X == -)
{
if (arr[_Pos.X + , _Pos.Y - ] == )
result = true;
}
else if (_Pos.X == -)
{
if (arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == )
result = true;
}
else
{
if (arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X, _Pos.Y - ] == )
result = true;
}
}
break;
default:
break;
}
return result;
} public override bool CanRightMove(int[,] arr, int rows, int columns)
{
bool result = false;
switch (_curChangeTimes)
{
case :
if (_Pos.Y < columns - )
{
if (_Pos.X == )
{
if (arr[_Pos.X, _Pos.Y + ] == )
result = true;
}
else
{
if (arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X - , _Pos.Y + ] == )
result = true;
}
}
break;
case :
if (_Pos.Y + < columns - )
{
if (_Pos.X == )
{
if (arr[_Pos.X, _Pos.Y + ] == )
result = true;
}
else if (_Pos.X == )
{
if (arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X - , _Pos.Y + ] == )
result = true;
}
else
{
if (arr[_Pos.X, _Pos.Y + ] == && arr[_Pos.X - , _Pos.Y + ] == && arr[_Pos.X - , _Pos.Y + ] == )
result = true;
}
}
break;
case :
if (_Pos.Y + < columns - )
{
if (_Pos.X == -)
{
if (arr[_Pos.X + , _Pos.Y + ] == )
result = true;
}
else
{
if (arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X, _Pos.Y + ] == )
result = true;
}
}
break;
case :
if (_Pos.Y < columns - )
{
if (_Pos.X == -)
{
if (arr[_Pos.X + , _Pos.Y + ] == )
result = true;
}
else if (_Pos.X == -)
{
if (arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y + ] == )
result = true;
}
else
{
if (arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X, _Pos.Y + ] == )
result = true;
}
}
break;
default:
break;
}
return result;
} public override bool CanDownMove(int[,] arr, int rows, int columns)
{
bool result = false;
switch (_curChangeTimes)
{
case :
if (_Pos.X < rows - )
{
if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y - ] == )
result = true;
}
break;
case :
if (_Pos.X < rows - )
{
if (arr[_Pos.X + , _Pos.Y] == && arr[_Pos.X + , _Pos.Y + ] == )
result = true;
}
break;
case :
if (_Pos.X + < rows - )
{
if (arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y + ] == && arr[_Pos.X + , _Pos.Y] == )
result = true;
}
break;
case :
if (_Pos.X + < rows - )
{
if (_Pos.X == -)
{
if (arr[_Pos.X + , _Pos.Y] == )
result = true;
}
else
{
if (arr[_Pos.X + , _Pos.Y - ] == && arr[_Pos.X + , _Pos.Y] == )
result = true;
}
}
break;
default:
break;
}
return result;
} public override int Appear()
{
int result = ;
switch (_curChangeTimes)
{
case :
result = ;
break;
case :
result = ;
break;
case :
result = -;
break;
case :
result = -;
break;
default:
break;
}
return result;
}
}
}
Blocks
///////////////////////////////////////////////////////////
// Class : Blocks.cs
// CLRVersion : 4.0.30319.42000
// NameSpace : BenNHTetris
// Created on : 2018/5/31 14:32:10
// Original author : JIYONGFEI
// JiYF笨男孩博客 : https://www.cnblogs.com/JiYF/
///////////////////////////////////////////////////////////
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace BenNHTetris
{
class Blocks
{
public static ArrayList BlockList = new ArrayList();
//随机获取一个砖块
public static Block GetBlock()
{
Random random = new Random();
int index = random.Next();
Block block;
switch (index)
{
case :
block = new Block1();
break;
case :
block = new Block2();
break;
case :
block = new Block3();
break;
case :
block = new Block4();
break;
case :
block = new Block5();
break;
case :
block = new Block6();
break;
case :
block = new Block7();
break;
default:
block = new Block1();
break;
}
return block;
}
}
}
Canvas
///////////////////////////////////////////////////////////
// Class : Canvas.cs
// CLRVersion : 4.0.30319.42000
// NameSpace : BenNHTetris
// Created on : 2018/5/31 11:43:28
// Original author : JIYONGFEI
// JiYF笨男孩博客 : https://www.cnblogs.com/JiYF/
///////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text; namespace BenNHTetris
{
class Canvas
{
public int m_rows; //行数
public int m_columns; //列数
public int[,] m_arr; //画布二维数组
public int m_score; //分数
private Block m_curBlock = null; //当前砖块
private Block m_nextBlock = null; //下一个砖块
private int m_height; //当前高度 /// <summary>
/// 构造画布
/// </summary>
public Canvas()
{
m_rows = ; //初始20
m_columns = ; //初始20
m_arr = new int[m_rows, m_columns];
for (int i = ; i < m_rows; i++)
{
for (int j = ; j < m_columns; j++)
{
m_arr[i, j] = ;
}
}
m_score = ;
m_height = ;
} //定时器 砖块定时下降或无法下降时生成新的砖块
public bool Run()
{
//判断是否为空
lock (m_arr)
{
if (m_curBlock == null && m_nextBlock == null)
{
m_curBlock = Blocks.GetBlock();
m_nextBlock = Blocks.GetBlock();
m_nextBlock.RandomShape();
m_curBlock.SetCenterPos(m_curBlock.Appear(), m_columns / - );
SetArrayValue();
}
else if (m_curBlock == null)
{
m_curBlock = m_nextBlock;
m_nextBlock = Blocks.GetBlock();
m_nextBlock.RandomShape();
m_curBlock.SetCenterPos(m_curBlock.Appear(), m_columns / - );
SetArrayValue();
}
else
{
if (m_curBlock.CanDownMove(m_arr, m_rows, m_columns) == true)
{
ClearCurBrick();
m_curBlock.DownMove();
SetArrayValue();
}
else
{
m_curBlock = null;
SetCurHeight();
ClearRow();
}
}
if (m_score >= )
return false;
if (m_height < m_rows)
return true;
else
return false;
}
} //根据清除当前砖块在m_arr中的值
private void ClearCurBrick()
{
int centerX = m_curBlock._center.X;
int centerY = m_curBlock._center.Y;
for (int i = ; i < m_curBlock._needRows; i++)
{
for (int j = ; j < m_curBlock._needColumns; j++)
{
int realX = m_curBlock._Pos.X - (centerX - i);
int realY = m_curBlock._Pos.Y - (centerY - j);
if (realX < || realX >= m_columns || realY < || realY >= m_rows)
{
continue;
}
else
{
if (m_curBlock._range[i, j] == )
{
continue;
}
else
{
m_arr[realX, realY] = ;
}
}
}
}
} //根据当前砖块设置m_arr的值
public void SetArrayValue()
{
int centerX = m_curBlock._center.X;
int centerY = m_curBlock._center.Y;
for (int i = ; i < m_curBlock._needRows; i++)
{
for (int j = ; j < m_curBlock._needColumns; j++)
{
int realX = m_curBlock._Pos.X - (centerX - i);
int realY = m_curBlock._Pos.Y - (centerY - j);
if (realX < || realX >= m_columns || realY < || realY >= m_rows)
{
continue;
}
else
{
if (m_curBlock._range[i, j] == )
{
continue;
}
else
{
m_arr[realX, realY] = ;
}
}
}
}
} //判断当前有没有填满的行,有则消除、加分
private void ClearRow()
{
int clearrows = ;
for (int i = m_rows - m_height; i < m_rows; i++)
{
bool isfull = true;
for (int j = ; j < m_columns; j++)
{
if (m_arr[i, j] == )
{
isfull = false;
break;
}
}
if (isfull == true)
{
clearrows++;
m_score++;
for (int k = ; k < m_columns; k++)
{
m_arr[i, k] = ;
}
}
}
for (int i = m_rows - ; i > m_rows - m_height - ; i--)
{
bool isfull = true;
for (int j = ; j < m_columns; j++)
{
if (m_arr[i, j] == )
{
isfull = false;
break;
}
}
if (isfull == true)
{
int n = i;
for (int m = n - ; m > m_rows - m_height - ; m--)
{
if (n == )
{
for (int k = ; k < m_columns; k++)
{
m_arr[n, k] = ;
}
}
else
{
for (int k = ; k < m_columns; k++)
{
m_arr[n, k] = m_arr[m, k];
}
n--;
}
}
}
}
m_height -= clearrows;
} //计算当期高度
private void SetCurHeight()
{
for (int i = ; i < m_rows; i++)
{
for (int j = ; j < m_columns; j++)
{
if (m_arr[i, j] == )
{
m_height = m_rows - i;
return;
}
}
}
} //左移
public void BrickLeft()
{
lock (m_arr)
{
if (m_curBlock != null && m_curBlock.CanLeftMove(m_arr, m_rows, m_columns) == true)
{
ClearCurBrick();
m_curBlock.LeftMove();
SetArrayValue();
}
}
} //右移
public void BrickRight()
{
lock (m_arr)
{
if (m_curBlock != null && m_curBlock.CanRightMove(m_arr, m_rows, m_columns) == true)
{
ClearCurBrick();
m_curBlock.RightMove();
SetArrayValue();
}
}
} //下移
public void BrickDown()
{
lock (m_arr)
{
if (m_curBlock != null && m_curBlock.CanDownMove(m_arr, m_rows, m_columns) == true)
{
ClearCurBrick();
m_curBlock.DownMove();
SetArrayValue();
}
}
} //变形
public void BrickUp()
{
lock (m_arr)
{
if (m_curBlock != null && m_curBlock.CanChange(m_arr, m_rows, m_columns) == true)
{
ClearCurBrick();
m_curBlock.Change();
SetArrayValue();
}
}
} //
public void DrawNewxBrick(Graphics gra, float itemwidth, float itemheight)
{
int[,] arr = new int[, ]{{,,,,},
{,,,,},
{,,,,},
{,,,,},
{,,,,,}};
switch (m_nextBlock._needColumns)
{
case :
arr[, ] = ;
arr[, ] = ;
arr[, ] = ;
arr[, ] = ;
break;
case :
for (int i = , m = ; i < ; i++, m++)
{
for (int j = , n = ; j < ; j++, n++)
{
arr[i, j] = m_nextBlock._range[m, n];
}
}
break;
case :
arr = m_nextBlock._range;
break;
default:
return;
}
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
if (arr[i, j] == )
{
gra.FillRectangle(Brushes.Orange, j * itemwidth, i * itemheight, itemwidth - , itemheight - );
}
}
}
}
}
}
4.运行效果展示
初始主界面
游戏过程
游戏结束
程序源代码工程文件下载
自定义窗体源码下载
C#俄罗斯方块小游戏程序设计与简单实现的更多相关文章
- js实现简单的俄罗斯方块小游戏
js实现简单的俄罗斯方块小游戏 开始 1. 创建一个宽为 200px,高为 360px 的背景容器 <!DOCTYPE html> <html lang="en" ...
- JavaScript小游戏实例:简单的键盘练习
键盘是一种常用的输入设备,灵活熟练地使用键盘进行输入是计算机用户需掌握的一门基本功.下面我们编写一个简单的键盘练习游戏. 1.刺破气泡交互式小动画 在编写简单的键盘练习游戏之前,先设计一个简单地刺破气 ...
- 完整版本的推箱子小游戏,最简单的纯C语言打造
/* 推箱子小游戏 1.定义绘制样式 用二维数组的方式 2.绘制图像 3.找出当前位置 4.逻辑判断,制造动作 根据数学xy轴的规律,这里使用ij 上移,行轴上升,行数减少 下移,行数下降,函数增加 ...
- Java经典小游戏——贪吃蛇简单实现(附源码)
一.使用知识 Jframe GUI 双向链表 线程 二.使用工具 IntelliJ IDEA jdk 1.8 三.开发过程 3.1素材准备 首先在开发之前应该准备一些素材,已备用,我主要找了一个图片以 ...
- C语言编程学习开发的俄罗斯方块小游戏
C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...
- 微信小游戏跳一跳简单手动外挂(基于adb 和 python)
只有两个python文件,代码很简单. shell.py: #coding:utf-8 import subprocess import math import os def execute_comm ...
- JS练习实例--编写经典小游戏俄罗斯方块
最近在学习JavaScript,想编一些实例练练手,之前编了个贪吃蛇,但是实现时没有注意使用面向对象的思想,实现起来也比较简单所以就不总结了,今天就总结下俄罗斯方块小游戏的思路和实现吧(需要下载代码也 ...
- Html5 小游戏 俄罗斯方块
导言 在一个风和日丽的一天,看完了疯狂HTML 5+CSS 3+JavaScript讲义,跟着做了书里最后一章的俄罗斯方块小游戏,并做了一些改进,作为自己前端学习的第一站. 游戏效果: 制作思路 因为 ...
- “倔驴”一个h5小游戏的实现和思考(码易直播)——总结与整理
3月23日晚上8点半(中国队火拼韩国的时候),做了一期直播分享.15年做的一个小游戏,把核心代码拿出来,现场讲写了一遍,结果后面翻车了,写错了两个地方,导致运行效果有点问题,直播边说话边写代码还真不一 ...
随机推荐
- Compile groovy mixed with java in Maven
Assuming that groovy codes are in src/main/groovy and java codes are in src/main/java. We can use 2 ...
- Oralce sql (+) 补充
Oracle 外连接 (1)左外连接 (左边的表不加限制) (2)右外连接(右边的表不加限制) (3)全外连接(左右两表都不加限制) 外连接(Outer Join) oute ...
- 用DirectX实现多视图渲染
什么是多视图 一般的3D程序都只有一个视图,对应整个窗口的客户区.多视图就是在一个窗口中放置多个视图,以便从不同的角度观察模型或者场景.很多图形软件都有这个功能,比如大家熟知的3DMax就有四个视图, ...
- Linux之清理linux内存cache
转自:https://www.cnblogs.com/madsnotes/articles/5740495.html 频繁的文件访问会导致系统的Cache使用量大增.例如:在使用grep从很多文件中搜 ...
- 【黑魔法】Covering Indexes、STRAIGHT_JOIN
今天给大家介绍两个黑魔法,这都是压箱底的法宝.大家在使用时,一定要弄清他们的适用场景及用法,用好了,就是一把开天斧,用不好那就是画蛇添足.自从看过耗子哥(左耳朵耗子)的博客,都会给对相应专题有兴趣的小 ...
- SAP S/4 1610 IDES + HANA 2.0 安装
前几天安装的都没带演示数据 ,这个版本带DEMO数据,学习比较好 我的机器配置: 内存:128G CPU:E5-2618L V4 硬盘:1T SSD 安装在VMware虚拟机中,安装完后,虚拟机大小只 ...
- wordclock中文模式快一个小时怎么调整
wordclock屏幕保护,设置为中文模式,显示的时间比系统时间要快一个小时,其实软件自带的配置文件可以设置调整到正常时间…… 工具/原料 wordclock 方法/步骤 桌面上右键菜 ...
- 自动化web前端测试,自动登录网站.目前发现最靠谱的方法是imacros
imacros免费版 登录宏代码的示例: //首先登出URL GOTO=http://yoursite/logout.html//打开登录页面URL GOTO=http://yoursite/logi ...
- 每天一个linux命令(16):which
1.命令简介 which (which) 命令的作用是在PATH变量指定的路径中搜索某个系统命令的位置并且返回第一个搜索结果.也就是说使用which命令就可以看到某个系统命令是否存在以及执行的到底是哪 ...
- Mac下的Chrome或Safari访问跨域设置,MBP上使用模拟器Simulator.app或iphone+Safari调试网页
Mac下的Chrome或Safari访问跨域设置: mac下终端启动Chrome $ open -a Google\ Chrome --args --disable-web-security 或 /A ...