// Dlg.h
CImage m_draw_image;
CDC m_draw_dc;
COLORREF m_table[60][80] = { 0, };
int m_count_map[60][80] = { 0, };
POINT m_pos = { 40, 30 }; // λ
Ήμ λ±μ μμΉ
int m_direction = 0; // 0 left / 1 up / 2 right / 3 bottom
int m_eat_count = 0;
void DrawMap();
void GameOver();
BOOL CMFCApplication1Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// μ΄ λν μμμ μμ΄μ½μ μ€μ ν©λλ€. μμ© νλ‘κ·Έλ¨μ μ£Ό μ°½μ΄ λν μμκ° μλ κ²½μ°μλ
// νλ μμν¬κ° μ΄ μμ
μ μλμΌλ‘ μνν©λλ€.
SetIcon(m_hIcon, TRUE); // ν° μμ΄μ½μ μ€μ ν©λλ€.
SetIcon(m_hIcon, FALSE); // μμ μμ΄μ½μ μ€μ ν©λλ€.
// TODO: μ¬κΈ°μ μΆκ° μ΄κΈ°ν μμ
μ μΆκ°ν©λλ€.
m_draw_image.Create(801, 601, 32);
m_draw_dc.Attach(m_draw_image.GetDC());
m_draw_dc.SetDCBrushColor(RGB(0, 0, 0));
srand((unsigned int)time(NULL));
int x, y;
for (int i = 0; i < 300; i++) {
x = rand() % 80;
y = rand() % 60;
if(m_table[y][x] == 0) m_table[y][x] = RGB(255, 0, 0);
else i--;
}
//m_table[m_pos.y][m_pos.x] = RGB(0, 255, 0);
m_count_map[m_pos.y][m_pos.x] = m_eat_count + 1;
SetTimer(1, 200, NULL);
DrawMap();
return TRUE; // ν¬μ»€μ€λ₯Ό 컨νΈλ‘€μ μ€μ νμ§ μμΌλ©΄ TRUEλ₯Ό λ°νν©λλ€.
}
void CMFCApplication1Dlg::DrawMap()
{
m_draw_dc.SelectStockObject(DC_BRUSH);
m_draw_dc.SelectStockObject(DC_PEN);
m_draw_dc.SetDCPenColor(RGB(48, 48, 12));
for (int y = 0; y < 60; y++) {
for (int x = 0; x < 80; x++)
{
if (m_count_map[y][x] > 0) {
m_count_map[y][x]--;
if (m_count_map[y][x] == 0) m_table[y][x] = 0;
}
m_draw_dc.SetDCBrushColor(m_table[y][x]);
m_draw_dc.Rectangle(x * 10, y * 10, x * 10 + 11, y * 10 + 11);
}
}
}
void CMFCApplication1Dlg::OnPaint()
{
CPaintDC dc(this); // 그리기λ₯Ό μν λλ°μ΄μ€ 컨ν
μ€νΈμ
λλ€.
if (IsIconic())
{
// ...
}
else
{
m_draw_image.Draw(dc, 0, 0);
// CDialogEx::OnPaint();
}
}
void CMFCApplication1Dlg::GameOver() {
KillTimer(1);
MessageBox(L"λ―Έμ
μ μλ£νμ§ λͺ»νμ΅λλ€.", L"Game Over!!", MB_ICONSTOP);
}
void CMFCApplication1Dlg::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == 1) {
//m_table[m_pos.y][m_pos.x] = 0;
if (m_direction == 0) m_pos.x--;
else if (m_direction == 1) m_pos.y--;
else if (m_direction == 2) m_pos.x++;
else m_pos.y++;
if (m_pos.x >= 0 && m_pos.x <= 79 && m_pos.y >= 0 && m_pos.y <= 59) {
if (m_table[m_pos.y][m_pos.x] == RGB(255, 0, 0)) {
m_eat_count++;
SetDlgItemInt(IDC_EAT_COUNT_EDIT, m_eat_count);
}
else if (m_table[m_pos.y][m_pos.x]) {
GameOver();
return;
}
m_table[m_pos.y][m_pos.x] = RGB(0, 255, 0);
m_count_map[m_pos.y][m_pos.x] = m_eat_count + 2;
DrawMap();
CClientDC dc(this);
m_draw_image.Draw(dc, 0, 0);
}
else {
// κ²μ μ€λ²
GameOver();
}
}
else {
CDialogEx::OnTimer(nIDEvent);
}
}
BOOL CMFCApplication1Dlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN) {
if (pMsg->wParam >= VK_LEFT && pMsg->wParam <= VK_DOWN) {
int old_direction = m_direction;
m_direction = pMsg->wParam - VK_LEFT;
if (old_direction > m_direction) {
if ((old_direction - m_direction) == 2) m_direction = old_direction;
}
else {
if ((m_direction - old_direction) == 2) m_direction = old_direction;
}
return TRUE;
}
}
return CDialogEx::PreTranslateMessage(pMsg);
}