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);
}

原文地址:https://www.cnblogs.com/yjw520/p/12258988.html

时间: 2024-10-10 04:26:04

MFC制作打地鼠小游戏的相关文章

jquery制作打地鼠小游戏

效果图: 素材: bg.jpg hole.pngmole.png <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scal

使用AxureRP7.0制作经典小游戏"大家来找茬"

本案例是<网站蓝图AxureRP7.0从入门到精通视频教程>中的最后一节,适用于对Axure基础知识掌握比较熟练的同学:教程由axure原型库网站录制,转载请注明出处!相信很多刚接触Axure或者学习了一段时间但并没有深入的同学们,看到这个案例一定很惊讶,使用Axure竟然能做出如此逼真的交互效果!通过此案例也可以激发广大同学们学习Axure的热情和信心!试想一下,如果你有情侣的话,把你们珍藏的合影拿出来处理几张,做成大家来找茬的小游戏,不但锻炼了自己的技能,还能给对方一个惊喜,岂不是一举两得

5Unity-3d Day05 打地鼠小游戏

用GUI写了一个打地鼠的小游戏   hiahia 摄像机的脚本里加代码: using UnityEngine; using System.Collections; public class MoleScript : MonoBehaviour { public GUISkin skin; public Texture2D bgImg; public int num; public Texture2D[] susliks = new Texture2D[12]; public Rect[] pos

【HTML】【实践】使用Canvas制作网页小游戏

HTML5中新增了Canvas元素,这个元素非常好玩,使用Canvas,我们可以使用代码绘制出我们想要的图形,用代码绘图,光是想想就兴奋啊. 于是我在学习了Canvas的部分内容后,动手制作了一款小游戏,这也是本人第一次独立开发web项目,所以内行人看到细节肯定忍不住吐槽,希望大家批评指正,给出宝贵意见,我们共同进步. 一.游戏介绍: 这个游戏的界面非常简单,左边一块用Canvas绘制的画布,右边有4个按钮,左边的画布上有一个红色的方块和一个黑色的方块,红色的方块是我们操作的对象,它是一个1×1

Qt 制作2048小游戏

相信大家都玩过2048把,下面是我用qt写得2048小游戏. 2048.pro HEADERS += Widget.h SOURCES += Widget.cpp main.cpp QT += widgets gui RESOURCES += ico.qrc RC_ICONS = 2048.ico widget.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QKeyEvent> #include &

QT-Creator C/C++ 打地鼠小游戏

废话少说先上图: 这个游戏纯属土鳖思路,没有用到什么游戏引擎. 1.使用按钮或QLabel铺满窗口. 2.通过简单算法随机动态的设置按钮矩阵中某个按钮的背景图像. 3.同步2过程反复设置多个按钮背景实现图像动起来,注意调节刷新时间. 4.重写按钮组鼠标事件,设置鼠标移入时光标的图案,可以是个小锤子. 5.单击事件后鼠标同理切换鼠标图案打下和抬起. 6.加入一点声音. 7.没有了. 相关代码: main.cpp #include "widget.h" #include <QAppl

屏幕坐标和世界坐标的转换+对象池技术(3D打地鼠小游戏)

游戏中可能经常会遇到需要某个物体跟着鼠标移动,然后又需要把物体放在某个鼠标指定的位置 实现方式 Camera.main.WorldToScreenPoint Camera.main.ScreenToWorldPoint 3D打地鼠实例 我这里用到的素材都比较简陋,几乎全是用Unity做的 首先是锤子 就是两个Cylinder,在把手的位置放一个空物体用于模拟锤子的动作,命名为Hammer,把锤子作为Hammer的子物体,给Hammer添加Animation动画: 在三个关键帧位置设置Hammer

一个基于Myeclipse开发的Java打地鼠小游戏(Appletcation)

1 package javaes.zixue.wangshang.daima; 2 3 import java.awt.Cursor; 4 import java.awt.Image; 5 import java.awt.Point; 6 import java.awt.Toolkit; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 9 import java.awt.event.Mous

用html+css+js做打地鼠小游戏

html 代码 first.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>打地鼠</title> <link rel="stylesheet" href="first.css"> </head> <body> <