loop problem

Hi everyone,I have a question to you. I have a robot class and I am trying to write program that based on this class. Through this class, I can create little robots in some coordinates.In this program I want to make a game like pacman.(but basically) I have a user robot and monster robots. I can control my user robot with arrows and my monsters mowe random directions but I couldn't move my user and monster robots simultaneously.How can I move them at same time in my program?

you can look my code;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
int main()
{
	Robot r(5,5,west,0);
	r.SetColor(orange);
	Robot mons1(0,0,north);
	mons1.SetColor(red);
	Robot mons2(9,9,south);
	mons2.SetColor(red);
	int n;
	RandGen gen;
	n = gen.RandInt(1,9);

	string message = "Press arrow keys to control robot.\n";
	message = message

		+ "-Right arrow	: Move towards east\n"
		+ "- Left arrow	: Move towards west\n"
		+ "- Up arrow	: Move towards north\n"
		+ "- Down arrow	: Move towards south\n"
		+ "- Insert		: Insert thing in bag\n"
		+ "- Delete		: Remove thing from bag\n";

	ShowMessage (message);
	
	//DisplayThingCount(r,r2);
				
	while (r.isAlive()== true && mons2.isAlive()== true 
					&& mons1.isAlive()== true)
	    {	
		
		if (IsPressed(keyRightArrow))
		{				
			r.Turn(east);
			r.Move();
		}
		else if (IsPressed(keyLeftArrow))
		{
			r.Turn(west);
			r.Move();
		}
		else if (IsPressed(keyUpArrow))
		{
			r.Turn(north);
			r.Move();
		}
		else if (IsPressed(keyDownArrow))
		{
			r.Turn(south);		
			r.Move();
		}
		else if (IsPressed(keyShift))
		{
			r.PickThing ();
		}
		else if (IsPressed(keyDelete))
		{
			r.PutThing();
		}
		
		mons2.MoveDiagonal(n);
		}
	//DisplayThingCount(r,r2);
	return 0;
}
Your program can only do one thing at a time.

You can only make it appear that the robots move at the same time.
From what you've posted, I guessing this is a text mode console game.

To make it appear that the robots move at the same time, you only want to redisplay the game map after you've input the user's move, then calculated both the monster's moves.

1
2
3
4
5
6
7
  while (! game_over)
  { player.accept_input (); 
     player.move_robot ();
     mons1.calculate_move (); 
     mons2.calcuate_move ();
     display_map (); 
  }


Since the map is only displayed at the end of the loop, it appears that the player and the monsters have all moved "at the same time".


Topic archived. No new replies allowed.