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年做的一个小游戏,把核心代码拿出来,现场讲写了一遍,结果后面翻车了,写错了两个地方,导致运行效果有点问题,直播边说话边写代码还真不一 ...
随机推荐
- pygame-KidsCanCode系列jumpy-part3-重力及碰撞检测
这个游戏叫jumpy,大致玩法就是模拟超级玛丽一样,可以不停在各个档板上跳动,同时受到重力的作用,会向下掉,如果落下时,没有站在档板上,就挂了. 这节,我们加入重力因素,继续改造sprites.py ...
- 开发入门,学Java还是学大数据?
经常有人问,我想学习开发,到底是学Java好还是学大数据好?或者是,学习大数据还有必要学Java吗? 依我说,这个提问的标准答案是:两者都学. 先来甩两张图. 一张是腾讯 ...
- Collection was modified; enumeration operation may not execute Dictionary 集合已修改;可能无法执行枚举操作
public void ForeachDic() { Dictionary dic = new Dictionary(); dic.Add("1", 10); dic.Add(&q ...
- SeaweedFS的配置使用
SeaweedFS是一个简单并且高度可扩展的分布式文件系统,可以存储数十亿的文件并且快速获得文件,特别适合于有效处理小文件,这里我们简称为weed,weed的主节点不管理文件元数据而是仅管理文件卷,这 ...
- [Canvas]空战游戏进阶 增加爆炸管理类
点此下载源码,欲观看效果请用Chrome打开index.html 图例: 源码: <!DOCTYPE html> <html lang="utf-8"> & ...
- Android编码学习之Fragment
1. 什么是Fragment Fragment是Android honeycomb 3.0新增的概念,Fragment名为碎片不过却和Activity十分相似.Fragment是用来描述一些行为或一部 ...
- boost::filesystem经常使用使用方法具体解释
提示: filesystem库提供了两个头文件,一个是<boost/filesystem.hpp>,这个头文件包括基本的库内容.它提供了对文件系统的重要操作. 同一时候它定义了一个类pat ...
- py3下怎么用StringIO
try: from StringIO import StringIO except ImportError: from io import StringIO
- Linux输入子系统框架分析(1)
在Linux下的输入设备键盘.触摸屏.鼠标等都能够用输入子系统来实现驱动.输入子系统分为三层,核心层和设备驱动层.事件层.核心层和事件层由Linux输入子系统本身实现,设备驱动层由我们实现.我们在设备 ...
- 分析轮子(六)- LinkedList.java
注:玩的是JDK1.7版本 一:先上类的继承结构图 二:再看一下他的底层实现数据结构 三:然后从源码中找点好玩的东西 1)双向链表的结构构成元素,头指针.尾指针.节点信息(前向指针.后向指针.节点信息 ...