📄 block.cs
字号:
}
public bool left()
{
//检测是否可以左移
if (GameField.isEmpty(square1.location.X / squareSize-1, square1.location.Y / squareSize) &&
GameField.isEmpty(square2.location.X / squareSize-1, square2.location.Y / squareSize) &&
GameField.isEmpty(square3.location.X / squareSize-1, square3.location.Y / squareSize) &&
GameField.isEmpty(square4.location.X / squareSize-1, square4.location.Y / squareSize))
{
Erase(GameField.winHandle);
square1.location = new Point(square1.location.X - squareSize, square1.location.Y);
square2.location = new Point(square2.location.X - squareSize, square2.location.Y);
square3.location = new Point(square3.location.X - squareSize, square3.location.Y);
square4.location = new Point(square4.location.X - squareSize, square4.location.Y);
Draw(GameField.winHandle);
return true;
}
else //如果不能左移了
{
return false;
}
}
public bool right()
{
//检测是否可以右移
if (GameField.isEmpty(square1.location.X / squareSize +1, square1.location.Y / squareSize) &&
GameField.isEmpty(square2.location.X / squareSize +1, square2.location.Y / squareSize) &&
GameField.isEmpty(square3.location.X / squareSize +1, square3.location.Y / squareSize) &&
GameField.isEmpty(square4.location.X / squareSize +1, square4.location.Y / squareSize))
{
Erase(GameField.winHandle);
square1.location = new Point(square1.location.X + squareSize, square1.location.Y );
square2.location = new Point(square2.location.X + squareSize, square2.location.Y);
square3.location = new Point(square3.location.X + squareSize, square3.location.Y);
square4.location = new Point(square4.location.X + squareSize, square4.location.Y);
Draw(GameField.winHandle);
return true;
}
else //如果不能右移了
{
return false;
}
}
/*旋转block*/
public void Rotate()
{
//保存每个小块的位置
Point oldPosition1 = square1.location;
Point oldPosition2 = square2.location;
Point oldPosition3 = square3.location;
Point oldPosition4 = square4.location;
//保存当前的方向
RotateDirections oldRotation = myRotation;
//开始试着旋转方块,旋转方向:顺时针方向 旋转中心点为形状拐点
Erase(GameField.winHandle);
switch(blockType)
{
case BlockTypes.square:
break;
case BlockTypes.line:
//直线的旋转只有两种方向
switch (myRotation)
{
case RotateDirections.North:
myRotation = RotateDirections.East;
square1.location = new Point(square2.location.X-squareSize,square2.location.Y);
square3.location = new Point(square2.location.X+squareSize,square2.location.Y);
square4.location = new Point(square2.location.X + 2 * squareSize, square2.location.Y);
break;
case RotateDirections.East:
myRotation = RotateDirections.North;
square1.location = new Point(square2.location.X,square2.location.Y-squareSize);
square3.location = new Point(square2.location.X, square2.location.Y +squareSize);
square4.location = new Point(square2.location.X, square2.location.Y + 2 * squareSize);
break;
}
break;
case BlockTypes.J:
//J形方块有四种旋转方向
switch (myRotation)
{
case RotateDirections.North:
myRotation = RotateDirections.East;
square1.location = new Point(square3.location.X+2*squareSize,square3.location.Y);
square2.location = new Point(square3.location.X+squareSize,square3.location.Y);
square4.location = new Point(square3.location.X,square3.location.Y-squareSize);
break;
case RotateDirections.East:
myRotation = RotateDirections.South;
square1.location = new Point(square3.location.X,square3.location.Y+2*squareSize);
square2.location = new Point(square3.location.X,square3.location.Y+squareSize);
square4.location = new Point(square3.location.X+squareSize,square3.location.Y);
break;
case RotateDirections.South:
myRotation = RotateDirections.West;
square1.location = new Point(square3.location.X-2*squareSize,square3.location.Y);
square2.location = new Point(square3.location.X-squareSize,square3.location.Y);
square4.location = new Point(square3.location.X,square3.location.Y+squareSize);
break;
case RotateDirections.West:
myRotation = RotateDirections.North;
square1.location = new Point(square3.location.X,square3.location.Y-2*squareSize);
square2.location = new Point(square3.location.X,square3.location.Y-squareSize);
square4.location = new Point(square3.location.X-squareSize,square3.location.Y);
break;
}
break;
case BlockTypes.L:
//L形方块有四种旋转方向
switch (myRotation)
{
case RotateDirections.North:
myRotation = RotateDirections.East;
square1.location = new Point(square3.location.X+2*squareSize,square3.location.Y);
square2.location = new Point(square3.location.X+squareSize, square3.location.Y);
square4.location = new Point(square3.location.X, square3.location.Y+squareSize);
break;
case RotateDirections.East:
myRotation = RotateDirections.South;
square1.location = new Point(square3.location.X, square3.location.Y + 2 * squareSize);
square2.location = new Point(square3.location.X, square3.location.Y + squareSize);
square4.location = new Point(square3.location.X - squareSize, square3.location.Y);
break;
case RotateDirections.South:
myRotation = RotateDirections.West;
square1.location = new Point(square3.location.X - 2 * squareSize, square3.location.Y);
square2.location = new Point(square3.location.X - squareSize, square3.location.Y);
square4.location = new Point(square3.location.X, square3.location.Y - squareSize);
break;
case RotateDirections.West:
myRotation = RotateDirections.North;
square1.location = new Point(square3.location.X, square3.location.Y - 2 * squareSize);
square2.location = new Point(square3.location.X, square3.location.Y - squareSize);
square4.location = new Point(square3.location.X + squareSize, square3.location.Y);
break;
}
break;
case BlockTypes.T:
//T形方块也有四种旋转方向
switch (myRotation)
{
case RotateDirections.North:
myRotation = RotateDirections.East;
square1.location = new Point(square2.location.X,square2.location.Y-squareSize);
square3.location = new Point(square2.location.X, square2.location.Y+squareSize);
square4.location = new Point(square2.location.X-squareSize, square2.location.Y);
break;
case RotateDirections.East:
myRotation = RotateDirections.South;
square1.location = new Point(square2.location.X+squareSize, square2.location.Y);
square3.location = new Point(square2.location.X-squareSize, square2.location.Y);
square4.location = new Point(square2.location.X, square2.location.Y-squareSize);
break;
case RotateDirections.South:
myRotation = RotateDirections.West;
square1.location = new Point(square2.location.X, square2.location.Y+squareSize);
square3.location = new Point(square2.location.X, square2.location.Y-squareSize);
square4.location = new Point(square2.location.X+squareSize, square2.location.Y);
break;
case RotateDirections.West:
myRotation = RotateDirections.North;
square1.location = new Point(square2.location.X-squareSize, square2.location.Y);
square3.location = new Point(square2.location.X+squareSize, square2.location.Y);
square4.location = new Point(square2.location.X, square2.location.Y+squareSize);
break;
}
break;
case BlockTypes.Z:
//Z形方块有两种旋转方向
switch (myRotation)
{
case RotateDirections.North:
myRotation = RotateDirections.East;
square1.location = new Point(square2.location.X, square2.location.Y - squareSize);
square3.location = new Point(square2.location.X - squareSize, square2.location.Y);
square4.location = new Point(square2.location.X - squareSize, square2.location.Y + squareSize);
break;
case RotateDirections.East:
myRotation = RotateDirections.North;
square1.location = new Point(square2.location.X-squareSize, square2.location.Y);
square3.location = new Point(square2.location.X, square2.location.Y+squareSize);
square4.location = new Point(square2.location.X+squareSize, square2.location.Y+squareSize);
break;
}
break;
case BlockTypes.S:
//S形方块有两种旋转方向
switch (myRotation)
{
case RotateDirections.North:
myRotation = RotateDirections.East;
square1.location = new Point(square3.location.X+squareSize,square3.location.Y+squareSize);
square2.location = new Point(square3.location.X+squareSize, square3.location.Y);
square4.location = new Point(square3.location.X, square3.location.Y-squareSize);
break;
case RotateDirections.East:
myRotation = RotateDirections.North;
square1.location = new Point(square3.location.X-squareSize, square3.location.Y+squareSize);
square2.location = new Point(square3.location.X, square3.location.Y+squareSize);
square4.location = new Point(square3.location.X+squareSize, square3.location.Y);
break;
}
break;
}
//旋转之后检测位置是否冲突
if (!(GameField.isEmpty(square1.location.X / squareSize, square1.location.Y / squareSize)&&
GameField.isEmpty(square2.location.X / squareSize, square2.location.Y / squareSize)&&
GameField.isEmpty(square3.location.X / squareSize, square3.location.Y / squareSize)&&
GameField.isEmpty(square4.location.X / squareSize, square4.location.Y / squareSize)))
{//如果有冲突则回到原来的状态
myRotation = oldRotation;
square1.location = oldPosition1;
square2.location = oldPosition2;
square3.location = oldPosition3;
square4.location = oldPosition4;
}
Draw(GameField.winHandle);
}
/*检测是否到顶*/
public int Top()
{
return Math.Min(square1.location.Y,Math.Min(square2.location.Y,Math.Min(square3.location.Y,square4.location.Y)));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -