AppleSeeker's Tech Blog
Welcome to AppleSeeker's space

毫无疑问,实现这个旋转的正方形只涉及了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);
}

image

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

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

posted on 2011-07-01 21:14  AppleSeeker(冯峰)  阅读(557)  评论(0编辑  收藏  举报