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();
}