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

📄 gamesview.cpp

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

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

#include "gamesDoc.h"
#include "gamesView.h"
#include "Myview.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGamesView

IMPLEMENT_DYNCREATE(CGamesView, CView)

BEGIN_MESSAGE_MAP(CGamesView, CView)
	//{{AFX_MSG_MAP(CGamesView)
		// 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(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
	ON_COMMAND(IDM_GAME, StartGame)
	ON_WM_TIMER()
	ON_WM_KEYDOWN()
END_MESSAGE_MAP()

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

CGamesView::CGamesView()
{
	// TODO: add construction code here
	m_nPointNum = 0;
	if( m_nPointNum > 0 )
		ClearPoint();
}

CGamesView::~CGamesView()
{
}

void CGamesView::StartGame()
{
	AfxMessageBox("dsfsf");
	/*
	//CStatic st;
	//st.Create("", WS_CHILD|WS_BORDER|WS_VISIBLE,CRect(0,0,400,400),this);
	m_nMoveType = 1;
	CreateNewPoint();
	CreateNewPoint(32, 32);
	m_nTimer = this->SetTimer(1,200,0);
	*/
}

void CGamesView::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 CGamesView::OnTimer( UINT nIDEvent )
{
	if (!m_bCreate)
		MovePoint();
}

void CGamesView::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 CGamesView::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;
	}
}

BOOL CGamesView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CGamesView drawing

void CGamesView::OnDraw(CDC* pDC)
{
	CGamesDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	
	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);
}

/////////////////////////////////////////////////////////////////////////////
// CGamesView printing

BOOL CGamesView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CGamesView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CGamesView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CGamesView diagnostics

#ifdef _DEBUG
void CGamesView::AssertValid() const
{
	CView::AssertValid();
}

void CGamesView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CGamesDoc* CGamesView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGamesDoc)));
	return (CGamesDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CGamesView message handlers

⌨️ 快捷键说明

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