카테고리 없음

c언어 게임 만들기(무궁화 꽃이 피었습니다)

20308박동진 2022. 12. 1. 00:21

#define _FONT_HERSHEY_SCRIPT_COMPLE
#define _CRT_SECURE_NO_WARNINGS
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
#define SUBMIT 4

#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<process.h>

#include <conio.h>
int keyControl();
void init();
void titleDraw();
int menuDraw();
void gotoxy(int x, int y);
void infoDraw();
int maplistDraw();
void drawMapes();
void drawMapno();
void drawMaphd();
void gameLoop(int);
void st();

int main() {
    st();
    init();
    int menuCode = menuDraw();
    while (1) {
        titleDraw();
        int menuCode = menuDraw();
        if (menuCode == 0) {
            int n = maplistDraw();
            if (n == 0) {
                gameLoop(0);
            }
            else if (n == 1) {
                gameLoop(1);
            }
            else if (n == 2) {
                gameLoop(2);
            }
        }
        else if (menuCode == 1) {
            infoDraw();
        }
        else if (menuCode == 2) {
            return 0;
        }
        system("cls");
    }
    return 0;
}

void st() {
    system("mode con cols=56 lines=20 | title C언어 프로젝트");
    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO ConsoleCursor;
    ConsoleCursor.bVisible = 0;
    ConsoleCursor.dwSize = 1;
    SetConsoleCursorInfo(consoleHandle, &ConsoleCursor);
    system("mode con cols=56 lines=20 | title C언어 프로젝트");
    printf("\n\n\n\n\n\n\n\n\n               Press Any Key to Continue\n");

    _getch();
}
void init() {
    system("mode con cols=56 lines=20 | title C언어 프로젝트");

    HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO ConsoleCursor;
    ConsoleCursor.bVisible = 0;
    ConsoleCursor.dwSize = 1;
    SetConsoleCursorInfo(consoleHandle, &ConsoleCursor);
}

void titleDraw() {
    printf("\n\n\n\n");

}

void gotoxy(int x, int y)

{

    COORD pos = { x,y };

    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);

}

int menuDraw() {
    int x = 23;
    int y = 12;
    gotoxy(x - 2, y);
    printf(" > 게임시작 ");
    gotoxy(x, y + 1);
    printf(" 게임정보 ");
    gotoxy(x, y + 2);
    printf(" 종료 ");
    while (1) {
        int n = keyControl();
        switch (n) {
        case UP: {
            if (y > 12) {
                gotoxy(x - 2, y);
                printf("   ");
                gotoxy(x - 2, --y);
                printf(" >");
            }
            break;
        }
        case DOWN: {
            if (y < 14) {
                gotoxy(x - 2, y);
                printf("   ");
                gotoxy(x - 2, ++y);
                printf(" > ");
            }
            break;
        }
        case SUBMIT: {
            return y - 12;
        }
        }
    }
}

int keyControl() {
    char temp = _getch();

    if (temp == 'w' || temp == 'W') {
        return UP;
    }
    else if (temp == 'a' || temp == 'A') {
        return LEFT;
    }
    else if (temp == 's' || temp == 'S') {
        return DOWN;
    }
    else if (temp == 'd' || temp == 'D') {
        return RIGHT;
    }
    else if (temp == ' ') {
        return SUBMIT;
    }
}

void infoDraw() {
    system("cls");
    printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓");
    printf("┃\n┃                                                     ┃\n┃                                                     ┃\n┃                                                     ┃\n┃                                                     ┃\n");
    printf("┃                      [ 조작법 ]                     ┃\n┃                                                     ┃\n");
    printf("┃              이동 : W(w), A(a), S(s), D(d)          ┃\n");
    printf("┃              선택 : 스페이스바                      ┃\n┃                                                     ┃\n┃                                                     ┃\n┃                                                     ┃\n┃                                                     ┃\n┃                                                     ┃");
    printf(" ┃                                                     ┃\n┃                                                     ┃\n┃                                                     ┃\n┃                                                     ┃\n┃     스페이스바를 누르면 메인화면으로 이동합니다.    ┃");
    printf(" ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛");

    while (1) {
        if (keyControl() == SUBMIT) {
            break;
        }
    }
}
int maplistDraw() {
    int x = 23;
    int y = 6;
    system("cls");
    printf("\n\n");
    printf("                      [ 맵 선택 ]");
    gotoxy(x - 2, y);
    printf(" > 쉬움");
    gotoxy(x, y + 1);
    printf(" 보통 ");
    gotoxy(x, y + 2);
    printf(" 어려움 ");
    gotoxy(x, y + 3);
    printf(" 뒤로가기 ");

    while (1) {
        int n = keyControl();
        switch (n) {
        case UP: {
            if (y > 6) {
                gotoxy(x - 2, y);
                printf("   ");
                gotoxy(x - 2, --y);
                printf(" >");
            }
            break;
        }
        case DOWN: {
            if (y < 9) {
                gotoxy(x - 2, y);
                printf("   ");
                gotoxy(x - 2, ++y);
                printf(" > ");
            }
            break;
        }
        case SUBMIT: {
            return y - 6;
        }
        }
    }
}

char tempMap[49][56];

char es[49][56] = {
    {"                                                        "},
    {"                                                        "},
    {"                                                        "},
    {"                                                        "},
    {"                                                        "},
    {"                                                        "},
    {"11111111111111111111111111111111111111111111111111111111"},
    {"00000000000000000000000000000111100000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00111100000000000000000000000000000000111110000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"01111000000000000000000111110000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00111100000000000000000000000000000000111110000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000111110000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000001111000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000111100000000000000000000000"},
    {"00000000001111000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000011000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000111100000000000000000000000"},
    {"00000000001111000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000011000000000"},
    {"00000000000000000000000000000000000000001110000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
     {"00000000001111000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000011000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000001111100000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000111000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000111110"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"11111111111111111111111111111111111111111111111111111111"},
};
char no[49][56] = {
    {"                                                        "},
    {"                                                        "},
    {"                                                        "},
    {"                                                        "},
    {"                                                        "},
    {"                                                        "},
    {"11111111111111111111111111111111111111111111111111111111"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000011110000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000001111000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000011110000000"},
    {"00000000000000000000111100000000000000000000000000000000"},
     {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000011000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
      {"00000000000000000000111100000000000000000000000000000000"},
     {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000011000000000000000000000000000"},
    {"00000001100000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000011110000000"},
     {"00000000000000000000000000000000000000000000000000000000"},
  {"00000000000000000000111100000000000000000000000000000000"},
     {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000001110000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000001111000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000011110000000000000000000000000011110000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000001111000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000011110000000000000000000000000011110000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"11111111111111111111111111111111111111111111111111111111"},
};
char hd[49][56] = {
    {"                                                        "},
    {"                                                        "},
    {"                                                        "},
    {"                                                        "},
    {"                                                        "},
    {"                                                        "},
    {"11111111111111111111111111111111111111111111111111111111"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000110000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000010000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000001100000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000010000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000001100000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000110000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000010000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000001100000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000011110000000000000000000000000010000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"00000000000000000000000000000000000000000000010000000000"},
    {"00000000000000000000000000000000000000000000000000000000"},
    {"11111111111111111111111111111111111111111111111111111111"},
};
void drawMapes() {
    system("cls");
    system("mode con cols=56 lines=50 | title C언어 프로젝트");
    for (int i = 0; i < 49; i++) {
        for (int j = 0; j < 56; j++) {
            char temp = tempMap[i][j];
            if (temp == '0') {

                printf(" ");
            }
            else if (temp == '1') {
                printf("#");
            }
            else if (temp == 's') {
                printf("@");
            }
            else if (temp == 'q') {
                printf("O");
            }
        }
        printf("\n");
    }
}
void drawMapno() {
    system("cls");
    for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 56; j++) {
            printf("%c", es[i][j]);
        }
        printf("\n");
    }
    Sleep(3000);
}
void drawMaphd() {
    system("cls");
    for (int i = 0; i < 20; i++) {
        for (int j = 0; j < 56; j++) {
            printf("%c", es[i][j]);
        }
        printf("\n");
    }
    Sleep(3000);
}

static int player_y;
static int player_x;
static int close_eye;
static int playing;

unsigned _stdcall Thread_young(void* args) {
    char* mugunghwa_list[10] = { "무", "궁", "화", "꽃", "이", "피", "었", "습", "니","다" };
    int x = 27, y = 4, index = 0;
    close_eye = 0;

    int time_rand = 0, time_count = 0;

    gotoxy(27, 4);
    printf("(∪.∪)...zzz");


    srand(time(NULL));

    while (player_y >= 6 && playing == 1) {
        if (time_count == 0) {
            time_rand = 2 + (rand() % 11) + 1;
            time_count++;
        }
        else if (time_count == time_rand) {
            time_count = 0;
            gotoxy(22, 4);
            printf("%s", mugunghwa_list[index]);
            index++;
        }
        else {
            Sleep(100);
            time_count++;
        }


        if (index == 10) {
            gotoxy(27, 4);
            close_eye = 1;
            printf("(- ^ -)?      ");
            index = 0;
            Sleep((2 + (rand() % 26) + 1) * 100);
            gotoxy(27, 4);
            printf("(∪.∪)...zzz");
            close_eye = 0;
        }


    }
}

void gameLoop(int mapCode) {
    srand((unsigned int)time(NULL));
    int n = rand() % 1000;
    playing = 1;
    if (mapCode == 0) {
        memcpy(tempMap, es, sizeof(tempMap));
    }
    else if (mapCode == 1) {
        memcpy(tempMap, no, sizeof(tempMap));
    }
    else if (mapCode == 2) {
        memcpy(tempMap, hd, sizeof(tempMap));
    }

    drawMapes();
    int x = 27, y = 48;
    char ch;

    player_y = y;
    player_x = x;
    int player_eye_open_y = -1;
    int player_eye_open_x = -1;
    _beginthreadex(NULL, 0, Thread_young, 0, 0, NULL);
    gotoxy(x, y);
    printf("@");
    while (playing) {

        ch = _getch();
        gotoxy(x, y);
        printf(" ");
        switch (ch)
        {
        case 'w':
            if (y > 0) y--;
            break;
        case 's':
            if (y < 48) y++;
            break;
        case 'a':
            if (x > 0) x--;
            break;
        case 'd':
            if (x < 55) x++;
            break;
        case 'W':
            if (y > 0) y--;
            break;
        case 'S':
            if (y < 48) y++;
            break;
        case 'A':
            if (x > 0) x--;
            break;
        case 'D':
            if (x < 55) x++;
        }
        gotoxy(x, y);
        player_y = y;
        player_x = x;
        printf("@");

        if (player_eye_open_y == -1 && close_eye == 1) {
            player_eye_open_y = y;
        }
        if (player_eye_open_x == -1 && close_eye == 1) {
            player_eye_open_x = x;
        }

        if ((close_eye == 1 && player_eye_open_y != y)||(close_eye == 1 && player_eye_open_x != x)) {
            system("mode con cols=56 lines=20 | title C언어 프로젝트");
            playing = 0;
            _getch();

            printf("\n\n\n\n\n\n\n             넌 죽음ㅋ 이것도 못하네 ㅋ\n\n\n");

            printf("             3초뒤 메인이동");
            Sleep(3000);
            break;
        }

        if (close_eye == 0) {
            player_eye_open_y = -1;
            player_eye_open_x = -1;
        }

        if (y <= 6) {
            system("mode con cols=56 lines=20 | title C언어 프로젝트");
            playing = 0;
            _getch();

            printf("\n\n\n\n\n\n\n             축하합니다. 그래도 사람이시군요!\n\n\n");

            printf("             3초뒤 메인이동");
            Sleep(3000);
            break;
        }

    }



}