mfc_snake_game - 8BitsCoding/RobotMentor GitHub Wiki


// 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);
}