Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can any expert programmers help me?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
in my game i want a target to be moving from side to side, but at the same time I want the player to be able to move the gun from side to side... how can I make it so both of these things can happen at once? Please someone help me if this is possible. Thanks! *By the way I'm a beginner programmer, so if possible could you explain the answer to this problem in terms that a semi-beginner would understand? Thanks again!*
 
By the way, here's the main input loop where the input is read from the user. How can i make this loop, and another for loop run at the same time?

while(1)
{
INPUT_RECORD InputRecord;
DWORD Events=0;

ReadConsoleInput(hInput,&InputRecord,1,&Events);

if(InputRecord.EventType == KEY_EVENT && InputRecord.Event.KeyEvent.bKeyDown)
{
if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_LEFT)
{
SetConsoleCursorPosition(hOutput, Player.position);
printf(" ");
Player.position.X--;
SetConsoleCursorPosition(hOutput, Player.position);
printf("%c", 1);
SetConsoleCursorPosition(hOutput, Player.position);
}
else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
{
SetConsoleCursorPosition(hOutput, Player.position);
printf(" ");
Player.position.X++;
SetConsoleCursorPosition(hOutput, Player.position);
printf("%c", 1);
SetConsoleCursorPosition(hOutput, Player.position);
}
else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT)
{

Xy.gun.X = Player.position.X;
Xy.gun.Y = Player.position.Y;
Xy.erase.Y = Xy.gun.Y;
Xy.erase.X = Xy.gun.X;
Xy.erase.Y++;
Xy.gun.Y++;
SetConsoleCursorPosition(hOutput, Xy.gun);

for(Xy.gun; Xy.gun.Y < Xy.home.Y; Xy.gun.Y++)
{
Sleep(7);
SetConsoleCursorPosition(hOutput, Xy.gun);
printf(&quot;.&quot;);
}

for(Xy.erase;Xy.erase.Y < Xy.home.Y; Xy.erase.Y++)
{
Sleep(7);
SetConsoleCursorPosition(hOutput, Xy.erase);
printf(&quot; &quot;);
}

}


}
 
Hi Ben ...
jus a passing suggestion

as such theres no way that u can move two objects simultaneously. but u can simulate the idea.

try using threads.

ciao
mystery !?

 
As mystery8 said, try using threads. Looks like you are using VC++. Remember to change your C++ Code Generation settings to Debug Multithread for the debug version and Multithread for the Release version.

Look up _afxbeginthread and _afxendthread in help or MSDN.
 
A thread is basically a process or program. When you run two threads in a program, you're essentially running two pieces of code at once. (What really happens is the operating system alternates between the two sets of code very quickly so it looks like they're going at the same time.)

That said, I don't think threads are a good way to implement what you're doing. First, they're a little too advanced for a beginner. Using threads means you have to make your code thread-safe and all kinds of other fun stuff. Second, you usually don't use threads for something like a game; they just overcomplicate the matter.

What's normally done in your case is to alternate between your game events on your own, from within your program. (Basically the same thing the OS would be doing for you if you were using threads.) For example, you'd have code like:

[tt]
int code;

while ( 1 )
{
code = figureOutWhatPlayerIsDoing();
performPlayerAction( code );

code = figureOutWhatTargetIsDoing();
performTargetAction( code );
}[/tt]


where each &quot;Action&quot; function would do something small like move the player or target 1 or 2 pixels in the correct direction.


Of course, that's if you only have two objects. If you plan to use more, you'll probably find it more convenient to put your functions in an array and just iterate through them like so:

[tt]

#define OBJECTS 24 /* just an example */

/* 24 functions that return an int and take void */
int ( *figureOutWhatStuffIsDoing[ OBJECTS ] )( void );

/* 24 functions that return void and take an int */
void ( *performAction[ OBJECTS ] )( int );

int code;
int i = 0;

/* initialize those arrays somehow */
...

while( 1 )
{
/* call each element of your function arrays */
code = figureOutWhatStuffIsDoing[ i ]();
performAction[ i ]( code );

i++;
i %= OBJECTS
}[/tt]


Yeah, arrays of functions are messy. Don't worry if you don't see how that syntax works; neither do I. In fact, I wouldn't be surprised if I made a mistake declaring them, so someone check me on that.

Hopefully this should get you started on what you're working on. Once you progress to more complex games, you'll probably want to do something different, in which case look at some more advanced game-programming books or tutorials.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top