📄 tanchiview.cpp
字号:
// TanchiView.cpp : implementation of the CTanchiView class
//
#include "stdafx.h"
#include "Tanchi.h"
#include "TanchiDoc.h"
#include "TanchiView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define LEFT 1
#define UP 2
#define RIGHT 3
#define DOWN 4
/////////////////////////////////////////////////////////////////////////////
// CTanchiView
IMPLEMENT_DYNCREATE(CTanchiView, CView)
BEGIN_MESSAGE_MAP(CTanchiView, CView)
//{{AFX_MSG_MAP(CTanchiView)
ON_WM_CHAR()
ON_WM_KEYDOWN()
ON_WM_TIMER()
//}}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)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTanchiView construction/destruction
CTanchiView::CTanchiView()
{
// TODO: add construction code here
}
CTanchiView::~CTanchiView()
{
}
BOOL CTanchiView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTanchiView drawing
void CTanchiView::OnDraw(CDC* pDC)
{
CTanchiDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CBrush brush(RGB(255,255,0)),*pOldBrush;
pOldBrush=pDC->SelectObject(&brush);
for(int i=0;i<m_nTotal;i++)
pDC->Rectangle(&m_rects[i]);
CBrush brush1(RGB(0,0,255));
pDC->SelectObject(&brush1);
pDC->Ellipse(&m_rCurrent);
//TRACE("%d,%d,%d,%d\n",m_rects[2].left,m_rects[2].top,m_rects[2].right,m_rects[2].bottom);
CPen pen(PS_SOLID,2,RGB(255,0,0)),*pOldPen;
pOldPen=pDC->SelectObject(&pen);
pDC->Rectangle(0,0,500,2);
pDC->Rectangle(0,0,2,400);
pDC->Rectangle(0,398,500,400);
pDC->Rectangle(498,0,500,400);
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
CString str;
str.Format("当前分数:%d",m_nTotal-1);
pDC->TextOut(500,0,str);
}
/////////////////////////////////////////////////////////////////////////////
// CTanchiView printing
BOOL CTanchiView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CTanchiView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CTanchiView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CTanchiView diagnostics
#ifdef _DEBUG
void CTanchiView::AssertValid() const
{
CView::AssertValid();
}
void CTanchiView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTanchiDoc* CTanchiView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTanchiDoc)));
return (CTanchiDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTanchiView message handlers
void CTanchiView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CView::OnChar(nChar, nRepCnt, nFlags);
}
void CTanchiView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
//如果反方向,则退出函数
if(nChar==VK_LEFT&&m_nDirection==RIGHT||nChar==VK_RIGHT&&m_nDirection==LEFT||
nChar==VK_UP&&m_nDirection==DOWN||nChar==VK_DOWN&&m_nDirection==UP)
return;
KillTimer(1);//停止计时器
int i;
for(i=m_nTotal-1;i>0;i--)
memcpy(&m_rects[i],&m_rects[i-1],sizeof(RECT));
switch (nChar)
{
case VK_LEFT:
if(m_nDirection!=RIGHT)
{
m_nDirection=LEFT;
m_rects[0].left-=20;
m_rects[0].right-=20;
}
break;
case VK_RIGHT:
if(m_nDirection!=LEFT)
{
m_nDirection=RIGHT;
m_rects[0].left+=20;
m_rects[0].right+=20;
}
break;
case VK_UP:
if(m_nDirection!=DOWN)
{
m_nDirection=UP;
m_rects[0].top-=20;
m_rects[0].bottom-=20;
}
break;
case VK_DOWN:
if(m_nDirection!=UP)
{
m_nDirection=DOWN;
m_rects[0].top+=20;
m_rects[0].bottom+=20;
}
break;
}
//越界判断
if(m_rects[0].left<0||m_rects[0].right>500||m_rects[0].top<0||m_rects[0].bottom>400)
{
CString str;
str.Format("游戏结束!越界!\r\n游戏分数 %d !",m_nTotal-1);
KillTimer(1);
MessageBox(str);
OnInitialUpdate();
return ;
}
//走到自身
for(i=1;i<m_nTotal;i++)
if(m_rects[0].left==m_rects[i].left&&m_rects[0].top==m_rects[i].top&&
m_rects[0].right==m_rects[i].right&&m_rects[0].bottom==m_rects[i].bottom)
{
CString str;
str.Format("游戏结束!走到自身!\r\n游戏分数 %d !",m_nTotal-1);
KillTimer(1);
MessageBox(str);
OnInitialUpdate();
return;
}
//吃食物
if(m_rects[0].left==m_rCurrent.left&&m_rects[0].top==m_rCurrent.top&&
m_rects[0].right==m_rCurrent.right&&m_rects[0].bottom==m_rCurrent.bottom)
{
m_nTotal++;
TRACE("m_nTotal=%d\n",m_nTotal);
SetShiWu();
}
Invalidate();
SetTimer(1,500,NULL);//开始计时器
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CTanchiView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
switch(m_nDirection)
{
case UP:
OnKeyDown(VK_UP,0,0);
break;
case DOWN:
OnKeyDown(VK_DOWN,0,0);
break;
case LEFT:
OnKeyDown(VK_LEFT,0,0);
break;
case RIGHT:
OnKeyDown(VK_RIGHT,0,0);
break;
}
CView::OnTimer(nIDEvent);
}
void CTanchiView::OnInitialUpdate()
{
//CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
for(int i=0;i<TOTAL;i++)
{
m_rects[i].left=0;
m_rects[i].top=0;
m_rects[i].right=0;
m_rects[i].bottom=0;
}
m_rects[0].left=0;
m_rects[0].top=0;
m_rects[0].right=20;
m_rects[0].bottom=20;
m_nTotal=1;
SetShiWu();
m_nDirection=RIGHT;
SetTimer(1,500,NULL);
}
void CTanchiView::SetShiWu()
{
srand((unsigned)time(NULL));
int x=rand()%25;
int y=rand()%20;
m_rCurrent.left=x*20;
m_rCurrent.right=(x+1)*20;
m_rCurrent.top=y*20;
m_rCurrent.bottom=(y+1)*20;
for(int i=0;i<m_nTotal;i++)
{
if(m_rCurrent.left==m_rects[i].left&&m_rCurrent.top==m_rects[i].top&&
m_rCurrent.right==m_rects[i].right&&m_rCurrent.bottom==m_rects[i].bottom)
SetShiWu();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -