1、课程设计实验报告 班级:光电104—2 姓名:刘云龙 学号:201058501220 一、实验题目:使用C语言编写一个小游戏(拼图游戏) 二、实验目的:C语言是每一个通信学生的必修课之一,此次课程设计要求我们使用C语言编写一个小游戏,在完成课程设计的过程中,加强了我们对C语言的综合应用。虽然在平时的课堂上积累了很多的理论知识,但在实际应用时还是有很多不明白的地方。为了更好地完成实验要求,通过翻课本,请教同学,在网上查找资料等途径解决了试验中遇到的疑难。在完成实验的过程中,暴露了自己在C语言学习上的很多不足和缺陷,同时也解决了很多平时不明白的地方。 三、实验
2、内容 本次试验选定的实验项目是使用C语言编写一个拼图游戏 以下是拼图游戏的源代码: // 九宫格拼图Dlg.cpp : implementation file // #include "stdafx.h" #include "九宫格拼图.h" #include "九宫格拼图Dlg.h" #include "spell.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif CSpell spell(3, 3
3、 2, 2); ////////////////////////////////////////////// // void ShowTranBmp(CDC * dc, CBitmap * bmp, COLORREF bgcolor, int x, int y, int w, int h, int bx, int by, int bw, int bh){ CDC mdc; CDC tdc; // 图像 CDC ddc; // 遮罩 CBitmap tbmp; CBitmap dbmp; CBitmap *ob; CBitmap *tob; CBi
4、tmap *dob; mdc.CreateCompatibleDC(dc); tdc.CreateCompatibleDC(dc); ddc.CreateCompatibleDC(dc); tbmp.CreateBitmap(bw, bh, 1, 32, NULL); dbmp.CreateBitmap(bw, bh, 1, 1, NULL); ob = mdc.SelectObject(bmp); tob = tdc.SelectObject(&tbmp); dob = ddc.SelectObject(&dbmp); tdc.BitBlt
5、bx, -by, bw+bx, bh+by, &mdc, 0, 0, SRCCOPY); tdc.SetBkColor(bgcolor); ddc.BitBlt(0, 0, bw, bh, &tdc, 0, 0, SRCCOPY); /////////////////////////////////// //显示 dc->BitBlt(x, y, w, h, &tdc, 0, 0, SRCINVERT); dc->BitBlt(x, y, w, h, &ddc, 0, 0, SRCAND); dc->BitBlt(x, y, w, h, &tdc, 0,
6、 0, SRCINVERT); mdc.SelectObject(ob); tdc.SelectObject(tob); ddc.SelectObject(dob); } void ShowTranBmp(CDC * dc, CBitmap * bmp, COLORREF bgcolor, int x, int y, int w, int h){ CDC tdc; // 图像 CDC ddc; // 遮罩 CBitmap dbmp; CBitmap *tob; CBitmap *dob; tdc.CreateCompatibleDC(dc
7、); ddc.CreateCompatibleDC(dc); dbmp.CreateBitmap(w, h, 1, 1, NULL); tob = tdc.SelectObject(bmp); dob = ddc.SelectObject(&dbmp); tdc.SetBkColor(bgcolor); ddc.BitBlt(0, 0, w, h, &tdc, 0, 0, SRCCOPY); /////////////////////////////////// //显示 dc->BitBlt(x, y, w, h, &tdc, 0, 0,
8、SRCINVERT); dc->BitBlt(x, y, w, h, &ddc, 0, 0, SRCAND); dc->BitBlt(x, y, w, h, &tdc, 0, 0, SRCINVERT); tdc.SelectObject(tob); ddc.SelectObject(dob); } ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg
9、 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 sup
10、port //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) virtual BOOL OnInitDialog(); afx_msg void OnPaint(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAb
11、outDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) ON_WM_PAINT() //}}AFX_MSG_MAP END_MESSAGE_MAP() BOOL CAboutDlg::OnInitDialog() { CDialog
12、OnInitDialog(); // TODO: Add extra initialization here SetDlgItemText(IDC_QQ, "252351107"); SetDlgItemText(IDC_EMAIL, "_tg15@"); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CAboutDl
13、g::OnPaint() { CPaintDC dc(this); // device context for painting CBitmap tg15; tg15.LoadBitmap(IDB_TG15); ShowTranBmp(&dc, &tg15, RGB(255,255,255), 8, 8, 93, 96); // Do not call CDialog::OnPaint() for painting messages } /////////////////////////////////////////////////////////////
14、//////////////// // CMyDlg dialog CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/) : CDialog(CMyDlg::IDD, pParent) { //{{AFX_DATA_INIT(CMyDlg) //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); begin = TR
15、UE; step = 0; pic.Load(IDR_PIC_VIEW); size = 3; border = TRUE; // m_pp5.SetText(_T("")); } void CMyDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CMyDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CMyDlg, CDialog) //{{AFX_MSG_MAP(CMyDlg)
16、 ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_COMMAND(ID_MENU_EXIT, OnMenuExit) ON_COMMAND(ID_MENU_ABOUT, OnMenuAbout) ON_WM_LBUTTONDOWN() ON_COMMAND(ID_MENU_BEGIN, OnMenuBegin) ON_COMMAND(ID_MENU_NUMBER, OnMenuNumber) ON_COMMAND(ID_MENU_VIEW, OnMenuView) ON_COMMAND(I
17、D_MENU_PERSON, OnMenuPerson) ON_COMMAND(ID_MENU_3, OnMenu3) ON_COMMAND(ID_MENU_4, OnMenu4) ON_COMMAND(ID_MENU_5, OnMenu5) ON_COMMAND(ID_MENU_6, OnMenu6) ON_COMMAND(ID_MENU_BORDER, OnMenuBorder) ON_COMMAND(ID_MENU_OPEN, OnMenuOpen) ON_WM_LBUTTONDBLCLK() //}}AFX_MSG_MAP END_MESSAGE_MA
18、P() ///////////////////////////////////////////////////////////////////////////// // CMyDlg message handlers BOOL CMyDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBO
19、X & 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); //
20、 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
21、 // TODO: Add extra initialization here CMenu* pMenu = this->GetMenu(); pMenu->CheckMenuItem(ID_MENU_3, MF_CHECKED); pMenu->CheckMenuItem(ID_MENU_BORDER, MF_CHECKED); pp5.m_link = _T(""); pp5.SubclassDlgItem(IDC_PP5, this); return TRUE; // return TRUE unless you set the focus to a
22、 control } void CMyDlg::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 /
23、/ to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CMyDlg::OnPaint() { CPaintDC dc(this); // device context for painting if (IsIconic()) { SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0)
24、 // 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,
25、 y, m_hIcon); } else { CDialog::OnPaint(); } Show(&dc); } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CMyDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CMyDlg::Show(CDC * dc){ CDC tdc; CBit
26、map bmp, *obmp; bmp.LoadBitmap(IDB_BG); tdc.CreateCompatibleDC(dc); obmp = tdc.SelectObject(&bmp); CDC bmpdc, picdc; bmpdc.CreateCompatibleDC(dc); picdc.CreateCompatibleDC(dc); CBitmap bbmp, * obbmp; CBitmap pbmp, * opbmp; bbmp.CreateCompatibleBitmap(dc, 360, 360); pbmp.Cre
27、ateCompatibleBitmap(dc, (360/size), (360/size)); obbmp = bmpdc.SelectObject(&bbmp); opbmp = picdc.SelectObject(&pbmp); pic.Render(&bmpdc, CRect(0, 0, 360, 360)); int i; if(border){ //画边框 CPen pen, * open; pen.CreatePen(PS_SOLID, 1, RGB(96, 96, 96)); open = bmpdc.SelectObject(&p
28、en); for(i = 0; i < size; i++){ bmpdc.MoveTo(0, (360/size)*(i+1)-1); bmpdc.LineTo(359, (360/size)*(i+1)-1); bmpdc.MoveTo((360/size)*(i+1)-1, 0); bmpdc.LineTo((360/size)*(i+1)-1, 359); } bmpdc.SelectObject(open); pen.DeleteObject(); pen.CreatePen(PS_SOLID, 1, RGB(240, 2
29、40, 240)); open = bmpdc.SelectObject(&pen); for(i = 0; i < size; i++){ bmpdc.MoveTo(0, (360/size)*i); bmpdc.LineTo(359, (360/size)*i); bmpdc.MoveTo((360/size)*i, 0); bmpdc.LineTo((360/size)*i, 359); } bmpdc.SelectObject(open); } obmp = tdc.SelectObject(&bmp); for(
30、i = 0; i < size; i++){ for(int j = 0; j < size; j++){ int n = spell.GetPic(i, j); if(n != size*size-1){ picdc.BitBlt(-(n%size)*(360/size), -(n/size)*(360/size), 360, 360, &bmpdc, 0, 0, SRCCOPY); tdc.BitBlt((360/size)*i+2, (360/size)*j+2, (360/size), (360/size), &picdc, 0, 0, SRC
31、COPY); } } } dc->BitBlt(10, 10, 364, 364, &tdc, 0, 0, SRCCOPY); pic.Render(&bmpdc, CRect(0, 0, 150, 150)); dc->BitBlt(393, 45, 150, 150, &bmpdc, 0, 0, SRCCOPY); dc->MoveTo(392, 44); dc->LineTo(392, 194); dc->LineTo(542, 194); dc->LineTo(542, 44); dc->LineTo(392, 44); bmp
32、dc.SelectObject(obbmp); picdc.SelectObject(opbmp); tdc.SelectObject(obmp); } void CMyDlg::OnMenuAbout() { // 关于对话框 CAboutDlg dlgAbout; dlgAbout.DoModal(); } void CMyDlg::OnMenuExit() { // 退出 OnOK(); } void CMyDlg::OnMenuBegin() { // 重新开始游戏 spell.Init(); step = 0; Se
33、tDlgItemText(IDC_STEP, "0"); begin = TRUE; Invalidate(FALSE); } void CMyDlg::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default if(begin){ if(point.x > 12 && point.x < 372 && point.y > 12 && point.y < 372){ int w = (point.x
34、 - 12) / (360/size); int h = (point.y - 12) / (360/size); int r = spell.Move(w, h); Invalidate(FALSE); if(0 <= r){ step ++; CString str; str.Format("%d", step); SetDlgItemText(IDC_STEP, str); } if(1 == r){ MessageBox("完成!"); begin = FALSE; }
35、 } } CDialog::OnLButtonDown(nFlags, point); } void CMyDlg::OnMenuNumber() { // 选择数字 pic.Load(IDR_PIC_NUMBER); Invalidate(FALSE); } void CMyDlg::OnMenuView() { // 选择风景 pic.Load(IDR_PIC_VIEW); Invalidate(FALSE); } void CMyDlg::OnMenuPerson() { // 选择草莓 pic.Load(IDR_P
36、IC_CM); Invalidate(FALSE); } void CMyDlg::OnMenu3() { // 选择3*3的方块 if(3 != size){ CMenu* pMenu = this->GetMenu(); pMenu->CheckMenuItem(ID_MENU_3, MF_CHECKED); pMenu->CheckMenuItem(ID_MENU_4, MF_UNCHECKED); pMenu->CheckMenuItem(ID_MENU_5, MF_UNCHECKED); pMenu->CheckMenuItem(
37、ID_MENU_6, MF_UNCHECKED); spell.ReCreate(3, 3, 2, 2); size = 3; OnMenuBegin(); } } void CMyDlg::OnMenu4() { // 选择4*4的方块 if(4 != size){ CMenu* pMenu = this->GetMenu(); pMenu->CheckMenuItem(ID_MENU_4, MF_CHECKED); pMenu->CheckMenuItem(ID_MENU_3, MF_UNCHECKED); pMenu->Che
38、ckMenuItem(ID_MENU_5, MF_UNCHECKED); pMenu->CheckMenuItem(ID_MENU_6, MF_UNCHECKED); spell.ReCreate(4, 4, 3, 3); size = 4; OnMenuBegin(); } } void CMyDlg::OnMenu5() { // 选择5*5的方块 if(5 != size){ CMenu* pMenu = this->GetMenu(); pMenu->CheckMenuItem(ID_MENU_5, MF_CHECKED);
39、 pMenu->CheckMenuItem(ID_MENU_3, MF_UNCHECKED); pMenu->CheckMenuItem(ID_MENU_4, MF_UNCHECKED); pMenu->CheckMenuItem(ID_MENU_6, MF_UNCHECKED); spell.ReCreate(5, 5, 4, 4); size = 5; OnMenuBegin(); } } void CMyDlg::OnMenu6() { // 选择5*5的方块 if(6 != size){ CMenu* pMenu = this->
40、GetMenu(); pMenu->CheckMenuItem(ID_MENU_6, MF_CHECKED); pMenu->CheckMenuItem(ID_MENU_3, MF_UNCHECKED); pMenu->CheckMenuItem(ID_MENU_4, MF_UNCHECKED); pMenu->CheckMenuItem(ID_MENU_5, MF_UNCHECKED); spell.ReCreate(6, 6, 5, 5); size = 6; OnMenuBegin(); } } void CMyDlg::OnMe
41、nuBorder() { // 是否有边框 border = !border; CMenu* pMenu = this->GetMenu(); pMenu->CheckMenuItem(ID_MENU_BORDER, border?MF_CHECKED:MF_UNCHECKED); Invalidate(FALSE); } void CMyDlg::OnMenuOpen() { // 打开文件 CFileDialog file(true, NULL, NULL, OFN_OVERWRITEPROMPT, "图片文件|*.bmp;*.jpg;*.gif|*.j
42、pg|*.jpg|*.bmp|*.bmp|*.gif|*.gif|", NULL); if(file.DoModal()==IDOK){ pic.Load(file.GetPathName()); Invalidate(FALSE); } } 四、实验结果 选择这个小游戏的时候,没有想到会有这么难做,开始做甚至不知道从哪里下手,问了很多的人,加上上网查阅,最后才把这个小游戏做出来。不过,这个游戏还是有很多可以改进的地方的,尤其哈斯关于无解的情况。对于这个问题,我改动了很多地方还是不行,也尝试换成不同的玩法拼图,不过都是没有做出来。尽管有很多需要改进的地方,但是看着自己制作的拼图游戏能够玩,还是很有成就感的。希望以后自己能够做出更多更好地小程序。






