-
Text Array Colors...
i had a problem with this earlier...but before anyone answered i suddenly figured it out...but then i came across another problem...
how do i stop the color from "bleeding" over onto the next variable in the array?
Code:
#include<iostream>
#include<windows.h>
using namespace std;
#define fgreen SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE ),FOREGROUND_GREEN)
#define fblue SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE ),FOREGROUND_BLUE)
char fgrn(char a)
{
fgreen;
return a;
}
char fbl(char a)
{
fblue;
return a;
}
char grass='#';
char charL='@';
#define g fgrn(grass)
#define c fbl(charL)
int main()
{
char ffg[2][2]={{c,g},
{g,c}};
cout<<ffg[0][1]<<ffg[1][1]<<endl;
return 0;
}
-
You need another function that turns it back to normal color - which if I remember correctly is red/blue/green combined. And I don't think you changed colors correctly - changing the color affects everything from the time you call it, it isn't an attribute of a char. So I believe after the call:
Code:
char ffg[2][2]={{c,g}, {g,c}};
everything you print out after this line will be blue since that was the last color call. Haven't tested it though...
-
:(
yea i know...how can i make it so it realizes that c is a diff color then g? i tried putting in a space with the color white attached to it...but the next letter still turned up either green or blue depending on the first color =/
-
The only way I know how to do this would be to change the color WHEN you print, not when you store the character.
Something like:
Code:
fgreen;
cout<<"#";
fblue;
cout<<"@";
// macro to change back to normal color
cout<<"Normal";
should work. Or even a function like:
Code:
void printchar(char c)
{
if (c=='#')
fgreen;
else if (c=='@')
fblue;
cout<<c;
// macro back to normal color;
}
-
man...i just dont know...trying out different things...but none are working =/ if i use a 'cout' in the function...then the colors turn out right but they wont stay in an array...but if i dont use cout but instead use the 'return' then it works...but the colors are the same as the first one posted...
is there no "[\color]" in c++? ;P
or is there some other way to make a colored map in console in which you can move the 'character' around?
or should i just give this up completely...and hurry up into my windows programming so i can just use 2d graphics? ;P
-
Instead of trying to put the colors in an array, JUST put the characters.
So your code
Code:
char grass='#';
char charL='@';
int main()
{
char ffg[2][2]={{c,g},
{g,c}};
cout<<ffg[0][1]<<ffg[1][1]<<endl;
return 0;
}
becomes
Code:
char grass='#';
char charL='@';
int main()
{
char ffg[2][2]={{charL,grass},
{grass,charL}};
printchar(ffg[0][1]); // if you use the function i wrote earlier
printchar(ffg[1][1]);
cout<<endl;
return 0;
}
-
but still the color problem =/
well geez...thx...ive racked my brain skimmed a few books...and asked the board...guess i cant do it =/
thanks anyways tho
-
Your problem is that you put the textcolor in a function you use when putting the text into an array. You need to call it when you print the actual text, otherwise only the last set color will be used for all your text.
Code:
#define fgreen SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE ),FOREGROUND_GREEN)
#define fblue SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE ),FOREGROUND_BLUE)
int main()
{
fgreen;
cout << "This text is green!" << endl;
fblue;
cout << "This text is blue!" << endl;
fgreen;
cout << "This is green again!" << endl;
return 0;
}
-
I'm confused then, what do you mean by color problem? Didn't my code work and if not what did it do?
-
im thinking ill use an array with an 'if' function checking the chars...and depending on what the char is will depend on what the color is set to...thx for your help!
-
Try flushing the buffer:
Code:
#define fgreen SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA
NDLE ),FOREGROUND_GREEN)
#define fblue SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA
NDLE ),FOREGROUND_BLUE)
int main()
{
fgreen;
cout << "This text is green!" << endl << flush;
fblue;
cout << "This text is blue!" << endl << flush;
fgreen;
cout << "This is green again!" << endl << flush;
return 0;
}
-
[code]
//Map arrays
//Practice17.cpp
#include<iostream>
#include<windows.h>
#include<color.h>
#include<basicf.h>
using namespace std;
char g='#';
char c='@';
char w='&';
char r='~';
char d='+';
const int IMAX(30);
int main()
{
char PMap[10][10]={{g,g,r,r,g,g,g,w,w,g},
{g,r,d,r,g,g,w,w,g,g},
{g,r,r,r,g,g,w,g,g,g},
{g,r,r,r,g,w,w,g,g,g},
{r,r,r,g,g,w,g,g,g,g},
{r,r,g,g,g,w,g,g,g,g},
{g,g,g,g,g,w,g,g,g,g},
{w,w,g,w,w,w,g,g,g,g},
{g,g,c,g,g,w,w,w,w,g},
{g,g,g,g,g,g,g,g,g,g}};
for(int x=0;x<10;x++)
{
for(int y=0;y<10; y++)
{
PMap[x][y];
if(PMap[x][y]==g)
{fgreen;}
if(PMap[x][y]==c)
{fiblue;}
if(PMap[x][y]==w)
{grey;}
if(PMap[x][y]==r)
{fblue;}
if(PMap[x][y]==d)
{brown;}
cout<<PMap[x][y];
}
cout<<endl;
}
white;
return 0;
}
[\code]
yea thas how i did it...it works great now...
BTW color.h and basicf.h two headers i made...color does all the defines for my colors...and basicf just does alot of things i need it to do...
-
Ouch... so close.
It should be [/CODE] not [\CODE] :)