console game woes

So I am designing a game, and it will have the standard puzzle things that console games usually do.
My first problem came up when I made the structure containing the string that becomes the graphics on-screen (i.e. "||| |||
|||---|o|
||| |||" for a door).
I wrote this:
1
2
3
4
5
 struct maze_locales
{
   string locale_1 = "<string content>";
   string locale_2 = "<string content>";
}

And when I compile it to test to see if it works, it says
Error: ISO C++ forbids initialization of member 'locale_1"
Error: making 'locale_1' static

What is going wrong?

Question 2: how do I make a console application respond to arrow key inputs?
Last edited on
You can't initialize a variable in a structure unless it is defined as static, you need to do it in the structures constructor, or a function in the structure.
Last edited on
You have to initialize your string in its struct/class constructor.

you need to do it in the structures constructor, or a function in the structure.

You cannot initialize it in a function in the structure, the default constructor would have already been called and the string would have already been initialized.
Last edited on
right, I meant if the constructor didn't initialize it, AFAIK its allowed to not initialize something.

1
2
3
4
5
6
7
8
9
10
struct hai
{
    int hai;
};

int main()
{
    hai hai;
    std::cout << hai.hai << std::endl;
}



warning: 'hai.hai::hai' is used uninitialized in this function [-Wuninitialized]|


although, a string does get initialized, I guess because it isn't a built-in type and actually has a constructor.
Last edited on
A string is an object, if no constructor was explicitly called, the default constructor of the string is automatically called in the struct's/class' default constructor.
Well, I'm working on a console game (as you know), let me give you some tips:


1.- Define correctly the libraries you're going to use. Allegro or Curses are (very) good options when wanting cross-platform console games, windows.h and conio.h are the best options when only using Windows if you don't want external libraries. Search ways to change the window title or instant pausing/key scanning, things like that.

2.- Use medium-sized ASCII images for EVERYTHING, makes the game much nicer and gives a better playing environment for text/puzzle games. Do not exceed 40-50 characters width or 10 lines high (The heigth can be greater on minigames). Remember to make cool main menus, and maybe add text color in some situations.

3.- Work on the 2 basic and essential global functions if your libraries don't satisfy you enough: Pausing and screen clearing. Personalize them with parameters, like using beeps (or sound) or not in what situations, or show or not "Press any key" or something like that when pausing (remember bool variables). The other 2 important (but optional) functions/qualities are sound (usually beeps) and text color (Don't abuse it very much, as it can get hard to understand and make the code like if for example you want a completely colored ASCII image).

4.- Using the arroy keys or the backspace and enter keys are very good ideas to select an option, or a slot in some puzzle, but remember, every single time you press a key, your functions and/or classes need to know what key you pressed and how to handle it, and a pointer for you to know what option/slot/whatever is selected is needed too, along with an obvious screen refreshing (clearing) each time.
For most Windows computers, (using an instant pausing/key scanning) the numbers for the most used keys in this situations are 72 for up arrow key, 80 for down arrow key, 77 for right arrow key, 75 for left arrow key, 13 (or 10) for enter key and 8 for backspace.

5.- Always be open for new ideas, and keep trying to accomplish what you want. You want a Tic-Tac-Toe with computer as the opponent? You want a combination lock working with the arrow keys? You want a labyrinth with "bird view"? You can always have help from the internet, your teacher, and your own mind.



PD: I recommend you to check the code of my game Skeptor (you already know it) for things like option selection or the paths/places minigame. It could help you with an idea of what I mean with the 5 points I mentioned before. Hope I helped you.
Last edited on
@samrux:
Thanks!
Oh, and remember:

1.- ASCII images coded with C++ need strange codes, IDs or whatever for characters over ASCII symbol 120. Also, when wanting to show " , you need \" , and fora single backslash you need two (\\).

2.- There are some things that will always help you in console C++ games, arrays (singledimensional and multidimensional), rand() (with srand(time(0))), loops and classes. Also, as always, avoid global variables, system() and gotos.
Last edited on
If you have a C++11 compiler:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
#include <iostream>

struct maze_locales
{
    std::string door_1 = R"(|||   |||
|||---|o|
|||   |||)" ;

    std::string door_2 = R"(!!!   !!!
!!!===!x!
!!!   !!!)" ;

};

int main()
{
    maze_locales ml ;
    std::cout << ml.door_1 << "\n\n\n" << ml.door_2 << '\n' ;
}

Output:

|||   |||
|||---|o|
|||   |||


!!!   !!!
!!!===!x!
!!!   !!!


http://www.stroustrup.com/C++11FAQ.html#member-init
http://www.stroustrup.com/C++11FAQ.html#raw-strings
Topic archived. No new replies allowed.