我是XNA的新手,现在我正在使用以下代码绘制一个矩形:
// Calculate particle intensity
intense = (int)((float)part.Life / PARTICLES_MAX_LIFE);
// Generate pen for the particle
pen = new Pen(Color.FromArgb(intense * m_Color.R , intense * m_Color.G, intense * m_Color.B));
// Draw particle
g.DrawRectangle(pen, part.Position.X, part.Position.Y,
Math.Max(1,8 * part.Life / PARTICLES_MAX_LIFE),
Math.Max(1,8 * part.Life / PARTICLES_MAX_LIFE));
pen.Dispose();
Run Code Online (Sandbox Code Playgroud)
使用我在网上找到的颜色填充矩形的所有方法似乎都不适用于我绘制矩形的方式.我怎么能用颜色填充它?
Cyr*_*ral 10
您的代码似乎很可能是为GDI而不是XNA,因此它无法正常工作.
但是,XNA包含一个非常有用的Rectangle结构.
这意味着您可以"拉伸"图像以填充矩形,因此创建一个Texture2D1x1像素的新图像,并在绘制时拉伸尺寸以增加尺寸..(或者您可以加载一个)
Texture2D texture = new Texture2D(graphics, 1, 1, false, SurfaceFormat.Color);
texture.SetData<Color>(new Color[] { Color.White });
return texture;
Run Code Online (Sandbox Code Playgroud)
您可以将此小纹理与Rectangle基于重载的方法结合使用SpriteBatch
spriteBatch.Draw(texture, new Rectangle(X, Y, Width, Height), Color.White);
Run Code Online (Sandbox Code Playgroud)
更改Width,Height,位置和颜色根据自己的喜好.