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

📄 03303940view.cpp

📁 是一款非常经典的推箱子游戏
💻 CPP
字号:
// 03303940View.cpp : implementation of the CMy03303940View class
//
#include <mmsystem.h>
#include "stdafx.h"
#include "03303940.h"
#include "DlgMap.h"
#include "03303940Doc.h"
#include "03303940View.h"
#include "DlgApplication.h"
#define MAP_BACKWALL   48
#define MAP_WALL       49
#define MAP_GROUND     50
#define MAP_BALL       51
#define MAP_BOX        52
#define MAP_BOXBALL    53
#define MAP_LEFTUNEND  58
#define MAP_RIGHTUNEND 55
#define MAP_UPUNEND    56
#define MAP_DOWNUNEND  57
#define MAP_LEFTMAN    54
#define MAP_RIGHTMAN   59
#define MAP_UPMAN      60
#define MAP_DOWNMAN    61
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMy03303940View

IMPLEMENT_DYNCREATE(CMy03303940View, CView)

BEGIN_MESSAGE_MAP(CMy03303940View, CView)
	//{{AFX_MSG_MAP(CMy03303940View)
	ON_COMMAND(ID_SELECT, OnSelect)
	ON_COMMAND(ID_APPLICATION, OnApplication)
	ON_COMMAND(ID_NEXT, OnNext)
	ON_COMMAND(ID_BEFORE, OnBefore)
	ON_COMMAND(ID_REPLAY, OnReplay)
	ON_WM_KEYDOWN()
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_TIMER()
	ON_COMMAND(ID_COLOR, OnColor)
	ON_COMMAND(ID_BEGIN, OnBegin)
	ON_UPDATE_COMMAND_UI(ID_BEFORE, OnUpdateBefore)
	ON_UPDATE_COMMAND_UI(ID_SELECT, OnUpdateSelect)
	ON_UPDATE_COMMAND_UI(ID_REPLAY, OnUpdateReplay)
	ON_UPDATE_COMMAND_UI(ID_NEXT, OnUpdateNext)
	ON_UPDATE_COMMAND_UI(ID_COLOR, OnUpdateColor)
	ON_UPDATE_COMMAND_UI(ID_BEGIN, OnUpdateBegin)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy03303940View construction/destruction

CMy03303940View::CMy03303940View()
{
	// TODO: add construction code here
	sign=FALSE;
	m_Color=RGB(0,0,0);
	m_Mission=1;
	m_left=new CBitmap;
	m_right=new CBitmap;
	m_up=new CBitmap;
	m_down=new CBitmap;
	m_wall=new CBitmap;
	m_box=new CBitmap;
	m_end=new CBitmap;
	m_unend=new CBitmap;
	m_ground=new CBitmap;
    m_left->LoadBitmap(IDB_LEFT);
	m_right->LoadBitmap(IDB_RIGHT);
	m_up->LoadBitmap(IDB_UP);
	m_down->LoadBitmap(IDB_DOWN);
	m_box->LoadBitmap(IDB_BOX);
	m_wall->LoadBitmap(IDB_WALL);
	m_end->LoadBitmap(IDB_END);
	m_unend->LoadBitmap(IDB_UNEND);
	m_ground->LoadBitmap(IDB_GROUND);
	m_begin.LoadBitmap(IDB_BEGIN);
//	sndPlaySound("11.wav",SND_ASYN|SND_LOOP);
}

CMy03303940View::~CMy03303940View()
{
	delete m_left;
	delete m_right;
	delete m_up;
	delete m_down;
	delete m_wall;
	delete m_box;
	delete m_end;
	delete m_unend;
	delete m_ground;
}

BOOL CMy03303940View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMy03303940View drawing

void CMy03303940View::OnDraw(CDC* pDC)
{
	CMy03303940Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if(!sign)
	{
		CDC MemDC;
		MemDC.CreateCompatibleDC(NULL);
		MemDC.SelectObject(m_begin);
		pDC->BitBlt(0,0,500,400,&MemDC,0,0,SRCCOPY);
	}	
	else
		DrawMap(pDC);
}

/////////////////////////////////////////////////////////////////////////////
// CMy03303940View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMy03303940View message handlers

void CMy03303940View::OnSelect() 
{
	// TODO: Add your command handler code here
	CDlgMap dlgmap;
	int id=dlgmap.DoModal();
	if(id==IDOK)
	{
		UpdateData();
		m_Mission=dlgmap.m_map;
		loadmap(m_Mission);
		m_ManPoision=GetManPoision();
		Invalidate();
	}
}

void CMy03303940View::OnApplication() 
{
	// TODO: Add your command handler code here
	CDlgApplication dlg;
	dlg.DoModal();
}
void CMy03303940View::loadmap(int Mission)
{
	FILE *pfile=fopen("map.txt","rb");
	if(pfile==NULL){
		MessageBox("程序路径错误或丢失地图文件(请参考说明)!!!!!","警告",MB_ICONHAND|MB_ICONSTOP|MB_ICONERROR );
		PostQuitMessage(0);
	}
	else
	{
		CString str;
		str.Format("[%d]",Mission);
		char map[20];
		fgets(map,20,pfile);
		while(strncmp(map,str,3)!=0)
			fgets(map,20,pfile);
		for(int i=0;i<14;i++)
			fgets(m_Map[i],20,pfile);
		fclose(pfile);
	}
}
CPoint CMy03303940View::GetManPoision()
{
	CPoint ManPoision;
	for(int i=0;i<14;i++)
	{
		for(int j=0;j<16;j++)
		{
			if((int)m_Map[i][j]==MAP_LEFTUNEND||(int)m_Map[i][j]==MAP_RIGHTUNEND||(int)m_Map[i][j]==MAP_UPUNEND||(int)m_Map[i][j]==MAP_DOWNUNEND||(int)m_Map[i][j]==MAP_LEFTMAN||(int)m_Map[i][j]==MAP_RIGHTMAN||(int)m_Map[i][j]==MAP_UPMAN||(int)m_Map[i][j]==MAP_DOWNMAN)
			{
				ManPoision.x=j;
				ManPoision.y=i;
			}
		}
	}
	return ManPoision;
}
void CMy03303940View::DrawMap(CDC *hdc)
{
	for(int i=0;i<14;i++)
	{
		for(int j=0;j<16;j++)
		{
			switch((int)m_Map[i][j])
			{
			case MAP_BACKWALL:
				DrawBackWall(j*30,i*30,hdc);
				break;
			case MAP_GROUND:
				AddBitmap(j*30,i*30,hdc,m_ground);
				break;
			case MAP_BOX:
				AddBitmap(j*30,i*30,hdc,m_box);
				break;
			case MAP_BALL:
				AddBitmap(j*30,i*30,hdc,m_unend);
				break;
			case MAP_LEFTUNEND:
			case MAP_LEFTMAN:
				AddBitmap(j*30,i*30,hdc,m_left);
				break;
			case MAP_RIGHTUNEND:
			case MAP_RIGHTMAN:
				AddBitmap(j*30,i*30,hdc,m_right);
				break;
			case MAP_UPUNEND:
			case MAP_UPMAN:
				AddBitmap(j*30,i*30,hdc,m_up);
				break;
			case MAP_DOWNUNEND:
			case MAP_DOWNMAN:
				AddBitmap(j*30,i*30,hdc,m_down);
				break;
			case MAP_WALL:
				AddBitmap(j*30,i*30,hdc,m_wall);
				break;
			case MAP_BOXBALL:
				AddBitmap(j*30,i*30,hdc,m_end);
				break;
			}
		}
	}
}
void CMy03303940View::DrawBackWall(int x,int y,CDC *hdc)
{
	hdc->FillSolidRect(x,y,30,30,m_Color);
}
void CMy03303940View::AddBitmap(int x,int y,CDC *hdc,CBitmap *bitmap)
{
	CDC MemDC;
	MemDC.CreateCompatibleDC(NULL);
	MemDC.SelectObject(bitmap);
	hdc->BitBlt(x,y,30,30,&MemDC,0,0,SRCCOPY);
}
BOOL CMy03303940View::IsFinish()
{
	BOOL isfinish=TRUE;
	int sign=0;
	for(int i=0;i<14;i++)
	{
		for(int j=0;j<16;j++)
		{
			if((int)m_Map[i][j]==MAP_BALL||(int)m_Map[i][j]==MAP_LEFTUNEND||(int)m_Map[i][j]==MAP_RIGHTUNEND||(int)m_Map[i][j]==MAP_UPUNEND||(int)m_Map[i][j]==MAP_DOWNUNEND)
			{
				isfinish=FALSE;
				sign=1;
				break;
			}
			if(sign==1)break;
		}
	}
	return isfinish;
}
void CMy03303940View::UpdateMap(UINT nChar)
{
	int x1,y1,x2,y2,x3,y3,sign;
	x1=GetManPoision().x;
	y1=GetManPoision().y;
	switch(nChar)
	{
	case VK_UP:
        x2 = x1;
        y2 = y1 - 1;
        x3 = x1;
        y3 = y1 - 2;
		sign=1;
        UpdateMap(x1, y1, x2, y2, x3, y3,sign);
        break;
    case VK_DOWN:
        x2 = x1;
        y2 = y1 + 1;
        x3 = x1;
        y3 = y1 + 2;
		sign=2;
        UpdateMap(x1, y1, x2, y2, x3, y3,sign);
        break;
    case VK_LEFT:
        x2 = x1 - 1;
        y2 = y1;
        x3 = x1 - 2;
        y3 = y1;
		sign=3;
        UpdateMap(x1, y1, x2, y2, x3, y3,sign);
        break;
    case VK_RIGHT:
        x2 = x1 + 1;
        y2 = y1;
        x3 = x1 + 2;
        y3 = y1;
		sign=4;
        UpdateMap(x1, y1, x2, y2, x3, y3,sign);
        break;
		
	}
}
void CMy03303940View::UpdateMap(int x1,int y1,int x2,int y2,int x3,int y3,int sign)
{
	switch(m_Map[y2][x2])
	{
	case MAP_WALL:
		break;
	case MAP_GROUND:
		if(m_Map[y1][x1]==MAP_LEFTUNEND||m_Map[y1][x1]==MAP_RIGHTUNEND||m_Map[y1][x1]==MAP_UPUNEND||m_Map[y1][x1]==MAP_DOWNUNEND)
			m_Map[y1][x1]=MAP_BALL;
		else m_Map[y1][x1]=MAP_GROUND; 
		if(sign==1) m_Map[y2][x2]=MAP_UPMAN;
		else if(sign==2) m_Map[y2][x2]=MAP_DOWNMAN;
		else if(sign==3) m_Map[y2][x2]=MAP_LEFTMAN;
		else m_Map[y2][x2]=MAP_RIGHTMAN;
		m_ManPoision.x=x2;
		m_ManPoision.y=y2;
		break;
	case MAP_BALL:
		if(m_Map[y1][x1]==MAP_LEFTUNEND||m_Map[y1][x1]==MAP_RIGHTUNEND||m_Map[y1][x1]==MAP_UPUNEND||m_Map[y1][x1]==MAP_DOWNUNEND)
			m_Map[y1][x1]=MAP_BALL;
		else m_Map[y1][x1]=MAP_GROUND;
		if(sign==1) m_Map[y2][x2]=MAP_UPUNEND;
		else if(sign==2) m_Map[y2][x2]=MAP_DOWNUNEND;
		else if(sign==3) m_Map[y2][x2]=MAP_LEFTUNEND;
		else m_Map[y2][x2]=MAP_RIGHTUNEND;
		m_ManPoision.x=x2;
		m_ManPoision.y=y2;
		break;
	case MAP_BOX:
		if(m_Map[y3][x3]==MAP_BOX||m_Map[y3][x3]==MAP_WALL||m_Map[y3][x3]==MAP_BOXBALL)
			break;
		else
		{
			if(m_Map[y3][x3]==MAP_BALL)
				m_Map[y3][x3]=MAP_BOXBALL;
			else m_Map[y3][x3]=MAP_BOX;
		}
		if(m_Map[y1][x1]==MAP_LEFTUNEND||m_Map[y1][x1]==MAP_RIGHTUNEND||m_Map[y1][x1]==MAP_UPUNEND||m_Map[y1][x1]==MAP_DOWNUNEND)
			m_Map[y1][x1]=MAP_BALL;
		else m_Map[y1][x1]=MAP_GROUND;
		if(sign==1) m_Map[y2][x2]=MAP_UPMAN;
		else if(sign==2) m_Map[y2][x2]=MAP_DOWNMAN;
		else if(sign==3) m_Map[y2][x2]=MAP_LEFTMAN;
		else m_Map[y2][x2]=MAP_RIGHTMAN;
		m_ManPoision.x=x2;
		m_ManPoision.y=y2;
		break;
	case MAP_BOXBALL:
		if(m_Map[y3][x3]==MAP_BOX||m_Map[y3][x3]==MAP_WALL||m_Map[y3][x3]==MAP_BOXBALL)
			break;
		else
		{
			if(m_Map[y3][x3]==MAP_BALL)
				m_Map[y3][x3]=MAP_BOXBALL;
			else m_Map[y3][x3]=MAP_BOX;
		}
		if(m_Map[y1][x1]==MAP_LEFTUNEND||m_Map[y1][x1]==MAP_RIGHTUNEND||m_Map[y1][x1]==MAP_UPUNEND||m_Map[y1][x1]==MAP_DOWNUNEND)
			m_Map[y1][x1]=MAP_BALL;
		else m_Map[y1][x1]=MAP_GROUND;
		if(sign==1) m_Map[y2][x2]=MAP_UPUNEND;
		else if(sign==2) m_Map[y2][x2]=MAP_DOWNUNEND;
		else if(sign==3) m_Map[y2][x2]=MAP_LEFTUNEND;
		else m_Map[y2][x2]=MAP_RIGHTUNEND;
		m_ManPoision.x=x2;
		m_ManPoision.y=y2;
		break;
	}
}
void CMy03303940View::OnNext() 
{
	// TODO: Add your command handler code here
	m_Mission++;
	if(m_Mission>42)
		m_Mission=1;
	loadmap(m_Mission);
	m_ManPoision=GetManPoision();
	Invalidate(FALSE);
}

void CMy03303940View::OnBefore() 
{
	// TODO: Add your command handler code here
	m_Mission--;
	if(m_Mission<1)
		m_Mission=42;
	loadmap(m_Mission);
	m_ManPoision=GetManPoision();
	Invalidate(FALSE);
}

void CMy03303940View::OnReplay() 
{
	// TODO: Add your command handler code here
	loadmap(m_Mission);
	m_ManPoision=GetManPoision();
	Invalidate(FALSE);
}

void CMy03303940View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	UpdateMap(nChar);
	if(IsFinish()==TRUE)
	{
		m_Mission++;
		if(m_Mission>42)
			m_Mission=1;
		loadmap(m_Mission);
		m_ManPoision=GetManPoision();
	}
	Invalidate(FALSE);
	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

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

void CMy03303940View::OnDestroy() 
{
	CView::OnDestroy();
	KillTimer(1);
	// TODO: Add your message handler code here
	
}

void CMy03303940View::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	char str[50];
	CStatusBar *pStatus;
	pStatus=(CStatusBar *)AfxGetApp()->m_pMainWnd->GetDescendantWindow(AFX_IDW_STATUS_BAR);
	CTime time=CTime::GetCurrentTime();
	sprintf(str,"%02d:%02d:%02d",time.GetHour(),time.GetMinute(),time.GetSecond());
	pStatus->SetPaneText(3,str);
	CView::OnTimer(nIDEvent);
}

void CMy03303940View::OnColor() 
{
	// TODO: Add your command handler code here
	CColorDialog ColorDlg;
	if(ColorDlg.DoModal()==IDOK)
	{
		m_Color=ColorDlg.GetColor();
	}
	Invalidate(FALSE);
}

void CMy03303940View::OnBegin() 
{
	// TODO: Add your command handler code here
	m_ManPoision=GetManPoision();
	loadmap(m_Mission);
	sign=TRUE;
	Invalidate();
}

void CMy03303940View::OnUpdateBefore(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(sign);
		
}

void CMy03303940View::OnUpdateSelect(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(sign);	
}

void CMy03303940View::OnUpdateReplay(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(sign);
}

void CMy03303940View::OnUpdateNext(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(sign);
}

void CMy03303940View::OnUpdateColor(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(sign);
}

void CMy03303940View::OnUpdateBegin(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(!sign);
}

⌨️ 快捷键说明

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