需要源码和资源请点赞关注收藏后评论区留言私信~~~
黑白棋游戏的Visual C++工程采用MFC对话框模式进行开发,下面对它进行详细介绍
一、游戏菜单的实现
首先要在工程资源中添加一个菜单资源类,菜单响应函数实现如下
// OthelloDlg.h : header file//#if !defined(AFX_OTHELLODLG_H__DAC0C8C2_DDB6_4DA7_A56E_440CDF9A626B__INCLUDED_)#define AFX_OTHELLODLG_H__DAC0C8C2_DDB6_4DA7_A56E_440CDF9A626B__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000/// COthelloDlg dialog#include "ChessBoard.h"class COthelloDlg : public CDialog{// Constructionpublic:void GameStart();void PlayBackMusic(BOOL bCheck);void InitMenu();COthelloDlg(CWnd* pParent = NULL);// standard constructorint m_nBlackCount;//黑子个数int m_nWhiteCount;//白子个数CChessBoard m_chess;//棋盘对象// Dialog Data//{{AFX_DATA(COthelloDlg)enum { IDD = IDD_OTHELLO_DIALOG };// NOTE: the ClassWizard will add data members here//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(COthelloDlg)protected:virtual void DoDataExchange(CDataExchange* pDX);// DDX/DDV support//}}AFX_VIRTUAL// Implementationprotected:HICON m_hIcon;// Generated message map functions//{{AFX_MSG(COthelloDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnAbout();afx_msg void OnExitGame();afx_msg void OnGameStart();afx_msg void OnHelp();afx_msg void OnLevelHigh();afx_msg void OnLevelLow();afx_msg void OnLevelNor();afx_msg void OnPlayMusic();afx_msg void OnBackBtn();//}}AFX_MSGafx_msg void OnRecalc(WPARAM wParam, LPARAM lParam);DECLARE_MESSAGE_MAP()};//{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_OTHELLODLG_H__DAC0C8C2_DDB6_4DA7_A56E_440CDF9A626B__INCLUDED_)
二、游戏帮助对话框的实现
黑白棋游戏中的帮助是使用一个对话框来实现的,首先要添加一个对话框资源到工程中,并填写说明
其类声明代码如下
#if !defined(AFX_HELPDLG_H__A6CEBADE_794E_4F8C_85FB_311FC78558A3__INCLUDED_)#define AFX_HELPDLG_H__A6CEBADE_794E_4F8C_85FB_311FC78558A3__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000// HelpDlg.h : header file///// CHelpDlg dialogclass CHelpDlg : public CDialog{// Constructionpublic:CHelpDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data//{{AFX_DATA(CHelpDlg)enum { IDD = IDD_HELP };// 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// Implementationprotected:// Generated message map functions//{{AFX_MSG(CHelpDlg)virtual void OnOK();//}}AFX_MSGDECLARE_MESSAGE_MAP()};//{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_HELPDLG_H__A6CEBADE_794E_4F8C_85FB_311FC78558A3__INCLUDED_)
类实现代码如下,需要实现对话框类的构造函数,析构函数和知道了按钮响应函数
// HelpDlg.cpp : implementation file//#include "stdafx.h"#include "Othello.h"#include "HelpDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/// CHelpDlg dialogCHelpDlg::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_MAPEND_MESSAGE_MAP()/// CHelpDlg message handlersvoid CHelpDlg::OnOK() {// TODO: Add extra validation hereCDialog::OnOK();}
三、游戏播放背景音乐的实现
播放游戏背景音乐,是通过调用Windows的API函数sndPlaySound来是实现的,当玩家选择游戏设置-播放音乐命令时,就播放音乐,相反如果取消,就停止播放音乐,要实现这个功能,需要如下几个步骤
1:在工程文件中,添加winmm.lib静态库文件以及头文件
2:实现COthellDlg类中的PlayBackMusic成员函数,代码如下
void COthelloDlg::PlayBackMusic(BOOL bCheck){if(bCheck){//播放指定音乐文件sndPlaySound("music.wav",SND_ASYNC); }else{//停止播放sndPlaySound(NULL,SND_PURGE); }}
创作不易 觉得有帮助请点赞关注收藏~~~