my compiler runtime checker - informs me that Line 47: elseif(r == py && c == px)
is a problem because variablepy and px are still uninitialised (when the program reaches that point).
So we can start by curing that problem.
That for loop starting on Line 28 - sure looks weird - but i suppose the consecutive if/else clauses make it a single statement loop.
Here is a Work In Progress, with color added (and some minor mods) with comments.
1. Stop characters being able to move outside game grid
2. Possible timer - To allow automatic movement of the missile (is that what it is?) and the goblin.
void setColor(unsignedshort color);
//Function for setting color
void setColor(unsignedshort color)
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
//some colour constants
constunsignedshort gridColor = 7; //bright white
constunsignedshort smileyColor = 14; //bright yellow
constunsignedshort goblinColor = 10; //bright green
constunsignedshort heartColor = 12; //bright red
constunsignedshort missleColor = 13; //bright purple
// for resetting cursor position each time we go round the row/column loops
int main ( )
int x = 5, y = 4; //SMILEY position
int x2, y2; //HEART position
int px=1, py=1; //MISSILE position ---- Relative to the character/heart/attack.
int aix=0, aiy=0; //Relative to the ai settings.
int pathx = 10, pathy = 8; //GOBLIN Position - Relative to the enemy.
int height = 15, width = 20; // Relative to the arena size;
int health = 20;
bool projectile_existence = false;
int goblinhealth = 10;
int proglife = 0;
aix = x;
aiy = y;
srand ( time(NULL));
x2 = rand() % 19 + 1;
y2 = rand() % 14 + 1;
cursorPos.X = cursorPos.Y = 0; //set cursor (start of display position
system("CLS"); //Clear screen at start of game only (reduces flicker)
//move cursor to required position
//now draw the grid and characters.
for(int r = 0; r < height; ++r) //row loop
for(int c = 0; c < width; ++c) //column loop
//check if this is the position of the smiley face
if(r == y && c == x)
//if so print smiley face
cout << (char)1; //Smiley Face
if(y == pathy && x == pathx)
//if co-incide with missle position - reduce health
health -= 10;
//else if it's position of heart
elseif(r == y2 && c == x2)
cout << (char)3; //Heart
//if HEART co-incide with SMILEY position
elseif(y == y2 && x == x2)
health += 5;
//and put another heart on screen at random position
x2 = rand () % 15 + 1;
y2 = rand () % 20 + 1;
//check if missile position
elseif(r == py && c == px)
cout << "|";
//if co-incide with goblin position
if(py == pathy && px == pathx)
//decrement goblins health
goblinhealth -= 5;
px = x; py = y;
//No more missile
projectile_existence = false;
//if Goblin position
elseif((r == pathy && c == pathx) && goblinhealth > 0)
cout << (char)2; //INVERTED SMILEY
//Now that all the character positions have been taken care of -
//if none of the above cases - then we print a dot
cout << '.';
}//end column loop
cout << endl; //newline for next row
}//end row loop
// AI Settings - Note This is now outside the row/column loops.
if (pathx != x)
if (pathx < x)
elseif (pathx > x)
elseif (pathy != y)
if (pathy < y)
elseif (pathy > y)
//Some text printed below the game grid
cout << endl;
cout << "Health:" << health << "\n";
cout << "Goblin Health:" << goblinhealth << "\n";
cout << "UP = w, DOWN = s, LEFT = a, RIGHT = d, FIRE = f, QUIT = q";
//OK - Get player control input
cout << endl;
input = getch();
//We will accept both upper and lower case charaters
projectile_existence = true;
}while (proglife < 0);
if(goblinhealth < 0)
pathy = 0;
pathx = 0;
}while(input != 'q' && health > 0);
cout << "\nSorry! You did lose!";
Well you will need either a default case that takes you back to the beginning, or a condition on the while loop, since if the user doesn't input anything, the case is basically skipped. Also...why do you have x += 0, y += 0, etc...those do nothing...
Now I did see something strange: The first hit, with the arrow (yep, is a arrow, not a missile), the goblin dosen't suffer nothing, and the secondtie he does gain 40 health... Then the third shot he dies... Why that happens??
Last edited on
Topic archived. No new replies allowed.
You are using a version without Ads of this website. Please, consider donating: