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.
I just realised, this is the same basic code as the thread about Setting the TextColor. I take it you've decided to give up on using a multi-colour display?
Here is a Work In Progress, with color added (and some minor mods) with comments.
TODO:
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.
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <time.h>
#include <string>
#include <windows.h>
#include <stdlib.h>
usingnamespace std;
void setColor(unsignedshort color);
//Function for setting color
void setColor(unsignedshort color)
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon,color);
}
//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
COORD cursorPos;
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;
char input;
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)
do
{
//move cursor to required position
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cursorPos);
//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
setColor(smileyColor);
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)
{
setColor(heartColor);
cout << (char)3; //Heart
}
//if HEART co-incide with SMILEY position
elseif(y == y2 && x == x2)
{
//increment health
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)
{
//print missile
setColor(missleColor);
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)
{
setColor(goblinColor);
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
else
{
setColor(gridColor);
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)
pathx++;
elseif (pathx > x)
pathx--;
}
elseif (pathy != y)
{
if (pathy < y)
pathy++;
elseif (pathy > y)
pathy--;
}
//Some text printed below the game grid
cout << endl;
setColor(gridColor);
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
do
{
cout << endl;
input = getch();
switch(input)
{
//We will accept both upper and lower case charaters
case'w':
case'W':
y--;
break;
case'a':
case'A':
x--;
break;
case's':
case'S':
y++;
break;
case'd':
case'D':
x++;
break;
case'f':
case'F':
projectile_existence = true;
px=x;
py=y;
break;
}
}while (proglife < 0);
py++;
proglife++;
if(goblinhealth < 0)
{
pathy = 0;
pathx = 0;
}
}while(input != 'q' && health > 0);
cout << "\nSorry! You did lose!";
cin.get();
}
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...
They where made just becuase that... to do nothing... I through in this way the smiley wouldn't move...
W00T!!! Did solved this! And was pretty simple... Just a empty else and deleting the two parenthesis, the ! mark, and invertin the signals (> would be <, etc). :DD
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??