Can someone help me with this console app please?

This is a discussion on Can someone help me with this console app please? within the C++ Programming forums, part of the General Programming Boards category; This is a bit complicated to explain, you need to see the program first to see the problem, so here ...

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

    Question Can someone help me with this console app please?

    This is a bit complicated to explain, you need to see the program first to see the problem, so here it is:
    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    #define STAR '%'
    #define S_HEIGHT 25
    #define S_WIDTH 80
    
    // Global Vars
    char Screen[S_WIDTH][S_HEIGHT];
    int X, Y;
    
    
    // Global specific Vars: FPS
    int StartingTime;
    int Time;
    int Fps;
    
    // Global Functions
    void EreaseScreen();
    bool FPS(int fps);
    bool InitVariables();
    void PrintScreen();
    bool PutCharInScreen();
    int FullScreen();
    void MoveStar();
    int RealX() { return (X + (S_WIDTH / 2)); }
    int RealY() { return (Y + (S_HEIGHT / 2)); }
    	// Cursor
    BOOL ShowScreenCursor (BOOL Show)
    {
    	CONSOLE_CURSOR_INFO cci = {0};
    	BOOL StatusCCI = FALSE;
    
    
    
    	if (Show) // show screen cursor
    	{
    		cci.dwSize   = 30;
    		cci.bVisible = TRUE;
    		StatusCCI    = SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cci);		
    	}
    	else // hide screen cursor
    	{
    		cci.dwSize   = 100;
    		cci.bVisible = FALSE;
    		StatusCCI    = SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cci);
    	}
    	return StatusCCI;
    }
    
    
    
    // Main
    int main()
    {
    	ShowScreenCursor(false);
    	// Going fullscreen...
    	FullScreen();
    	InitVariables();
    
    	while (1)
    	{
    		if (FPS(Fps)) // Time to move star...
    		{
    			StartingTime = GetTickCount();
    			system("cls");
    			EreaseScreen();
    			MoveStar();
    
    			if (PutCharInScreen())
    				PrintScreen();
    		}
    	}
    
    	return 0;
    }
    
    
    
    
    
    // Global functions definitions
    void EreaseScreen()
    {
    	for (int i = 0; i < S_HEIGHT; i++)
    		for (int j = 0; j < S_WIDTH; j++)
    			Screen[j][i] = '\0';
    }
    
    bool FPS(int fps)
    {
    	Time = GetTickCount() - StartingTime;
    
    	if (Time > fps)
    		return true;
    	else 
    		return false;
    }
    
    bool InitVariables()
    {
    	// Global Vars
    	X = 0;
    	Y = -10;
    	EreaseScreen();
    	Screen[RealX()][RealY()] = STAR;
    
    	// Global specific Vars: FPS
    	Fps = 500;
    	StartingTime = GetTickCount();
    	Time = 0;
    
    	return true;
    }
    
    void PrintScreen()
    {
    	for (int i = 0; i < S_HEIGHT; i++)
    	{
    		for (int j = 0; j < S_WIDTH; j++)
    			cout << Screen[j][i];
    		cout << endl;
    	}
    }
    
    bool PutCharInScreen()
    {
    	if (RealX() > S_WIDTH || RealX() < 0)
    	{
    		cout << "ERROR" << endl;
    		return false;
    	}
    	if (RealY() > S_HEIGHT || RealY() < 0)
    	{
    		cout << "ERROR" << endl;
    		return false;
    	}
    
    	Screen[RealX()][RealY()] = STAR;
    	return true;
    }
    
    int FullScreen()
    {
    	keybd_event(VK_MENU,0x38,0,0);
    	keybd_event(VK_RETURN,0x1c,0,0);
    	keybd_event(VK_RETURN,0x1c,KEYEVENTF_KEYUP,0);
    	keybd_event(VK_MENU,0x38,KEYEVENTF_KEYUP,0);
    	return 0;
    }
    
    void MoveStar()
    {
    	if (Y <= 0 && X >= 0)
    	{
    		X++;
    		Y++;
    	}
    	if (Y >= 0 && X >= 0)
    	{
    		X--;
    		Y++;
    	}
    	if (Y >= 0 && X <= 0)
    	{
    		X--;
    		Y--;
    	}
    	if (Y <= 0 && X <= 0)
    	{
    		X++;
    		Y--;
    	}
    }
    Now, if you run the program, you will see the 'STAR' kinda jumps up and goes down every time it moves, if you get out of full screen you will see that effect gets much bigger, I think the problem is that when the 'STAR' moves, the vertical scrolling bar moves, and goes back to its original position very fast, thus it seems the 'STAR' moves.
    Can anyone tell me how to stop the vertical scrolling bar from moving? Thanks in advance and sorry for the long post.
    Why drink and drive when you can smoke and fly?

  2. #2
    Registered User MicroFiend's Avatar
    Join Date
    Nov 2002
    Posts
    80

    not sure whether ur still checkig the thread but ...

    i see what you mean and thr is no way around that im afraid .. er well with ur current coding that is ^^ try this tho.. instead of using seperate subs, erasescreen (which will print then shift up) printstar and printscreen use a combined sub that does it all at once... its alot neater (less subs required) and will work perfectly seeming to have no vscrol jump (just invisible ^^) heres an example:

    Code:
    void DrawScreen()
    {
        for (int i = 0; i < S_HEIGHT; i++)
        {
            for (int j = 0; j < S_WIDTH; j++)
            {
                if(j==RealX() && i==RealY())
                {
                    Screen[j][i] = STAR; //star sub
                }
                else
                {
                    Screen[j][i] = '\0'; //erase sub
                }
                cout << Screen[j][i]; //print screen sub
            }
        }			
    }
    ive tried this implementation with your current coding and it works great.. altho i cant see what ur trying to do with the move star bit... so i tested it with a slight mod.. a bouncing star ^^ hope this helps tho

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Thanks a lot for you're help, I am using that function now, but I still have some problems....
    I have the code know as I really wanted it, here it is:
    Code:
    #include <iostream>
    #include <windows.h>
    using namespace std;
    
    #define STAR '%'
    #define S_HEIGHT 25
    #define S_WIDTH 80
    
    // Global Vars
    char Screen[S_WIDTH][S_HEIGHT];
    int X, Y;
    
    
    // Global specific Vars: FPS
    int StartingTime;
    int Time;
    int Fps;
    
    // Global specific Vars: Changement
    int R;
    bool Change;
    
    // Global Functions
    void DrawScreen();
    bool FPS(int fps);
    bool InitVariables();
    int FullScreen();
    void MoveStar();
    int RealX() { return (X + (S_WIDTH / 2)); }
    int RealY() { return (Y + (S_HEIGHT / 2)); }
    	// Cursor
    BOOL ShowScreenCursor (BOOL Show)
    {
    	CONSOLE_CURSOR_INFO cci = {0};
    	BOOL StatusCCI = FALSE;
    
    
    
    	if (Show) // show screen cursor
    	{
    		cci.dwSize   = 30;
    		cci.bVisible = TRUE;
    		StatusCCI    = SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cci);		
    	}
    	else // hide screen cursor
    	{
    		cci.dwSize   = 100;
    		cci.bVisible = FALSE;
    		StatusCCI    = SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cci);
    	}
    	return StatusCCI;
    }
    	// STAR Directions
    bool North();
    bool NorthEast();
    bool East();
    bool SouthEast();
    bool South();
    bool SouthWest();
    bool West();
    bool NorthWest();
    
    
    
    // Main
    int main()
    {
    	ShowScreenCursor(false);
    	// Going fullscreen...
    	FullScreen();
    	InitVariables();
    	// Create seed for rand()
    	srand(GetTickCount());
    
    	while (1)
    	{
    		if (FPS(Fps)) // Time to move star...
    		{
    			StartingTime = GetTickCount();
    			system("cls");
    			MoveStar();
    			DrawScreen();
    		}
    	}
    	return 0;
    }
    
    
    
    
    
    // Global functions definitions
    void DrawScreen()
    {
        for (int i = 0; i < S_HEIGHT; i++)
        {
            for (int j = 0; j < S_WIDTH; j++)
            {
                if(j == RealX() && i == RealY())
                {
                    Screen[j][i] = STAR; //star sub
                } else {
                    Screen[j][i] = '\0'; //erase sub
                }
                cout << Screen[j][i]; //print screen sub
            }
        }			
    }
    
    bool FPS(int fps)
    {
    	Time = GetTickCount() - StartingTime;
    
    	if (Time > fps)
    		return true;
    	else 
    		return false;
    }
    
    bool InitVariables()
    {
    	// Global Vars
    	X = 0;
    	Y = 0;
    	Screen[RealX()][RealY()] = STAR;
    
    	// Global specific Vars: FPS
    	Fps = 100;
    	StartingTime = GetTickCount();
    	Time = 0;
    
    	// Global specific Vars: Changement
    	R = 0;
    	Change = false;
    
    	return true;
    }
    
    int FullScreen()
    {
    	keybd_event(VK_MENU,0x38,0,0);
    	keybd_event(VK_RETURN,0x1c,0,0);
    	keybd_event(VK_RETURN,0x1c,KEYEVENTF_KEYUP,0);
    	keybd_event(VK_MENU,0x38,KEYEVENTF_KEYUP,0);
    	return 0;
    }
    
    void MoveStar()
    {
    	switch (R)
    	{
    	case 0:
    		if (!North())
    			Change = true;
    		break;
    	case 1:
    		if (!NorthEast())
    			Change = true;
    		break;
    	case 2:
    		if (!East())
    			Change = true;
    		break;
    	case 3:
    		if (!SouthEast())
    			Change = true;
    		break;
    	case 4:
    		if (!South())
    			Change = false;
    		break;
    	case 5:
    		if (!SouthWest())
    			Change = true;
    		break;
    	case 6:
    		if (!West())
    			Change = true;
    		break;
    	case 7:
    		if (!NorthWest())
    			Change = true;
    		break;
    	}
    
    	if (Change)
    	{
    		R = rand() % 8;
    		Change = false;
    	}
    }
    
    
    // STAR Directions
    bool North()
    {
    	if (RealY() > 0)
    	{
    		Y--;
    		return true;
    	} 
    	else
    		return false;
    }
    
    bool NorthEast()
    {
    	if (RealY() > 0 && RealX() < S_WIDTH)
    	{
    		Y--;
    		X++;
    		return true;
    	} 
    	else
    		return false;
    }
    
    bool East()
    {
    	if (RealX() < S_WIDTH)
    	{
    		X++;
    		return true;
    	} 
    	else
    		return false;
    }
    
    bool SouthEast()
    {
    	if (RealY() < S_HEIGHT && RealX() < S_WIDTH)
    	{
    		Y++;
    		X++;
    		return true;
    	} 
    	else
    		return false;
    }
    
    bool South()
    {
    	if (RealY() < S_HEIGHT)
    	{
    		Y++;
    		return true;
    	} 
    	else
    		return false;
    }
    
    bool SouthWest()
    {
    	if (RealY() < S_HEIGHT && RealX() > 0)
    	{
    		Y++;
    		X--;
    		return true;
    	} 
    	else
    		return false;
    }
    
    bool West()
    {
    	if (RealX() > 0)
    	{
    		X--;
    		return true;
    	} 
    	else
    		return false;
    }
    
    bool NorthWest()
    {
    	if (RealY() > 0 && RealX() > 0)
    	{
    		Y--;
    		X--;
    		return true;
    	} 
    	else
    		return false;
    }
    As you can see, the star goes on in a straight line until it hits an edge, then it goes in a random direction and so on. it works fine, the only problem now is when it gets to the lower part of the screen, it dissapears! I cant figure out why this happens but I think it has to do with the same old problem....
    Why drink and drive when you can smoke and fly?

  4. #4
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    If it disappears at the bottom of the screen, this is maybe a sync problem. You star is drawn and erased by "cls" before the monitor has a chance to display it.

    On a sidenote, your timer-based method to create a constant fps environment is rather... CPU intensitive. Your code produces a program with 100% percessor usage.

    Instead of using a timer and loop until it exceeds a value, try using sleep(int msec) if it's available to your compiler/platform. It will put the machine in sleep mode msec milliseconds. FPS=1/msec.

    I didn't run your program, but I hope this helped anyhow.


    edit: why do you use cls at all ? Doesn't void DrawScreen() already clear every character on the screen? (the \0 line)
    Last edited by darksaidin; 07-26-2003 at 04:19 PM.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Well if I dont use cls, it will print the whole 2d array all over again under the other one, so things get screwed up.
    And thanks for the timer tip!
    Why drink and drive when you can smoke and fly?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console app termination
    By C+/- in forum C++ Programming
    Replies: 2
    Last Post: 05-24-2008, 12:29 PM
  2. Console App w/ Threads and Events?
    By sean in forum C# Programming
    Replies: 1
    Last Post: 07-02-2004, 01:16 AM
  3. Need help migrating console app to windows app
    By DelphiGuy in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2004, 07:05 PM
  4. MSVC Console app - two enters?
    By LuckY in forum Windows Programming
    Replies: 4
    Last Post: 12-30-2003, 02:13 PM
  5. Project Builder console app
    By Luigi in forum C++ Programming
    Replies: 0
    Last Post: 12-28-2002, 07:57 PM

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