Jump to content
atemus

C++ Joc Consola fara refresh

Recommended Posts

Salut RST, sunt nou pe forum si m-am gandit sa creez primul meu topic cu un program 'easy, intrucat foarte multi tineri de liceu doresc sa creeze un joculet intr-o consola si nu au nicio idee despre aceasta. Tehnica mea presupune urmatorul lucru, ci anume nu apelam functia de sistem: system("cls"), pentru a putea da refresh la consola, ci pur si simplu parcurgem fiecare caracter mutand practic pozitia cursorului din consola. Pentru a putea crea o mica grafica la jocul nostru, putem foarte bine sa afisam o matrice cu N linii si M coloane dupa care utilizand tehnica de mai sus putem strabate fiecare caracter din matricea noastra fara a mai da refresh la consola. 

 

De asemenea puteti utiliza aceasta tehnica si pentru a putea crea diverse jocuri cum ar fi snake, chiar si mario utilizand codul ASCII :D  Si pentru ca tot am abordat subiectul Mario, lansez un challenge pentru tinerii din liceu cu aceasta tema, utilizand tehnica mea descrisa mai sus :D 

 

PS: "Codul a fost compilat cu succes in code blocks, daca doriti sa-l compilati in alt editor, atunci trebuie sa rezolvati pe cont prorpiu erorile aparute"

 

#include <iostream>
#include <windows.h>

#define LINE    20
#define COLUMN  40

#define BLACK       0
#define GRAY        8
#define BLUE        1
#define LIGHTBLUE   9
#define AQUA        3
#define LIGHTAQUA   11
#define RED         4
#define LIGHTRED    12
#define PURPLE      5
#define LIGHTPURPLE 13
#define YELLOW      6
#define LIGHTYELLOW 14
#define WHITE       7
#define LIGHTWHITE  15
#define GREEN       2
#define LIGHTGREEN  10

using namespace std;

char MAP[LINE][COLUMN] =   {"#######################################",
                            "#                                     #",
                            "#                                     #",
                            "#                                     #",
                            "#                                     #",
                            "#                                     #",
                            "#                                     #",
                            "#              @                      #",
                            "#                                     #",
                            "#                                     #",
                            "#                                     #",
                            "#                                     #",
                            "#                                     #",
                            "#                                     #",
                            "#                                     #",
                            "#                                     #",
                            "#                                     #",
                            "#######################################"};

void gotox(int x, int y)
{
    COORD coord;
    coord.X = y;
    coord.Y = x;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void ShowConsoleCursor(bool showFlag)
{
    HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);

    CONSOLE_CURSOR_INFO     cursorInfo;

    GetConsoleCursorInfo(out, &cursorInfo);
    cursorInfo.bVisible = showFlag; // set the cursor visibility
    SetConsoleCursorInfo(out, &cursorInfo);
}


char getCursorChar()    /// Function which returns character on console's cursor position || Totally not copied from the Internet
{
    char c = '\0';
    CONSOLE_SCREEN_BUFFER_INFO con;
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hcon != INVALID_HANDLE_VALUE &&
        GetConsoleScreenBufferInfo(hcon,&con))
    {
        DWORD read = 0;
        if (!ReadConsoleOutputCharacterA(hcon,&c,1,
            con.dwCursorPosition,&read) || read != 1
            )
            c = '\0';
    }
    return c;
}

char readChar(int x,int y)  /// Function which reads character at specific coordinates
{
    gotox(x,y);
    char ccccc = getCursorChar();
    return ccccc;
}

void setColor(WORD c)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

void out(char* t, WORD c)
{
    setColor(c);
    cout<<t;
    setColor(LIGHTWHITE);
}

void oout(char t, WORD c)
{
    setColor(c);
    cout<<t;
    setColor(LIGHTWHITE);
}

void cls()
{
    system("cls");
}

void drawGraphic(void)
{
    cls();
    for(int i=0; i<LINE; i++)
    {
        for(int j=0; j<COLUMN; j++)
        {
            switch(MAP[i][j])
            {
            case '#':
                {
                    oout(MAP[i][j], LIGHTYELLOW);
                }break;
            case ' ':
                {
                    cout<<" ";
                }break;
            case '@':
                {
                    oout(MAP[i][j], LIGHTRED);
                }
            }
        }
        cout<<endl;
    }
}

void setMenu(int speed, int x, int y)
{
    gotox(0, COLUMN);

    setColor(LIGHTGREEN);
    cout<<"Speed: "<<speed<<" NUM 8 - increase slow"<<endl;
    gotox(1, COLUMN + 9);
    cout<<" NUM 2 - decrease speed"<<endl;
    gotox(3, COLUMN);
    cout<<"Position: "<<x<<" ; "<<y<<endl;
    gotox(5, COLUMN);
    cout<<"SPACE: * block"<<endl;
    gotox(6, COLUMN);
    cout<<"NUM 0: Remove all blocks"<<endl;
    setColor(LIGHTWHITE);
}

int main()
{
    ShowConsoleCursor(false);
    drawGraphic();

    int speed = 60;
    setMenu(speed, 0, 0);

    char player = (char)16;

    while(true)
    {
        for(int i=0; i<LINE; i++)
        {
            for(int j=0; j<COLUMN; j++)
                switch(readChar(i, j))
                {
                case '@':
                    {
                        setMenu(speed, i, j);
                        if(GetAsyncKeyState(VK_UP) != 0)
                        {
                            if(readChar(i-1, j) == ' ')
                            {
                                gotox(i, j);
                                oout(' ', LIGHTRED);
                                gotox(i-1, j);
                                oout('@', LIGHTRED);
                            }
                            Sleep(speed - 30);
                        }
                        else if(GetAsyncKeyState(VK_DOWN) != 0)
                        {
                            if(readChar(i+1, j) == ' ')
                            {
                                gotox(i, j);
                                oout(' ', LIGHTRED);
                                gotox(i+1, j);
                                oout('@', LIGHTRED);
                            }
                            Sleep(speed);
                        }
                        else if(GetAsyncKeyState(VK_LEFT) != 0)
                        {
                            if(readChar(i, j-1) == ' ')
                            {
                                gotox(i, j);
                                oout(' ', LIGHTRED);
                                gotox(i, j-1);
                                oout('@', LIGHTRED);
                            }
                            Sleep(speed - 30);
                        }
                        else if(GetAsyncKeyState(VK_RIGHT) != 0)
                        {
                            if(readChar(i, j+1) == ' ')
                            {
                                gotox(i, j);
                                oout(' ', LIGHTRED);
                                gotox(i, j+1);
                                oout('@', LIGHTRED);
                            }
                            Sleep(speed);
                        }
                        else if(GetAsyncKeyState(VK_NUMPAD8) != 0)
                        {
                            speed += 10;
                            Sleep(speed);
                            setMenu(speed, i, j);
                        }
                        else if(GetAsyncKeyState(VK_NUMPAD2) != 0)
                        {
                            if(speed <= 30)
                                setMenu(30, i, j);
                            else
                            {
                                speed -= 10;
                                Sleep(speed);
                                setMenu(speed, i, j);
                            }
                        }
                        else if(GetAsyncKeyState(VK_SPACE) != 0)
                        {
                            if(readChar(i-1, j) == ' ')
                            {
                                gotox(i-1, j);
                                oout('*', LIGHTBLUE);
                                Sleep(speed);
                            }
                            else if(readChar(i+1, j) == ' ')
                            {
                                gotox(i+1, j);
                                oout('*', LIGHTBLUE);
                                Sleep(speed);
                            }
                            else if(readChar(i, j-1) == ' ')
                            {
                                gotox(i, j-1);
                                oout('*', LIGHTBLUE);
                                Sleep(speed);
                            }
                            else if(readChar(i, j+1) == ' ')
                            {
                                gotox(i, j+1);
                                oout('*', LIGHTBLUE);
                                Sleep(speed);
                            }
                        }
                        else if(GetAsyncKeyState(VK_NUMPAD0) != 0)
                        {
                            if(readChar(i-1, j) != ' ')
                            {
                                gotox(i-1, j);
                                oout(' ', LIGHTBLUE);
                                Sleep(speed);
                            }
                            else if(readChar(i+1, j) != ' ')
                            {
                                gotox(i+1, j);
                                oout(' ', LIGHTBLUE);
                                Sleep(speed);
                            }
                            else if(readChar(i, j-1) != ' ')
                            {
                                gotox(i, j-1);
                                oout(' ', LIGHTBLUE);
                                Sleep(speed);
                            }
                            else if(readChar(i, j+1) != ' ')
                            {
                                gotox(i, j+1);
                                oout(' ', LIGHTBLUE);
                                Sleep(speed);
                            }
                        }
                        else if(GetAsyncKeyState(VK_ESCAPE) != 0)
                        {
                            exit(0);
                        }
                    }break;
                }
        }
    }
    return 0;
}

 

Edited by atemus
  • Thanks 1
  • Upvote 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...