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

📄 5_1view.cpp

📁 简单的躲炸弹的游戏
💻 CPP
字号:
// 5_1View.cpp : implementation of the CMy5_1View class
//

#include "stdafx.h"
#include "5_1.h"

#include "5_1Doc.h"
#include "5_1View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMy5_1View

IMPLEMENT_DYNCREATE(CMy5_1View, CView)

BEGIN_MESSAGE_MAP(CMy5_1View, CView)
	//{{AFX_MSG_MAP(CMy5_1View)
	ON_WM_KEYDOWN()
	ON_WM_CREATE()
	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()

/////////////////////////////////////////////////////////////////////////////
// CMy5_1View construction/destruction

CMy5_1View::CMy5_1View()
{
	// TODO: add construction code here
    cloud.LoadBitmap(IDB_BITMAP1);
}

CMy5_1View::~CMy5_1View()
{
}

BOOL CMy5_1View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMy5_1View drawing

void CMy5_1View::OnDraw(CDC* pDC)
{
	CMy5_1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	 CDC Dc;
	 if(Dc.CreateCompatibleDC(pDC)==FALSE)
		  AfxMessageBox("Can't create DC");
	 		 //在不同位置显示位图
  	Dc.SelectObject(cloud);
	pDC->StretchBlt(0,0,800,600,&Dc,0,0,800,600,SRCCOPY);
	//显示其他位图
	game.Draw(pDC);

}

/////////////////////////////////////////////////////////////////////////////
// CMy5_1View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMy5_1View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMy5_1View message handlers

void CMy5_1View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	int i;
	switch(nChar)
	{
	//子弹按钮
	case VK_NUMPAD3:
		//界面数组位置为2
		game.yy[game.m2.point.x+game.m2.direction][game.m2.point.y]=2;
			break;
	case VK_LEFT:
		//向左移动1
		game.m2.point.x--;
		//老鼠方向为左
		game.m2.direction=-1;
		//如果是边界,从右边出现
		if(game.m2.point.x<0)
			game.m2.point.x=15;
		//如果下面空,下掉
		if(game.yy[game.m2.point.x][game.m2.point.y+1]!=1)
			for(i=game.m2.point.y;i<12;i++)
				//当位置不空
				if(game.yy[game.m2.point.x][i]==1)
				{
					//停止在上面
					game.m2.point.y=i-1;
					break;
				}
		break;
		//道理同上
	case VK_RIGHT:
	    game.m2.point.x++;
		game.m2.direction=1;
		if(game.m2.point.x>15)
			game.m2.point.x=0;
		if(game.yy[game.m2.point.x][game.m2.point.y]!=1)
			for(i=game.m2.point.y;i<12;i++)
				if(game.yy[game.m2.point.x][i]==1)
				{
					game.m2.point.y=i-1;
					break;
				}
		break;		
	//上跳
	case VK_UP:
		//如果上面能站住,上移
		if(game.yy[game.m2.point.x][game.m2.point.y-1]==1)
			game.m2.point.y=game.m2.point.y-2;
		//否则,如果前面能够站住,上移
		else if(game.yy[game.m2.point.x+game.m2.direction][game.m2.point.y-1]==1)
			{
				game.m2.point.x+=game.m2.direction;
				game.m2.point.y=game.m2.point.y-2;
			}
		//否则,如果后面能够,上移
		else if(game.yy[game.m2.point.x-game.m2.direction][game.m2.point.y-1]==1)
			{
				game.m2.point.x-=game.m2.direction;
				game.m2.point.y=game.m2.point.y-2;
				game.m2.direction=-game.m2.direction;
			}
		break;
	//下跳
	case VK_DOWN:
		//下掉
		for(i=game.m2.point.y+2;i<12;i++)
			if(game.yy[game.m2.point.x][i]==1)
			{
				game.m2.point.y=i-1;
				break;
			}
		break;
	//以下道理同上
	case 75:
		game.yy[game.m1.point.x+game.m1.direction][game.m1.point.y]=3;
			break;
	case 65:
		game.m1.point.x--;
		game.m1.direction=-1;
		if(game.m1.point.x<0)
			game.m1.point.x=15;
		if(game.yy[game.m1.point.x][game.m1.point.y+1]!=1)
			for(i=game.m1.point.y;i<12;i++)
				if(game.yy[game.m1.point.x][i]==1)
				{
					game.m1.point.y=i-1;
					break;
				}
		break;
	case 68:
	    game.m1.point.x++;
		game.m1.direction=1;
		if(game.m1.point.x>15)
			game.m1.point.x=0;
		if(game.yy[game.m1.point.x][game.m1.point.y]!=1)
			for(i=game.m1.point.y;i<12;i++)
				if(game.yy[game.m1.point.x][i]==1)
				{
					game.m1.point.y=i-1;
					break;
				}
		break;		
	case 87:
		if(game.yy[game.m1.point.x][game.m1.point.y-1]==1)
			game.m1.point.y=game.m1.point.y-2;
		else if(game.yy[game.m1.point.x+game.m1.direction][game.m1.point.y-1]==1)
		{
			game.m1.point.x+=game.m1.direction;
			game.m1.point.y=game.m1.point.y-2;
		}
		else if(game.yy[game.m1.point.x-game.m1.direction][game.m1.point.y-1]==1)
		{
			game.m1.point.x-=game.m1.direction;
			game.m1.point.y=game.m1.point.y-2;
			game.m1.direction=-game.m1.direction;
		}
	
		break;
	case 83:
		for(i=game.m1.point.y+2;i<12;i++)
			if(game.yy[game.m1.point.x][i]==1)
			{
				game.m1.point.y=i-1;
				break;
			}
		break;
	}
	//重画
	CDC* pDC=GetDC();
	OnDraw(pDC);
	ReleaseDC(pDC);

	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

int CMy5_1View::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	SetTimer(1,250,NULL);
	return 0;
}

void CMy5_1View::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	game.BallMove();
	game.KillMouse();
	game.KillBall();
	game.GetHeart();
	OnDraw(GetDC());
	int i,j;
	for(i=0;i<16;i++)
		for(j=0;j<12;j++)
			if(game.yy[i][j]==2||game.yy[i][j]==3)
				game.yy[i][j]=0;
	CView::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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