C# 游戏循环和输入
我正在尝试为游戏制作一个简单的可重用引擎,例如乒乓球。我在时间安排和输入方面遇到了麻烦。
例如,使用 XNA 游戏类,更新方法每秒触发 60 次。为了在我从头开始制作的课程中做到这一点,我会使用计时器还是完全不同的东西?
为了澄清我的输入问题,我如何检测按键事件(例如:按键被按下)?我认为这与代表有关,但我不太确定。
编辑:我没有使用 XNA。
I am trying to make a simple reusable engine for a game, such as pong. I am having trouble with the timing and inputs.
For instance, using the XNA Game Class, the Update Method is fired 60 times per second. To do that in a class that I make from scratch, would I use timers, or something entirely different?
To clarify on my trouble with inputs, how would I detect a Key event (EX: Key is pressed down)? I think that it has to do with delegates, but I'm not really sure.
EDIT: I am not using XNA.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想要使用带有回调的计时器,甚至是完全是游戏循环的第二个线程。我已经用 Java 和 C# 完成了它;我倾向于使用计时器,因为毫秒分辨率对于大多数游戏来说已经足够了。如果您想要查看一些(Java)示例代码,请查看我的基本游戏引擎。至于游戏输入......好吧,查看示例代码。它管理事件驱动的输入并将其封装,以便可以轮询数据。 :P
或者,查看 MonoXNA。它还没有 100% 完成,但 2D 部分和基本游戏内容(游戏循环、输入等)已经完成。
You may want to use timers with a callback, or even a second thread that is entirely a game loop. I've done it in both Java and C#; I tend to use timers since millisecond resolution is good enough for most games. If you want some (Java) sample code to look at, check out my rudimentary game engine. As for game input... well, check out the sample code. It manages event-driven input and encapsulates it so that the data can be polled. :P
Alternatively, look into MonoXNA. It isn't 100% complete yet, but the 2D parts and basic game stuff (game loop, input, etc) are.
您真正需要做的只是在更新方法中检查键盘输入。或者,您可以创建一个可在更新方法中调用的 updateInput 方法。
本质上就是这样!当然,这是一种基本方法,由于您正在考虑制作可重用引擎,因此您应该查看 GameComponent 类。在 XNA 中,您可以制作可在不同游戏项目中重复使用的组件。
希望这有帮助!
All you really have to do is just check for keyboard input in your update method. Or, you can create an updateInput method that can be called in your update method.
That's essentially it! Of course, this is a rudimentary approach and since you are looking at making a reusable engine, you should look into the GameComponent class. In XNA, you make components that can be reused throughout different game projects.
Hope this helps!