MFC课程设计拼图游戏.docx
《MFC课程设计拼图游戏.docx》由会员分享,可在线阅读,更多相关《MFC课程设计拼图游戏.docx(18页珍藏版)》请在冰豆网上搜索。
MFC课程设计拼图游戏
课程设计
VisualC++
指导老师:
孔令德
班级1020542
学号03
姓名李伯涵
1设计描述
1.1项目简介1
1.2设计目的1
2设计分析
2.1设计特点1
2.2主要头文件、源文件1
3项目代码
3.1头文件2
3.2源文件3
4游戏截图1
1设计描述
1.1项目简介
设计主要为MFC对位图的处理,包括窗口背景、位图透明处理、双缓冲处理动画等。
内容为一张分成若干块的图片,当鼠标点击其中一部分,被点击部分自动移动到空白部分,其中默认有两张图,也可以自己导入图片,游戏可选择难度,即图片被分成的块数。
界面右边有实例图,并统计移动次数。
1.2设计目的
设计是对于mfc处理位图的巩固,及鼠标消息的应用,学习双缓冲处理技术、对话框的设计、在菜单栏工具栏添加功能按钮,获得系统资源,对mfc技术进行小规模综合应用。
2设计分析
2.1设计特点
程序默认窗口大小显示,动画自然,可以导入自己喜欢的图片,选择适当难度。
2.2主要头文件、源文件
1头文件:
pintu.hpintuDlg.hpicture.hspell.h
2源文件:
pintu.cpppintuDlg.cpppicture.cppspell.cpp
3项目代码
3.1头文件
#include"resource.h"
//CMyApp:
classCMyApp:
publicCWinApp
{
public:
CMyApp();
DECLARE_MESSAGE_MAP()
}
#include"picture.h"
classCMyDlg:
publicCDialog
{
public:
CMyDlg(CWnd*pParent=NULL);
protected:
HICONm_hIcon;
intstep;//移动的步数
BOOLbegin;//游戏是否完成
CPicturepic;
intsize;//方块数
BOOLborder;//边框
voidShow(CDC*dc);
DECLARE_MESSAGE_MAP()
};
#include
classCPicture{
public:
CPicture();
~CPicture();
BOOLLoad(UINTnIDRes);
BOOLLoad(LPCTSTRpszPathName);
BOOLLoad(CFile&file);
BOOLLoad(CArchive&ar);
BOOLLoad(IStream*pstm);
BOOLRender(CDC*pDC,CRectrc=CRect(0,0,0,0),
LPCRECTprcMFBounds=NULL)const;
CSizeGetImageSize(CDC*pDC=NULL)const;
operatorIPicture*(){
returnm_spIPicture;
}
voidGetHIMETRICSize(OLE_XSIZE_HIMETRIC&cx,OLE_YSIZE_HIMETRIC&cy)const{
cx=cy=0;
const_cast(this)->m_hr=m_spIPicture->get_Width(&cx);
ASSERT(SUCCEEDED(m_hr));
const_cast(this)->m_hr=m_spIPicture->get_Height(&cy);
ASSERT(SUCCEEDED(m_hr));
}
voidFree(){
if(m_spIPicture){
m_spIPicture.Release();
}
}
protected:
CComQIPtrm_spIPicture;
HRESULTm_hr;
};
classCSpell{
protected:
int*map;
intwidth,height;//长宽
intblank;//空白块的位置
#defineLENGTHwidth*height//总长度
public:
CSpell(intw=3,inth=3,intbx=0,intby=0);//构造函数,传入长宽和空白块位置
~CSpell();
intGetPic(intw,inth);//获取个某个位置的图片
intMove(intw,inth);//移动某个位置的图片
voidInit();//初始化(生成随机位置)
voidReCreate(intw,inth,intbx=0,intby=0);//重新构建大小
};
3.2源文件
#include"stdafx.h"
#include"pintu.h"
#include"pintuDlg.h"
BOOLCMyApp:
:
InitInstance()
{
AfxEnableControlContainer();
#ifdef_AFXDLL
Enable3dControls();
#else
Enable3dControlsStatic();
#endif
CMyDlgdlg;
m_pMainWnd=&dlg;
intnResponse=dlg.DoModal();
if(nResponse==IDOK)
{
//TODO:
Placecodeheretohandlewhenthedialogis
}
elseif(nResponse==IDCANCEL)
{
//TODO:
Placecodeheretohandlewhenthedialogis
}
ReturnFALSE;
}
#include"stdafx.h"
#include"pintu.h"
#include"pintuDlg.h"
#include"spell.h"
CSpellspell(3,3,2,2);
voidShowTranBmp(CDC*dc,CBitmap*bmp,COLORREFbgcolor,intx,inty,intw,inth,intbx,intby,intbw,intbh){
CDCmdc;
CDCtdc;//图像
CDCddc;//遮罩
CBitmaptbmp;
CBitmapdbmp;
CBitmap*ob;
CBitmap*tob;
CBitmap*dob;
mdc.CreateCompatibleDC(dc);
tdc.CreateCompatibleDC(dc);
ddc.CreateCompatibleDC(dc);
tbmp.CreateBitmap(bw,bh,1,32,NULL);
dbmp.CreateBitmap(bw,bh,1,1,NULL);
ob=mdc.SelectObject(bmp);
tob=tdc.SelectObject(&tbmp);
dob=ddc.SelectObject(&dbmp);
tdc.BitBlt(-bx,-by,bw+bx,bh+by,&mdc,0,0,SRCCOPY);
tdc.SetBkColor(bgcolor);
ddc.BitBlt(0,0,bw,bh,&tdc,0,0,SRCCOPY);
dc->BitBlt(x,y,w,h,&tdc,0,0,SRCINVERT);
dc->BitBlt(x,y,w,h,&ddc,0,0,SRCAND);
dc->BitBlt(x,y,w,h,&tdc,0,0,SRCINVERT);
mdc.SelectObject(ob);
tdc.SelectObject(tob);
ddc.SelectObject(dob);
classCAboutDlg:
publicCDialog
{
public:
CAboutDlg();
DECLARE_MESSAGE_MAP()
};
voidCAboutDlg:
:
OnPaint()
{
CPaintDCdc(this);//devicecontextforpainting
CBitmaplygame;
lygame.LoadBitmap(IDB_ABOUTIM);
ShowTranBmp(&dc,&lygame,RGB(0,0,0),8,8,307,92);
CMyDlg:
:
CMyDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CMyDlg:
:
IDD,pParent)
{
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
begin=TRUE;
step=0;
pic.Load(IDR_IMAGEB);
size=3;
border=TRUE;
}
BOOLCMyDlg:
:
OnInitDialog()
{
CDialog:
:
OnInitDialog();
ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<0xF000);
SetIcon(m_hIcon,TRUE);SetIcon(m_hIcon,FALSE);CMenu*pMenu=this->GetMenu();
pMenu->CheckMenuItem(ID_MENU_3,MF_CHECKED);
pMenu->CheckMenuItem(ID_MENU_BORDER,MF_CHECKED);
returnTRUE;}
voidCMyDlg:
:
OnPaint()
{
CPaintDCdc(this);
if(IsIconic())
{
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
dc.DrawIcon(x,y,m_hIcon);
}
else
{
CDialog:
:
OnPaint();
}
Show(&dc);
}
HCUR