毫无疑问,实现这个旋转的正方形只涉及了XNA中的基础内容。现在对项目进行简单的修改从而使显示出来的图形更加生动且更富吸引力。

按代码所示来修改Draw函数的代码,将会看到绘制到屏幕上的图形发生了重大的变化

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);
    // Reset the world matrix
    _effect.World = Matrix.Identity;
    // Loop for each square
    for (int i = 0; i < 20; i++)
    {
        foreach (EffectPass pass in _effect.CurrentTechnique.Passes)
        {
            // Apply a further rotation
            _effect.World = Matrix.CreateRotationZ(_angle) * _effect.World;
            // Scale the object so that it is shown slightly smaller
            _effect.World = Matrix.CreateScale(0.85f) * _effect.World;
            // Apply the pass
            pass.Apply();
            // Draw the square
            GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, _vertices, 0, 2);
        }
    }
    base.Draw(gameTime);
}

XNA开发—增添一些亮点

遗憾屏幕截图无法反应出项目中真实的运行效果;虽然动态内容肯定要比静止的图像好的多,但这种使用短小代码块来生成效果的方式非常好。

在循环中所做的就是绘制20个形状,每一个都比上一个略小并旋转到不同的角度。缩放和旋转操作是累计的,这意味着第一个(最大的)正方形以指定的角度_angle进行旋转,则第二个正方形就会以该角度的两倍进行旋转,第三个则以三倍角度旋转,以此类推。

相关文章:

  • 2022-02-01
  • 2021-10-17
  • 2021-09-28
  • 2021-10-03
  • 2022-12-23
  • 2021-11-29
  • 2021-07-18
  • 2022-01-03
猜你喜欢
  • 2021-11-23
  • 2021-09-08
  • 2022-12-23
  • 2021-08-27
  • 2021-06-11
  • 2021-07-02
相关资源
相似解决方案