Java 2D 游戏 - 计时器与线程?

发布于 2024-12-10 09:54:56 字数 303 浏览 0 评论 0原文

大家好,我正在制作我的第一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

歌入人心 2024-12-17 09:54:56

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.

生活了然无味 2024-12-17 09:54:56

这取决于游戏的类型。

对于“快速游戏”(例如射击游戏),具有屈服/等待(视情况而定)的专用游戏循环(可能在线程中)通常会发挥最佳作用。对于“慢速游戏”,计时器可以正常工作 - 然而,有两种类型的计时器:

  1. “Swing”计时器(javax.swing.Timer)
    1. 优点:在 EDT 上运行,因此可以触摸 Swing/AWT 对象
    2. 缺点:在 EDT 上运行,因此分辨率和时序保证较差
  2. “服务”计时器 (java.util.Timer)
    1. 优点:
      1. 支持“固定速率”计时器
      2. 在自己的线程上运行(因此分辨率/一致性更好,但不能保证)
    2. 缺点:不能在 EDT 上运行(因此无法接触 Swing/AWT)。

许多情况中,游戏仍然必须计算最后一个事件发生时间与当前事件发生时间之间的“增量”,并在计算中使用它。如果没有使用增量的模型,游戏速度会因系统而异。

就我个人而言,我会推荐 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:

  1. a "Swing" timer (javax.swing.Timer)
    1. advantage: runs on the EDT so can touch Swing/AWT objects
    2. disadvantage: runs on the EDT so has poor resolution and timing guarantees
  2. a "service" timer (java.util.Timer)
    1. advantages:
      1. supports "fixed rate" timer
      2. runs on own thread (so resolution/consistency is better, but not guaranteed)
    2. disadvantages: does not run on the EDT (so can't touch Swing/AWT).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文