【发布时间】:2012-10-15 01:30:32
【问题描述】:
我刚开始学习 .NET,所以这很可能是一个大错误:
我正在尝试制作一个简单的乒乓球游戏,使用表格然后 System::Drawing::Graphics 类将游戏绘制到窗体。
我的代码的重要部分如下所示:
(主游戏循环):
void updateGame()
{
//Update Game Elements
(Update Paddle And Ball) (Ex. paddle.x += paddleDirection;)
//Draw Game Elements
//Double Buffer Image, Get Graphics
Bitmap dbImage = new Bitmap(800, 600);
Graphics g = Graphics::FromImage(dbImage);
//Draw Background
g.FillRectangle(Brushes::White, 0, 0, 800, 600);
//Draw Paddle & Ball
g.FillRectangle(Brushes::Black, paddle);
g.FillRectangle(Brushes::Red, ball);
//Dispose Graphics
g.Dispose();
//Draw Double Buffer Image To Form
g = form.CreateGraphics();
g.DrawImage(dbImage, 0, 0);
g.Dispose();
//Sleep
Thread.sleep(15);
//Continue Or Exit
if(contineGame())
{
updateGame();
}
else
{
exitGame();
}
}
(表单初始化代码)
void InitForm()
{
form = new Form();
form.Text = "Pong"
form.Size = new Size(800, 600);
form.FormBorderStyle = FormBorderStyle::Fixed3D;
form.StartLocation = StartLocation::CenterScreen;
Application::Run(form);
}
PS。这不是确切的代码,我只是从记忆中写出来的,所以这就是任何错别字或 错误的名称,或者一些与初始化表单有关的重要代码行。
这就是代码。 我的问题是游戏肯定不是每 15 毫秒更新一次(大约 60 fps),它的速度要慢得多,所以我必须做的是每次移动桨/球更远的距离以补偿它不更新很快,而且看起来很糟糕。
简而言之,在绘制图形时,游戏速度会大大降低。我感觉它与我的双重缓冲有关,但我无法摆脱它,因为这会产生一些令人讨厌的闪烁。我的问题是, 如何摆脱这种滞后?
【问题讨论】: