我现在在Windows窗体中制作Pong游戏,而我渲染图形的方式是每100毫秒从一个任务绘制一个位图,然后使表单无效以强制它重新绘制位图,它按预期工作,除了表单将"闪烁"每次我重新绘制位图时,有没有办法让它像这样闪烁?
以下是代码:
void IGameView.Draw(Ball ball, Paddle paddle, Paddle paddle2)
{
//Draw to bitmap
Graphics gameObj = Graphics.FromImage(gameObjects);
BackColor = Color.Black; // Clear area to allow redrawing of all game objects
Pen pen = new Pen(Color.White, 5);
gameObj.DrawEllipse(pen, new Rectangle(ball.Point, new Size(10, 10))); // Ball
gameObj.DrawLine(pen, new Point(Size.Width / 2, 0), new Point(Size.Width / 2, Size.Height)); // Net
gameObj.DrawLine(pen, paddle.Position, new Point(paddle.Position.X, paddle.Position.Y + paddle.Size.Height));
gameObj.DrawLine(pen, paddle2.Position, new Point(paddle2.Position.X, paddle2.Position.Y + paddle2.Size.Height));
pen.Dispose();
gameObj.Dispose();
Invalidate(); // Invalidate to force redraw of game objects
}
private void GameForm_Paint(object sender, PaintEventArgs e)
{
using (Graphics graphics = e.Graphics)
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(() => GameForm_Paint(this, e)));
}
else
{
graphics.DrawImage(gameObjects, new Point(0, 0));
}
}
}