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#俄罗斯方块小游戏程序设计与简单实现的更多相关文章

  1. js实现简单的俄罗斯方块小游戏

    js实现简单的俄罗斯方块小游戏 开始 1. 创建一个宽为 200px,高为 360px 的背景容器 <!DOCTYPE html> <html lang="en" ...

  2. JavaScript小游戏实例:简单的键盘练习

    键盘是一种常用的输入设备,灵活熟练地使用键盘进行输入是计算机用户需掌握的一门基本功.下面我们编写一个简单的键盘练习游戏. 1.刺破气泡交互式小动画 在编写简单的键盘练习游戏之前,先设计一个简单地刺破气 ...

  3. 完整版本的推箱子小游戏,最简单的纯C语言打造

    /* 推箱子小游戏 1.定义绘制样式 用二维数组的方式 2.绘制图像 3.找出当前位置 4.逻辑判断,制造动作 根据数学xy轴的规律,这里使用ij 上移,行轴上升,行数减少 下移,行数下降,函数增加 ...

  4. Java经典小游戏——贪吃蛇简单实现(附源码)

    一.使用知识 Jframe GUI 双向链表 线程 二.使用工具 IntelliJ IDEA jdk 1.8 三.开发过程 3.1素材准备 首先在开发之前应该准备一些素材,已备用,我主要找了一个图片以 ...

  5. C语言编程学习开发的俄罗斯方块小游戏

    C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...

  6. 微信小游戏跳一跳简单手动外挂(基于adb 和 python)

    只有两个python文件,代码很简单. shell.py: #coding:utf-8 import subprocess import math import os def execute_comm ...

  7. JS练习实例--编写经典小游戏俄罗斯方块

    最近在学习JavaScript,想编一些实例练练手,之前编了个贪吃蛇,但是实现时没有注意使用面向对象的思想,实现起来也比较简单所以就不总结了,今天就总结下俄罗斯方块小游戏的思路和实现吧(需要下载代码也 ...

  8. Html5 小游戏 俄罗斯方块

    导言 在一个风和日丽的一天,看完了疯狂HTML 5+CSS 3+JavaScript讲义,跟着做了书里最后一章的俄罗斯方块小游戏,并做了一些改进,作为自己前端学习的第一站. 游戏效果: 制作思路 因为 ...

  9. “倔驴”一个h5小游戏的实现和思考(码易直播)——总结与整理

    3月23日晚上8点半(中国队火拼韩国的时候),做了一期直播分享.15年做的一个小游戏,把核心代码拿出来,现场讲写了一遍,结果后面翻车了,写错了两个地方,导致运行效果有点问题,直播边说话边写代码还真不一 ...

随机推荐

  1. python 3.5 连接mysql数据库

    python 3.5 要连接mysql数据库,必须先安装pymysql模块,该模块可以操作mysql数据. 1.安装pymysql模块:使用pip进行安装 cmd打开运行模式,切换目录到pip的scr ...

  2. openstack之~glance安装部署

    接着部署完keystone后,接着部署glance 第一:部署安装glance glance关于数据库的操作: [root@controller /]# mysql -u root -p Enter ...

  3. Introducing stapbpf – SystemTap’s new BPF backend

    https://developers.redhat.com/blog/2017/12/13/introducing-stapbpf-systemtaps-new-bpf-backend/

  4. golang dlv 远程调试

    因为不知道delvel 是如何设置源码的,本地编译的上传到服务器上,服务器要调试看不到源码,很是忧伤,所以干脆使用远程调试吧: 在服务器上 ps x|grep game 查找到gameserver的进 ...

  5. javascript 回调函数定义 模板

    函数定义: function callfun(in_param,callback) { console.log(in_param); var out_param='bbbbb'; return (ty ...

  6. virltualbox 升级之后 苹果虚拟机报The installed support driver doesn't match the version of the user解决方案

    1.反安装virtualbox后,不要重启 2.删除virtualbox的安装目录 3.进入%userprofile% 目录 (比如: C:\users\me) 删除 .VirtualBox Virt ...

  7. 第三天:MDN CSS学习笔记

    一:CSS基础 1:DOM 当浏览器显示文档时,它必须将文档的内容与其样式信息结合.它分两个阶段处理文档: 浏览器将 HTML 和 CSS 转化成 DOM (文档对象模型).DOM在计算机内存中表示文 ...

  8. 每天一个linux命令:chmod

    1.命令简介 chmod(Change mode) 用来将每个文件的模式更改为指定值.Linux/Unix 的档案调用权限分为三级 : 档案拥有者.群组.其他. u :目录或者文件的当前的用户 g : ...

  9. QGIS Server使用记录

    目录 0. 简述 1. 下载QGIS桌面64位版本 2. 下载安装QGIS Server程序 3. 下载安装Apache服务器 4.使用及问题处理 0. 简述 关于QGIS Server相关的文档很少 ...

  10. 解析 .Net Core 注入——注册服务

    在学习 Asp.Net Core 的过程中,注入可以说是无处不在,对于 .Net Core 来说,它是独立的一个程序集,没有复杂的依赖项和配置文件,所以对于学习 Asp.Net Core 源码的朋友来 ...