constantly print an array to the screen?

This is a discussion on constantly print an array to the screen? within the C++ Programming forums, part of the General Programming Boards category; Hello. I'm working on what I consider to be my first "real" attempt at making a game. Of course, its ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    12

    constantly print an array to the screen?

    Hello. I'm working on what I consider to be my first "real" attempt at making a game. Of course, its a simple console app using ascii characters. So far I have managed to print the character to the screen implement movement. However, I can't seem to figure out how to print an array to the screen and have it remain there constantly. I have tried a bunch of differnt loops and placing the function to print the array all over the place, but it always results in the cursor jumping around the screen, causing horrible flickering. Heres my work so far.

    Code:
    #include <iostream>
    #include <conio.h> // kbhit() and getch()
    #include <windows.h>
    
    using namespace std;
    
    const int X =100; // max width of maze
    const int Y =40; // max height of maze
    char ascii = 0x01;
    char maze[X][Y];
    
    int x=10, y=10; //starting position of player
    
    
    //not my code - don't remeber where I found it
    void gotoxy(short x, short y) {
    	HANDLE hConsoleOutput;
    	COORD Cursor_Pos = {x, y};
    
    	hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    	SetConsoleCursorPosition(hConsoleOutput, Cursor_Pos);
    }
    
    //ditto
    void clrscr(void) {
    	CONSOLE_SCREEN_BUFFER_INFO csbi;
    	HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    	COORD coord = {0, 0};
    	DWORD count;
    
    	GetConsoleScreenBufferInfo(hStdOut, &csbi);
    	FillConsoleOutputCharacter(hStdOut, ' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &count);
    	SetConsoleCursorPosition(hStdOut, coord);
    
    }
    
    void fillmaze()
    {
        for(int i=0; i<X; i++)
        {
    
            for(int j=0; j<Y; j++)
            {
                //arrayname[x] = 1st dimenstion, "xxxxxx" = elements of 2nd dimenstion
                //right now these are just arbitrary elements. soon to be turned into a map/maze
                strcpy(maze[0],"|234568910");
                strcpy(maze[1],"|bccvbnmop");
                strcpy(maze[2],"|wertuiqwe");
                strcpy(maze[3],"|;;;;;;;;;");
                strcpy(maze[4],"|---------");
                strcpy(maze[5],"|fffffffff");
            }
        }
    }
    
    void printmaze()
    {
        for(int i=0; i<X; i++)
        {
            for(int j=0; j<Y; j++)
            {
                cout << maze[i][j];
            }
            cout<<"\n";
    
        }
    }
    
    
    int setCursor(int x, int y)
    {
        //sets the position of the cursor
        // could be used instead of gotoxy
        COORD init = {x,y};
        HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
        SetConsoleCursorPosition(hStdOut, init);
    
    }
    
    void clear() //what is going Onnn!
    {
        clrscr();
        printmaze();
        //gotoxy(x,y);
        //cout << ascii;
    
    
    }
    
    void move()
    {
    //setCursor(x,y);
    //gotoxy(x,y); // causes the cursor to stay with ascii
    
    if (kbhit())
          {
    
               // while (x != 0 || y != 0)
                //{
                char input;
                input = getch();
    
                switch (input)
                {
                case 'w':
                    //clear();
                    clrscr();
                    gotoxy(x,y-=1);
                    //printmaze();
                    cout << ascii;
                    break;
    
                case 'a':
                    clrscr();
                    gotoxy(x-=1,y);
                    cout << ascii;
                    break;
    
    			case 's':
                    clrscr();
                    gotoxy(x,y+=1);
                    cout << ascii;
                    break;
    
                case 'd':
                    clrscr();
                    gotoxy(x+=1,y);
                    cout << ascii;
                    break;
    
                case 'e':
                    exit(0);
                    break;
    
                }
    
              //}
          }
    }
    
    int main()
    {
    
        fillmaze(); // load the maze
        //printmaze(); // print the maze
        gotoxy(x,y); //place the cursor at initial position of the character
        cout << ascii; // prints character to screen
        gotoxy(0,0);// place the cursor back to 0,0 because it gets moved to the [x][] location after the array is printed
    
        int infinite = 0;
        while (infinite != 1)
            {
    
               //  printmaze();
                move();
               // gotoxy(0,0);
            }
    
    return 0;
    }
    think before you code...

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not simplify it a bit and only refresh the screen when something new happens?

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    12
    As of now, the screen is only cleared when a button is pressed. Another problem I'm having is the for some reason I can't seem to pin point, my cursor keeps snaps to and gets stuck at position (0,0) and will not move.
    think before you code...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  2. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 03:07 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. Using write() to print array elements
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-01-2002, 09:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21