2D Game example->Added colors!

in programming •  8 years ago  (edited)

img

A continuation of the 2d game example, with colors, and moving the player without pressing enter each time.

gameimg
If you only want to see the game concept, you can download it HERE.

IMPORTANT: Don't download .exe files without trusting the source, they could contain viruses.
When looking at tutorial examples it's best to compile them yourself.
If you're afraid I put a virus in it, you can also check the file dimensions after compiling with dev c++. If it's different in size then I probably infected it. Since you are supposedly becoming a programmer you should AT LEAST know this ^_^.
The controls are:
W = UP
S = DOWN
A = LEFT
D = RIGHT
By the way, control+c will exit any application in the console.
Disclaimer: This method requires you to adjust to your keyboards alt-codes.
Google them and adjust them.

This is for those who perhaps want to go even forward with it, and it also shows you why you shouldn't use the windows or linux console without ncurses to do games such as this, the console's rendering is simply... slow.

Besides that it renders character by character, not frame per frame.
Here is the code so you can compile it yourself:

#include <iostream>
#include <string>
#include <windows.h>
#include <conio.h>
using namespace std;
int x(0), y(0);
int walla[80];
int wallb[80];
int wallc = 0;
int maxsizea = 20;
int maxsize(maxsizea+1);
int render()
{
    int a(0);
    int b(0);
    HANDLE hConsole;
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    a:
    while(a <= maxsize)
    {
               SetConsoleTextAttribute(hConsole, 34);
               if(a == maxsize && b != maxsize)
               {
                    cout << "\n";
                    b++;
                    a = 0;
               }
               if(b==maxsize)
               {
                        break;
               }
               if(x==a&&y==b)
               {
                             SetConsoleTextAttribute(hConsole, 3);
                             cout << " 0 ";
                             a++;
                             SetConsoleTextAttribute(hConsole, 2);
               }
               else if(b == 0 || b == maxsizea || a == 0 || a == maxsizea)
               {
                SetConsoleTextAttribute(hConsole, 136);
                cout << "[=]";
                walla[wallc]=a;
                wallb[wallc]=b;
                wallc++;
                a++;
                SetConsoleTextAttribute(hConsole, 2);
               }
               else
               {
                cout << " + ";
                a++;
               }          
    }
    
    SetConsoleTextAttribute(hConsole, 3);
    cout << endl;
}
int main()
{
    x = maxsizea/2;
    y = maxsizea/2;
    render();
int c(0);
while(1)
{
switch(c=getch()) // Get input, taken from c before cpp 
{
case 119: // Code for up.
     if(y!=1)
     {
       system("cls");
       y--;
       render();//key up
       cout << "Coords \nX: " << x << "\n" << "Y: " << y << endl;
     }
     break;
case 115:
     if(y != maxsize-2)
     {
     system("cls");
     y++;
     render();// key down
     cout << "Coords \nX: " << x << "\n" << "Y: " << y << endl;
     }
     break;
case 100:
     if(x!=maxsize-2)
     {
       system("cls");
       x++;
       render();//key right
       cout << "Coords \nX: " << x << "\n" << "Y: " << y << endl;
     }
     break;
case 97:
     if(x!=1)
     {
       system("cls");
       x--;
       render();//key left
       cout << "Coords \nX: " << x << "\n" << "Y: " << y << endl;
     }
     break;
default:
    break;
}
}
}

If you enjoy what I do, please don't forget to upvote and Foo

Everything written, or linked here is of my own work & design

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!