📄 renzhe2view.cpp
字号:
// renzhe2View.cpp : implementation of the CRenzhe2View class
//
#include "stdafx.h"
#include "renzhe2.h"
#include "renzhe2Doc.h"
#include "renzhe2View.h"
#include "resource.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRenzhe2View
IMPLEMENT_DYNCREATE(CRenzhe2View, CView)
BEGIN_MESSAGE_MAP(CRenzhe2View, CView)
ON_WM_CONTEXTMENU()
//{{AFX_MSG_MAP(CRenzhe2View)
ON_WM_KEYDOWN()
ON_WM_TIMER()
ON_COMMAND(ID_MENUzhuce, OnMENUzhuce)
ON_COMMAND(ID_Primary, OnPrimary)
ON_COMMAND(ID_Senior, OnSenior)
ON_COMMAND(ID_Junior, OnJunior)
ON_COMMAND(ID_GameWudi, OnGameWudi)
ON_COMMAND(ID_GameScore100, OnGameScore100)
ON_COMMAND(ID_GameScore200, OnGameScore200)
ON_COMMAND(ID_GameScore500, OnGameScore500)
ON_COMMAND(ID_Instructions, OnInstructions)
ON_COMMAND(ID_GJunior, OnGJunior)
ON_COMMAND(ID_GPrimer, OnGPrimer)
ON_COMMAND(ID_GSenior, OnGSenior)
ON_COMMAND(ID_Gtianxiawudi, OnGtianxiawudi)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CRenzhe2View construction/destruction
CRenzhe2View::CRenzhe2View()
{
// TODO: add construction code here
}
CRenzhe2View::~CRenzhe2View()
{
}
BOOL CRenzhe2View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CRenzhe2View drawing
void CRenzhe2View::OnDraw(CDC* pDC)
{
CRenzhe2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CString str_score,str_name1("Sorce:"),str_name2("所用时间:"),str_time,str_shuoming("游戏说明:按空格键可开始游戏,在按空格键可暂停游,上下左右为各方向键");
CString totletime;
CRect rect2;
CBrush *oldbrush,newbrush1,newbrush2;
CPen *oldpen,newpen;
int m_yanse;
//COLORREF RGB(125,205,100);
newbrush1.CreateSolidBrush(RGB(125,205,100));
GetClientRect(&rect2);
oldbrush=pDC->SelectObject(&newbrush1);
for(int i=0;i<=pDoc->m_long;i++)
pDC->Rectangle(pDoc->rect[i]);
m_yanse=rand()%255;
newbrush2.CreateSolidBrush(RGB(m_yanse*3,m_yanse,m_yanse+150));
pDC->SelectObject(&newbrush2);
//if(pDoc->m_doushu>=1)
pDC->Rectangle(pDoc->douzi[pDoc->m_doushu].dou_rect);
pDC->SelectObject(oldbrush);
//newbrush2.DeleteObject();
newpen.CreatePen(PS_SOLID,2,RGB(0,0,255));
oldpen=pDC->SelectObject(&newpen);
pDC->MoveTo(rect2.left,rect2.top+50);
pDC->LineTo(rect2.right,rect2.top+50);
pDC->SelectObject(oldpen);
//pDC->Rectangle(rect2.left+50,rect2.top+10,rect2.left+100,rect2.top+40);
pDC->SetTextColor(RGB(10,100,255));
str_score.Format("%ld",pDoc->m_score);
pDC->TextOut(60,15,str_score);
pDC->TextOut(10,15,str_name1);
pDC->TextOut(rect2.right-150,15,str_name2);
str_time.Format("%8.1f",pDoc->time);
pDC->TextOut(rect2.right-80,15,str_time);
pDC->TextOut(90,15,str_shuoming);
if(pDoc->game_zhongzhi==1)
pDC->TextOut(200,400,"GAME OVER");
if(pDoc->m_score==pDoc->m_totlescore) //改变分数可以改变游戏结束前蛇吃豆的数量
{
KillTimer(1);
pDoc->game_zhongzhi=1;
pDC->TextOut(200,400,"YOU WIN THE GAME !!");
totletime.Format("%8.1f",pDoc->time);
pDC->TextOut(125,430,"总用时(秒):");
pDC->TextOut(200,430,totletime);
}
if(pDoc->m_dialog==1)
{
pDoc->m_dialog=0;
P_name you;
if(you.DoModal()==IDOK)
{
pDC->TextOut(rect2.right-240,15,you.m_personname);
pDC->TextOut(rect2.right-290,15,"玩家:");
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CRenzhe2View printing
BOOL CRenzhe2View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CRenzhe2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CRenzhe2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CRenzhe2View diagnostics
#ifdef _DEBUG
void CRenzhe2View::AssertValid() const
{
CView::AssertValid();
}
void CRenzhe2View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CRenzhe2Doc* CRenzhe2View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRenzhe2Doc)));
return (CRenzhe2Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CRenzhe2View message handlers
void CRenzhe2View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CRenzhe2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CRect rect2;
GetClientRect(&rect2);
switch(nChar)
{
case VK_SPACE:
{
if(pDoc->zhongzhi==1&&pDoc->game_zhongzhi==0)
{
SetTimer(1,pDoc->m_gamelevel,0);
pDoc->zhongzhi=0;
}
else
{
KillTimer(1);
pDoc->zhongzhi=1;
}
break;
}
case VK_UP:
{
if(!(pDoc->L_point.y<(rect2.top+40))&&pDoc->m_heng==1)
{
//pDoc->L_point.y-=10;
//pDoc->R_point.y-=10;
pDoc->m_heng=0;
pDoc->u_yundong=1;
pDoc->d_yundong=0;
pDoc->l_yundong=0;
pDoc->r_yundong=0;
}
break;
}
case VK_DOWN:
{
if(!(pDoc->R_point.y>rect2.bottom)&&pDoc->m_heng==1)
{
//pDoc->R_point.y+=10;
//pDoc->L_point.y+=10;
pDoc->m_heng=0;
pDoc->u_yundong=0;
pDoc->d_yundong=1;
pDoc->l_yundong=0;
pDoc->r_yundong=0;
}
break;
}
case VK_LEFT:
{
if(!(pDoc->L_point.x<rect2.left)&&(pDoc->m_heng==0))
{
//pDoc->L_point.x-=10;
//pDoc->R_point.x-=10;
pDoc->m_heng=1;
pDoc->u_yundong=0;
pDoc->d_yundong=0;
pDoc->l_yundong=1;
pDoc->r_yundong=0;
}
break;
}
case VK_RIGHT:
{
if(!(pDoc->R_point.x>rect2.right)&&pDoc->m_heng==0)
{
//pDoc->L_point.x+=10;
//pDoc->R_point.x+=10;
pDoc->m_heng=1;
pDoc->u_yundong=0;
pDoc->d_yundong=0;
pDoc->l_yundong=0;
pDoc->r_yundong=1;
}
break;
}
}
/*while(1) //循环的目的是如果豆子的位置与蛇重合,则重新产生豆子
{
if(pDoc->zhongzhi==0&&pDoc->douzi[pDoc->m_doushu].dou_cunzai==1)
{int x=rand()%(500);
int y=(rand()%500)+60;
//int x=rand()%(rect2.right);
//int y=(rand()%(rect2.bottom-60))+60; //让豆子产生在说明栏以下
pDoc->douzi[pDoc->m_doushu].dou_rect=CRect(x-10,y-10,x,y);
pDoc->m_doushu++;
pDoc->douzi[pDoc->m_doushu].dou_cunzai=0;
}
for(int j=0;j<=pDoc->m_long;j++)
if(pDoc->douzi[pDoc->m_doushu].dou_rect==pDoc->rect[j])
pDoc->dou_weizhi=1;
else
pDoc->dou_weizhi=0;
if(pDoc->dou_weizhi==0)
break;
}
InvalidateRect(pDoc->douzi[pDoc->m_doushu-1].dou_rect);*/
// pDoc->rect[0]=CRect(pDoc->L_point,pDoc->R_point);
// Invalidate();
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CRenzhe2View::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CRenzhe2Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CRect rect3,rect1,s_rect(60,15,100,40),t_rect; //rect1是蛇的运动重绘区
GetClientRect(&rect3);
rect1=CRect(0,rect3.top+51,rect3.right,rect3.bottom);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -