Java 2D 游戏 - 计时器与线程?
大家好,我正在制作我的第一个 2D Java 游戏,目前我正在使用计时器来控制和更新一切。我想知道哪种方法最适合 2D Java 游戏,计时器还是线程?目前我的 gameCanvas 类中有一个计时器,它渲染/绘制所有内容以及检查碰撞。然后我还为我的玩家设置了一个计时器,用于移动他。
我使用计时器的唯一原因是因为我是 Java 编程新手,考虑到我必须提出的所有内容,这似乎是当时最简单的方法:
Timer timer = new Timer(5,this);
timer.start();
你们会推荐什么?
Hey guys i'm making my first 2D Java game and I am currently using Timers to control and update everything. I was wondering what method was best for a 2D Java game, Timers or Threads? currently I have a Timer in my gameCanvas class which renders/draws everything aswell as checking collision. I Then also have a Timer for my player, which is used for moving him.
The only reason I used timers is because I'm new to Java Programming and it seemed the easiest way at the time considering all I had to put was this:
Timer timer = new Timer(5,this);
timer.start();
What would you guys recommend?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
pst 关于这两种方法的优缺点的说法是绝对正确的。
不过,我也不推荐。我会使用“专为游戏设计”的游戏引擎,例如 Slick2D,它将极大地帮助您游戏中的更新和逻辑、主游戏循环的结构,以及处理您将遇到的许多低级问题。
如果您正在考虑使用 Swing 来玩游戏,我也会远离它,原因如下 在我的回答中。
Slick2D 是预先构建的,用于处理计时问题,并计算渲染帧之间的增量等。还可以获得很多您不必自己构建的好东西,例如输入处理(键盘、鼠标、控制器)、加速图形(您的游戏将自动受益于图形加速,因为引擎会照顾到这一点)你),以及当您准备好使用音频时,它还有一个很棒的音频库。
自己写这些东西会让你想哭。除非您真的热衷于构建引擎本身(而不是想花时间构建游戏逻辑本身),否则当您可以使用现成的成熟工具时,构建引擎会花费大量时间。
What pst says about the pros and cons of the two approaches is absolutely correct.
However, I wouldn't recommend either. I would use a "designed for games" game engine such as Slick2D, and it will help you immensely with the update and logic in a game, the structure of the main game loop, as well at take care of a lot of the low-level issues you'll run into.
If you're considering using Swing for games, I would also stay far away from that, for the reasons outlined in my answer here.
Slick2D comes pre-built to handle the timing issues, and figuring out the deltas between rendering frames, etc. You also get a lot of nice stuff that you don't have to build on your own, such as input handling (keyboard, mouse, controllers), accelerated graphics (your games will automatically benefit from graphics acceleration because the engine takes care of this for you), and when you get ready for audio it also has a great audio library.
Writing all this stuff yourself is going to make you want to cry. Unless you're really into building the engine itself (as opposed to wanting to spend your time building the game logic itself), building engines is a lot of time spent when you could just use a mature tool off-the-shelf.
这取决于游戏的类型。
对于“快速游戏”(例如射击游戏),具有屈服/等待(视情况而定)的专用游戏循环(可能在线程中)通常会发挥最佳作用。对于“慢速游戏”,计时器可以正常工作 - 然而,有两种类型的计时器:
在许多情况中,游戏仍然必须计算最后一个事件发生时间与当前事件发生时间之间的“增量”,并在计算中使用它。如果没有使用增量的模型,游戏速度会因系统而异。
就我个人而言,我会推荐 LWJGL:轻量级 Java 游戏库,它提供了非常适合“快速游戏”循环的高分辨率时钟。它还支持“直接”OpenGL 绑定;-)
我没有任何开发“慢速游戏”的经验。
快乐编码。
It depends on the type of game.
For a "fast game" (e.g. a shooter) a dedicate game loop (perhaps in a thread) with a yield/wait (as appropriate) will generally function best. For a "slow games" timers will work just fine -- there are however, two types of Timers:
In many cases the game must still compute the "delta" between when the last event occured and when the current event occurred and use this in calculations. Without a model that uses a delta, game speed will vary between systems.
Personally, I would recommend LWJGL: Lightweight Java Game Library which offers a high-resolution clock well suited for "fast game" loops. It also supports "direct" OpenGL bindings ;-)
I do not have any experience developing "slow games".
Happy coding.