📄 snakeview.cpp
字号:
// SnakeView.cpp : implementation of the CSnakeView class
//
#include "stdafx.h"
#include "Snake.h"
#include <Mmsystem.h>
#include "SnakeDoc.h"
#include "SnakeView.h"
#include "Square.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSnakeView
IMPLEMENT_DYNCREATE(CSnakeView, CView)
BEGIN_MESSAGE_MAP(CSnakeView, CView)
//{{AFX_MSG_MAP(CSnakeView)
ON_WM_CHAR()
ON_COMMAND(ID_PAUSE, OnPause)
ON_UPDATE_COMMAND_UI(ID_PAUSE, OnUpdatePause)
ON_COMMAND(ID_START, OnStart)
ON_UPDATE_COMMAND_UI(ID_START, OnUpdateStart)
ON_WM_TIMER()
ON_COMMAND(ID_TOW, OnTow)
ON_UPDATE_COMMAND_UI(ID_TOW, OnUpdateTow)
ON_COMMAND(ID_ONE, OnOne)
ON_UPDATE_COMMAND_UI(ID_ONE, OnUpdateOne)
ON_COMMAND(ID_THREE, OnThree)
ON_UPDATE_COMMAND_UI(ID_THREE, OnUpdateThree)
ON_COMMAND(ID_MUSIC, OnMusic)
ON_UPDATE_COMMAND_UI(ID_MUSIC, OnUpdateMusic)
ON_COMMAND(ID_EXIT, OnExit)
//}}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_UPDATE_COMMAND_UI(ID_DOWN,UpdateOnDown)
ON_UPDATE_COMMAND_UI(ID_UP,UpdateOnUp)
ON_UPDATE_COMMAND_UI(ID_LEFT,UpdateOnLeft)
ON_UPDATE_COMMAND_UI(ID_RIGHT,UpdateOnRight)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSnakeView construction/destruction
CSnakeView::CSnakeView()
{
// TODO: add construction code here
sp=9;
bMusic=TRUE;
this->Init();
this->OnMusic();
nLevel=1;
}
CSnakeView::~CSnakeView()
{
}
BOOL CSnakeView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CSnakeView drawing
void CSnakeView::OnDraw(CDC* pDC)
{
CSnakeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// Square win;
win.DrawBakGnd(pDC,0,0,400,400,RGB(0,0,0),FALSE);
win.DrawBakGnd(pDC,0,0,300,300,RGB(110,190,240),TRUE);
for(int i=0;i<3;i++)
win.DrawCell(pDC,11,i,RGB(255,0,0));
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CSnakeView printing
BOOL CSnakeView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CSnakeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CSnakeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CSnakeView diagnostics
#ifdef _DEBUG
void CSnakeView::AssertValid() const
{
CView::AssertValid();
}
void CSnakeView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CSnakeDoc* CSnakeView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSnakeDoc)));
return (CSnakeDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CSnakeView message handlers
void CSnakeView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
nKey=nChar;
CView::OnChar(nChar, nRepCnt, nFlags);
}
void CSnakeView::UpdateOnUp(CCmdUI* pCmdUI)
{
pCmdUI->Enable(nKey=='w'||nKey=='W');
}
void CSnakeView::UpdateOnDown(CCmdUI* pCmdUI)
{
pCmdUI->Enable(nKey=='s'||nKey=='S');
}
void CSnakeView::UpdateOnRight(CCmdUI* pCmdUI)
{
pCmdUI->Enable(nKey=='f'||nKey=='F');
}
void CSnakeView::UpdateOnLeft(CCmdUI* pCmdUI)
{
pCmdUI->Enable((nKey=='A')||nKey=='a');
}
void CSnakeView::OnPause()
{
// TODO: Add your command handler code here
bPause=FALSE;
bStart=TRUE;
}
void CSnakeView::OnUpdatePause(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(bPause);
}
void CSnakeView::OnStart()
{
// TODO: Add your command handler code here
SetTimer(1,100,NULL);
nKey='f';
bStart=TRUE;
bPause=TRUE;
// this->Init();
}
void CSnakeView::OnUpdateStart(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(bStart);
}
void CSnakeView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(!bPause)
return ;
static con=0;
con++;
CDC* pdc=GetDC();
int Orow,Ocol;
Ocol=col;
Orow=row;
this->RandData();
switch(nKey)
{
case 'W':
case 'w':
if(win.CanUp())
{
nKey=0;
row--;
win.bUp=TRUE;
win.bRight=FALSE;
win.bLeft=FALSE;
}
break;
case 's':
case 'S':
if(win.CanDown())
{
nKey=0;
row++;
win.bDown=TRUE;
win.bRight=FALSE;
win.bLeft=FALSE;
}
break;
case 'a':
case 'A':
if(win.CanLeft())
{
nKey=0;
col--;
win.bLeft=TRUE;
win.bDown=FALSE;
win.bUp=FALSE;
}
break;
case 'F':
case 'f':
if(win.CanRight())
{
nKey=0;
col++;
win.bRight=TRUE;
win.bUp=FALSE;
win.bDown=FALSE;
}
break;
default:
break;
}
if(con%sp==0)
{
if(win.bUp&&row==Orow)
row--;
if(win.bRight&&col==Ocol)
col++;
if(win.bDown&&row==Orow)
row++;
if(win.bLeft&&col==Ocol)
col--;
}
//col++;
if(col>14||col<0||row<0||row>14)
{
KillTimer(1);
MessageBox("你输了");
Invalidate();
this->Init();
return;
}
if(Ocol!=col||Orow!=row)
{
win.DrawCell(pdc,row,col,RGB(255,0,0));
if(!this->Result(row,col))
{
KillTimer(1);
MessageBox("你输了!");
Invalidate();
this->Init();
return;
}
if(!this->IncreaseData(row,col))
return;
this->AddData(row,col);
this->InpData(pdc,row,col);
}
ReleaseDC(pdc);
CView::OnTimer(nIDEvent);
}
void CSnakeView::AddData(int row,int col)
{
SnGrid[nControl_1].col=col;
SnGrid[nControl_1].row=row;
nControl_1++;
if(nControl_1>nMumber-1)
nControl_1=0;
}
void CSnakeView::InpData(CDC* pDC,int row,int col)
{
win.DrawCell(pDC,SnGrid[nControl_2].row,SnGrid[nControl_2].col,RGB(110,190,240));
SnGrid[nControl_2].row=-1;
SnGrid[nControl_2].col=-1;
nControl_2++;
if(nControl_2>nMumber-1)
nControl_2=0;
}
BOOL CSnakeView::IncreaseData(int row,int col)
{
if(Grid[row][col]==1)
{
bFill=TRUE;
Grid[row][col]=0;
SnGrid[nControl_1].col=col;
SnGrid[nControl_1].row=row;
nControl_1++;
if(nControl_1>(nMumber-1))
nControl_1=0;
for(int m=nMumber-1;m>=nControl_2;m--)
SnGrid[m+1]=SnGrid[m];
SnGrid[nControl_2].row=-1;
SnGrid[nControl_2].col=-1;
nControl_2++;
//if(nControl_2>(nMumber-1))
//nControl_2=0;
nMumber++;
nScore+=100;
this->DisplayScocre(nScore,nLevel);
return FALSE;
}
return TRUE;
}
void CSnakeView::RandData()
{
int row,col;
BOOL flag;
// bFill=TRUE;
if(bFill)
{
row=rand()%15;
col=rand()%15;
for(int k=0;k<nMumber;k++)
{
flag=TRUE;
if(SnGrid[k].col==col&&SnGrid[k].row==row)
{
flag=FALSE;
break;
}
}
if(flag)
{
Grid[row][col]=1;
CDC *pDC=GetDC();
win.DrawCell(pDC,row,col,RGB(255,255,0));
ReleaseDC(pDC);
bFill=FALSE;
}
}
}
void CSnakeView::Init()
{
nKey='f';
SnGrid[0].row=-1;
SnGrid[0].col=-1;
SnGrid[1].row=11;
SnGrid[1].col=0;
SnGrid[2].row=11;
SnGrid[2].col=1;
SnGrid[3].row=11;
SnGrid[3].col=2;
nScore=0;
bMusic=TRUE;
// sp=9;
col=2;
row=11;
win.bDown=FALSE;
win.bUp=FALSE;
win.bRight=TRUE;
win.bLeft=FALSE;
bPause=TRUE;
bStart=TRUE;
for(int i=4;i<50;i++)
SnGrid[i].col=-1;
SnGrid[i].row=-1;
bFill=TRUE;
nControl_1=0;
nControl_2=1;
for(int m=0;m<16;m++)
for( int n=0;n<16;n++)
Grid[m][n]=0;
nMumber=4;
// this->DisplayScocre(nScore,nLevel);
}
BOOL CSnakeView::Result(int row,int col)
{
for( int k=0;k<nMumber;k++)
if((SnGrid[k].col==col)&&SnGrid[k].row==row)
return FALSE;
return TRUE;
}
void CSnakeView::OnTow()
{
// TODO: Add your command handler code here
sp=3;
nLevel=2;
}
void CSnakeView::OnUpdateTow(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(nLevel==2);
}
void CSnakeView::OnOne()
{
// TODO: Add your command handler code here
sp=9;
nLevel=1;
}
void CSnakeView::OnUpdateOne(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(nLevel==1);
}
void CSnakeView::OnThree()
{
// TODO: Add your command handler code here
sp=2;
nLevel=3;
}
void CSnakeView::OnUpdateThree(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(nLevel==3);
}
void CSnakeView::OnMusic()
{
// TODO: Add your command handler code here
if(bMusic)
{
HWND hWnd;
hWnd = GetSafeHwnd();
char inBuf[300],outBuf[60],fileName[255];
MCIERROR mciError;
strcpy(fileName,"music.mid");
wsprintf( inBuf,"open %s type sequencer alias myseq",fileName);
mciError = mciSendString( inBuf, outBuf, sizeof(outBuf), NULL);
if (mciError == 0)
{
mciError = mciSendString("play myseq notify",NULL,0, hWnd);
if (mciError != 0)
mciSendString("close myseq",NULL,0,NULL);
}
}
else
{
//bMusic = true;
mciSendString("close myseq",NULL,0,NULL);
}
bMusic=!bMusic;
}
void CSnakeView::OnUpdateMusic(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck(!bMusic);
}
void CSnakeView::DisplayScocre(int score,int level)
{
CString str;
str.Format(" 成绩:%d",score);
CDC* pDC=GetDC();
CBrush brush,*Oldbrush;
brush.CreateSolidBrush(RGB(255,(255*score/5000),0,0));
Oldbrush=pDC->SelectObject(&brush);
pDC->Rectangle(301,220,400,(220-(220*score)/5000));
pDC->SetBkColor(RGB(0,0,0));
pDC->SetTextColor(RGB(255,255,255));
pDC->TextOut(322,240,str);
str.Format(" 水平:%d",level);
pDC->TextOut(322,280,str);
pDC->SelectObject(Oldbrush);
}
void CSnakeView::OnExit()
{
// TODO: Add your command handler code here
// ::PostQuitMessage(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -