C#如何添加暂停功能? [英] C# How do I add Pause function?
本文介绍了C#如何添加暂停功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的密钥输入:
Here is my Key Input:
if (Input.KeyPressed(Keys.P))
{
gameTimer.Stop();
string paused = "Paused\n Press Enter to continue";
lblGameOver.Text = paused;
lblGameOver.Visible = true;
}
else
{
if (Input.KeyPressed(Keys.Enter))
gameTimer.Start();
lblGameOver.Visible = false;
}
我在哪里错过了?游戏进入Pause,但不会恢复...
Where did I miss? The game goes to Pause, but won`t resume...
推荐答案
使用计时器进行游戏的整个想法并不是最好的。较新的方法是使用window.requestAnimationFrame
。请参阅:
http://paulirish.com/2011/requestanimationframe-for-smart -animating [ ^ ]。
请注意在代码示例中使用回退到window.setTimeout
。
暂停游戏非常简单。您只需为暂停/运行状态创建一些标志变量,并在游戏暂停时不处理帧。你可以看到完整的代码示例(对不起,它不是真的很小,是一个完全成熟的游戏,但发现技术类似,请参阅代码中的states.paused
),例如,在这里:画布上的俄罗斯方块 [ ^ ]。
-SA
The whole idea of using the timer for gaming is not the best. The newer approach is usingwindow.requestAnimationFrame
. Please see:
http://paulirish.com/2011/requestanimationframe-for-smart-animating[^].
Note the use of the fallback towindow.setTimeout
in the code sample.
The pausing of the game is pretty much trivial. You simply create some flag variable for paused/running state and don't process frames when the game is paused. You can see the complete code sample (sorry, it's not really minimal, is a fully-fledged game, but finding the technique is similar, please seestates.paused
in the code), for example, here: Tetris on Canvas[^].
—SA
我自己想出来了:
I figured it out by myself:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Input.ChangeState(e.KeyCode, true);
if (Input.KeyPressed(Keys.P))
{
gameTimer.Stop();
}
if (Input.KeyPressed(Keys.Enter))
{
gameTimer.Start();
}
}
必须在Keydown下
Had to be under Keydown
这篇关于C#如何添加暂停功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文