📄 body.cs
字号:
using System;
using System.Drawing;
using System.Collections;
namespace Employee
{
/// <summary>
/// Body 的摘要说明。
/// </summary>
public class Body
{
public Body()
{
maxWidth = 10;
maxHeight = 15;
}
public enum MOVE_TYPE {MOVE_LEFT=0, MOVE_RIGHT=1, MOVE_DOWN=2, MOVE_FALL=3, MOVE_ROATE=4};
private ArrayList blockList = new ArrayList();
private Shape nextShape;
private int maxWidth;
private int maxHeight;
public bool SetNextShape(Shape s)
{
nextShape = s;
nextShape.Position = new Point(4, 0);
bool ret = false;
//*
while (!ShapeCanPlace(nextShape))
{
Point pt = nextShape.Position;
pt.Y--;
nextShape.Position = pt;
ret = true;
}
//*/
return ret;
}
public void Draw(Graphics g)
{
DrawNextShape(g);
for (int i=0; i<blockList.Count; i++)
{
((Block)(blockList[i])).Draw(g);
}
}
public bool MoveShape(Graphics g, MOVE_TYPE m)
{
Shape s = new Shape(nextShape.IndexDef);
s.Copy(nextShape);
Point pt = s.Position;
switch (m)
{
case MOVE_TYPE.MOVE_FALL:
{
while (ShapeCanPlace(s))
{
pt.Y++;
s.Position = pt;
}
nextShape.Draw(g, true);
pt.Y--;
s.Position = pt;
nextShape.Copy(s);
nextShape.Draw(g);
PlaceShape();
return true;
//break;
}
case MOVE_TYPE.MOVE_DOWN:
pt.Y++;
break;
case MOVE_TYPE.MOVE_LEFT:
pt.X--;
break;
case MOVE_TYPE.MOVE_RIGHT:
pt.X++;
break;
case MOVE_TYPE.MOVE_ROATE:
s.Roate();
break;
default:
break;
}
s.Position = pt;
if (ShapeCanPlace(s))
{
nextShape.Draw(g, true);
nextShape.Copy(s);
nextShape.Draw(g);
}
else
{
if (m == MOVE_TYPE.MOVE_DOWN)
{
PlaceShape();
return true;
}
}
return false;
}
public bool ShapeCanPlace(Shape s)
{
for (int i=0; i<4; i++)
{
Point pt = s.Position;
Point ptOff = s.GetBlock(i).Position;
pt.Offset(ptOff.X, ptOff.Y);
if (PositionHasBlock(pt))
return false;
}
return true;
}
public bool PositionHasBlock(Point pt)
{
if (pt.Y < 0) return false;
Rectangle rc = new Rectangle(0, 0, maxWidth, maxHeight);
if (!rc.Contains(pt))
{
return true;
}
for (int i=0; i<blockList.Count; i++)
{
if (((Block)(blockList[i])).Position == pt)
{
return true;
}
}
return false;
}
public void PlaceShape()
{
for (int i=0; i<4; i++)
{
Point pt = nextShape.Position;
Point ptOff = nextShape.GetBlock(i).Position;
pt.Offset(ptOff.X, ptOff.Y);
nextShape.GetBlock(i).Position = pt;
blockList.Add(nextShape.GetBlock(i));
}
}
public void Reset()
{
blockList.Clear();
}
public int ClearLines()
{
int count = 0;
for (int i=0; i<maxHeight; i++)
{
bool clear = true;
for (int j=0; j<maxWidth; j++)
{
if (!PositionHasBlock(new Point(j, i)))
{
clear = false;
break;
}
}
if (clear)
{
for (int n=blockList.Count-1; n>=0; n--)
{
if (((Block)(blockList[n])).Position.Y == i)
{
blockList.RemoveAt(n);
}
else if (((Block)(blockList[n])).Position.Y < i)
{
Point pt = ((Block)(blockList[n])).Position;
pt.Y++;
((Block)(blockList[n])).Position = pt;
}
}
count++;
}
}
return count;
}
public void DrawNextShape(Graphics g)
{
nextShape.Draw(g);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -