【问题标题】:How do I shrink and grow a rectangle in monogame?如何在monogame中缩小和增长一个矩形?
【发布时间】:2021-11-25 05:53:02
【问题描述】:

现在我正在创建一个 wack-a-mole 游戏作为我的任务。但是为了使游戏功能尽可能完美,我需要使我的痣纹理的矩形随着痣从孔中向上移动而增长。我还需要做到这一点,以便当痣整体向后移动时矩形会缩小。现在,当它没有完全向上时,您可以击中孔下方的痣。我正在使用 monogame 作为代码。

【问题讨论】:

    标签: monogame rectangles


    【解决方案1】:

    与所有作业问题一样,我不愿给出完整的答案。请尝试理解代码并以自己的方式重写。

    这是一个使用状态机的可能解决方案:

    //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
    

    在不注明出处的情况下复制和粘贴将违反license 的“SA”条款。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 2017-07-12
      • 2011-04-19
      • 1970-01-01
      • 2021-06-03
      • 1970-01-01
      • 2013-04-25
      相关资源
      最近更新 更多