需要源码和资源请点赞关注收藏后评论区留言私信~~~
扫雷游戏的Visual C++工程采用MFC对话框模式进行开发,下面进行具体讲解
一、游戏菜单的实现
在扫雷游戏中,通过如下几步即可实现添加游戏的菜单
1:在扫雷游戏工程的资源中添加一个菜单资源
2:给每个菜单栏添加响应函数到CMineDlg类中
3:菜单响应函数的实现
代码如下
// MineDlg.h : header file
//
#if !defined(AFX_MINEDLG_H__0EE906D3_ACB0_418A_85F5_7B2125CF0ADC__INCLUDED_)
#define AFX_MINEDLG_H__0EE906D3_ACB0_418A_85F5_7B2125CF0ADC__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/
// CMineDlg dialog
#include "MyMine.h"
class CMineDlg : public CDialog
{
// Construction
public:
void InitMenu();
CMineDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CMineDlg)
enum { IDD = IDD_MINE_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMineDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
BOOL m_bStart;
CMyMine mine;
void PlayBackMusic(BOOL bCheck);
// Generated message map functions
//{{AFX_MSG(CMineDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
virtual void OnOK();
virtual void OnCancel();
afx_msg void OnAbout();
afx_msg void OnExitGame();
afx_msg void OnHelp();
afx_msg void OnPlayMusic();
afx_msg void OnStartGame();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MINEDLG_H__0EE906D3_ACB0_418A_85F5_7B2125CF0ADC__INCLUDED_)
二、游戏帮助对话框的实现
扫雷游戏中的帮助是使用一个对话框来实现的,其实现步骤如下
1:添加一个对话框资源到工程中,并填写说明文字
2:编写一个ChelpDlg类,主要是加载对话框资源,通过资源中的文字说明对游戏操作方法进行描述,同时只包含单击知道了按钮的响应函数,类声明如下
#if !defined(AFX_HELPDLG_H__EE6E6AF7_690A_431B_A0A9_C94F68CCE3E1__INCLUDED_)
#define AFX_HELPDLG_H__EE6E6AF7_690A_431B_A0A9_C94F68CCE3E1__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// HelpDlg.h : header file
//
/
// CHelpDlg dialog
class CHelpDlg : public CDialog
{
// Construction
public:
CHelpDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CHelpDlg)
enum { IDD = IDD_HELP_DLG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CHelpDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CHelpDlg)
virtual void OnOK();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_HELPDLG_H__EE6E6AF7_690A_431B_A0A9_C94F68CCE3E1__INCLUDED_)
CHelpDlg对话框类的实现如下,需要实现对话框类的构造函数,析构函数和知道了按钮响应函数,代码如下
// HelpDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Mine.h"
#include "HelpDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CHelpDlg dialog
CHelpDlg::CHelpDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHelpDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CHelpDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CHelpDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHelpDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CHelpDlg, CDialog)
//{{AFX_MSG_MAP(CHelpDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CHelpDlg message handlers
void CHelpDlg::OnOK()
{
CDialog::OnOK();
}
三、游戏英雄榜对话框的实现
扫雷游戏英雄榜的实现,分为如下几个步骤
1:创建一个对话框资源,并添加响应的控件
2:配置文件
3:添加CHeroDlg类,其中需要包含对话框资源和设置可写记录标志接口函数声明,用于外部函数调用时,设置是否对配置文件进行写操作,类声明如下
#if !defined(AFX_HERODLG_H__6DD769D0_3A89_47E8_A982_DE8BD87975FE__INCLUDED_)
#define AFX_HERODLG_H__6DD769D0_3A89_47E8_A982_DE8BD87975FE__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// HeroDlg.h : header file
//
/
// CHeroDlg dialog
class CHeroDlg : public CDialog
{
// Construction
public:
void SetWriteFlg(BOOL bflg);
CHeroDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CHeroDlg)
enum { IDD = IDD_HERO_LIST };
CString m_name;
int m_time;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CHeroDlg)
public:
virtual int DoModal();
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CHeroDlg)
virtual void OnOK();
afx_msg void OnBtn();
virtual BOOL OnInitDialog();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
BOOL m_bWriteflg;
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_HERODLG_H__6DD769D0_3A89_47E8_A982_DE8BD87975FE__INCLUDED_)
CHeroDlg类的实现中通过调用系统API韩素华,来对配置文件进行读写操作,而设置读写标志接口函数,是对类的一个成员变量进行赋值操作,达到写入或者读取的区分,代码如下
// HeroDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Mine.h"
#include "HeroDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/
// CHeroDlg dialog
CHeroDlg::CHeroDlg(CWnd* pParent /*=NULL*/)
: CDialog(CHeroDlg::IDD, pParent)
{
m_bWriteflg = FALSE;
}
void CHeroDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CHeroDlg)
DDX_Text(pDX, IDC_NAME_EDIT, m_name);
DDX_Text(pDX, IDC_TIME_EDIT, m_time);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CHeroDlg, CDialog)
//{{AFX_MSG_MAP(CHeroDlg)
ON_BN_CLICKED(IDOK_BTN, OnBtn)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/
// CHeroDlg message handlers
void CHeroDlg::OnOK()
{
}
void CHeroDlg::SetWriteFlg(BOOL bflg)
{
m_bWriteflg = bflg;
}
int CHeroDlg::DoModal()
{
char pszTmp[128] = {0};
//读取配置文件
GetPrivateProfileString("HERO", "name", "",
pszTmp, 127, ".\\hero.ini");
m_name = CString(pszTmp);
if(!m_bWriteflg)
{
GetPrivateProfileString("HERO", "time", "0",
pszTmp, 127, ".\\hero.ini");
m_time = atoi(pszTmp);
}
return CDialog::DoModal();
}
void CHeroDlg::OnBtn()
{
UpdateData(TRUE);
if(m_bWriteflg)
{
CString tmp;
tmp.Format("%d", m_time);
WritePrivateProfileString("HERO", "name", m_name, ".\\hero.ini");
WritePrivateProfileString("HERO", "time", tmp, ".\\hero.ini");
}
m_bWriteflg = FALSE;
CDialog::OnOK();
}
BOOL CHeroDlg::OnInitDialog()
{
CDialog::OnInitDialog();
if(m_bWriteflg)
{
SetDlgItemText(IDOK_BTN, "记录");
}
return TRUE;
}
四、游戏背景音乐的实现
通过调用Windows的API函数sndPlaySound实现,步骤不再赘述,代码如下
void CMineDlg::PlayBackMusic(BOOL bCheck)
{
if(bCheck)
{ //播放指定音乐文件
sndPlaySound("music.wav",SND_ASYNC);
}
else
{ //停止播放
sndPlaySound(NULL,SND_PURGE);
}
}
测试效果如下
文章来源:https://uudwc.com/A/W1av
创作不易 觉得有帮助请点赞关注收藏~~~文章来源地址https://uudwc.com/A/W1av