MFC制作打地鼠小游戏

游戏说明如下:

打中老鼠加一分,一局时间为60秒,上面有个进度条和文字进行计时。

难度可以自己选择,难度低是老鼠每一秒出现一次,难度中是老鼠每0.6秒出现一次,难度高是老鼠每0.2秒出现一次。

效果图(背景、美化可以自己去改进):

代码如下:

1.

// y10Dlg.h : header file
//

#if !defined(AFX_Y10DLG_H__8D9895CE_53DF_4FA0_823C_1B5C468F422E__INCLUDED_)
#define AFX_Y10DLG_H__8D9895CE_53DF_4FA0_823C_1B5C468F422E__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CY10Dlg dialog

class CY10Dlg : public CDialog
{
// Construction
public:
HCURSOR m_hCursor;
CY10Dlg(CWnd* pParent = NULL); // standard constructor

CTime t;
CStatic* pMouse[9];
CBitmap* p[9];
CString strTime;
int nowMouse;
int gameScore;
int hour,minute,second;
// Dialog Data
//{{AFX_DATA(CY10Dlg)
enum { IDD = IDD_Y10_DIALOG };
CStatic m_Bitmap7;
CStatic m_Bitmap2;
CStatic m_Bitmap9;
CStatic m_Bitmap8;
CStatic m_Bitmap6;
CStatic m_Bitmap5;
CStatic m_Bitmap4;
CStatic m_Bitmap3;
CStatic m_Bitmap1;
CProgressCtrl m_Progress;
CString m_curTime;
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CY10Dlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);
//}}AFX_VIRTUAL

// Implementation
protected:
HICON m_hIcon;

// Generated message map functions
//{{AFX_MSG(CY10Dlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
afx_msg void OnStart();
afx_msg void OnContinue();
afx_msg void OnPause();
afx_msg void OnButton5();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_Y10DLG_H__8D9895CE_53DF_4FA0_823C_1B5C468F422E__INCLUDED_)

2.

// y10Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "y10.h"
#include "y10Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

//CDadishuDlg(CWnd* pParent = NULL); // standard constructor

// 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_MSG
DECLARE_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()

/////////////////////////////////////////////////////////////////////////////
// CY10Dlg dialog


CY10Dlg::CY10Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CY10Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CY10Dlg)
m_curTime = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
hour=0;
minute=0;
second=0;

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CY10Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CY10Dlg)
DDX_Control(pDX, IDC_m7, m_Bitmap7);
DDX_Control(pDX, IDC_m2, m_Bitmap2);
DDX_Control(pDX, IDC_m9, m_Bitmap9);
DDX_Control(pDX, IDC_m8, m_Bitmap8);
DDX_Control(pDX, IDC_m6, m_Bitmap6);
DDX_Control(pDX, IDC_m5, m_Bitmap5);
DDX_Control(pDX, IDC_m4, m_Bitmap4);
DDX_Control(pDX, IDC_m3, m_Bitmap3);
DDX_Control(pDX, IDC_m1, m_Bitmap1);
DDX_Control(pDX, IDC_PROGRESS1, m_Progress);
DDX_Text(pDX, IDC_STATIC_curTIME, m_curTime);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CY10Dlg, CDialog)
//{{AFX_MSG_MAP(CY10Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_SETCURSOR()
ON_BN_CLICKED(IDC_START, OnStart)
ON_BN_CLICKED(IDC_CONTINUE, OnContinue)
ON_BN_CLICKED(IDC_PAUSE, OnPause)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_WM_TIMER()
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CY10Dlg message handlers

BOOL CY10Dlg::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 dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here
/*CString strTime1;
CTime tm;
tm=CTime::GetCurrentTime(); //获取当前系统时间
strTime1=tm.Format("%y-%m-%d %X"); //格式化系统时间。即使系统时 间按照Format中设置的格式显示
SetDlgItemText(IDC_STATIC_curTIME,strTime); //初始化编辑框显示
SetTimer(3,1000,NULL); //启动定时器*/

gameScore=0;//分数
nowMouse=0;//当前老鼠
GetDlgItem(IDC_PAUSE)->EnableWindow(FALSE);
GetDlgItem(IDC_CONTINUE)->EnableWindow(FALSE);
CheckRadioButton(IDC_RADIO1,IDC_RADIO3,IDC_RADIO3);
//CDialogEx::SetBackgroundImage(IDB_BITMAP1);
return TRUE; // return TRUE unless you set the focus to a control
}

void CY10Dlg::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 CY10Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int 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 icon
dc.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 CY10Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

BOOL CY10Dlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
// TODO: Add your message handler code here and/or call default
SetCursor(LoadCursor(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDC_CURSOR1)));
return TRUE;
//m_hCursor=AfxGetApp()->LoadCursor(IDC_CURSOR1);
// SetCursor(m_hCursor);
return CDialog::OnSetCursor(pWnd, nHitTest, message);
}
void CY10Dlg::OnStart()
{
// TODO: Add your control notification handler code here

GetDlgItem(IDC_PAUSE)->EnableWindow(TRUE);
GetDlgItem(IDC_CONTINUE)->EnableWindow(FALSE);
m_Progress.SetRange(0,60);
int n;
n=GetCheckedRadioButton(IDC_RADIO1,IDC_RADIO3);
if(n==IDC_RADIO3)
SetTimer(1,1000,NULL);
else if(n==IDC_RADIO2)
SetTimer(1,600,NULL);
else if(n==IDC_RADIO1)
SetTimer(1,200,NULL);
SetTimer(2,1000,NULL);
}

void CY10Dlg::OnContinue()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_PAUSE)->EnableWindow(TRUE);
GetDlgItem(IDC_CONTINUE)->EnableWindow(FALSE);
GetDlgItem(IDC_START)->EnableWindow(FALSE);
int n;
n=GetCheckedRadioButton(IDC_RADIO1,IDC_RADIO3);
if(n==IDC_RADIO3)
SetTimer(1,1000,NULL);
else if(n==IDC_RADIO2)
SetTimer(1,600,NULL);
else if(n==IDC_RADIO1)
SetTimer(1,200,NULL);
SetTimer(2,1000,NULL);
}

void CY10Dlg::OnPause()
{
// TODO: Add your control notification handler code here
KillTimer(1);
KillTimer(2);
GetDlgItem(IDC_PAUSE)->EnableWindow(FALSE);
GetDlgItem(IDC_START)->EnableWindow(FALSE);
GetDlgItem(IDC_CONTINUE)->EnableWindow(TRUE);
}

void CY10Dlg::OnButton5()
{
// TODO: Add your control notification handler code here
CDialog::OnCancel();
}

void CY10Dlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
switch(nIDEvent)
{
case 1:
//pMouse[nowMouse]->SetIcon(0);//清除当前按钮上的老鼠
//nowMouse=rand()%12;//随机产生下一个老鼠
//pMouse[nowMouse]->SetIcon(AfxGetApp()->LoadIcon(IDI_ICON1));
int n;
n=GetCheckedRadioButton(IDC_RADIO1,IDC_RADIO3);

HBITMAP hBitmap;
hBitmap = ::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP3));
m_Bitmap1.SetBitmap(hBitmap);
m_Bitmap2.SetBitmap(hBitmap);
m_Bitmap3.SetBitmap(hBitmap);
m_Bitmap4.SetBitmap(hBitmap);
m_Bitmap5.SetBitmap(hBitmap);
m_Bitmap6.SetBitmap(hBitmap);
m_Bitmap7.SetBitmap(hBitmap);
m_Bitmap8.SetBitmap(hBitmap);
m_Bitmap9.SetBitmap(hBitmap);
hBitmap = ::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP1));
nowMouse=rand()%9+1;
if(nowMouse==1)
{
m_Bitmap1.SetBitmap(hBitmap);
}
if(nowMouse==2)
{
m_Bitmap2.SetBitmap(hBitmap);
}
if(nowMouse==3)
{
m_Bitmap3.SetBitmap(hBitmap);
}
if(nowMouse==4)
{
m_Bitmap4.SetBitmap(hBitmap);
}
if(nowMouse==5)
{
m_Bitmap5.SetBitmap(hBitmap);
}
if(nowMouse==6)
{
m_Bitmap6.SetBitmap(hBitmap);
}
if(nowMouse==7)
{
m_Bitmap7.SetBitmap(hBitmap);
}
if(nowMouse==8)
{
m_Bitmap8.SetBitmap(hBitmap);
}
if(nowMouse==9)
{
m_Bitmap9.SetBitmap(hBitmap);
}
break;
case 2:
second++;
if(second==60)//设置钟表的显示
{
minute++;second=0;
}
if(minute==60)
{
hour++;minute=0;
}
CTime tm;
tm=CTime::GetCurrentTime();
m_curTime=tm.Format("%Y-%m-%d %H:%M:%S");
UpdateData(FALSE); //显示系统时间*/

m_Progress.SetPos(second);
strTime.Format("%02d:%02d:%02d",hour,minute,second);//显示时间进度
CWnd *pWnd=GetDlgItem(IDC_STATIC_TIME);
pWnd->SetWindowText(strTime); //在设置时间新值,表明定时器已经工作。
//CWnd *pW=GetDlgItem(IDC_STATIC_curTIME);
break;
}

CDialog::OnTimer(nIDEvent);
}

BOOL CY10Dlg::OnCommand(WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class

CString str;
/*for (int i=0;i<9;i++)
{
if (LOWORD(wParam)==pMouse[i]->GetDlgCtrlID())//当前id与次元id相同,则选中
{
break;
}
}
*/
//i是当前选中老鼠编号
if (nowMouse==1)
{
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 CY10Dlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default

//CPoint point;//定义一个用于确定光标位置的位置
GetCursorPos(&point);//获取当前光标的位置,以便使得菜单可以跟随光标
ScreenToClient(&point);
//得到窗体的控件句柄
CWnd* hControlWnd = (CWnd*)ChildWindowFromPoint(point);
UINT nID = hControlWnd->GetDlgCtrlID();

HBITMAP hBitmap;
hBitmap = ::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP3));
m_Bitmap1.SetBitmap(hBitmap);
m_Bitmap2.SetBitmap(hBitmap);
m_Bitmap3.SetBitmap(hBitmap);
m_Bitmap4.SetBitmap(hBitmap);
m_Bitmap5.SetBitmap(hBitmap);
m_Bitmap6.SetBitmap(hBitmap);
m_Bitmap7.SetBitmap(hBitmap);
m_Bitmap8.SetBitmap(hBitmap);
m_Bitmap9.SetBitmap(hBitmap);
hBitmap = ::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP2));
if(nowMouse==1 && nID==IDC_m1)
{
m_Bitmap1.SetBitmap(hBitmap);
CString str;
gameScore++;
str.Format("%d",gameScore);
CStatic *pStatic=(CStatic*)GetDlgItem(IDC_STATIC_SCORE);
pStatic->SetWindowText(str);
}
if(nowMouse==2 && nID==IDC_m2)
{
m_Bitmap2.SetBitmap(hBitmap);
CString str;
gameScore++;
str.Format("%d",gameScore);
CStatic *pStatic=(CStatic*)GetDlgItem(IDC_STATIC_SCORE);
pStatic->SetWindowText(str);
}
if(nowMouse==3 && nID==IDC_m3)
{
m_Bitmap3.SetBitmap(hBitmap);
CString str;
gameScore++;
str.Format("%d",gameScore);
CStatic *pStatic=(CStatic*)GetDlgItem(IDC_STATIC_SCORE);
pStatic->SetWindowText(str);
}
if(nowMouse==4 && nID==IDC_m4)
{
m_Bitmap4.SetBitmap(hBitmap);
CString str;
gameScore++;
str.Format("%d",gameScore);
CStatic *pStatic=(CStatic*)GetDlgItem(IDC_STATIC_SCORE);
pStatic->SetWindowText(str);
}
if(nowMouse==5 && nID==IDC_m5)
{
m_Bitmap5.SetBitmap(hBitmap);
CString str;
gameScore++;
str.Format("%d",gameScore);
CStatic *pStatic=(CStatic*)GetDlgItem(IDC_STATIC_SCORE);
pStatic->SetWindowText(str);
}
if(nowMouse==6 && nID==IDC_m6)
{
m_Bitmap6.SetBitmap(hBitmap);
CString str;
gameScore++;
str.Format("%d",gameScore);
CStatic *pStatic=(CStatic*)GetDlgItem(IDC_STATIC_SCORE);
pStatic->SetWindowText(str);
}
if(nowMouse==7 && nID==IDC_m7)
{
m_Bitmap7.SetBitmap(hBitmap);
CString str;
gameScore++;
str.Format("%d",gameScore);
CStatic *pStatic=(CStatic*)GetDlgItem(IDC_STATIC_SCORE);
pStatic->SetWindowText(str);
}
if(nowMouse==8 && nID==IDC_m8)
{
m_Bitmap8.SetBitmap(hBitmap);
CString str;
gameScore++;
str.Format("%d",gameScore);
CStatic *pStatic=(CStatic*)GetDlgItem(IDC_STATIC_SCORE);
pStatic->SetWindowText(str);
}
if(nowMouse==9 && nID==IDC_m9)
{
m_Bitmap9.SetBitmap(hBitmap);
CString str;
gameScore++;
str.Format("%d",gameScore);
CStatic *pStatic=(CStatic*)GetDlgItem(IDC_STATIC_SCORE);
pStatic->SetWindowText(str);
}

CDialog::OnLButtonDown(nFlags, point);
}

posted @ 2020-02-04 12:34  橱窗  阅读(655)  评论(0编辑  收藏  举报