• Advertisement
Sign in to follow this  

Game Loop Implementation on Windows Forms C#

This topic is 1681 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys,

 

I need help. 

 

I am designing a two player card game in C#, but i have a problem figuring out the following:

  1. The Main Game Loop (UI thread or a sperate thread) 
  2. Human Player and AI player should they be on different threads?

I am going to run experiments with different pairings at the end of the game development.

 

my thoughts are below: 

 

MainGameLoop() 
//player A or B ()
//display card played ()
//check the scores ()
}
 
AI_Player()
{
//Constant thread runing to identify if its AI's turn to play.
}
 
HumanPlayer()
{
   //A click event from the UI form.
}

 

Share this post


Link to post
Share on other sites
Advertisement

Hi,

 

As you mentioned, you try to figure out the right way. In your case, I would definitely suggest you, to implement the following model:

 

1) Main-Loop which is responsible for the main execution

b) Updates Game UI, Updates Game Graphics, Updates Game States

 

2) The question about the seperation of threads concerning your players is not that easy to answer...

 

Is it necessary, that they act and react at the same time, simultaneously or every of the player turn by turn? If they act turn by turn, one thread should enough.. this  thread can handle manual (human actions) and automatic (AI actions).

 

Does this help you?

Share this post


Link to post
Share on other sites

Thanks Mario, 

 

Well it does answer and gives me a different look at my problem. 

 

  • But does that mean the main execution loop will be the UI thread, 

          because as i said ealier i will take picture box click event responses from the UI.

 

  • My players will act turn by turn (where will this player turn switch be in the main executioin loop ? ) 

Share this post


Link to post
Share on other sites

Hi,

 

I'm glad, that I was able to bring you to another view of your problem.

 

In Windows-Forms you primary have one Thread, which is the UI Thread. This thread normally is redrawing the forms and controls and every action concerning that should be implemented within the OnPaint-Methods of your components. Therefore, all your actions, which are not about the UI should be seperated into an extra thread, e.g. this could be a thread you instantiate manually for the human player, and e.g. a Background-Worker thread which you could use for the AI-player.

 

In fact, It's up to you, how you get these things done. I think an easy and efficient way to solve your problem could be this one:

 

1) Subclass a Windows-Form or your Picture-Box, you probably need for implementing the custom drawing

2) Implement Event-Handlers for the Picture-Box and Event-Receivers for your class which handles a Player

3) Subclass the Player to be a human player

4) Subclass the Player to be an AI-player (and if you like, include Thread-Handling in the way of a background worker there)

 

I hope this brings you a little further :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement