level 1
流氓小强爱打架
楼主
老师叫我改改我的贪吃蛇 (要求是:双击鼠标左键,蛇自动走到双击的位置)(我的思路:用获取客户区坐标的函数 void GetClientRect(LPRECT IpRect) ,再让蛇移动到双击的位置。)。。可是我不会用这个函数 附上我的贪吃蛇代码 ,求大神帮我改改,使之达到括号的要求。 明天就要交作业了 我实在是想不出来了
附上代码:
#include "stdafx.h"
#include "贪吃蛇2.h"
#include "贪吃蛇2Doc.h"
#include "贪吃蛇2View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
struct Snake
{
int x,y;
int length;
int direction;
}Snake[50];
struct Food
{
int x;
int y;
int isfood;
}Food;
/////////////////////////////////////////////////////////////////////////////
// CMy2View
IMPLEMENT_DYNCREATE(CMy2View, CView)
BEGIN_MESSAGE_MAP(CMy2View, CView)
//{{AFX_MSG_MAP(CMy2View)
ON_WM_KEYDOWN()
ON_WM_TIMER()
ON_WM_RBUTTONDOWN()
ON_COMMAND(ID_PAUSE, OnPause)
ON_COMMAND(ID_CONTINUE, OnContinue)
ON_COMMAND(ID_EXIT, OnExit)
ON_COMMAND(ID_START, OnStart)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONDBLCLK()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMy2View construction/destruction
CMy2View::CMy2View()
{
// TODO: add construction code here
}
CMy2View::~CMy2View()
{
}
BOOL CMy2View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMy2View drawing
void CMy2View::OnDraw(CDC* pDC)
{
CMy2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);
dcMemory.SelectObject(&bitmap);
int m,n;
for(m=0;m<50;m++)
{
for(n=0;n<40;n++)
{
pDC->StretchBlt(0+10*m,0+10*n,10,10,&dcMemory,0,0,48,48,SRCCOPY);
}
}
bitmap.Detach();
//bitmap.LoadBitmap(IDB_BITMAP2);
//dcMemory.SelectObject(&bitmap);
//pDC->StretchBlt(0+10,0+10,10,10,&dcMemory,0,0,48,48,SRCCOPY);
//bitmap.Detach();
// CBrush backBrush(RGB(0,0,0));
// CBrush* pOldBrush = pDC->SelectObject(&backBrush);
//CRect rect;
//pDC-PatBlt(rect.left, rect.top, rect.Width(), rect.Height(),PATCOPY);
// >GetClipBox(&rect);
// pDC-> pDC->SelectObject(pOldBrush);
//pDC->Rectangle(19,19,400,400);
Onint();
}
/////////////////////////////////////////////////////////////////////////////
// CMy2View printing
BOOL CMy2View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMy2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMy2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMy2View diagnostics
#ifdef _DEBUG
void CMy2View::AssertValid() const
{
CView::AssertValid();
}
void CMy2View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMy2Doc* CMy2View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy2Doc)));
return (CMy2Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMy2View message handlers
void CMy2View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
switch(nChar)
{
case VK_UP:if(Snake[0].direction!=2)Snake[0].direction=1;break;
case VK_DOWN:if(Snake[0].direction!=1)Snake[0].direction=2;break;
case VK_LEFT:if(Snake[0].direction!=4)Snake[0].direction=3;break;
case VK_RIGHT:if(Snake[0].direction!=3)Snake[0].direction=4;break;
/*case VK_NUMPAD7:if(Snake[0].direction!=0x63)Snake[0].direction=0x67;break;
case VK_NUMPAD3:if(Snake[0].direction!=0x67)Snake[0].direction=0x63;break;
case VK_NUMPAD9:if(Snake[0].direction!=0x61)Snake[0].direction=0x69;break;
case VK_NUMPAD1:if(Snake[0].direction!=0x69)Snake[0].direction=0x61;break;*/
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CMy2View::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CDC *pDC=GetDC();
CString soure;
/*//难度提升
if(Snake[0].length==2)SetTimer(1,370,NULL);
if(Snake[0].length==3)SetTimer(1,270,NULL);
if(Snake[0].length==6)SetTimer(1,200,NULL);
if(Snake[0].length==9)SetTimer(1,100,NULL);*/
soure.Format("得分:%d!",(Snake[0].length-3)*10);
//撞界判断
if(Snake[0].x*10<=0||Snake[0].y*10<=0||Snake[0].x*10>=490||Snake[0].y*10>=390)
{
KillTimer(1);
AfxMessageBox(soure);
restart();
// s=0;
}
//蛇身相撞判断
if(Snake[0].length>3)
for(int sn=Snake[0].length-1;sn>0;sn--)
{
if(Snake[0].x*10==Snake[sn].x*10&&Snake[0].y*10==Snake[sn].y*10)
{
KillTimer(1);
AfxMessageBox(soure);
// s=0;
restart();
}
}
///////////////////////////////////////////////////////////////////////////
pDC->SelectStockObject(BLACK_PEN);
pDC->Rectangle(Snake[Snake[0].length-1].x*10,Snake[Snake[0].length-1].y*10,(Snake[Snake[0].length-1].x+1)*10,(Snake[Snake[0].length-1].y+1)*10);
for(int i=Snake[0].length-1;i>0;i--)
{
Snake[i].x=Snake[i-1].x;
Snake[i].y=Snake[i-1].y;
}
//行走方向判断
if(Snake[0].direction==1)Snake[0].y--;
if(Snake[0].direction==2)Snake[0].y++;
if(Snake[0].direction==3)Snake[0].x--;
if(Snake[0].direction==4)Snake[0].x++;
/*if(Snake[0].direction==0x67)
{
Snake[0].x--;
Snake[0].y--;
}
if(Snake[0].direction==0x63)
{
Snake[0].x++;
Snake[0].y++;
}
if(Snake[0].direction==0x69)
{
Snake[0].x++;
Snake[0].y--;
}
if(Snake[0].direction==0x61)
{
Snake[0].x--;
Snake[0].y++;
}*/
pDC->SelectStockObject(BLACK_PEN);
CBrush DrawBrush=(RGB(71,97,255));
CBrush *Drawbrush=pDC->SelectObject(&DrawBrush);
pDC->Rectangle(Snake[0].x*10,Snake[0].y*10,(Snake[0].x+1)*10,(Snake[0].y+1)*10);
pDC->SelectObject(DrawBrush);
//判断吃豆的条件,撞到就吃
if(Snake[0].x*10==Food.x*10&&Snake[0].y*10==Food.y*10)
{
Snake[0].length++;
Food.isfood=1;
Snake[Snake[0].length-1].x=Snake[Snake[0].length-2].x;
Snake[Snake[0].length-1].y=Snake[Snake[0].length-2].y;
}
//如果食物被吃了 就生成
if(Food.isfood==1)
{
srand((unsigned)time(NULL));
do
{
for(int isfo=Snake[0].length-1;isfo>=0;isfo--)
if(Snake[0].x*10==Snake[isfo].x*10&&Snake[0].y*10==Snake[isfo].y*10)
{
Food.x=rand()%25;
Food.y=rand()%25;
// isfo=Snake[0].length-1;
}
}
while(Food.x*10<70||Food.y*10<70||Food.x*10>490||Food.y*10>390);
pDC->Rectangle(Food.x*10,Food.y*10,(Food.x+1)*10,(Food.y+1)*10);
Food.isfood=0;
}
CView::OnTimer(nIDEvent);
}
void CMy2View::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
KillTimer(1);
CView::OnRButtonDown(nFlags, point);
}
/*void CMy2View::OnStart()
{
// TODO: Add your command handler code here
SetTimer(1,500,NULL);
AfxMessageBox("3秒后开始游戏!");
}*/
void CMy2View::OnPause()
{
// TODO: Add your command handler code here
KillTimer(1);
//AfxMessageBox("暂停游戏...");
}
void CMy2View::OnContinue()
{
// TODO: Add your command handler code here
SetTimer(1,50,NULL);
}
void CMy2View::OnExit()
{
// TODO: Add your command handler code here
AfxMessageBox("退出游戏...");
exit (0);
}
void CMy2View::OnInitialUpdate()
{
CView::OnInitialUpdate();
Snake[0].x=10;
Snake[0].y=10;
Snake[1].x=11;
Snake[1].y=10;
Snake[2].x=12;
Snake[2].y=10;
Snake[0].direction=3;
Snake[0].length=3;
Food.isfood=1;
// TODO: Add your specialized code here and/or call the base class
}
void CMy2View::Onint()
{
CDC *pDC=GetDC();
CBrush DrawBrush=(RGB(71,97,255));
CBrush *Drawbrush=pDC->SelectObject(&DrawBrush);
for(int i=0;i<=Snake[0].length-1;i++)
pDC->Rectangle(Snake[i].x*10,Snake[i].y*10,(Snake[i].x+1)*10,(Snake[i].y+1)*10);
pDC->SelectObject(DrawBrush);
}
//DEL void CMy2View::OnDifficulty1()
//DEL {
//DEL // TODO: Add your command handler code here
//DEL SetTimer(1,500,NULL);
//DEL AfxMessageBox("3秒后开始游戏!");
//DEL }
//DEL void CMy2View::OnDifficulty2()
//DEL {
//DEL // TODO: Add your command handler code here
//DEL SetTimer(1,250,NULL);
//DEL AfxMessageBox("3秒后开始游戏!");
//DEL }
//DEL void CMy2View::OnDifficulty3()
//DEL {
//DEL // TODO: Add your command handler code here
//DEL SetTimer(1,150,NULL);
//DEL AfxMessageBox("3秒后开始游戏!");
//DEL }
void CMy2View::OnStart()
{
// TODO: Add your command handler code here
//void Invalidate( BOOL bErase = TRUE );
SetTimer(1,50,NULL);
//AfxMessageBox("3秒后开始游戏!");
}
void CMy2View::restart()
{
/*Snake[0].x=10;
Snake[0].y=10;
Snake[1].x=11;
Snake[1].y=10;
Snake[2].x=12;
Snake[2].y=10;
Snake[0].direction=3;
Snake[0].length=3;
Food.isfood=1;
CDC *pDC=GetDC();
CBrush DrawBrush=(RGB(71,97,255));
CBrush *Drawbrush=pDC->SelectObject(&DrawBrush);
for(int i=0;i<=Snake[0].length-1;i++)
pDC->Rectangle(Snake[i].x*10,Snake[i].y*10,(Snake[i].x+1)*10,(Snake[i].y+1)*10);
pDC->SelectObject(DrawBrush);*/
CDC *pDC=GetDC();
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);
dcMemory.SelectObject(&bitmap);
int m,n;
for(m=0;m<50;m++)
{
for(n=0;n<40;n++)
{
pDC->StretchBlt(0+10*m,0+10*n,10,10,&dcMemory,0,0,48,48,SRCCOPY);
}
}
bitmap.Detach();
Snake[0].x=10;
Snake[0].y=10;
Snake[1].x=11;
Snake[1].y=10;
Snake[2].x=12;
Snake[2].y=10;
Snake[0].direction=3;
Snake[0].length=3;
Food.isfood=1;
//CDC *pDC=GetDC();
CBrush DrawBrush=(RGB(71,97,255));
CBrush *Drawbrush=pDC->SelectObject(&DrawBrush);
for(int i=0;i<=Snake[0].length-1;i++)
pDC->Rectangle(Snake[i].x*10,Snake[i].y*10,(Snake[i].x+1)*10,(Snake[i].y+1)*10);
pDC->SelectObject(DrawBrush);
//Onget();
}
/*void CMy2View::Onget()
{
CDC *pDC=GetDC();
CBrush DrawBrush=(RGB(0,0,0));
CBrush *Drawbrush=pDC->SelectObject(&DrawBrush);
pDC->Rectangle(200,200,210,210);
pDC->Rectangle(200,210,210,220);
pDC->SelectObject(DrawBrush);
} */
void CMy2View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetTimer(1,50,NULL);
CView::OnLButtonDown(nFlags, point);
}
//DEL void CMy2View::OnLButtonUp(UINT nFlags, CPoint point)
//DEL {
//DEL // TODO: Add your message handler code here and/or call default
//DEL
//DEL CView::OnLButtonUp(nFlags, point);
//DEL }
//DEL void CMy2View::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
//DEL {
//DEL // TODO: Add your message handler code here and/or call default
//DEL
//DEL CView::OnKeyUp(nChar, nRepCnt, nFlags);
//DEL }
//DEL void CMy2View::OnRButtonUp(UINT nFlags, CPoint point)
//DEL {
//DEL // TODO: Add your message handler code here and/or call default
//DEL
//DEL CView::OnRButtonUp(nFlags, point);
//DEL }
void CMy2View::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//void GetClientRect(LPRECT IpRect);
//lpRect->Rectangle(0,0,10,10);
CView::OnLButtonDblClk(nFlags, point);
}
2014年06月12日 12点06分
1
附上代码:
#include "stdafx.h"
#include "贪吃蛇2.h"
#include "贪吃蛇2Doc.h"
#include "贪吃蛇2View.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
struct Snake
{
int x,y;
int length;
int direction;
}Snake[50];
struct Food
{
int x;
int y;
int isfood;
}Food;
/////////////////////////////////////////////////////////////////////////////
// CMy2View
IMPLEMENT_DYNCREATE(CMy2View, CView)
BEGIN_MESSAGE_MAP(CMy2View, CView)
//{{AFX_MSG_MAP(CMy2View)
ON_WM_KEYDOWN()
ON_WM_TIMER()
ON_WM_RBUTTONDOWN()
ON_COMMAND(ID_PAUSE, OnPause)
ON_COMMAND(ID_CONTINUE, OnContinue)
ON_COMMAND(ID_EXIT, OnExit)
ON_COMMAND(ID_START, OnStart)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONDBLCLK()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMy2View construction/destruction
CMy2View::CMy2View()
{
// TODO: add construction code here
}
CMy2View::~CMy2View()
{
}
BOOL CMy2View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMy2View drawing
void CMy2View::OnDraw(CDC* pDC)
{
CMy2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);
dcMemory.SelectObject(&bitmap);
int m,n;
for(m=0;m<50;m++)
{
for(n=0;n<40;n++)
{
pDC->StretchBlt(0+10*m,0+10*n,10,10,&dcMemory,0,0,48,48,SRCCOPY);
}
}
bitmap.Detach();
//bitmap.LoadBitmap(IDB_BITMAP2);
//dcMemory.SelectObject(&bitmap);
//pDC->StretchBlt(0+10,0+10,10,10,&dcMemory,0,0,48,48,SRCCOPY);
//bitmap.Detach();
// CBrush backBrush(RGB(0,0,0));
// CBrush* pOldBrush = pDC->SelectObject(&backBrush);
//CRect rect;
//pDC-PatBlt(rect.left, rect.top, rect.Width(), rect.Height(),PATCOPY);
// >GetClipBox(&rect);
// pDC-> pDC->SelectObject(pOldBrush);
//pDC->Rectangle(19,19,400,400);
Onint();
}
/////////////////////////////////////////////////////////////////////////////
// CMy2View printing
BOOL CMy2View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMy2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMy2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMy2View diagnostics
#ifdef _DEBUG
void CMy2View::AssertValid() const
{
CView::AssertValid();
}
void CMy2View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMy2Doc* CMy2View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMy2Doc)));
return (CMy2Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMy2View message handlers
void CMy2View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
switch(nChar)
{
case VK_UP:if(Snake[0].direction!=2)Snake[0].direction=1;break;
case VK_DOWN:if(Snake[0].direction!=1)Snake[0].direction=2;break;
case VK_LEFT:if(Snake[0].direction!=4)Snake[0].direction=3;break;
case VK_RIGHT:if(Snake[0].direction!=3)Snake[0].direction=4;break;
/*case VK_NUMPAD7:if(Snake[0].direction!=0x63)Snake[0].direction=0x67;break;
case VK_NUMPAD3:if(Snake[0].direction!=0x67)Snake[0].direction=0x63;break;
case VK_NUMPAD9:if(Snake[0].direction!=0x61)Snake[0].direction=0x69;break;
case VK_NUMPAD1:if(Snake[0].direction!=0x69)Snake[0].direction=0x61;break;*/
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CMy2View::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CDC *pDC=GetDC();
CString soure;
/*//难度提升
if(Snake[0].length==2)SetTimer(1,370,NULL);
if(Snake[0].length==3)SetTimer(1,270,NULL);
if(Snake[0].length==6)SetTimer(1,200,NULL);
if(Snake[0].length==9)SetTimer(1,100,NULL);*/
soure.Format("得分:%d!",(Snake[0].length-3)*10);
//撞界判断
if(Snake[0].x*10<=0||Snake[0].y*10<=0||Snake[0].x*10>=490||Snake[0].y*10>=390)
{
KillTimer(1);
AfxMessageBox(soure);
restart();
// s=0;
}
//蛇身相撞判断
if(Snake[0].length>3)
for(int sn=Snake[0].length-1;sn>0;sn--)
{
if(Snake[0].x*10==Snake[sn].x*10&&Snake[0].y*10==Snake[sn].y*10)
{
KillTimer(1);
AfxMessageBox(soure);
// s=0;
restart();
}
}
///////////////////////////////////////////////////////////////////////////
pDC->SelectStockObject(BLACK_PEN);
pDC->Rectangle(Snake[Snake[0].length-1].x*10,Snake[Snake[0].length-1].y*10,(Snake[Snake[0].length-1].x+1)*10,(Snake[Snake[0].length-1].y+1)*10);
for(int i=Snake[0].length-1;i>0;i--)
{
Snake[i].x=Snake[i-1].x;
Snake[i].y=Snake[i-1].y;
}
//行走方向判断
if(Snake[0].direction==1)Snake[0].y--;
if(Snake[0].direction==2)Snake[0].y++;
if(Snake[0].direction==3)Snake[0].x--;
if(Snake[0].direction==4)Snake[0].x++;
/*if(Snake[0].direction==0x67)
{
Snake[0].x--;
Snake[0].y--;
}
if(Snake[0].direction==0x63)
{
Snake[0].x++;
Snake[0].y++;
}
if(Snake[0].direction==0x69)
{
Snake[0].x++;
Snake[0].y--;
}
if(Snake[0].direction==0x61)
{
Snake[0].x--;
Snake[0].y++;
}*/
pDC->SelectStockObject(BLACK_PEN);
CBrush DrawBrush=(RGB(71,97,255));
CBrush *Drawbrush=pDC->SelectObject(&DrawBrush);
pDC->Rectangle(Snake[0].x*10,Snake[0].y*10,(Snake[0].x+1)*10,(Snake[0].y+1)*10);
pDC->SelectObject(DrawBrush);
//判断吃豆的条件,撞到就吃
if(Snake[0].x*10==Food.x*10&&Snake[0].y*10==Food.y*10)
{
Snake[0].length++;
Food.isfood=1;
Snake[Snake[0].length-1].x=Snake[Snake[0].length-2].x;
Snake[Snake[0].length-1].y=Snake[Snake[0].length-2].y;
}
//如果食物被吃了 就生成
if(Food.isfood==1)
{
srand((unsigned)time(NULL));
do
{
for(int isfo=Snake[0].length-1;isfo>=0;isfo--)
if(Snake[0].x*10==Snake[isfo].x*10&&Snake[0].y*10==Snake[isfo].y*10)
{
Food.x=rand()%25;
Food.y=rand()%25;
// isfo=Snake[0].length-1;
}
}
while(Food.x*10<70||Food.y*10<70||Food.x*10>490||Food.y*10>390);
pDC->Rectangle(Food.x*10,Food.y*10,(Food.x+1)*10,(Food.y+1)*10);
Food.isfood=0;
}
CView::OnTimer(nIDEvent);
}
void CMy2View::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
KillTimer(1);
CView::OnRButtonDown(nFlags, point);
}
/*void CMy2View::OnStart()
{
// TODO: Add your command handler code here
SetTimer(1,500,NULL);
AfxMessageBox("3秒后开始游戏!");
}*/
void CMy2View::OnPause()
{
// TODO: Add your command handler code here
KillTimer(1);
//AfxMessageBox("暂停游戏...");
}
void CMy2View::OnContinue()
{
// TODO: Add your command handler code here
SetTimer(1,50,NULL);
}
void CMy2View::OnExit()
{
// TODO: Add your command handler code here
AfxMessageBox("退出游戏...");
exit (0);
}
void CMy2View::OnInitialUpdate()
{
CView::OnInitialUpdate();
Snake[0].x=10;
Snake[0].y=10;
Snake[1].x=11;
Snake[1].y=10;
Snake[2].x=12;
Snake[2].y=10;
Snake[0].direction=3;
Snake[0].length=3;
Food.isfood=1;
// TODO: Add your specialized code here and/or call the base class
}
void CMy2View::Onint()
{
CDC *pDC=GetDC();
CBrush DrawBrush=(RGB(71,97,255));
CBrush *Drawbrush=pDC->SelectObject(&DrawBrush);
for(int i=0;i<=Snake[0].length-1;i++)
pDC->Rectangle(Snake[i].x*10,Snake[i].y*10,(Snake[i].x+1)*10,(Snake[i].y+1)*10);
pDC->SelectObject(DrawBrush);
}
//DEL void CMy2View::OnDifficulty1()
//DEL {
//DEL // TODO: Add your command handler code here
//DEL SetTimer(1,500,NULL);
//DEL AfxMessageBox("3秒后开始游戏!");
//DEL }
//DEL void CMy2View::OnDifficulty2()
//DEL {
//DEL // TODO: Add your command handler code here
//DEL SetTimer(1,250,NULL);
//DEL AfxMessageBox("3秒后开始游戏!");
//DEL }
//DEL void CMy2View::OnDifficulty3()
//DEL {
//DEL // TODO: Add your command handler code here
//DEL SetTimer(1,150,NULL);
//DEL AfxMessageBox("3秒后开始游戏!");
//DEL }
void CMy2View::OnStart()
{
// TODO: Add your command handler code here
//void Invalidate( BOOL bErase = TRUE );
SetTimer(1,50,NULL);
//AfxMessageBox("3秒后开始游戏!");
}
void CMy2View::restart()
{
/*Snake[0].x=10;
Snake[0].y=10;
Snake[1].x=11;
Snake[1].y=10;
Snake[2].x=12;
Snake[2].y=10;
Snake[0].direction=3;
Snake[0].length=3;
Food.isfood=1;
CDC *pDC=GetDC();
CBrush DrawBrush=(RGB(71,97,255));
CBrush *Drawbrush=pDC->SelectObject(&DrawBrush);
for(int i=0;i<=Snake[0].length-1;i++)
pDC->Rectangle(Snake[i].x*10,Snake[i].y*10,(Snake[i].x+1)*10,(Snake[i].y+1)*10);
pDC->SelectObject(DrawBrush);*/
CDC *pDC=GetDC();
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);
dcMemory.SelectObject(&bitmap);
int m,n;
for(m=0;m<50;m++)
{
for(n=0;n<40;n++)
{
pDC->StretchBlt(0+10*m,0+10*n,10,10,&dcMemory,0,0,48,48,SRCCOPY);
}
}
bitmap.Detach();
Snake[0].x=10;
Snake[0].y=10;
Snake[1].x=11;
Snake[1].y=10;
Snake[2].x=12;
Snake[2].y=10;
Snake[0].direction=3;
Snake[0].length=3;
Food.isfood=1;
//CDC *pDC=GetDC();
CBrush DrawBrush=(RGB(71,97,255));
CBrush *Drawbrush=pDC->SelectObject(&DrawBrush);
for(int i=0;i<=Snake[0].length-1;i++)
pDC->Rectangle(Snake[i].x*10,Snake[i].y*10,(Snake[i].x+1)*10,(Snake[i].y+1)*10);
pDC->SelectObject(DrawBrush);
//Onget();
}
/*void CMy2View::Onget()
{
CDC *pDC=GetDC();
CBrush DrawBrush=(RGB(0,0,0));
CBrush *Drawbrush=pDC->SelectObject(&DrawBrush);
pDC->Rectangle(200,200,210,210);
pDC->Rectangle(200,210,210,220);
pDC->SelectObject(DrawBrush);
} */
void CMy2View::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetTimer(1,50,NULL);
CView::OnLButtonDown(nFlags, point);
}
//DEL void CMy2View::OnLButtonUp(UINT nFlags, CPoint point)
//DEL {
//DEL // TODO: Add your message handler code here and/or call default
//DEL
//DEL CView::OnLButtonUp(nFlags, point);
//DEL }
//DEL void CMy2View::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
//DEL {
//DEL // TODO: Add your message handler code here and/or call default
//DEL
//DEL CView::OnKeyUp(nChar, nRepCnt, nFlags);
//DEL }
//DEL void CMy2View::OnRButtonUp(UINT nFlags, CPoint point)
//DEL {
//DEL // TODO: Add your message handler code here and/or call default
//DEL
//DEL CView::OnRButtonUp(nFlags, point);
//DEL }
void CMy2View::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//void GetClientRect(LPRECT IpRect);
//lpRect->Rectangle(0,0,10,10);
CView::OnLButtonDblClk(nFlags, point);
}