📄 palette.cs
字号:
}
/// <summary> public void DeasilRotate()
/// 顺时针旋转
/// </summary>
public void DeasilRotate()
{
for (int i = 0; i < runBlock.Length;i++ )
{
int x = runBlock.XPos + runBlock[i].Y;
int y = runBlock.YPos + runBlock[i].X;
if (x < 0 || x > _width - 1) //超出左右边界
return;
if (y < 0 || y > _height - 1) //超出上下边界
return;
if(!coorArr[x,y].IsEmpty) //旋转后有方块
return ;
}
runBlock.erase(gpPlatette);
runBlock.DeasilRotate();
runBlock.Paint(gpPlatette);
}
/// <summary> public void DisecRotate()
/// 逆时针旋转
/// </summary>
public void DisecRotate()
{
for (int i = 0; i < runBlock.Length; i++)
{
int x = runBlock.XPos - runBlock[i].Y;
int y = runBlock.YPos - runBlock[i].X;
if (x < 0 || x > _width - 1) //超出左右边界
return;
if (y < 0 || y > _height - 1) //超出上下边界
return;
if (!coorArr[x, y].IsEmpty) //旋转后有方块
return;
}
runBlock.erase(gpPlatette);
runBlock.DisecRotate();
runBlock.Paint(gpPlatette);
}
/// <summary>
/// 重绘画布背景
/// </summary>
/// <param name="gra">背景画布</param>
private void PaintBackGround(Graphics gra)
{
gra.Clear(disapperColor); //清空画板
for(int i=0;i<_width;i++)
{
for (int j = 0; j < _height;j++ )
{
if (!coorArr[i, j].IsEmpty) //此小块内不为空则要重绘
{
SolidBrush sb = new SolidBrush(coorArr[i,j]);
gra.FillRectangle(sb,i*rectPix+1,j*rectPix+1,rectPix-1,rectPix-2);
}
}
}
}
/// <summary>
/// 重画整 个画板(背景(下边已经存在的方块)和正在下落的方块)
/// </summary>
/// <param name="gra">背景画布</param>
public void PaintPlatte(Graphics gra)
{
PaintBackGround(gra);
if(runBlock!=null)
{
runBlock.Paint(gra);
}
}
/// <summary>
/// 重画下一个方块
/// </summary>
/// <param name="gra">背景画布</param>
public void PaintReady(Graphics gra)
{
if (readyBlock != null)
readyBlock.Paint(gra);
}
/// <summary>
/// 检查、结束方块的移动
/// </summary>
public void CheckAndOverBlock()
{
bool over = false;
for (int i = 0; i < runBlock.Length;i++ )
{
int x = runBlock.XPos + runBlock[i].X;
int y = runBlock.YPos - runBlock[i].Y;
if(y==_height-1) //到达下边界
{
over = true;
break;
}
if(!coorArr[x,y+1].IsEmpty) //如果下边有其它方块,
{
over = true;
break;
}
}
if(over)
{
for (int i = 0; i < runBlock.Length;i++ ) //把当前方块放入coorArr(当成背景色)
{
coorArr[runBlock.XPos + runBlock[i].X, runBlock.YPos - runBlock[i].Y] = runBlock.BlockColor;
}
DeleFullRow(); //检查是否有满行
//产生的新广场
runBlock = readyBlock;
runBlock.XPos = _width / 2;
int y = 0;
for (int i = 0; i < runBlock.Length; i++) //找出方块的最高点
{
if (runBlock[i].Y > y)
{
y = runBlock[i].Y;
}
}
runBlock.YPos = y;
for (int i = 0; i < runBlock.Length;i++ )
{
if (!coorArr[runBlock.XPos + runBlock[i].X, runBlock.YPos - runBlock[i].Y].IsEmpty) //游戏结束
{
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
gpPlatette.DrawString("GAME OVER",new Font("Arial Black",30f),new SolidBrush(Color.Yellow),new RectangleF(0,_height*rectPix/2-100,_width*rectPix,100),sf);
myTimer.Stop();
return;
}
}
blockNum++;
// gpPlatette.Clear(disapperColor); //清空画布
runBlock.Paint(gpPlatette);
Thread.Sleep(20);
readyBlock = bFactory.GetBlock();
readyBlock.XPos = 2;
readyBlock.YPos = 2;
gpReady.Clear(disapperColor); //清空画布
readyBlock.Paint(gpReady);
}
}
/// <summary>
/// 删除满行
/// </summary>
public void DeleFullRow()
{
//找出当前方块纵坐标的范围
int lowRow = runBlock.YPos - runBlock[0].Y;
int highRow = lowRow;
for (int i = 1; i < runBlock.Length;i++ )
{
int y = runBlock.YPos - runBlock[i].Y;
if(y<lowRow)
{
lowRow = y;
}
if(y>lowRow)
{
highRow = y;
}
}
bool rePaint = false;
for (int i = lowRow; i <= highRow;i++ )
{
bool isFull =true;
for (int j = 0; j < _width;j++ ) //判断 是否满行
{
if(coorArr[j,i].IsEmpty)
{
isFull = false;
break;
}
}
if(isFull) //是满行
{
rePaint=true; //要重画
for(int k=i;k>0;k--) //将第N行的图案替换成第N-1行
{
for(int j=0;j<_width;j++)
{
coorArr[j,k]=coorArr[j,k-1];
}
}
for(int j=0;j<_width;j++) //清空第0行
{
coorArr[j,0]=Color.Empty;
}
rowDelNum++; //清空行数加1
if (rowDelNum >= levelNum) // 设置级数
{
level++;
levelNum +=levelModu; //为下一级增加难度(所要删除的行数)
this.myTimer.Interval = 500 - 50 * (level - 1);
}
}
}
if(rePaint)
PaintBackGround(gpPlatette);
}
/// <summary>
/// 暂停
/// </summary>
public void Pause()
{
if(myTimer.Enabled==true)
{
myTimer.Enabled = false;
this.atPause = DateTime.Now;
}
}
/// <summary>
/// 继续
/// </summary>
public void GoOn()
{
if (myTimer.Enabled == false)
{
myTimer.Enabled = true;
this.atStart = DateTime.Now;
}
}
/// <summary>
/// 关闭资源
/// </summary>
public void Close()
{
myTimer.Close();
gpPlatette.Dispose();
gpReady.Dispose();
}
/// <summary>
/// 显示当前状态
/// </summary>
public void ShowStatus()
{
gpStatus.Clear(Color.White);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
showStatusFontSize = this._width * this._height / 60;
gpStatus.DrawString("级 别:" + level + "\n\n块 数:" + blockNum + "\n\n分 数:" + rowDelNum*100 + "\n\n速 度:" + (speed.ToString()).Substring(0, 4) + "块/每秒 ", new Font("Arial Black", showStatusFontSize), new SolidBrush(Color.Black), new RectangleF(10, 20, 100, 200), sf);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -