Иван Божков
Иван Божков

Reputation: 264

C++ parallel loops

I'm making a console game called alien spaceships as a homework. It should look something like this http://img74.imageshack.us/img74/8362/alieninvadersfdcl192720mu1.jpg . So far so good I ain't allowed to use classes nor objects => only functions and arrays. I have one while loop that checks the buttons I press on the keyboard and according to the button applies some functions. The problem comes when I try to shoot a missle because it's done with a "for" loop and when I shoot I can't move. Can someone give me an idea how the model is supposed to look like and how can I make something like this work. I don't think it's needed to post my code, but if you want I'll post it.

Upvotes: 1

Views: 391

Answers (2)

Pandrei
Pandrei

Reputation: 4951

I assume you use a matrix to store all the data, and periodically you print the content of the matrix (that's how you create a console game). So, your code should look something like this:

render()
{
   update_position(x,y);
   if(missile_fired)
       update_missile_position();
}

main()
{
for(;;)
{
  read_input(&x,&y);
  render();
  draw_image();
}
}

Upvotes: 0

Johan
Johan

Reputation: 3778

I assume that you're not willing to play with multiple threads. It is not mandatory for a simple game like this and would add a bit of complexity.

So generic loop for monothreaded game is:

state new_state = createInitialState();
do
{
  input = readInput(); // non blocking !
  new_state = modifyState(input, new_state);
  updateScreen(new_state);
}
while (!exitCondition(input));

None of these functions should loop for long.

In your case, the missile position should be updated in modifyState taking into account the time since the last modifyState.

Upvotes: 1

Related Questions