现在我正在制作一个"打鼹鼠"游戏作为我的任务。但为了让游戏尽可能完美地运行,我需要让我的鼹鼠纹理的矩形随着鼹鼠从洞中向上移动而增长。我还需要让矩形缩小当摩尔在整体上移动的时候。现在你可以在洞下面的痣还没到的时候打到它。我使用monogame作为代码。
对于所有的家庭作业问题,我都在犹豫是否要给出一个完整的答案。请试着理解代码并按照自己的风格重写。
下面是使用状态机的一个可能的解决方案:
//Class level variables
Vector2 AdjustStepSize;
// Place existing large mole Rectangle values:
Vector2 BasePosition; // left and top
Vector2 BaseSize; // width and height
float HoldTimerLength = 30; // Number of steps to show before shrink
int GrowSpeed = 5; //Number of steps to grow/shrink. Adjust as needed
int GrowStep = 0;
float HoldTimer = 0;
int State = 0; // 0 = Do nothing, 1 Grow, 2 wait for holdtimer, 3 shrink
Rectangle DrawRectangle;
// Place the following code in the constructor
AdjustStepSize = BaseSize / (GrowSpeed << 2);
//in Update:
if (State == 2)
{
//Do collision testing
}
switch(State)
{
case 0:
break;
case 1:
if (++GrowStep == GrowSpeed) State = 2;
break;
case 2:
if (--HoldTimer == 0) State = 3;
break;
case 3:
if (--GrowStep == 0) State = 0;
break;
}
var tmp = (AdjustStepSize * GrowStep + BasePosition);
DrawRectangle.Location = new Point(tmp.X,tmp.Y);
tmp = (BaseSize - AdjustStepSize * GrowStep);
DrawRectangle.Size = new Point(tmp.X,tmp.Y);
// new method, Called when the mole should pop-up
public void Pop()
{
HoldTimer = HoldTimerLength; // reset timer
State = 1;
}
// draw using DrawRectangle
未注明出处的复制和粘贴将违反"SA">