Creative Code

콘솔-(3) 본문

코딩 study/C++

콘솔-(3)

빛하루 2023. 10. 9. 18:47
#include <iostream>
#include <conio.h>
#include <Windows.h>

using namespace std;

// 16가지 ANSI 컬러를 열거형으로 정의
enum color16 {
    Black = 0,
    Blue,
    Green,
    Cyan,
    Red,
    Magenta,
    Brown,
    Lightgray,
    Darkgray,
    Lightblue,
    Lightgreen,
    Lightcyan,
    Lightred,
    Lightmagenta,
    Yellow,
    White
};

// 콘솔 창 내의 커서 위치를 x, y 좌표로 이동하는 함수 정의
void gotoxy(int x, int y) {
    COORD pos = { x, y }; // COORD 구조체를 사용하여 커서 위치를 설정합니다.
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); // 커서 위치를 변경합니다.
}

// 텍스트 색상과 배경 색상을 설정하는 함수 정의
void setcolor(int txtcolor, int color) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), txtcolor + color * 16); // 텍스트 색상과 배경 색상을 설정합니다.
}

int main() {
    srand(time(NULL)); // 난수 생성기 초기화

    setcolor(Lightblue, White); // 초기 텍스트 색상과 배경 색상 설정

    for (int i = 0; i < 16; i++) { // 16번 반복
        setcolor(rand() % 16, rand() % 16); // 랜덤한 텍스트 색상과 배경 색상 설정
        cout << "******" << endl; // '*' 문자를 출력합니다.
    }
}

'코딩 study > C++' 카테고리의 다른 글

콘솔-(2)  (0) 2023.10.09
콘솔-(1)  (0) 2023.10.08