关于下落对象
众所周知,俄罗斯方块有七种下落对象,根据形状,我先建了一个叫GO(gameobject)的枚举类型,内容是I、L、O、Z、T、tL、tZ(自行脑补形状,是不是很传神嘿嘿)
public enum GO {I=0,L,O,Z,T,tL,tZ };
我们的需求是让下落对象左右移动、旋转,移动很简单,只要x++、x--就行了
而旋转,这我想了很长时间,终于想到了解决办法
记得初中数学老师讲过,一个点如何绕原点旋转90度,其实就是把坐标x、y对调,然后新的y取相反数就可以了
举个例子,假如原坐标是(3,2),旋转90度就是(2,-3)
所以,我们只要设中心的旋转坐标为(0,0),其他坐标按位置也写出来,然后带入旋转函数里面,按想要的方式旋转啦!
比如L的四个矢量坐标就是(0,0)(0,-1)(0,1)(1,1),这个中间的坐标我叫centureblock,如果我想让这个下落对象出现在坐标(4,5)处,centureblock就是(4,5)得到的坐标就是(0+4,0+5)(0+4,-1+5)(0+4,1+5)(1+4,1+5)
好像还是不太能说明白,那么直接上代码吧
class Objectgame
{
private GO type;
private Tpoint centurePoint;
private Tpoint[] point4;
private byte angle;
private int[,] vectorX;
private int[,] vectorY;
public Objectgame()
{
point4 = new Tpoint[4];
//centurePoint = new Tpoint();
vectorX = new int[7, 3];
vectorY = new int[7, 3];
int n;//这是预先导入的矢量
n = 0;
vectorX[n, 0] = 0; vectorX[n, 1] = 0; vectorX[n, 2] = 0;
vectorY[n, 0] = -1; vectorY[n, 1] = 1; vectorY[n, 2] = 2;
n = 1;
vectorX[n, 0] = 0; vectorX[n, 1] = 0; vectorX[n, 2] = 1;
vectorY[n, 0] = -1; vectorY[n, 1] = 1; vectorY[n, 2] = 1;
n = 2;
vectorX[n, 0] = 0; vectorX[n, 1] = 1; vectorX[n, 2] = 1;
vectorY[n, 0] = -1; vectorY[n, 1] =-1; vectorY[n, 2] = 0;
n = 3;
vectorX[n, 0] = 0; vectorX[n, 1] = 1; vectorX[n, 2] = 1;
vectorY[n, 0] = -1; vectorY[n, 1] = 0; vectorY[n, 2] = 1;
n = 4;
vectorX[n, 0] = -1; vectorX[n, 1] = 1; vectorX[n, 2] = 0;
vectorY[n, 0] = 0; vectorY[n, 1] = 0; vectorY[n, 2] = 1;
n = 5;
vectorX[n, 0] = 0; vectorX[n, 1] = 0; vectorX[n, 2] = -1;
vectorY[n, 0] = -1; vectorY[n, 1] = 1; vectorY[n, 2] = 1;
n = 6;
vectorX[n, 0] = 0; vectorX[n, 1] = -1; vectorX[n, 2] = -1;
vectorY[n, 0] = -1; vectorY[n, 1] = 0; vectorY[n, 2] = 1;
}
public Tpoint[] GetPoint(GO t,Tpoint p,byte a)//旋转角度=90*(a-1)a为1—4
{
type = t;
centurePoint = p;
angle = a;
point4[0] = new Tpoint();
point4[1] = new Tpoint();
point4[2] = new Tpoint();
point4[3] = new Tpoint();
point4[0].x= centurePoint.x;
point4[0].y = centurePoint.y;
for (byte i=1;i<4;i++)
{
int dx, dy,temp;
dx = vectorX[(int)type, i-1];
dy = vectorY[(int)type, i-1];
for (byte j=0;j<angle-1;j++)
{
temp = dx;dx = dy;dy = -temp;
}
point4[i].x = centurePoint.x+dx;
point4[i].y = centurePoint.y+dy;
}
return point4;//返回一个坐标数组,包含下落对象的四个坐标
}
}
这样的话移动和旋转都解决了,还有一件重要的事,就是我不能让我们的下落对象跑到格子外面去,我们要加一些限制条件,欲知后事如何,且听下回分解