简体   繁体   English

当用户与代码的不同部分进行交互时,我是否需要一个线程来实时跟踪某些内容?

[英]Do I need a thread to track something in real time while the user interacts with a different part of the code?

I am making an open world text based zombie survival RPG in Java and I want to be able to have real time events in the game such as having the player spam space as a zombie is running after the player, until the player reaches some sort of checkpoint such as a door to a house or something. 我正在用Java开发一个基于开放世界的基于文本的僵尸生存RPG,我希望能够在游戏中具有实时事件,例如在僵尸追赶玩家之前让玩家拥有垃圾邮件空间,直到玩家达到某种程度。检查点,例如房屋门或其他东西。

For starters, I wanted to allow the player to control power in buildings by turning on generators which take fuel to power. 对于初学者,我想允许玩家通过打开将燃料作为动力的发电机来控制建筑物的动力。 I wanted the fuel to run out after about 7 game days, which would each be 6 real time hours long. 我希望燃料在大约7个游戏日后用完,每个游戏日需要6个实时小时。

I have never used a thread, so I'm not really sure if that's what I need to use, but if it is can somebody please walk me through how to program my example? 我从未使用过线程,因此我不确定是否需要使用该线程,但是如果有人可以,请引导我逐步了解如何编写示例程序? If not then can somebody please tell me how I can do this? 如果没有,可以告诉我我该怎么做吗?

Thanks! 谢谢! PS Some people tell me that they don't understand what I mean by "text based game". PS有些人告诉我,他们不理解“基于文本的游戏”的意思。 It's a game with no graphics besides words and sometimes a character-filled map of nearby surroundings 这是一款除了文字外没有其他图形的游戏,有时甚至是周围周围人物的地图

---- Edit ---- The WorldObject is an object in a game that can be placed into the ArrayList of a Room object. ----编辑---- WorldObject是游戏中的一个对象,可以放置到Room对象的ArrayList中。 Room objects can be "viewed" by using the static RoomViewer class which is accessed by the main Console class. 使用静态ConsoleViewer类可以“查看”房间对象,该类可以通过主Console类访问。

The Generator class extends the WorldObject class and it should have some additional methods which return how much fuel it currently has out of 42 points total (I can do this myself), Resupply the fuel in increments (I can do this myself), and loose 1 point every real time hour (I need help with this part). Generator类扩展了WorldObject类,它应该具有一些其他方法,这些方法可以返回当前总共42点中有多少燃料(我可以自己做),以增量方式重新提供燃料(我可以自己做)和松散每个实时小时1点(我需要此部分的帮助)。

Yes. 是。 I can't give a detailed answer without more details about the code, but any time that you need processing to happen in a timeline independent of UI code, you'll need that processing to run in its own thread. 没有更多有关代码的详细信息,我无法给出详细的答案,但是,每当您需要在独立于UI代码的时间轴上进行处理时,都需要该处理在其自己的线程中运行。 In your case, you may want to use a Timer , which handles a background thread for you automatically. 在您的情况下,您可能需要使用Timer ,它可以自动为您处理后台线程。

No, you don't need to use a thread based solution. 不,您不需要使用基于线程的解决方案。 But it is a possible way to implement this. 但这是实现此目的的一种可能方法。 You should first learn the basics so you can make an educated decision if this is the best solution for your specific situation. 您首先应该学习基础知识,以便根据自己的具体情况确定最佳解决方案,从而做出明智的决定。

For a game you typically don't need to use different threads. 对于游戏,您通常不需要使用其他线程。 Most games run in a main game loop that does everything that is needed in the game, eg capture input events (check the keys currently pressed), manipulate world state, process KI moves, process user actions, draw everything. 大多数游戏在主游戏循环中运行,该循环完成游戏所需的一切,例如捕获输入事件(检查当前按下的键),操纵世界状态,处理KI动作,处理用户动作,绘制所有内容。 The main game loop would run in small time units that you could call ticks. 主游戏循环将以较小的时间单位运行,您可以将其称为“滴答”。 You would manipulate every game object by a certain amount for every tick. 您将为每个刻度操纵每个游戏对象一定数量。 For example you would iterate in a phase of the loop over all your generators and reduce the amount of fuel left by a certain amount for every running generator. 例如,您将在所有发电机上循环进行一个阶段,并为每台正在运行的发电机减少一定量的燃料。

If the ticks represent a time span small enough it looks like real time - in fact that wouldn't be different in a thread based solution, you would just split the work. 如果滴答声代表的时间跨度足够小,则它看起来像实时的-实际上,在基于线程的解决方案中这不会有所不同,您只需拆分工作即可。 Unless you need to do computational complex work in parallel there is no use of this - the threads would sleep most of the time. 除非您需要并行执行复杂的计算工作,否则将不会使用此方法-线程大部分时间都处于休眠状态。

It is much easier to implement and to control the game world with a main loop solution. 使用主循环解决方案可以更轻松地实现和控制游戏世界。 There is typically no need to use a thread (or agent) based solution in a scenario like the one you described. 在您所描述的场景中,通常不需要使用基于线程(或代理)的解决方案。

If it is text based you probably wouldn't even need (implicit) threads for animations or event processing. 如果它是基于文本的,那么您甚至可能甚至不需要(隐式)线程来进行动画或事件处理。 But still, to make a decision for your specific case you need to know the tools first. 但是,要针对您的具体情况做出决定,您需要首先了解这些工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM