No ASCII characters displayed

I am trying to make a simple console (ASCII) game and when I try to print out characters in ascii, I just get a bunch of weird random stuff on the screen when i try to compile it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  // Copyright (C) 2014 by IceStorm. All Rights Reserved

//Create an ASCII Game using Text.

#include <iostream>
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <windows.h>

#include "Enemy.h"
#include "Levels.h"

using namespace std;

//Create a boolean variable that will hold the current state of the game (initialized to false).
bool stopGame = false;

//Create an int that will hold the current game speed
int gameSpeed = 0000;

int main(void)
{
for (int x=0; x<=20; x++) {
cout <<"		 █████╗ ███████╗ ██████╗██╗██╗  " <<endl;
cout <<"		██╔══██╗██╔════╝██╔════╝██║██║     " <<endl;
cout <<"		███████║███████╗██║     ██║██║      " <<endl;
cout <<"		██╔══██║╚════██║██║     ██║██║      " <<endl;
cout <<"		██║  ██║███████║╚██████╗██║██║      " <<endl;
cout <<"		╚═╝  ╚═╝╚══════╝ ╚═════╝╚═╝╚═╝      " <<endl;
cout <<"	 		                           " <<endl;
cout <<"	 	 ██████╗  █████╗ ███╗  ███╗███████╗" <<endl;
cout <<"		██╔════╝ ██╔══██╗████╗ ████║██╔════╝" <<endl;
cout <<"		██║  ███╗███████║██╔████╔██║█████╗  " <<endl;
cout <<"		██║   ██║██╔══██║██║╚██╔╝██║██╔══╝  " <<endl;
cout <<"		╚██████╔╝██║  ██║██║ ╚═╝ ██║███████╗" <<endl;
cout <<"		╚═════╝ ╚═╝  ╚═╝╚═╝     ╚═╝╚══════╝" <<endl;


}
system ("PAUSE");

while (stopGame == false) { //Enter a while loop if the game is not stopped to examine key states

MainMenu:
cout <<"\t\t\t\tMAIN MENU\n\n"<< endl;
cout <<"\t\t\tNew Game "<<  endl;
cout <<"\t\t\tHow To Play"<< endl;
cout << endl;
system("cls"); //refresh

//Examine key states

if (GetAsyncKeyState(VK_UP)) { //If the up key state is returned as true, highlight the first option
while (stopGame == false){
cout <<"\t\t\t\tMAIN MENU\n\n"<< endl;
cout <<"\t\t\t*New Game "<<  endl;
cout <<"\t\t\tHow To Play"<< endl;
cout << endl;
system("cls") //refresh
}
}

if (GetAsyncKeyState(VK_RETURN)) {
break;
}

else if (GetAsyncKeyState(VK_DOWN)){

cout <<"\t\t\t\tMAIN MENU\n\n"<< endl;
cout <<"\t\t\tNew Game" <<  endl;
cout <<"\t\t\t*How To Play"<< endl;
cout << endl;

system("cls");

cout << "The object of the game is to get to the exit \"<\" without dying. \n";
cout << "There are many types of enemies in this game. They are listed in order of lowest to highest precedence: \n";
cout << "P = Minion\n S = Shooter\n N = Neutral\n M = Mace\n B = Boss\n";
cout << endl;
cout << "The most common type of enemy is the Minion. The minions move in a certain way across the map and only do damage if you touch it. The Shooter is a type of enemy \n";
cout << "which shoots projectiles in your direction. Projectiles vary in type and will be discussed after all the enemies have been discussed. Neutrals are enemies that \n";
cout << "are just like Minions, but they do not harm you if you touch them. But, if you shoot a projectile at them, They will start to damage you severely and also make \n";
cout << "surrounding neutrals attack you. Maces are enemies that have the ability to resurrect dead enemies and can also teleport to various places on the map. But, they \n";
cout << "have bad eyesight and cannot see you unless you are within a close distance (usually a radius of two units) to them and can be avoided as long as you don't provoke \n";
cout << "them or get close to them. A Boss is a final enemy found at the end of each course and their abilities vary. Usually they are powerful and can do serious damage to you.\n";
cout << " There are different types of weapons in this game, they are divided into Projectile Weaponry and Point Blank Weaponry \n";
cout << "\t\t\tProjectiles: "<< endl;
cout << "- = Bullet\n + = throwing knife\n * = Grenade \n";
cout << endl;
cout << " A bullet is the standard type of";
}
}
Break:
    for (int load = 0; load < 10; load++) { //Generate a false loading screen
     cout << "\n\n\n\t\t\tLOADING.";
     Sleep(0500);
     system("cls");
     cout << "\n\n\n\t\t\tLOADING..";
     Sleep(0500);
     system("cls");
     cout << "\n\n\n\t\t\tLOADING...";
     Sleep(0500);
     system("cls");
    }
    bool courseClear = levelOne();
    while (stopGame) {
        if (stopGame == true)
            break;
    }




return 0;
}


It just prints out a bunch of weird text when it runs ASCII game. I tried to make it print it out using wide characters (wcout) but that didnt print anything at all. Help?
Last edited on
The problem is that those characters are not standard ASCII. I don't know what encoding the Windows command prompt uses by default (I think it can differ) but maybe it will work if you make sure to save the file with the same encoding.
How do I do that?
I am trying to make a simple console (ASCII) game


There's your first problem. Get SFML and just make a graphic game. It's easier.

when I try to print out characters in ascii, I just get a bunch of weird random stuff on the screen when i try to compile it.


You are not printing ASCII. You are trying to print extended characters that are outside of ASCII. This is notoriously difficult on windows.


This depends on 2 things:

1) The way your cpp file is being saved
2) The way your console is accepting text output


To put it simply... characters are really numbers. Each number represents a different character. Which number represents which character depends on how the text is encoded. In one encoding one number might be a different character than in another encoding.

ASCII is pretty much standard and uniform everywhere. But it only covers the basic English alphabet and a few common symbols (anything on a US keyboard is ASCII... the extra "box" characters you are using are not)


What is probably happening is your source code is being saved as UTF-8... but when that UTF-8 text is being sent to the console... the console is interpretting it as Extended-ASCII... which is why you are getting garbage.

Your file and the console must be encoded the same way.


So you have 2 options here:

1) Change the console encoding to match your source code (This can theoretically be done on Windows with SetConsoleCodePage... though I have never gotten it to work properly -- and it's not portable anyway)

or

2) Change your text to match whatever encoding scheme is used by your console. However this is also problematic because different people might be using different encoding schemes. So even if you get it working on your computer... if you give the program to someone else it might not work on theirs.




...or

3) Don't try to make a game in the console. It's retarded.
So you have 2 options here:

Actually three.

There's your first problem. Get SFML and just make a graphic game. It's easier.

The thing is, I'm just a beginner. I don't have the skills to pull off a full fledged 2D game.

Last edited on
If you start small, it's not that hard. A simple vertical shooter is actually very simple to make and will teach you a LOT more about the program flow in games than a text-based game will.



Also... what IDE are you using? I was able to get this to display properly by saving the file encoded as codepage 437. This is easy to do in Visual Studio.

Lastly: why the bogus load screen? Making people wait is lame. I hate it when games make me wait unnecessarily -- or even necessarily.
what IDE are you using?

Code::Blocks

Even though I am a Newb, I atleast have the sense to use C::B instead of the piece of crap called Dev C++. It seems as though every beginner is somehow attracted to it.
why the bogus load screen?

To make it seem more cooler and more like an actual game.

piece of crap called Dev C++
personal opinions are ok, however it should be mentioned that Orwell DevC++ is an acceptable IDE and comes with more or less the same compiler as code::blocks.
http://orwelldevcpp.blogspot.com/
Code::Blocks


Unfortunately, I don't see any way to set the file encodings with C::B.

I also went to see if I could use Notepad++ to change the encoding, but I didn't see codepage 437 in any of the options. It seems like the text editor in VS is the only one that allows me to save in this encoding.


C::B does convert to UTF-8 though. Let me see if I can get SetConsoleCodePage to work....

To make it seem more cooler and more like an actual game.


Load screens are like the one part of games everybody wishes weren't there. Think of it from a gamer perspective... load screens are annoying right?

If you want to make the game seem more like a game... then make it more like a game (ie: graphics). Seriously... SFML makes graphics surprisingly simple and you won't have problems like this weird encoding thing

</soapbox>

I'll reply or edit later if I figure out how to change the console encoding.



EDIT:

So thanks to a SO post, I was successful in getting it to display with wprintf, but not with cout or wcout.

BUT... doing so breaks normal cout output... so I had to change the mode back after printing that stuff.


So it's pretty hackish and certainly is not portable... but this will do what you want on Windows. Note my comments -- I added them where the changes are required:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <io.h> // <- need to add this include for _setmode
#include <fcntl.h> // <- need to add this include for _O_TEXT
#include <cstdio> // <- need to add this include for wprintf

using namespace std;

int main()
{
    _setmode(_fileno(stdout), 0x20000); // <- set the output mode to UTF-16
wprintf(  // <- use wprintf to output UTF16 characters.
  // note how the strings start with an 'L' prefix now
L"		 █████╗ ███████╗ ██████╗██╗██╗\n"
L"		██╔══██╗██╔════╝██╔════╝██║██║\n"
L"		███████║███████╗██║     ██║██║\n"
L"		██╔══██║╚════██║██║     ██║██║\n"
L"		██║  ██║███████║╚██████╗██║██║\n"
L"		╚═╝  ╚═╝╚══════╝ ╚═════╝╚═╝╚═╝\n"
L"\n"
L"	 	 ██████╗  █████╗ ███╗  ███╗███████╗\n"
L"		██╔════╝ ██╔══██╗████╗ ████║██╔════╝\n"
L"		██║  ███╗███████║██╔████╔██║█████╗\n"
L"		██║   ██║██╔══██║██║╚██╔╝██║██╔══╝\n"
L"		╚██████╔╝██║  ██║██║ ╚═╝ ██║███████╗\n"
L"		╚═════╝ ╚═╝  ╚═╝╚═╝     ╚═╝╚══════╝\n" );
// also note I couldn't use wcout... for whatever reason it wasn't working

    _setmode(_fileno(stdout), _O_TEXT); // <- then switch back to normal text mode
      //  or else normal 'cout' usage will not work properly 



Again.. blech. Graphics are easier.
Last edited on
Thanks, @Disch. I will try it as soon as I get to my home computer and I will edit this post if I have any problems.
For contrast... here's how you could do a splash screen with graphics. It's not that hard... and looks and feels a million times better.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <sfml.h>

int main()
{
    // Create our window.  Make it 640 x 480... give it a title that says "My Game"
    sf::RenderWindow wnd( sf::VideoMode(640,480), "My Game" );

    // Set the window so that it updates at 60 FPS.  This will ensure a fixed logic rate so 
    //  the the game will run at a consistent speed no matter how fast the user's computer is.
    wnd.setFramerateLimit(60);

    // Load a PNG file for the splash screen into a texture.
    sf::Texture texture;
    texture.loadFromFile("splashscreen.png");

    // Put that texture in a sprite so we can draw it
    sf::Sprite spr(texture);

    // Simple game loop -- Running this basic game consists of 2 parts:
    //  1)  Checking to see what the user did and responding appropriately
    //  2)  Draw the scene
    //
    // These two things are looped over and over until the program exits

    bool rungame = true;
    sf::Event evt;
    while(rungame)
    {
        // See what the user did
        while(wnd.pollEvent(evt))
        {
            if(evt.type == sf::Event::Closed)       // if they indicated they want to exit the program
                rungame = false;                    //  then kill our game loop
        }

        // Once we processed everything the user did, draw our scene.
        wnd.clear();        // clear everything that was drawn previously
        wnd.draw(spr);      // draw our splash screen sprite
        wnd.display();      // display it so the user can actually see what we just drew.
    }
}
Topic archived. No new replies allowed.