课程设计MFC打字游戏.docx
- 文档编号:5201461
- 上传时间:2022-12-13
- 格式:DOCX
- 页数:17
- 大小:45.86KB
课程设计MFC打字游戏.docx
《课程设计MFC打字游戏.docx》由会员分享,可在线阅读,更多相关《课程设计MFC打字游戏.docx(17页珍藏版)》请在冰豆网上搜索。
课程设计MFC打字游戏
附件:
集中上机实习报告封面模板
集中上机实习报告
指导教师:
甘玲
姓名:
陈琪
学号:
2010211794
班级:
0491002
时间:
一、需求分析
1.选题
接到课程设计任务后,我们对曾经非常感兴趣的几款经典小游戏作了分析并筛选,然后根据老是给的推荐题目以及自己的编程能力做了选择,决定了编写在dos化境下的打字游戏,锻炼在自己对函数,指针,类的综合运用,以及对颜色控制函数的使用。
2.功能
在windows窗口环境下实现打字游戏的运行,正确与错误字符的统计,以及时间的统计。
游戏开始后,屏幕上方同时有四个数字分四列下落,每个数字由三位组成(随机生成,但首位不可相同)。
玩家用键盘依次输入数字,如果输入的数字序列与某一列正在下落的数字三位全部相同,则给玩家加10分,该列数字消失,该列新产生数字并下落;如果输入的数字某一位不相同,则输入的数字序列全部作废,重新接收输入。
某一列数字到达屏幕底部则游戏结束
二、总体设计说明
1.设计思路
用结构体存储每组数字,然后利用MFC里面的Timer作为时间流程,并重写对话框类里面的OnPaint()函数实现图形的绘制
2.程序框图
操作控制:
如图1所示
结
束
判断正误
输入
开
始
游
戏
打字游戏
三、代码实现
本程序MFC主要代码
//打字游戏Dlg.cpp:
implementationfile
//
//打字游戏Dlg.h:
headerfile
//
#if!
defined(AFX_DLG_H__C9D22D72_CFA5_4621_8784_36A4BE47F2E5__INCLUDED_)
#defineAFX_DLG_H__C9D22D72_CFA5_4621_8784_36A4BE47F2E5__INCLUDED_
#if_MSC_VER>1000
#pragmaonce
#endif//_MSC_VER>1000
/////////////////////////////////////////////////////////////////////////////
//CMyDlgdialog
structNumber{
intn[3];
intx;
inty;
intcolor[3];//判断是否被键入
};
classCMyDlg:
publicCDialog
{
//Construction
public:
CMyDlg(CWnd*pParent=NULL);//standardconstructor
//DialogData
//{{AFX_DATA(CMyDlg)
enum{IDD=IDD_MY_DIALOG};
//NOTE:
theClassWizardwilladddatamembershere
//}}AFX_DATA
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CMyDlg)
public:
virtualBOOLPreTranslateMessage(MSG*pMsg);
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport
//}}AFX_VIRTUAL
//Implementation
protected:
HICONm_hIcon;
structNumbernumber[4];//数字
intflag,now;//flag为选中列,now为位数
//Generatedmessagemapfunctions
voidSetInit();//初始化随机数
intisSame();//判断是否相同
intisWin(intflag);//判断成功
voidsetNew(intflag);//新增随机数
intisLost();//判断是否失败
intcount;//记分器
//{{AFX_MSG(CMyDlg)
virtualBOOLOnInitDialog();
afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam);
afx_msgvoidOnPaint();
afx_msgHCURSOROnQueryDragIcon();
afx_msgvoidOnTimer(UINTnIDEvent);
afx_msgvoidOnstart();
afx_msgvoidOnabout();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.
#endif//!
defined(AFX_DLG_H__C9D22D72_CFA5_4621_8784_36A4BE47F2E5__INCLUDED_)
#include"stdafx.h"
#include"打字游戏.h"
#include"打字游戏Dlg.h"
#include"time.h"
#include"string.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CAboutDlgdialogusedforAppAbout
classCAboutDlg:
publicCDialog
{
public:
CAboutDlg();
//DialogData
//{{AFX_DATA(CAboutDlg)
enum{IDD=IDD_ABOUTBOX};
//}}AFX_DATA
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport
//}}AFX_VIRTUAL
//Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg:
:
CAboutDlg():
CDialog(CAboutDlg:
:
IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
voidCAboutDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//Nomessagehandlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CMyDlgdialog
CMyDlg:
:
CMyDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CMyDlg:
:
IDD,pParent)
{
//{{AFX_DATA_INIT(CMyDlg)
//NOTE:
theClassWizardwilladdmemberinitializationhere
//}}AFX_DATA_INIT
//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
voidCMyDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDlg)
//NOTE:
theClassWizardwilladdDDXandDDVcallshere
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDlg,CDialog)
//{{AFX_MSG_MAP(CMyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_COMMAND(ID_start,Onstart)
ON_COMMAND(ID_about,Onabout)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CMyDlgmessagehandlers
BOOLCMyDlg:
:
OnInitDialog()
{
CDialog:
:
OnInitDialog();
//Add"About..."menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);//Setbigicon
SetIcon(m_hIcon,FALSE);//Setsmallicon
//TODO:
Addextrainitializationhere
SetInit();//初始化随机数
SetTimer(1,500,NULL);//初始化时间
returnTRUE;//returnTRUEunlessyousetthefocustoacontrol
}
voidCMyDlg:
:
OnSysCommand(UINTnID,LPARAMlParam)
{
if((nID&0xFFF0)==IDM_ABOUTBOX)
{
CAboutDlgdlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog:
:
OnSysCommand(nID,lParam);
}
}
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
voidCMyDlg:
:
OnPaint()
{
if(IsIconic())
{
CPaintDCdc(this);//devicecontextforpainting
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
//Centericoninclientrectangle
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
//Drawtheicon
dc.DrawIcon(x,y,m_hIcon);
}
else
{
CPaintDCdc(this);
dc.SetBkMode(TRANSPARENT);
dc.TextOut(360,320,"你的分数:
");
dc.TextOut(440,320,itoa(count,newchar,10));
for(inti=0;i<4;i++)
{
for(intj=0;j<3;j++)
{
if(number[i].color[j]==1)
dc.SetTextColor(RGB(255,0,0));
if(number[i].color[j]==0)
dc.SetTextColor(RGB(0,0,0));
if(number[i].color[j]==-1)
continue;
dc.TextOut(number[i].x+j*7,number[i].y,itoa(number[i].n[j],newCHAR,10),1);
}
}
CDialog:
:
OnPaint();
}
}
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
HCURSORCMyDlg:
:
OnQueryDragIcon()
{
return(HCURSOR)m_hIcon;
}
//初始化随机四列数字
voidCMyDlg:
:
SetInit()
{
inti,j,r;
intnum[10]={0,1,2,3,4,5,6,7,8,9};
count=0;//初始化分数
flag=-1,now=0;//flag为选中列,now为位数
srand(time(NULL));
for(i=0;i<3;i++)//每组的第i个
{
do{
for(j=0;j<4;j++)
{
r=rand()%9;
this->number[j].n[i]=num[r];
}
}while(i==0&&isSame()==1);
}
for(i=0;i<4;i++)
{
number[i].y=0;
number[i].x=50+100*i;
for(j=0;j<3;j++)
number[i].color[j]=0;
}
}
//判断首位是否相同
intCMyDlg:
:
isSame()
{
intflag=0;
for(inti=0;i<3;i++)
for(intj=i+1;j<4;j++)
{
if(number[i].n[0]==number[j].n[0])
{
return1;
}
}
returnflag;
}
//获取消息,键盘按键事件
BOOLCMyDlg:
:
PreTranslateMessage(MSG*pMsg)
{
//TODO:
Addyourspecializedcodehereand/orcallthebaseclass
inti,j;
if(pMsg->message==WM_KEYDOWN)
{
//当初始为第一个数字的时候
if(flag==-1)
{
for(i=0;i<4;i++)
{
if(number[i].n[0]==pMsg->wParam-48)
{//击中第一个数字
number[i].color[0]=1;
now++;//now记录当前位数
flag=i;//记录当前列
}
}
}else
{
if(number[flag].n[now]==pMsg->wParam-48)//连续击中数字
{
number[flag].color[now]=1;
now++;
if(isWin(flag)==1)
{//如果赢了
count+=10;
for(i=0;i<3;i++)
number[flag].color[i]=0;
setNew(flag);//重新创建
number[flag].y=0;
Invalidate(true);
flag=-1;
now=0;
}
}else//输错了就重新开始
{//重新清0
for(i=0;i<3;i++)
number[flag].color[i]=0;
flag=-1;
now=0;
}
}
Invalidate(true);
}
returnCDialog:
:
PreTranslateMessage(pMsg);
}
//判断某个数字是否可以消掉
intCMyDlg:
:
isWin(intflag)
{
for(inti=0;i<3;i++)
{
if(number[flag].color[i]==0)
return0;
}
return1;
}
voidCMyDlg:
:
OnTimer(UINTnIDEvent)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
CStringcs="失败了,你的得分是:
";
cs+=itoa(count,newchar,10);
if(nIDEvent==1)
{
for(inti=0;i<4;i++)
number[i].y+=10;
if(isLost()==1)
{
KillTimer
(1);
MessageBox(cs);
}
Invalidate(true);
}
CDialog:
:
OnTimer(nIDEvent);
}
//新增随机数
voidCMyDlg:
:
setNew(intflag)
{
intr,i;
intnum[10]={0,1,2,3,4,5,6,7,8,9};
srand(time(NULL));
do{
for(i=0;i<3;i++)
{
r=rand()%9;
this->number[flag].n[i]=num[r];
}
}while(isSame()==1);
}
intCMyDlg:
:
isLost()
{
//判断是否失败
for(inti=0;i<4;i++)
{
if(number[i].y>280)
return1;
}
return0;
}
voidCMyDlg:
:
Onstart()
{
//TODO:
Addyourcommandhandlercodehere
SetInit();//开始游戏
SetTimer(1,500,NULL);
Invalidate(true);
}
voidCMyDlg:
:
Onabout()
{
//TODO:
Addyourcommandhandlercodehere
MessageBox("作者:
计算机,陈琪");
}
四、问题解决
本次遇到了主要两个问题;
1.就是设计存储结构的时候,最开始用的数组,但是后来经过测试的时候发现不容易控制,然后经过仔细思考决定用结构体数组,这样简便,使得存储更加方便
2.第二个就是掉下来的数字可能有相同的,就会出现打字错误,仔细检查后终于发现了这个问题,采用在之前先检查随机数是否重复
五、总结展望
参考资料和程序成品,我了解了游戏的最基本形式。
ontimer()函数是控制游戏运行的根本函数,借助这一函数,能实现贪吃蛇、俄罗斯方块、赛车等多个游戏的开发。
随机数生成函数rand()的应用在贪吃蛇游戏中是一个关键步骤,通过不断的循环和调用,能保证游戏的持续进行和更新,其实不止是贪吃蛇,在大多数需要随机生成元素的游戏中,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 课程设计 MFC 打字 游戏
![提示](https://static.bdocx.com/images/bang_tan.gif)