MFC制作打地鼠小游戏
生活随笔
收集整理的這篇文章主要介紹了
MFC制作打地鼠小游戏
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、游戲說明如下:
打中老鼠加一分,當?shù)梅种敌∮?0分時,老鼠每0.8秒出現(xiàn)一次,當?shù)梅种荡笥诘扔?0分并且小于50分時,老鼠每0.6秒出現(xiàn)一次。當?shù)梅种荡笥诘扔?0分時,老鼠每0.4秒出現(xiàn)一次。這個游戲是參考別人的部分代碼,并在其基礎上改進了一些。具體代碼如下:
1、
// dadishuDlg.h : header file //#if !defined(AFX_DADISHUDLG_H__40135455_30B1_45C8_B123_A78832063FD4__INCLUDED_) #define AFX_DADISHUDLG_H__40135455_30B1_45C8_B123_A78832063FD4__INCLUDED_#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000/ // CDadishuDlg dialogclass CDadishuDlg : public CDialog { // Construction public:CTime t;CButton * pMouse[12];CString strTime;int nowMouse;int gameScore;int hour,minute,second;CDadishuDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data//{{AFX_DATA(CDadishuDlg)enum { IDD = IDD_DADISHU_DIALOG };// NOTE: the ClassWizard will add data members here//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CDadishuDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV supportvirtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);//}}AFX_VIRTUAL// Implementation protected:HICON m_hIcon;// Generated message map functions//{{AFX_MSG(CDadishuDlg)virtual BOOL OnInitDialog();afx_msg void OnSysCommand(UINT nID, LPARAM lParam);afx_msg void OnPaint();afx_msg HCURSOR OnQueryDragIcon();afx_msg void OnContinue();afx_msg void OnPause();afx_msg void OnTimer(UINT nIDEvent);afx_msg void OnStart();afx_msg void OnCancel();//}}AFX_MSGDECLARE_MESSAGE_MAP() };//{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_DADISHUDLG_H__40135455_30B1_45C8_B123_A78832063FD4__INCLUDED_)
// dadishuDlg.cpp : implementation file //#include "stdafx.h" #include "dadishu.h" #include "dadishuDlg.h"#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif/ // CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialog { public:CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg)enum { IDD = IDD_ABOUTBOX };//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support//}}AFX_VIRTUAL// Implementation protected://{{AFX_MSG(CAboutDlg)//}}AFX_MSGDECLARE_MESSAGE_MAP() };CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) {//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT }void CAboutDlg::DoDataExchange(CDataExchange* pDX) {CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAP }BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)//{{AFX_MSG_MAP(CAboutDlg)// No message handlers//}}AFX_MSG_MAP END_MESSAGE_MAP()/ // CDadishuDlg dialogCDadishuDlg::CDadishuDlg(CWnd* pParent /*=NULL*/): CDialog(CDadishuDlg::IDD, pParent) {//{{AFX_DATA_INIT(CDadishuDlg)// NOTE: the ClassWizard will add member initialization here//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32hour=0;minute=0;second=0;m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); }void CDadishuDlg::DoDataExchange(CDataExchange* pDX) {CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CDadishuDlg)// NOTE: the ClassWizard will add DDX and DDV calls here//}}AFX_DATA_MAP }BEGIN_MESSAGE_MAP(CDadishuDlg, CDialog)//{{AFX_MSG_MAP(CDadishuDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_CONTINUE, OnContinue)ON_BN_CLICKED(IDC_PAUSE, OnPause)ON_WM_TIMER()ON_BN_CLICKED(IDC_START, OnStart)ON_BN_CLICKED(IDC_CANCEL, OnCancel)//}}AFX_MSG_MAP END_MESSAGE_MAP()/ // CDadishuDlg message handlersBOOL CDadishuDlg::OnInitDialog() {CDialog::OnInitDialog();// Add "About..." menu item to system menu.// IDM_ABOUTBOX must be in the system command range.ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// Set the icon for this dialog. The framework does this automatically// when the application's main window is not a dialogSetIcon(m_hIcon, TRUE); // Set big iconSetIcon(m_hIcon, FALSE); // Set small icon// TODO: Add extra initialization heregameScore=0;//分數(shù)nowMouse=0;//當前老鼠GetDlgItem(IDC_PAUSE)->EnableWindow(FALSE);GetDlgItem(IDC_CONTINUE)->EnableWindow(FALSE);for (int i=0;i<12;i++){//獲取12個按鈕的指針pMouse[i]=(CButton *)GetDlgItem(IDC_BUTTON1+i);}return TRUE; // return TRUE unless you set the focus to a control }void CDadishuDlg::OnSysCommand(UINT nID, LPARAM lParam) {if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialog::OnSysCommand(nID, lParam);} }// If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework.void CDadishuDlg::OnPaint() {if (IsIconic()){CPaintDC dc(this); // device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);// Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height() - cyIcon + 1) / 2;// Draw the icondc.DrawIcon(x, y, m_hIcon);}else{CDialog::OnPaint();} }// The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CDadishuDlg::OnQueryDragIcon() {return (HCURSOR) m_hIcon; }void CDadishuDlg::OnContinue() {// TODO: Add your control notification handler code hereGetDlgItem(IDC_PAUSE)->EnableWindow(TRUE);GetDlgItem(IDC_CONTINUE)->EnableWindow(FALSE);GetDlgItem(IDC_START)->EnableWindow(FALSE);SetTimer(1,800,NULL);SetTimer(2,1000,NULL);}void CDadishuDlg::OnPause() {// TODO: Add your control notification handler code hereKillTimer(1);KillTimer(2);GetDlgItem(IDC_PAUSE)->EnableWindow(FALSE);GetDlgItem(IDC_START)->EnableWindow(FALSE);GetDlgItem(IDC_CONTINUE)->EnableWindow(TRUE);}void CDadishuDlg::OnTimer(UINT nIDEvent) {// TODO: Add your message handler code here and/or call defaultswitch(nIDEvent){case 1:pMouse[nowMouse]->SetIcon(0);//清除當前按鈕上的老鼠nowMouse=rand()%12;//隨機產(chǎn)生下一個老鼠pMouse[nowMouse]->SetIcon(AfxGetApp()->LoadIcon(IDI_ICON1));break;case 2:second++;if(second==60)//設置鐘表的顯示{minute++;second=0;}if(minute==60){hour++;minute=0;}strTime.Format("%02d:%02d:%02d",hour,minute,second);//顯示時間進度CWnd *pWnd=GetDlgItem(IDC_STATIC_time);pWnd->SetWindowText(strTime); //在設置時間新值,表明定時器已經(jīng)工作。break;}CDialog::OnTimer(nIDEvent); }BOOL CDadishuDlg::OnCommand(WPARAM wParam, LPARAM lParam) {// TODO: Add your specialized code here and/or call the base classCString str;for (int i=0;i<12;i++){if (LOWORD(wParam)==pMouse[i]->GetDlgCtrlID())//當前id與次元id相同,則選中{break;}}//i是當前選中老鼠編號if (nowMouse==i){gameScore++;str.Format("%d",gameScore);CStatic *pStatic=(CStatic *)GetDlgItem(IDC_STATIC_score);pStatic->SetWindowText(str);if (gameScore>=10){SetTimer(1,600,NULL);if (gameScore>=50){SetTimer(1,400,NULL);}}}return CDialog::OnCommand(wParam, lParam); }void CDadishuDlg::OnStart() {// TODO: Add your control notification handler code hereGetDlgItem(IDC_PAUSE)->EnableWindow(TRUE);GetDlgItem(IDC_CONTINUE)->EnableWindow(FALSE);SetTimer(1,800,NULL);SetTimer(2,1000,NULL);}void CDadishuDlg::OnCancel() {// TODO: Add your control notification handler code hereCDialog::OnCancel();}
總結
以上是生活随笔為你收集整理的MFC制作打地鼠小游戏的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MFC对话框绘制灰度直方图
- 下一篇: 语义分割中的类别不平衡的权重计算