【发布时间】:2009-11-13 08:52:55
【问题描述】:
问题来了:
我一直在做一个小游戏,怪物从主窗体的墙壁(边缘)反弹,它会游泳,但是当它应该迭代一个列表时,它只绘制每种类型的怪物他们每个人都调用他们的 OnPaint 和 Move 方法:
private void Pacmen_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle rect = e.ClipRectangle;
g.Clear(backgroundColor);
foreach (Hydra h in hydraList) {
h.OnPaint(e);
h.Move(e);
} // end foreach
foreach (Ghost gh in ghostList) {
gh.OnPaint(e);
gh.Move(e);
} // end foreach
}
这是鬼的方法:
public void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.HighQuality;
GraphicsPath path = new GraphicsPath();
SolidBrush fillBrush = new SolidBrush(color);
SolidBrush eyeBrush = new SolidBrush(Color.Black);
path.AddArc(pos, (float)180, (float)180);
path.AddLine((float)pos.Right, (float)(pos.Y + pos.Height / 2),
(float)pos.Right, (float)pos.Bottom);
path.AddLine((float)pos.Right, (float)pos.Bottom,
(float)(pos.X + pos.Width / 2), (float)(pos.Bottom - radius / 2));
path.AddLine((float)(pos.X + pos.Width / 2), (float)(pos.Bottom - radius / 2),
(float)pos.Left, (float)pos.Bottom);
path.AddLine((float)pos.Left, (float)pos.Bottom,
(float)pos.Left, (float)(pos.Y + pos.Height / 2));
g.FillPath(fillBrush, path);
g.FillEllipse(eyeBrush, new Rectangle(pos.X + pos.Width / 4, pos.Y + pos.Height / 4, radius / 4, radius / 5));
g.FillEllipse(eyeBrush, new Rectangle(pos.X + 3 * pos.Width / 4, pos.Y + pos.Height / 4, radius / 4, radius / 5));
} // end OnPaint
public void Move(PaintEventArgs e)
{
pos.Offset(xSpeed, ySpeed);
}
知道为什么只有一个会出现吗?谢谢!
【问题讨论】:
-
我认为这不值得回答,但您确实应该在调用 OnPaint 之间缓存 SolidBrushes - 将它们存储在类中的某个地方。他们也确实需要处理。
-
...而且您也不应该一遍又一遍地重新创建路径...