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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I'm trying to use an event loop to simulate multi-tasking....

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I'm basically a beginner programmer, so this problem is kind of frustrating me, because i have no idea whats wrong. Whats going on here is I am using an event loop to simulate multi-tasking in a DOS game i'm creating. What the game consists of basically is a gun at the top that shoots at a target which I want to move near the bottom. All the functions are working with the event loop (the input reading function, the collision function) but the one that isn't working correctly is the moving target function. I want the target to move from side to side, however for some reason it is only moving when the user inputs a command. I want the target to be moving indefinitly from side to side, until the program ends. What is wrong with my code? If someone could help me, that would be great. Thanks!

...And now, the code:

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <ctype.h>

#define SCREEN_WIDTH 79
#define SCREEN_HEIGHT 24

void input(void);
void collision(void);
void targetmoveright(void);

typedef struct _XY{
COORD goal;
COORD gun;
COORD home;
COORD leftwall;
COORD rightwall;
COORD erase;
COORD setbackleft;
COORD setbackright;
}XY;

typedef struct _PLAYER{
COORD start;
COORD position;
}PLAYER;

XY Xy;
PLAYER Player;
int game=1;

HANDLE hOutput;
HANDLE hInput;

INPUT_RECORD InputRecord;
DWORD Events=0;

void main()
{
hInput=GetStdHandle(STD_INPUT_HANDLE);
hOutput=GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleMode(hInput,ENABLE_PROCESSED_INPUT);

Player.start.X=SCREEN_WIDTH/2;
Player.start.Y=2;

Player.position = Player.start;

Xy.goal.X = 25;
Xy.goal.Y = 10;

Xy.leftwall.X = 2;
Xy.leftwall.Y = 2;

Xy.rightwall.X = 76;
Xy.rightwall.Y = 2;

Xy.home.Y = 23;

Xy.setbackleft.X = 4;
Xy.setbackleft.Y = 2;

Xy.setbackright.X = 74;
Xy.setbackright.Y = 2;

SetConsoleCursorPosition(hOutput, Xy.goal);
printf(&quot;*&quot;);

SetConsoleCursorPosition(hOutput, Xy.leftwall);
printf(&quot;|&quot;);

SetConsoleCursorPosition(hOutput, Xy.rightwall);
printf(&quot;|&quot;);

SetConsoleCursorPosition(hOutput, Player.start);
printf(&quot;%c&quot;, 1);

SetConsoleCursorPosition(hOutput, Player.position);

while(1) // Main Event Loop
{

input(); // Reads/Processes User Input
collision(); // Checks for collision with side walls
targetmoveright(); // Moves the Target



}

}

void input(void)
{
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(&quot; &quot;);
Player.position.X--;
SetConsoleCursorPosition(hOutput, Player.position);
printf(&quot;%c&quot;, 1);
SetConsoleCursorPosition(hOutput, Player.position);
}
else if(InputRecord.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)
{
SetConsoleCursorPosition(hOutput, Player.position);
printf(&quot; &quot;);
Player.position.X++;
SetConsoleCursorPosition(hOutput, Player.position);
printf(&quot;%c&quot;, 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(5);
SetConsoleCursorPosition(hOutput, Xy.gun);
printf(&quot;.&quot;);
}

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

}


}
}

void collision(void)
{
if(InputRecord.Event.KeyEvent.bKeyDown && Player.position.X == 3)
{
Player.position = Xy.setbackleft;
SetConsoleCursorPosition(hOutput, Xy.leftwall);
printf(&quot;| &quot;);
SetConsoleCursorPosition(hOutput, Player.position);
printf(&quot;%c&quot;, 1);
SetConsoleCursorPosition(hOutput, Player.position);
}
if(InputRecord.Event.KeyEvent.bKeyDown && Player.position.X == 75)
{
Player.position = Xy.setbackright;
SetConsoleCursorPosition(hOutput, Xy.rightwall);
printf(&quot;|&quot;);
SetConsoleCursorPosition(hOutput, Player.position);
printf(&quot;%c&quot;, 1);
printf(&quot; &quot;);
SetConsoleCursorPosition(hOutput, Player.position);
}
}

void targetmoveright(void)
{

if(Xy.goal.X <= Xy.rightwall.X)
{
SetConsoleCursorPosition(hOutput, Xy.goal);
printf(&quot; &quot;);
Xy.goal.X++;
SetConsoleCursorPosition(hOutput, Xy.goal);
printf(&quot;*&quot;);
}

if(Xy.goal.X == Xy.rightwall.X)
{
SetConsoleCursorPosition(hOutput, Xy.goal);
printf(&quot; &quot;);
Xy.goal.X--;
SetConsoleCursorPosition(hOutput, Xy.goal);
printf(&quot;*&quot;);
}
if(Xy.goal.X == Xy.leftwall.X)
{
SetConsoleCursorPosition(hOutput, Xy.goal);
printf(&quot; &quot;);
Xy.goal.X++;
SetConsoleCursorPosition(hOutput, Xy.goal);
printf(&quot;*&quot;);
}
}
 
Call PeekConsoleInput. If it returns true, then call Input. ReadConsoleInput is a blocking routine: it won't return unless it gets something.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top