⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shegame.cpp

📁 一个用vc编写的俄罗斯方块和贪吃蛇游戏的源代码
💻 CPP
字号:
// SheGame.cpp: implementation of the CSheGame class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "games.h"
#include "SheGame.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

IMPLEMENT_DYNCREATE(CSheGame, CGamsWnd)

BEGIN_MESSAGE_MAP(CSheGame, CGamsWnd)
	//{{AFX_MSG_MAP(CSheGame)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(IDM_GAME, StartGame)
	ON_COMMAND(IDM_STOP, StopGame)
	ON_WM_TIMER()
	ON_WM_KEYDOWN()
	ON_WM_PAINT()
END_MESSAGE_MAP()



CSheGame::CSheGame()
{
	m_nPointNum = 0;	
}

CSheGame::~CSheGame()
{
	if( m_nPointNum > 0 )
		ClearPoint();
}

void CSheGame::StartGame()
{
/*	CDC* pDC = GetDC();
	HICON hic = AfxGetApp()->LoadIcon(IDI_ICON1);
	if (hic == NULL)
		AfxMessageBox("error");
	pDC->DrawIcon(50, 50, hic);
	ReleaseDC(pDC);
*/
	if(m_nPointNum == 0)
	{
		m_nMoveType = 1;
		CreateNewPoint();
		CreateNewPoint(32, 32);
	}
	m_nTimer = this->SetTimer(1,200,0);
}

void CSheGame::StopGame()
{
	if(m_nTimer)
		KillTimer(m_nTimer);
}

void CSheGame::OnTimer( UINT nIDEvent )
{
	if (!m_bCreate)
		MovePoint();
}

void CSheGame::ClearPoint()
{
	for(POSITION pos = m_pPointList.GetHeadPosition(); pos!= NULL;)
	{
		CPoint* pt = (CPoint*)m_pPointList.GetNext(pos);
		delete pt;
	}
	m_pPointList.RemoveAll();
}

void CSheGame::CreateNewPoint(int x, int y)
{
	m_bCreate = TRUE;
	if(x ==0 && y == 0)
	{
		srand((unsigned)time(NULL));
		m_nX = (rand() % 200 + 1) / 32 * 32;
		m_nY = (rand() % 200 + 1) / 32 * 32;
		for(POSITION pos = m_pPointList.GetHeadPosition(); pos!= NULL;)
		{
			CPoint* pt = (CPoint*)m_pPointList.GetNext(pos);
			if(!(m_nX == pt->x && m_nY == pt->y))
				continue;
			m_nX = (rand() % 400 + 1) / 32 * 32;
			m_nY = (rand() % 400 + 1) / 32 * 32;
			pos = m_pPointList.GetHeadPosition();
		}
	}
	else
	{
		CPoint* pt = new CPoint(x, y);
		m_pPointList.AddHead(pt);
		m_nPointNum ++;
	}

	CDC* pDC = GetDC();
	HICON hic = AfxGetApp()->LoadIcon(IDI_POINT);
	if (hic == NULL)
		AfxMessageBox("error");
	pDC->DrawIcon(m_nX, m_nY, hic);
	m_bCreate = FALSE;
	ReleaseDC(pDC);
}

void CSheGame::MovePoint()
{
	int x = 0, y = 0;
	switch(m_nMoveType)
	{
	case 1:
		x += 32;
		break;
	case 2:
		x -= 32;
		break;
	case 3:
		y -= 32;
		break;
	case 4:
		y += 32;
		break;
	}
	CPoint* pt = (CPoint*)m_pPointList.GetHead();
	if (pt->x + x == m_nX && pt->y + y == m_nY)
	{
		CPoint* pNew = new CPoint(m_nX, m_nY);
		m_pPointList.AddHead(pNew);
		m_nPointNum ++;
		CreateNewPoint();
		return;
	}
	CDC* pDC = GetDC();
	HICON hic = AfxGetApp()->LoadIcon(IDI_POINT);
	HICON hicNULL = AfxGetApp()->LoadIcon(IDI_NULL);
	if(m_nPointNum == 1)
	{
		pDC->DrawIcon(pt->x, pt->y, hicNULL);
		pt->x += x;
		pt->y += y;
		pDC->DrawIcon(pt->x, pt->y, hic);
		m_pPointList.SetAt(m_pPointList.GetHeadPosition(), pt);
	}
	else
	{
		CPoint* pLast = (CPoint*)m_pPointList.GetTail();
		pDC->DrawIcon(pLast->x, pLast->y, hicNULL);
		m_pPointList.RemoveTail();
		delete pLast;
		CPoint* pNew = new CPoint(pt->x + x, pt->y + y);
		m_pPointList.AddHead(pNew);
		pDC->DrawIcon(pNew->x, pNew->y, hic);
	}

	ReleaseDC(pDC);

}

void CSheGame::OnKeyDown( UINT nChar, UINT nRepCnt, UINT nFlags )
{
	switch(nChar)
	{
	case 39:
		if(m_nMoveType != 2)
			m_nMoveType = 1;
		break;
	case 37:
		if(m_nMoveType != 1)
			m_nMoveType = 2;
		break;
	case 38:
		if(m_nMoveType != 4)
			m_nMoveType = 3;
		break;
	case 40:
		if(m_nMoveType != 3)
			m_nMoveType = 4;
		break;
	}
}

void CSheGame::OnPaint()
{
	CGamsWnd::OnPaint();
	CDC* pDC = GetDC();
	HICON hic = AfxGetApp()->LoadIcon(IDI_POINT);
	
	for(POSITION pos = m_pPointList.GetHeadPosition(); pos!= NULL;)
	{
		CPoint* pt = (CPoint*)m_pPointList.GetNext(pos);
		pDC->DrawIcon(pt->x, pt->y, hic);
	}
	pDC->DrawIcon(m_nX, m_nY, hic);
	ReleaseDC(pDC);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -