MFC制作打地鼠小游戏

一、游戏说明如下:

打中老鼠加一分,当得分值小于10分时,老鼠每0.8秒出现一次,当得分值大于等于10分并且小于50分时,老鼠每0.6秒出现一次。当得分值大于等于50分时,老鼠每0.4秒出现一次。这个游戏是参考别人的部分代码,并在其基础上改进了一些。具体代码如下:

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 dialog

class 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 support
	virtual 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_MSG
	DECLARE_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_)


2、


// 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 About

class 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_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()

/
// CDadishuDlg dialog

CDadishuDlg::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 Win32
	hour=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 handlers

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

	gameScore=0;//分数
	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 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 CDadishuDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDadishuDlg::OnContinue() 
{
	// TODO: Add your control notification handler code here
	 GetDlgItem(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 here
	  KillTimer(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 default

	switch(nIDEvent)
	{
		case 1:
			pMouse[nowMouse]->SetIcon(0);//清除当前按钮上的老鼠
			nowMouse=rand()%12;//随机产生下一个老鼠
			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); //在设置时间新值,表明定时器已经工作。
			break;
				

	}
	
	
	CDialog::OnTimer(nIDEvent);
}

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

	 CString 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 here
		GetDlgItem(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 here
	CDialog::OnCancel();
	
}


3、运行效果如下:







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值