오다기리 박의 알고리즘 노트

[핵심 API로 배우는 윈도우프로그래밍(강경우, 한빛아카데미)] 3장 10번 풀이(미완성) 본문

WIN32 API

[핵심 API로 배우는 윈도우프로그래밍(강경우, 한빛아카데미)] 3장 10번 풀이(미완성)

오다기리 박 2017. 11. 3. 17:52
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <Windows.h>
#include<tchar.h>
#include<string.h>
#include<math.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam);
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
    HWND hwnd;
    MSG msg;
 
    WNDCLASS WndClass;
    WndClass.style = CS_HREDRAW | CS_VREDRAW;
    WndClass.lpfnWndProc = WndProc;
    WndClass.cbClsExtra = 0;
    WndClass.cbWndExtra = 0;
    WndClass.hInstance = hInstance;
    WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    WndClass.lpszMenuName = NULL;
    WndClass.lpszClassName = _T("Window Class Name");
 
    RegisterClass(&WndClass); //윈도우 클래스를 커널에 등록
 
    hwnd = CreateWindow( //윈도우 핸들값 반환
        _T("Window Class Name"), //윈도우 클래스 이름
        _T("홍길동의 첫 번째 윈도우"), //윈도우 타이틀이름
        WS_OVERLAPPEDWINDOW, //윈도우 스타일
        100//윈도우 위치 x좌표
        100,//윈도우 위치 y좌표
        1000//윈도우 가로크기
        1000//윈도우 세로크기
        NULL//부모 윈도우핸들
        NULL//메뉴핸들
        hInstance, //응용프로그램 인스턴스
        NULL); //생성 윈도우 정보
 
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
 
    while (GetMessage(&msg, NULL00))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}
 
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    static int headX = 60, headY = 20;
    static int tailX[11], tailY[11];
    static int tailCount = 0;
    static bool isGoing = false;
    static int Direction = 1;
 
    HPEN blackPen, redPen, bluePen;
    HBRUSH blackBrush, whiteBrush;
    static int foodX[10], foodY[10];
 
    switch (iMsg)
    {
    case WM_CREATE:
        tailX[0= 20, tailY[0= 20;
        for (int i = 0; i < 10; i++)
        {
            foodX[i] = ((rand() % 15* 40+ 20//20~560
            foodY[i] = ((rand() % 15* 40+ 20//20~560
        }
        break;
 
    case WM_KEYDOWN:
        if (wParam == VK_RIGHT && (tailX[0+ 60<= 600//오른쪽키
        {
            headX = tailX[0+ 40;
            headY = tailY[0];
            Direction = 1;
        }
        else if (wParam == VK_LEFT && (tailX[0- 60>= 0//왼쪽키
        {
            headX = tailX[0- 40;
            headY = tailY[0];
            Direction = 3;
        }
        else if (wParam == VK_UP && (tailY[0- 60>= 0//위쪽키
        {
            headX = tailX[0];
            headY = tailY[0- 40;
            Direction = 4;
        }
        else if (wParam == VK_DOWN && (tailY[0+ 60<= 600//아래키
        {
            headX = tailX[0];
            headY = tailY[0+ 40;
            Direction = 2;
        }
        if (wParam == VK_RETURN)
        {
            if (isGoing) isGoing = false;
            else isGoing = true;
 
            if (isGoing)
                SetTimer(hwnd, 1700NULL);
            else
                KillTimer(hwnd, 1);
            InvalidateRgn(hwnd, NULL, TRUE);
            break;
        }
        InvalidateRgn(hwnd, NULL, TRUE);
        break;
 
    case WM_TIMER:
        if (Direction == 1 && (headX + 20< 600//오른
        {
            headX += 40;
            tailX[0= headX - 40;
        }
        else if (Direction == 3 && (headX - 20> 0//왼
        {
            headX -= 40;
            tailX[0= headX + 40;
        }
        else if (Direction == 4 && (headY - 20> 0//위
        {
            headY -= 40;
            tailY[0= headY + 40;
        }
        else if (Direction == 2 && (headY + 20< 600//아래
        {
            headY += 40;
            tailY[0= headY - 40;
        }
        for (int i = 0; i < 10; i++//먹이 먹으면
        {
            if (headX == foodX[i] && headY == foodY[i])
            {
                foodX[i] = foodY[i] = -10;
                tailCount++;
                if (Direction == 1)
                {
                    tailX[tailCount] = tailX[tailCount - 1- 40;
                    tailY[tailCount] = tailY[tailCount - 1];
                }
                else if (Direction == 2)
                {
                    tailX[tailCount] = tailX[tailCount - 1];
                    tailY[tailCount] = tailY[tailCount - 1- 40;
                }
                else if (Direction == 3)
                {
                    tailX[tailCount] = tailX[tailCount - 1+ 40;
                    tailY[tailCount] = tailY[tailCount - 1];
                }
                else if (Direction == 4)
                {
                    tailX[tailCount] = tailX[tailCount - 1];
                    tailY[tailCount] = tailY[tailCount - 1+ 40;
                }
            }
        }
        
        InvalidateRgn(hwnd, NULL, TRUE);
        break;
 
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        Rectangle(hdc, 00600600);
 
        blackPen = CreatePen(PS_SOLID, 1, RGB(000));
        whiteBrush = CreateSolidBrush(RGB(255255255));
        blackBrush = CreateSolidBrush(BLACK_BRUSH);
        bluePen = CreatePen(PS_SOLID, 1, RGB(00255));
        redPen = CreatePen(PS_SOLID, 1, RGB(25500));
 
        SelectObject(hdc, blackPen);
        SelectObject(hdc, blackBrush);
        for (int i = 0; i < 10; i++)
        {
            Ellipse(hdc, foodX[i] - 20, foodY[i] - 20, foodX[i] + 20, foodY[i] + 20); //먹이
        }
 
        SelectObject(hdc, redPen);
        SelectObject(hdc, whiteBrush);
        Ellipse(hdc, headX - 20, headY - 20, headX + 20, headY + 20); //머리
 
        SelectObject(hdc, bluePen);
        for (int i = 0; i <= tailCount; i++)
            Ellipse(hdc, tailX[i] - 20, tailY[i] - 20, tailX[i] + 20, tailY[i] + 20); //꼬리
 
        DeleteObject(blackBrush);
        DeleteObject(whiteBrush);
        DeleteObject(blackPen);
        DeleteObject(redPen);
        DeleteObject(bluePen);
        EndPaint(hwnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
 
    }
    return DefWindowProc(hwnd, iMsg, wParam, lParam);
}
cs