Small 2D Console Game Example! // Tutorial 3 attatchment

in #programming8 years ago (edited)

img

I know this will come as a shock to you but if you followed my tutorial that reffered you to this, you can already create small 2d games!

Don't believe me? Damn. I guess I have to show you that you lack imagination.
If you didn't know, the roguelike games like Nethack have been a craze for quite a long time!
Go ahead google it!
img

This was called good graphics back in the day, because it had colors.

img
What? People playing ugly text based games with no graphics?
Yes. I know it comes as a shock to you but computers didn't always have graphics, hell they didn't even have video cards either!
So the first games that came out were simply. Text.

Now I'm going to show you an example that uses only things that you've already read.

I have to mention, the console isn't meant for displaying games, if you really would want to make a console game,

you would have use a library such as [ncurses](Link to library)! Otherwise the console would render everything way

too slowly even for modern computers!

##But how can I even begin to do that!?
Well you're not being creative, or you're not using your imagination and I'm going to prove it to you!
How do you make a 2d game?
Well first what does that 2D stand for?
Dimensions. 2 Dimensions be precise.
Height And Width.
X and Y

Alright but how do I display that on c++?

You will need two variables to process them.

Processing variables (as I like to call them)

Yeah, it's really quite simple in principle
here's a simple example to demonstrate.

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char chr[] = "Hello!";
    int proc = 0;
    int done = sizeof(chr);
    while(proc<=done)
    {
        cout << chr[proc];
        proc++;
    }
    system("pause");
    return 0;
}

Again, this is all done with imagination.
So now you should have a basic idea of how to print them.
Next what would we need in a game?
You're probably thinking: A player!, but how do I put him there or make him move!?
Simple, you give this player coordonates/a position on our map. (px, py)
Now you're probably saying: Alright that makes a bit of sense.. Still can't understand how to tell the program to place him at that position.
You use if checks while printing the map! If the player's coordonates are at that part of the map, you print the player!
That just leaves one problem, how do we move our player?
We ask the user to enter a for example and when his position is on 2 on the left-right axis we take the players position on that axis and move it to one lower space.
img
(px = 3;)(px--)
That makes sense now right?

Enough blabering, let's open your mind to the posibilities your current knowledge offers you:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
    int x = 9; //Map size on x axis
    int y = 8; //Map size on y axis
    int px = 4;//Player x coordonate
    int py = 4;//Player y coordonate
    int ix = 0;//Processing Variable for printing the map on x axis
    int iy = 0;//Processing Variable  for pringing the map on y axis
    while(1)
    {
    system("cls");//Clearing screen after each move
    string input;
    while(iy <= y)//While map hasn't been printed, or while iy is smaller or equal to max map value on y axis
    {
        if(ix != px && ix != x && iy != y && iy != py)
        {       
                if(ix == 0 || ix == 8) //If possition = 0 create a wall to make a map border same if it's maximum value.
                {
                    cout << "||"; //Vertical map border/wall
                    ix++;
                }
                else if(iy == 0|| iy == 8)
                {
                cout << "==";//Horizontal map wall/border
                ix++;
                }
                else
                {
                cout << "X ";//X stands for map space.
                ix++;
                }
        }
        else if(ix != x && iy == py && ix == px) //If this is our player's position
        {
            cout << "H "; //H stands for the player
            ix++;
        }
        else
        {
            if(ix == 0 || ix == 8)
            {
                cout << "||";
                ix++;
            }
            else if(iy == 0|| iy == 8)
            {
                cout << "==";//Horizontal map wall/border
                ix++;
            }
            else
            {
                cout << "X "; 
                ix++;
            }
        }
        if(ix == x) //When ix processing variable reaches maximum value on x axis then move to new line
        {           //Translation, after we print the first line, we go to the next one.
            cout << endl;
            iy++;
            ix = 0;
        }
    }
    cout << "Which Direction do you wish to move?\n";
    cin >> input;
    if(input == "w" && py != 1 || input == "W" && py != 1)//Up  the 1 represents the minimum position the player
    {                                                       //can be on because 0 is a wall. 
        py--; //Move player one space back on the y axis
        input = "";
        ix = 0;//Resetting ints
        iy = 0;//Resetting ints
    }
    if(input == "s" && py != 7|| input == "S" && py != 8)//Down
    {
        py++; //Move player one space forward on the y axis
        input = "";
        ix = 0;
        iy = 0;
    }
    if(input == "a" && px != 1 || input == "A" && px != 1) //Left
    {
        px--; //Move player one space back on the x axis
        input = "";
        ix = 0;
        iy = 0;
    }
    if(input == "d" && px != 7 || input == "D" && px != 7) //Right
    {
        px++; //Move player one space forward on the x axis
        input = "";
        ix = 0;
        iy = 0;
    }
    if(input == "x" || input == "X")//exit
    {
        return 0;
    }
    }
}

See? I told you. If you followed my tutorials you already know how to do this.

All you needed was a little imagination!

You now have an environment, a player on that environment, and you're capable of moving him!
Of course this is just a very rough and simple example, but I'm very sure you're much more aware of your
possibilities right now.
Feel free to try adding more features to the game, all you need is imagination!

Still confused? Well don't worry most programmers get confused, even experienced ones.

img
When you get confused take a break relax and return later.
Sometimes I go to sleep and dream the solution.
Or if I stay up all day working on a project and get stuck somewhere usually I sleep off a little and then I'll figure it out in about 2 minutes.

Now, the point is you need to start thinking about how to convert ideas into programs!
That's what a programmer does!

Feel free to ask me anything in the comments
If you liked this and it helped you please upvote & follow to see new tutorials, and also motivate me to keep posting tutorials in return :)
It only takes 3-6 seconds!

The Header Image is of my own design
Everything I've written here is my own work
List of public domain images:
nethack
oohcolors
how2run
ihniwid

Coin Marketplace

STEEM 0.18
TRX 0.14
JST 0.029
BTC 58068.07
ETH 3133.85
USDT 1.00
SBD 2.44