Thread: A particular character on the screen

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    A particular character on the screen

    I have a program with lots of random letters in it, and it is constantly changing them.

    How might I make a particular character on the screen one color, and then as the row scrolls up, the new character that was there is the same color?

    This is for a console program with Microsoft VC++ 6.0

    So if my screen started as

    AZYFAYDUFY
    DIOWrYEOR
    FDOIYEWRO
    DOYIDOIGE

    when it changed to

    DIOWRYEOR
    FDOIyEWRO
    DOYIDOIGE
    AIEYARONA

    because it scrolled up, how would I make that one lowercase letter always one color?

    Code:
    HANDLE stdOut = GetSTDHandle(STD_OUTPUT_HANDLE);
    
    int main()
    {
    .....
    
    //make red text
    SetConsoleTextAttribute(stdOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
    cout << randchar << flush;
    Thats a truncated version of my code... that is how I'd make it red, but it randomly chooses a number between 1 and 16, and then it decides on a letter using a switch statement (10 as a number is A). If it wasn't constantly outputing random characters, this would be easy.

    Note: that cout isn't how I have it to display, but you'd need my whole code for it.

    [email protected] is my email if you want the whole program's source.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Code:
    class CLettersOnScreen
    {
    int x;
    int y;
    char c;
    xxx color; // However you define your color
    };
    
    
    CLettersOnScreen *letters=new CLeyttersOnScreen;
    
    letters->x=10;
    letters->y=15;
    letters->c='O';
    letters->Color=blue;
    
    // Now change the letter to the next letter.
    
    letters->c="N";
    
    delete letters;
    You would probably want a function called display to draw the letter with the appropriate color. You would call that after any changes.
    Best Regards,

    Bonkey

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    OK, thank you for that code. But how would I implement it into my code?
    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <time.h>
    
    HANDLE stdOut=GetStdHandle(STD_OUTPUT_HANDLE);
    
    main()
    {
            int i=1;
            int j=0;
            int k=1;
            int l=0;
            char Matrix[]="0123456789ABCDEF                         ";
            int CharNum;
            int BrightLength=0;
            const int COLORLENGTH=25; //length of each color change
            const int COLORCHANCE=0; //chance of color change
            const int COLORCHANCEMAX=12;
    
            srand(time(NULL));
    
            ios::sync_with_stdio();
    
    
    
            do
            {
                    if(l==COLORCHANCE&&k<BrightLength) //0==how often color change happens
                    {
                            SetConsoleTextAttribute(stdOut, FOREGROUND_GREEN|FOREGROUND_INTENSITY);
                            k++;
                    }
                    else if(k==BrightLength)
                    {
                            SetConsoleTextAttribute(stdOut, 
    FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
                            k=0;
                    }
                    else
                    {
                            SetConsoleTextAttribute(stdOut, FOREGROUND_GREEN);
                            BrightLength=rand()%COLORLENGTH; //25==how long color change lasts
                            k=0;
                            l=rand()%COLORCHANCEMAX; //max value that how often color change happens 
    is compared to to find when color change happens
                    }
    
                    CharNum=(rand()%32);
    
                    cout<<Matrix[CharNum];
    
                    cout<<" ";
                    for(j=0; j<=500; j++)
                    {
                            cout<<"";
                    }
            }
            while(i<2);
    
            return 0;
    }
    Where would I place your code?

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    I would do something like this.

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <time.h>
    
    HANDLE stdOut=GetStdHandle(STD_OUTPUT_HANDLE);
    
    class CPointOnScreen
    {
    public:
    	char c;
    	WORD Attributes;
    	void Draw();	
    };
    
    void CPointOnScreen::Draw()
    {
    	SetConsoleTextAttribute(stdOut,Attributes);
    	cout << c << " ";
        for(int j=0; j<=500; j++)
    	{
    		cout<<"";
    	}
    }
    
    main()
    {
    
    		CPointOnScreen RowColumn[24][80]; // Assume standard 24*80 screen
    		CPointOnScreen *point;
            int k=1;
            int l=0;
            int BrightLength=0;
            const int COLORLENGTH=25; //length of each color change
            const int COLORCHANCE=0; //chance of color change
            const int COLORCHANCEMAX=12;
    		char Matrix[]="0123456789ABCDEF                         ";
    
            srand(time(NULL));
    
            ios::sync_with_stdio();
    
    
    
    		int row=0;
    		int column=0;
    
    		for(row=0;row<24;row++)
    		{
    			for(column=0;column<80;column++)
    			{
                    if(l==COLORCHANCE&&k<BrightLength) //0==how often color change happens
                    {
                            //SetConsoleTextAttribute(stdOut, FOREGROUND_GREEN|FOREGROUND_INTENSITY);
    						RowColumn[row][column].Attributes=FOREGROUND_GREEN|FOREGROUND_INTENSITY;
                            k++;
                    }
                    else if(k==BrightLength)
                    {
                            //SetConsoleTextAttribute(stdOut, FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
    						RowColumn[row][column].Attributes=FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY;
                            k=0;
                    }
                    else
                    {
                            //SetConsoleTextAttribute(stdOut, FOREGROUND_GREEN);
    						RowColumn[row][column].Attributes=FOREGROUND_GREEN;
                            BrightLength=rand()%COLORLENGTH; //25==how long color change lasts
                            k=0;
                            l=rand()%COLORCHANCEMAX; //max value that how often color change happens is compared to to find when color change happens
                    }
    
                    RowColumn[row][column].c=Matrix[(rand()%32)];
    			}
    		}
    
    		do
            {
    
    				for(column=0;column<80;column++)
    				{
    					for(row=0;row<23;row++)
    					{
    						if( row != 23 )
    							RowColumn[row][column].c=RowColumn[row+1][column].c;
    						else
    						{
    							RowColumn[row][column].c=Matrix[(rand()%32)];
    						}
    						RowColumn[row][column].Draw();
    					}
    				}
    
            }
            while(true);
    
            return 0;
    }

    The idea is, loop thru and set paramaters of each character space on the screen. Then move the characters up thru the spaces with the space maintaining its color attribute and only the character displayed changes.

    Hope this helps.
    Last edited by bonkey; 10-02-2002 at 11:40 AM.
    Best Regards,

    Bonkey

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    I am stil tweeking on it. You'll need to be able to paint the character at a given position and I can't get cout to do that. I am looking for alternatives.

    If I find onw I will post working code.
    Best Regards,

    Bonkey

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    Here it is with a nasty flicker cause I had to clear the screen to move the cursor. I'm sure there is a better way. If you could just move the cursor to the top left without a clear it would look good.

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <time.h>
    
    HANDLE stdOut=GetStdHandle(STD_OUTPUT_HANDLE);
    
    class CPointOnScreen
    {
    public:
    	int x;
    	int y;
    	char c;
    	WORD Attributes;
    	void Draw();	
    };
    
    void CPointOnScreen::Draw()
    {
    	COORD point;
    	point.X = y; point.Y = x;
    	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),point);
    
    	SetConsoleTextAttribute(stdOut,Attributes);
    	cout << c << " ";
    }
    
    int main()
    {
    
    	
    		CPointOnScreen RowColumn[24][80]; // Assume standard 24*80 screen
            int k=1;
            int l=0;
            int BrightLength=0;
            const int COLORLENGTH=25; //length of each color change
            const int COLORCHANCE=0; //chance of color change
            const int COLORCHANCEMAX=12;
    		char Matrix[]="0123456789ABCDEF                         ";
    
            srand(time(NULL));
    
            ios::sync_with_stdio();
    
    
    
    		int row=0;
    		int column=0;
    
    		for(row=0;row<24;row++)
    		{
    
    			for(column=0;column<80;column++)
    			{
                    if(l==COLORCHANCE&&k<BrightLength) //0==how often color change happens
                    {
    						RowColumn[row][column].Attributes=FOREGROUND_GREEN|FOREGROUND_INTENSITY;
                            k++;
                    }
                    else if(k==BrightLength)
                    {
    						RowColumn[row][column].Attributes=FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY;
                            k=0;
                    }
                    else
                    {
    						RowColumn[row][column].Attributes=FOREGROUND_GREEN;
                            BrightLength=rand()%COLORLENGTH; //25==how long color change lasts
                            k=0;
                            l=rand()%COLORCHANCEMAX; //max value that how often color change happens is compared to to find when color change happens
                    }
        
    				for(int j=0; j<=500; j++)
    					cout<<"";
    
                    RowColumn[row][column].c=Matrix[(rand()%32)];
    				RowColumn[row][column].x=row;
    				RowColumn[row][column].y=column;
    			}
    
    		}
    
    
    		while(true)
    		{
    			system("cls");
    
    			for(row=0;row<24;row++)
    			{
    				for(column=0;column<80;column++)
    				{
    					if( row != 23 )
    						RowColumn[row][column].c=RowColumn[row+1][column].c;
    					else
    					{
    						RowColumn[row][column].c=Matrix[(rand()%32)];
    						for(int j=0; j<=500; j++)
    							cout<<"";
    					}
    					RowColumn[row][column].Draw();
    				}
    			}
    		}
    
    		
    		return 0;
    }

    Edit: Removed clear and added code to reposition the cursor. Still some flicker. Mabe random char paints within the grid would look better than line by line updates.
    Last edited by bonkey; 10-02-2002 at 01:39 PM.
    Best Regards,

    Bonkey

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Yeah... that flicker is evil...

    What I'm trying to do is get it to look like it is scrolling up (my friend got the idea from the movie The Matrix) but there is a picture you see based on the colors... thats why I chose red. I don't want it to choose a color based on what character, but on what position on the screen (so 5 over and 5 down is red, for example) Then when that letter goes up a row, the one that is placed there turns red... like layers on photoshop?

    Do you use any instant messengers?
    Last edited by Trauts; 10-02-2002 at 04:11 PM.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Here's some slightly faster code.

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <time.h>
    
    HANDLE stdOut=GetStdHandle(STD_OUTPUT_HANDLE);
    
    class CPointOnScreen
    {
    public:
    	int x;
    	int y;
    	char c;
    	WORD Attributes;
    	void Draw();	
    };
    
    void CPointOnScreen::Draw()
    {
    	COORD point;
    	point.X = y; point.Y = x;
     	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),point);
    
    	SetConsoleTextAttribute(stdOut,Attributes);
    	cout << c << " ";
    }
    
    int main()
    {
    
    	
    		CPointOnScreen RowColumn[24][80]; // Assume standard 24*80 screen
            int k=1;
            int l=0;
            int BrightLength=0;
            const int COLORLENGTH=25; //length of each color change
            const int COLORCHANCE=0; //chance of color change
            const int COLORCHANCEMAX=12;
    		char Matrix[]="0123456789ABCDEF                         ";
    
            srand(time(NULL));
    
            ios::sync_with_stdio();
    
    
    
    		int row=0;
    		int column=0;
    
    		for(row=0;row<24;row++)
    		{
    
    			for(column=0;column<80;column++)
    			{
                    if(l==COLORCHANCE&&k<BrightLength) //0==how often color change happens
                    {
    						RowColumn[row][column].Attributes=FOREGROUND_GREEN|FOREGROUND_INTENSITY;
                            k++;
                    }
                    else if(k==BrightLength)
                    {
    						RowColumn[row][column]. Attributes=FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY;
                            k=0;
                    }
                    else
                    {
    						RowColumn[row][column].Attributes=FOREGROUND_GREEN;
                            BrightLength=rand()%COLORLENGTH; //25==how long color change lasts
                            k=0;
                            l=rand()%COLORCHANCEMAX; //max value that how often color change happens is compared to to find when color change happens
                    }
        
    
                    RowColumn[row][column].c=Matrix[(rand()%32)];
    				RowColumn[row][column].x=row;
    				RowColumn[row][column].y=column;
    			}
    
    		}
    
    
    		while(true)
    		{
    			system("cls");
    
    			for(row=0;row<24;row++)
    			{
    				for(column=0;column<80;column++)
    				{
    					if( row != 23 )
    						RowColumn[row][column].c=RowColumn[row+1][column].c;
    					else
    					{
    						RowColumn[row][column].c=Matrix[(rand()%32)];
    						for(int j=0; j<=500; j++)
    							cout<<"";
    					}
    					RowColumn[row][column].Draw();
    				}
    			}
    		}
    
    		
    		return 0;
    }
    Last edited by Trauts; 10-03-2002 at 08:48 AM.

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    It does exactly what you want. There is basically an array of attributes that the characters pass thru. Look closely when you execute my code, you will see that the spots stay 1 color and not the characters.

    The flicker is the last herdal. I am working on a way to clear that up. I have not had success yet. The idea is is to only redraw the ones that change color and not all the other chars. Thereby reducing the flicker effect.

    I will post the flicker free code if I can get it to work.
    Best Regards,

    Bonkey

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    many thanks.

    I notice that. I still don't know how you did it, though.

  11. #11
    Registered User
    Join Date
    Jun 2002
    Posts
    44
    Im not sure what you need to use conio.h,but you probably have it,and it has gotoxy(int x,int y) to move the cursor,like this to move to top-left:
    gotoxy(1,1);
    ego sum via et veritas et vitae -Jesus Christ

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    So how do I get it to run like the first version up top, where there is no visible refresh, it just scrolls quickly, but it always prints the color red in certain positions? I still don't know how to choose which positions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. get wide character and multibyte character value
    By George2 in forum C++ Programming
    Replies: 27
    Last Post: 01-27-2008, 05:10 AM
  3. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  4. Render text
    By Livijn in forum C++ Programming
    Replies: 6
    Last Post: 07-06-2007, 03:32 PM
  5. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM