简体   繁体   中英

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.

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.

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". 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. Room objects can be "viewed" by using the static RoomViewer class which is accessed by the main Console class.

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).

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. In your case, you may want to use a Timer , which handles a background thread for you automatically.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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