[CPP] 콘솔 매트릭스

2018. 1. 22. 02:07

cybertramp LANG/C or C++


매트릭스 프로그램 소스코드



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* 
*    Written by cybertramp
*/
 
#include <Windows.h>
#include <iostream>
#include <ctime>
 
CRITICAL_SECTION CriticalSection;
const int nWidth = 40// 출력크기  
const int nHeight = 40;
 
void gotoxy(int x, int y)
{
    COORD pos = {x, y};
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
 
void textcolor(int color_number)
{
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_number);
}
 
class Unit
{
private:
    int nSpeed;
    int nValue;
    int x;
private:
    static DWORD WINAPI UnitThread(LPVOID lpParam)
    {
        using std::cout;
        const Unit* lpData = (Unit*)lpParam;
 
        int y = -1;
        while(y <= nHeight)
        {
            EnterCriticalSection(&CriticalSection);
            switch(lpData->nSpeed)
            {
            case 20:
                textcolor(10);
                break;
            case 40:
                textcolor(2);
                break;
            }
 
            gotoxy(lpData->x, y);
            cout << " ";
 
            y++;
            gotoxy(lpData->x, y);
            cout << lpData->nValue;
            LeaveCriticalSection(&CriticalSection);
 
            Sleep(lpData->nSpeed);
        }
 
        EnterCriticalSection(&CriticalSection);
        gotoxy(lpData->x, (nHeight + 1));
        cout << " ";
        LeaveCriticalSection(&CriticalSection);
 
        delete lpData;
        return 0;
    }
public:
    Unit(void) : nSpeed((rand() % 2 + 1* 20), nValue(rand() % 2), x(rand() % nWidth)
    {
        DWORD dwThreadID;
        CloseHandle(CreateThread(NULL0, UnitThread, this0&dwThreadID));
    }
};
 
int main(void)
{
    srand((unsigned)time(NULL));
    InitializeCriticalSection(&CriticalSection);
    system("mode con cols=32 lines=32");
 
    while(true)
    {
        new Unit;
        Sleep(20);
    }
 
    DeleteCriticalSection(&CriticalSection);
    return 0;
}
 
cs