0

I'm currently working on a mini game project, and I have a problem with this specific mechanic:

void monsterMove(){
   //monster moves randomly
}

void playerMove(){
   //accepting input as player movement using W, A, S, D
}

However, the project requires the monsters to keep moving at all times, even when the player is not moving.

After some research, I figured out that multithreading is needed to implement this mech since both monsterMove() and playerMove() needs to run concurrently even when playerMove() hasn't received any input from user.

Specifically there are 2 questions I want to address:

  1. Which function needs to be made as a thread?
  2. How to build the thread?

Sadly, I found no resource with specifically this type of question because everything on the Internet just seem to point out how multithreading can work as parallelism in a way, but not in such implementations.

I was thinking that monsterMove() will be run recursively while playerMove() will be made as the thread, since monsterMove() will need to run every n seconds even when playerMove() thread is not finished yet (no input yet). Although I might be wrong for the most part.

Thank you in advanced!

(P.S. Just to avoid misunderstandings, I am specifically asking about how thread and multithreading works, not the logic of the mech.)

Edit: Program is now working! However, any answer/code related to how this program is done with multithreading is immensely appreciated. :)

11
  • 3
    What is your platform? Anyway, you don't need multithreading for this. Commented Jun 11, 2022 at 8:26
  • 1
    @bolakecil A non-threaded version would just scan the keyboard in a non-blocking fashion Commented Jun 11, 2022 at 8:35
  • 2
    IDK why your mentor is dead set on multithreading, this is the typical situation that is solved in a single thread using a "game loop" that runs at a fixed rate - at each iteration input is checked in a non-blocking fashion, state is updated (or not) accordingly, all components actions are evaluated and finally a new paint is done. It's also similar to how GUIs work anyhow (it's not like there's a thread for each widget on screen to keep it updating even if the user doesn't interact). Commented Jun 11, 2022 at 8:41
  • 1
    @bolakecil Everything except the implementation is available in Jabberwocky's answer below. if(_kbhit()) { char ch = _getch(); ... } Commented Jun 11, 2022 at 9:14
  • 2
    I wish teachers would make it clear if the intention of some homework is to use a certain mechanism like multithreading (no matter how usefull/useless that is) or just to solve the task in an efficient way. Commented Jun 11, 2022 at 11:20

1 Answer 1

2

You don't need multithreading for this:

The basic structure is depicted in this pseudocode:

while (1)
{
   check if input is available using kbhit

   if (input available)
     read user input using getch

   move player depending on user input
   move monsters
}

You could use multithreading using the CreatdThread function, but your code will just become overly complex.

Sign up to request clarification or add additional context in comments.

3 Comments

Ah! I might be not specific enough in this, pardon me. With the code above, the monsters won't move until any input is accepted, correct? I need the monsters to move even when the user hasn't inputted anything, just like how the ghosts will move in Pacman game without us moving.
@bolakecil no, if you had read the documentation of kbhit you would know that this function is non blocking. I made the answer more clear
AH! Thank you. After analyzing your answer, it seems to me that I put monsterMove() in the wrong order, hence bugging. Now it's giving my desired output :) Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.