class GameControlor:IShapeMoveable
{
GameOverDelegate gameOverDelegate;
#region 01:游戏背景+Ground
Ground ground;
internal Ground Ground
{
get { return ground; }
set { ground = value; }
}
#endregion
#region 02:画笔+draw
Graphics draw;
public Graphics Draw
{
get { return draw; }
set { draw = value; }
}
#endregion
#region 03:游戏的形状+Shape
Shape shape;
internal Shape Shape
{
get { return shape; }
set { shape = value; }
}
#endregion
#region 04:形状工厂+ShapeFactory
ShapeFactory shapeFactory;
internal ShapeFactory ShapeFactory
{
get { return shapeFactory; }
set { shapeFactory = value; }
}
#endregion
#region 05:构造函数,将draw传进来+GameControlor(Graphics draw)
public GameControlor(Graphics draw,GameOverDelegate gameOverDelegate)
{
this.gameOverDelegate = gameOverDelegate;
this.draw = draw;
ground = new Ground();
shapeFactory = new ShapeFactory();
shape = shapeFactory.GetRandomShape();
shape.StartShapeMoveThread(this);
}
#endregion
/// <summary>
/// 游戏开始
/// </summary>
/// <param name="draw"></param>
public void GameBegain()
{
Ground.InitiBlock();
Redisplay();
}
/// <summary>
/// 显示游戏背景
/// </summary>
/// <param name="draw"></param>
public void Redisplay()
{
ground.DrawSelf(draw);//显示背景
shape.DrawMe(draw);//显示形状
}
/// <summary>
/// 控制器实现接口代码
/// </summary>
void IShapeMoveable.ShapeMove(Shape shape)
{
//判断shape还能否移动
if (IsShapeMoveable(shape))
shape.MoveDown(draw);
else
{
if (ground.ISGameOver(shape))//判断游戏是否结束
{
gameOverDelegate();//委托调用函数
}
else
{
ground.KillShape(shape);
this.shape = shapeFactory.GetRandomShape();//产生一个新的shape
this.shape.StartShapeMoveThread(this);//注意this的应用
Redisplay();
}
}
}
/// <summary>
/// 判断shape还能否移动
/// </summary>
/// <param name="shape"></param>
/// <returns></returns>
public bool IsShapeMoveable(Shape shape)//默认是向下移动
{
return ground.IsShapeMoveable(shape,ShapeDirection.Down);
}
/// <summary>
/// 由键盘得到shape的移动方向,并且产生相应的动作
/// </summary>
/// <param name="keyData"></param>
public void ProcessDialogKey(Keys keyData)
{
switch (keyData)
{
case Keys.Down:
if (ground.IsShapeMoveable(shape,ShapeDirection.Down))
{
shape.MoveDown(draw);
}
break;
case Keys.Right:
if(ground.IsShapeMoveable(shape,ShapeDirection.Right))
{
shape.MoveRight(draw);
}
break;
case Keys.Left:
if (ground.IsShapeMoveable(shape, ShapeDirection.Left))
{
shape.MoveLeft(draw);
}
break;
}
}
}