Hi everyone! This is my first post. I'm relatively new at c++, but not completely ignorant, so please bear with me.

Anyway, I'm trying to create a game in a console application. However, I don't really know how to have multiple things happening at once. At this point, I want it to run like

(moves ship around);
presses spacebar to fire
fires, but can also move and fire again before first shot ends

if that makes sense. Later, I may want to add another ship, etc. The problem is, my fire() function relies on a wait() function to delay the output. How can I do both (and later more) things at once?

I'll attach my code below. It should compile and run perfectly in its current state. Let me know if you see anything I should change or if you have any ideas. Thanks!

PS - I'm unfamiliar with daniweb etiquette. If I have more questions about elements of this program that don't relate to this issue, should I post here or in another thread? Also, sorry if I shouldn't have posted all my source in the first post. Thanks again!

#include <iostream>
#include <conio.h>
#include <vector>
#include <ctime>
#include <tchar.h>
#include <Windows.h>
#include <stdio.h>

using namespace std;

void gotoxy(short x, short y)
{
   HANDLE hConsoleOutput;
   COORD Cursor_Pos = {x, y};

   hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleCursorPosition(hConsoleOutput, Cursor_Pos);
}

int getch2()                //works with the arrow keys
{
int input;
       input = getch();
	   if(input ==224)
	   {
		   input = getch();
	   }
   return input;
}

void wait (double seconds)
{
 clock_t endwait;
 endwait = clock () + seconds * CLOCKS_PER_SEC ;
 while (clock() < endwait) {}
}

void cursorkiller(int onoroff, HANDLE hOut)
{
   CONSOLE_CURSOR_INFO ConCurInf;
   if(onoroff == 0)
   {
       ConCurInf.dwSize = 10;
    ConCurInf.bVisible = FALSE;
   }else if(onoroff == 1)
   {
       ConCurInf.dwSize = 10;
       ConCurInf.bVisible = TRUE;
   }

   SetConsoleCursorInfo(hOut,
                        &ConCurInf);
}

void resizeall (int wide, int high, HANDLE hOut)
{
   CONSOLE_SCREEN_BUFFER_INFO SBInfo;
   COORD NewSBSize;
   GetConsoleScreenBufferInfo(hOut,
                              &SBInfo);

   NewSBSize.X = wide;
   NewSBSize.Y = high;
   SetConsoleScreenBufferSize(hOut,
                              NewSBSize);

   CONSOLE_SCREEN_BUFFER_INFO SBInfo2;
   SMALL_RECT DisplayArea = {0, 0, 0, 0};

   hOut = GetStdHandle(STD_OUTPUT_HANDLE);

   GetConsoleScreenBufferInfo(hOut,
                              &SBInfo2);
   DisplayArea.Bottom = high-1;
   DisplayArea.Right = wide-1;
   SetConsoleWindowInfo(hOut,
                            TRUE,
                            &DisplayArea);
}

void drawship(int x, int y, HANDLE hOut)
{
   gotoxy(x,y);

   printf("³");

   gotoxy(x-1, y+1);
   printf("ÉÏ»");

   gotoxy(x-1, y+2);

   printf("º º");
}
void deleteship(int x, int y, HANDLE hOut)
{

   gotoxy(x,y);
   printf(" ");
   gotoxy(x-1, y+1);
   printf("   ");
   gotoxy(x-1, y+2);
   printf("   ");
}
void coutc(int color, char* output)
{
HANDLE handle= GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute( handle, color);
printf(output);
SetConsoleTextAttribute( handle, 15);
}
enum Colors { black=0, blue, green, cyan, red, purple, dullyellow, lightgrey, darkgrey, blue2, brightgreen, lightcyan, brightred, lightmagenta, yellow, white };
void fire (int x, int y, HANDLE hOut)
{
   gotoxy(x, y-1);
   int y2 = y-1; //to write top *
   int y3 = y-1;//to erase bottom *
  
       for (int u =0; u<6; u++) //initial draw
       {
		   if(y2==0)
		   {
			   u=7;
		   }
		   coutc(brightred, "Û");
           y2--;
           gotoxy(x, y2);
       }
       while(y2>=0)
       {
		   wait(.01);
           gotoxy(x, y3);
           printf(" ");
           gotoxy(x, y2);
		   coutc(brightred, "Û");
           y2--;
           y3--;
       }

	   for(int y =0; y<6; y++)
	   {
		   wait(.01);
		   y3--;
	   }

}

vector <int> getbuffsize(HANDLE hOut)
{
   // HANDLE hOut;
    CONSOLE_SCREEN_BUFFER_INFO SBInfo;

   // hOut = GetStdHandle(STD_OUTPUT_HANDLE);

    GetConsoleScreenBufferInfo(hOut,
                               &SBInfo);

    vector <int> sizes(2);
    sizes[0] = SBInfo.dwSize.X;
    sizes[1] = SBInfo.dwSize.Y;

    return sizes;
}


int main ()
{
   srand ( time(NULL) );
   HANDLE hOut;
   hOut = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleTitle(_T("Super Game"));
										resizeall(100, 45, hOut);//open to change
   cursorkiller(0, hOut);
									   vector <int> buffersizes(2);
										buffersizes = getbuffsize(hOut);
										int xlimit, ylimit;
										xlimit = buffersizes[0] -2;
										ylimit = buffersizes[1] -3;

   int x = 5;
   int y = 3;
   char input;
   while(1!=0)
   {
       input = getch2();
       deleteship(x,y,hOut);
       if(input==72 && y>0 ) //up
       {
           y--;
       }else if(input==80 && y<ylimit ){ //down
           y++;
       }else if(input==75 && x>1){ //left
           x--;
       }else if(input==77 && x<xlimit){ //right
           x++;
       }else if(input == 32) //spacebar
       {
           drawship(x,y,hOut);
           fire(x,y,hOut);
       }

   if(input != 32){
       drawship(x,y,hOut);}
   }

   getch();

}

Well if you're unfamiliar with classes and inheritance i might suggest adding another argument to fire() like fire(int s,etc..) then using wait(int seconds) inside fire(int s) passing s to wait() through fire().Kinda messy but here:

void wait(int seconds) {//code}
  void fire(int s) {
  wait(s)
  //code
}

You should though start learning how to create objects.

Thanks for the quick response! I'll see what I can do with the code you recommended.

You should though start learning how to create objects

Would you recommend a good tutorial or lesson on this?
Thanks!

Any tutotrial should do the job.Just learn about classes which means .. learn c++:) .. good luck

All right, I can now move while shooting. I ended up doing it differently than you suggested, however. My code now works something like this

void drawship
void deleteship
void move 
void firewait
{
listen for input with kbhit() while counting down time;
if input detected, pass the value to move()
if time met and no input detected, close
}

int main()
{
//stuff
[user presses fire]
draw laser at pt. x,y
firewait()
delete and draw needed parts
firewait
//etc.
}

Thanks for the input though. I'll post back if I have any more issues.

If you want to do many things at the same time ... I would suggest you get a grip on Multi threading as well ... Might be useful

For the sake of anyone searching Google for how to do this type of thing, I'll post the relevant code that I'm currently using to do it.

char firewait(double seconds)
{
   char keypress = 'n';
   clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait) {

					if(keypress == 'n')
					{
                           if(_kbhit())
                             {
                              int input = _getch();
                                  if(input == 224)
                                  {
                                      input = _getch();
                                      keypress = 'm';
                                  }else if (input == something else)
								  {
									  do stuff 
                                                                           change keypress
								  }
                             }
					}
                           }
return keypress;
}

Sorry that the indents got messed up:(

The process of my code is like so:

draw needed elements;
char g = firewait();
if(a key has been pressed)
{
do the appropriate thing
}
repeat as necessary

This allows input to be accepted while drawing your stuff. Also, I find it helpful to run firewait several times before drawing stuff, as that allows more inputs to be accepted. For example, I might want to change stuff on the screen every five seconds, but also want to be able to move every .01 seconds.

Let me know if this is helpful or if I was unclear. Thanks to everyone who gave me advice!

This article has been dead for over six months. Start a new discussion instead.