📄 blockfactory.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
namespace MyTetris
{
/// <summary> class BlockFactory
/// 生产方块的类
/// </summary>
class BlockFactory
{
/// <summary>
/// 存放所有方块样式信息!
/// </summary>
private BlockInfoArr info;
/// <summary>
/// 方块颜色(背景色)
/// </summary>
protected Color disapperColor;
/// <summary>
/// 每个方块的像素
/// </summary>
protected int rectPix;
/// <summary> public BlockFactory()
/// 构造函数
/// </summary>
public BlockFactory()
{
Config config = new Config();
config.LoadFromXml();
info=new BlockInfoArr();
info=config.Info;
disapperColor=config.BackColor;
rectPix=config.RectPix;
}
/// <summary> public Block GetBlock()
/// 从方块组中随机获取一个方块信息
/// </summary>
/// <returns></returns>
public Block GetBlock()
{
Random rd = new Random();
int blockNum =Convert.ToInt32(rd.Next(0,info.Length));
BitArray ba = info[blockNum].Id; //随机取出一个方块样式赋给ba
int strNum = 0; //确定方块样式中被填充的小方块个数,(确定数组的长度)
foreach(bool b in ba)
{
if(b)
{
strNum++;
}
}
Point[] structArr = new Point[strNum]; //新建一个Point数组,并确定其长度,以创建新的Block
int k = 0;
for (int i = 0; i < ba.Length;i++ )
{
if(ba[i]) //将为1的位转换成(五乘五)矩阵的下标
{
structArr[k].X = i / 5 - 2;
structArr[k].Y = 2 - i % 5;
k++;
}
}
return new Block(structArr, info[blockNum].Bcolor,disapperColor,rectPix); //创建block并返回
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -