본문 바로가기
<TOY PROJECT>/CPP_CONSOLE

2. 간단한 탑뷰 게임

by CodeGrimie 2021. 1. 27.

출구로 나가면 게임이 종료된다.

코인을 먹으면 카운트는 올라간다..

 

▼ 실행화면

 

▼ 전체코드

#include <cstdio>
#include <clocale>
#include <windows.h>

const static unsigned int FRAME = 30;
const static unsigned int FPS = (1000 / FRAME);

const static unsigned int WIDTH = 10;
const static unsigned int HEIGHT = 10;

const static unsigned int KEY_LEFT = 37;
const static unsigned int KEY_TOP = 38;
const static unsigned int KEY_RIGHT = 39;
const static unsigned int KEY_DOWN = 40;
const static unsigned int KEY_EXIT = 27;

static bool isGameRunning = false;
static int  coinCount = 0;

HANDLE                      hConsole;
CONSOLE_SCREEN_BUFFER_INFO  consoleInfo;
WORD                        saved_attributes;

static int map[WIDTH * HEIGHT] = {
    0,0,0,0,0,0,0,0,0,0,
    0,1,1,1,1,1,1,1,1,0,
    0,1,1,1,1,1,1,1,1,0,
    0,1,1,1,1,1,1,1,1,0,
    0,1,1,1,1,1,1,1,1,0,
    0,1,1,1,1,1,1,1,1,0,
    0,1,1,1,1,1,1,1,1,0,
    0,1,1,1,1,1,1,1,1,0,
    0,1,1,1,1,1,1,1,1,0,
    0,0,0,0,0,0,0,0,0,0
};

typedef struct _Point {
    int x;
    int y;
}Point;

Point player = { WIDTH /4, HEIGHT / 2 };
Point saved = { player.x, player.y };
Point exitTile = { 4, 2 };

Point coinTile[5] = {
    {2,2},
    {3,3},
    {6,6},
    {7,7},
    {5,5},
};

int GetKey()
{
    while (true)
    {
        for (int i = 8; i <= 256; i++)
        {
            if (GetAsyncKeyState(i) & 0x7FFF)
            {
                if ((i >= 37 && i <= 40) || i == 27)
                    return i;
            }
        }
    }
}

void Init()
{
    _wsetlocale(LC_ALL, L"Korean");
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
    saved_attributes = consoleInfo.wAttributes;

    isGameRunning = true;
    map[player.x + 10 * player.y] = 2;
    map[exitTile.x + 10 * exitTile.y] = 3;
    for (int i = 0; i < 5; i++)
    {
        map[coinTile[i].x + 10 * coinTile[i].y] = 4;
    }
}

void Quit()
{
    SetConsoleTextAttribute(hConsole, saved_attributes);
}

void Input()
{
    saved.x = player.x;
    saved.y = player.y;

    int input = GetKey();
    switch (input)
    {
    case KEY_LEFT:
    {
        if (player.x > 1)
            player.x--;
        wprintf(L"키보드 : LEFT\n");
    }
    break;
    case KEY_TOP:
    {
        if (player.y > 1)
            player.y--;
        wprintf(L"키보드 : TOP\n");
    }
    break;
    case KEY_RIGHT:
    {
        if (player.x < WIDTH - 2)
            player.x++;
        wprintf(L"키보드 : RIGHT\n");
    }
    break;
    case KEY_DOWN:
    {
        if (player.y < HEIGHT - 2)
            player.y++;
        wprintf(L"키보드 : DOWN\n");
    }
    break;
    case KEY_EXIT:
    {
        wprintf(L"키보드 : ESCAPE\n");
        isGameRunning = false;
    }
    break;
    default:
    {
        printf("KEY_CODE : %d\n", input);
    };
    }
}

void Update()
{
    map[saved.x + 10 * saved.y] = 1;
    map[player.x + 10 * player.y] = 2;

    for (int i = 0; i < 5; i++)
    {
        if (player.x == coinTile[i].x && player.y == coinTile[i].y)
        {
            coinCount++;
            coinTile[i].x = -1;
            coinTile[i].y = -1;
        }
    }

    if (player.x == exitTile.x && player.y == exitTile.y)
        isGameRunning = false;
}

void Render()
{
    wprintf(L"〓〓〓〓〓〓〓〓〓〓\n");
    wprintf(L"〓   ㈜의  모험   〓\n");
    wprintf(L"〓〓〓〓〓〓〓〓〓〓\n");

    for (int i = 0; i < WIDTH * HEIGHT; i++)
    {
        if (i > 0 && i % 10 == 0)
            printf("\n");

        if (map[i] == 0)
            wprintf(L"■");
        else if (map[i] == 1)
            wprintf(L"□");
        else if (map[i] == 2)
            wprintf(L"㈜");
        else if (map[i] == 3)
            wprintf(L"▣");
        else if (map[i] == 4)
            wprintf(L"♡");
    }
    printf("\n");

    wprintf(L"코인 : %d\n", coinCount);
}

int main()
{
    Init();

    while (isGameRunning)
    {
        Render();
        Input();
        Update();
        Sleep(FPS);
        system("cls||clear");
    }

    Quit();
    return (0);
}

댓글