1、c+实现拼图游戏课程设计说明书 课程名称: 面向对象程序设计与c+语言 设计题目: c+实现拼图游戏 2015年 6月 26 日目录1 课程设计简介 51.1 课程设计的目的 51.2 课程设计内容 52课程设计实现过程 62.1系统结构框图 62.2模块流程图 62.3关键代码 62.4主要数据结构 62.5测试与评价 63设计总结 74参考文献 8整个写完后使用更新域(在目录上点击右键选择更新域)确定各部分的页码!1 课程设计简介(宋体,三号)1.1 课程设计的目的(宋体,小三)通过c+来实现拼图游戏,用户可以使用鼠标和键盘来控制游戏(正文字体要求为宋体,字号为小四,行间距20磅,每段前空
2、2字符)1.2 课程设计内容(宋体小三)数据结构,将整个所有图像块记为一个二维数组m_board,写了一个Board类来控制图像的移动和修改。Flip类是用来实现这个功能的,其中定义了移动一格的帧数、速度等信息。然后整个游戏是一个类Game。算法及其他,基本上全是小算法,有一点就是游戏开始时必须将所有的图像块置乱,但是如果是随机打乱就不能保证这个游戏一定有可行解,这个问题的讨论可以参见8数码问题,这种问题的有解是需要条件的。至于如何保证这个游戏有解,我用了最笨的方法,就是进行逆操作,一开始随机对原图进行操作,循环一定次数以后的图像就是置乱的图像了,但是这种方法有一个缺陷,当图像块非常多时,比如
3、20*20,这种方法只能置乱图像的一部分,而另一部分却和原图差不多。为了消除这个问题,对每种操作(比如向上移动)重复执行随机次,这样就基本上解决的前面的问题了。其实这个问题可以用数学方法来解决,又快又省力,但是自己对这一部分不是很了解,只能用笨方法了。关于平滑运动 ,只要在图像变换的时候插入几帧图像就好了,利用定时器来不断显示,写这个图像滑动效果很纠结,细节的问题考虑的比较多,一个好的数据结构的支持是必不可少的。(正文字体要求为宋体,字号为小四,行间距20磅,每段前空2字符) 2课程设计实现过程(宋体,三号)(要求写详细过程)(正文字体要求为宋体,字号为小四,行间距20磅,每段前空2字符)该部
4、分必须包括:系统结构框图、各模块流程图、关键代码、主要的数据结构、测试与评价等2.12.3头文件(宋体,小三)#include stdh.hclass Boardprivate: int m_bMAXN_BOARDMAXN_BOARD; / empty block position : ex ey / old empty block position oex,oey int m_ex,m_ey; int m_oex,m_oey; int m_dir; / reverse-direction of empty block bool m_bMove; / if the board moved in
5、t m_nx,m_ny; int m_nBlock;public: Board(); int Create(); bool Check(); / check if the game win int GetMat(int ,int ); int SetMat(int ,int , int ); int SetXY(int ,int ); int GetEX(); int GetEY(); int GetOEX(); int GetOEY(); int GetDir(); int Move(int , int ); bool CheckMove(); int ResetMove(); int Up
6、(); int Down(); int Left(); int Right(); int Click(int , int); / when User click the picture int Init(); ;#include stdh.h/* Class Flip stored all elements of the flipping-block. It will be used when the clicked block moving to the destination.*/class Flip public: int m_cnt; int m_ux,m_uy; / uX , uY
7、pixels are moved per m_time int m_time; / uSeconds int m_dir; /direction of flip int m_cx,m_cy; / current pos of block int m_sx,m_sy; / source pos of block int m_dx,m_dy; / destination pos of block int GetXY(int , int& , int&); int GetTime(); int SetXY(int ,int ,int ,int ); int SetTime(int ); int Se
8、tCnt(int ,int ,int ); int SetDir(int); bool Finish(HWND ); / check if the flipping over;#include stdh.h#include Flip.h#include Board.hclass Gameprivate: Board m_board; Flip m_flip; bool m_bMove; int m_ex,m_ey; int m_nx,m_ny; int m_nBlock; int m_ux,m_uy; int m_vFlip; int m_xFlip,m_yFlip; HBITMAP m_hB
9、mp; HBITMAP m_hMemBmp; BITMAP m_bmp; HDC m_mdc,m_bufdc; /double cache HWND m_hWnd;public: Game(); int SetLevel(int ,int ); int LoadBmp(HINSTANCE,int); int Init(HWND); / Init the game int Create(HWND); int Paint(HWND); int Click(int , int); /User clicked the picture int Key(WPARAM); /User push down t
10、he keyboard int InitFlip(); / init the m_flip and then flipping start. bool CheckWin(); int Release(HWND );/NO_DEPENDENCIES/ Microsoft Visual C+ generated include file./ Used by res.rc/#define IDB_AC 103#define IDB_BH 104/ Next default values for new objects/ #ifdef APSTUDIO_INVOKED#ifndef APSTUDIO_
11、READONLY_SYMBOLS#define _APS_NEXT_RESOURCE_VALUE 105#define _APS_NEXT_COMMAND_VALUE 40001#define _APS_NEXT_CONTROL_VALUE 1001#define _APS_NEXT_SYMED_VALUE 101#endif#endif#pragma once#include #include #include using namespace std;const int MAXN_BOARD = 10;const int CLIENT_X = 1024;const int CLIENT_Y
12、= 768;const int OFFSET_LINE = 1;const int DIR_UP = 0;const int DIR_DOWN = 1;const int DIR_LEFT = 2;const int DIR_RIGHT = 3;const int FLIP_CNT = 8;const int FLIP_TIME = 18;2.2源文件(宋体,小三)#include Board.hBoard:Board() memset(m_b,-1,sizeof(m_b); m_nx = m_ny = m_ex = m_ey = 0; m_nBlock = 0;int Board:Creat
13、e() memset(m_b,-1,sizeof(m_b); m_nx = m_ny = m_ex = m_ey = 0; m_nBlock = 0; return 0;int Board:SetXY(int nx, int ny) m_nx = nx; m_ny = ny; m_nBlock = m_nx * m_ny; m_oex = m_ex = m_nx - 1; m_oey = m_ey = m_ny - 1; return 0;int Board:SetMat(int x, int y, int val) m_byx = val; return val;int Board:GetM
14、at(int x, int y) return m_byx;int Board:GetEX() return m_ex;int Board:GetEY() return m_ey;int Board:GetOEX() return m_oex;int Board:GetOEY() return m_oey;int Board:GetDir() return m_dir;bool Board:Check() int ck = 0; bool flag = true; for(int i = 0; i m_ny; +i) for(int j = 0; j m_nx; +j) if(ck != m_
15、bij) flag = false; break; +ck; if(flag = false & ck m_nBlock-1) return false; return true;int Board:Init() / VARIABLE: nStep , nLoop / / nStep: / In order to generate the random block positions and / to make sure must have a solution, we / have to simulate The-Inverse-Action of the user. / Then nSte
16、p mean we will execute the Inverse-Action times. / And each Action will be random generated. / / nLoop: / Due to the reason that the method that above-mentioned has / a big Drawback: if the number of blocks is huge, then method would / only change a small area of all. Avoiding this I add a new varia
17、ble / nloop, which means a same actions that executed times. It was proved / that after this improved the new-method work well. / int nStep = (rand()&(18)-1)+100; int nLoop; int tmp; int id = 0; for(int i = 0; i m_ny; +i) for(int j = 0; j m_nx; +j) m_bij = id; +id; m_bm_ny-1m_nx-1 = -1; m_oey = m_ey
18、; m_oex = m_ex; for(int i = 0; i nStep; +i) tmp = (rand()&3); nLoop = rand()&7; / loop of action for(int j = 0; j Up(); break; case 1: this-Down(); break; case 2: this-Left(); break; case 3: this-Right(); break; return 0;int Board:Move(int nx, int ny) m_oex = m_ex; m_oey = m_ey; m_ex = nx; m_ey = ny
19、; return 0;bool Board:CheckMove() return m_bMove;int Board:ResetMove() m_bMove = false; return 0;int Board:Up() m_bMove = false; Move(m_ex,m_ey); if(m_ey = 0) return -1; m_bm_eym_ex = m_bm_ey-1m_ex; m_bm_ey-1m_ex = -1; -m_ey; m_dir = DIR_DOWN; m_bMove = true; return 0;int Board:Down() m_bMove = fals
20、e; Move(m_ex,m_ey); if(m_ey = m_ny - 1) return -1; m_bm_eym_ex = m_bm_ey+1m_ex; m_bm_ey+1m_ex = -1; +m_ey; m_dir = DIR_UP; m_bMove = true; return 0;int Board:Left() m_bMove = false; Move(m_ex,m_ey); if(m_ex = 0) return -1; m_bm_eym_ex = m_bm_eym_ex-1; m_bm_eym_ex-1 = -1; -m_ex; m_dir = DIR_RIGHT; m_
21、bMove = true; return 0;int Board:Right() m_bMove = false; Move(m_ex,m_ey); if(m_ex = m_nx - 1) return -1; m_bm_eym_ex = m_bm_eym_ex+1; m_bm_eym_ex+1 = -1; +m_ex; m_dir = DIR_LEFT; m_bMove = true; return 0;int Board:Click(int x, int y) m_bMove = false; if(x = m_nx | y = m_ny) return -1; if(abs(x-m_ex
22、) + abs(y-m_ey) 1) return -1; if(x - m_ex 0) this-Right(); return 0; if(m_ex - x 0) this-Left(); return 0; if(y - m_ey 0) this-Down(); return 0; if(m_ey - y 0) this-Up(); return 0; return 0;#include PingTu.hGame:Game() m_bMove = false; m_nx = m_ny = m_ux = m_uy = 0; m_nBlock = 0;int Game:Create(HWND
23、 hWnd) m_bMove = false; m_nx = m_ny = m_ux = m_uy = 0; m_nBlock = 0; m_mdc = CreateCompatibleDC(GetDC(hWnd); m_bufdc = CreateCompatibleDC(GetDC(hWnd); srand(unsigned(time(NULL); m_board.Create(); return 0;int Game:Init(HWND hWnd) m_ux = m_bmp.bmWidth/m_nx; m_uy = m_bmp.bmHeight/m_ny; m_hWnd = hWnd;
24、m_board.Init(); MoveWindow(hWnd,0,0, m_bmp.bmWidth+m_ux+m_nx*OFFSET_LINE+20, m_bmp.bmHeight+20, TRUE); / initialize flip m_flip.SetCnt(FLIP_CNT,m_ux,m_uy); m_flip.SetTime(FLIP_TIME); return 0;int Game:SetLevel(int x, int y) m_nx = x; m_ny = y; m_board.SetXY(x,y); m_nBlock = m_nx * m_ny; return 0;int
25、 Game:LoadBmp(HINSTANCE hInst, int id) m_hBmp = LoadBitmap(hInst,MAKEINTRESOURCE(id); GetObject(m_hBmp,sizeof(BITMAP),&m_bmp); return 0;int Game:Paint(HWND hWnd ) int x,y,k,i,j; int cur_block; HDC hdc; hdc = GetDC(hWnd); DeleteObject(m_hMemBmp); m_hMemBmp = CreateCompatibleBitmap(GetDC(hWnd),CLIENT_
26、X,CLIENT_Y); SelectObject(m_mdc,m_hMemBmp); SelectObject(m_bufdc,m_hBmp); cur_block = m_board.GetMat(m_board.GetOEX(),m_board.GetOEY(); for(i = 0; i m_ny; +i) for( j = 0; j m_nx; +j) k = m_board.GetMat(j,i); x = k%m_nx; y = k/m_nx; if( -1 = k ) /StretchBlt(m_mdc,j*m_ux+OFFSET_LINE,i*m_uy+OFFSET_LINE
27、, / m_ux-2*OFFSET_LINE,m_uy-2*OFFSET_LINE, / m_bufdc,0,0,m_bmp.bmWidth,m_bmp.bmHeight,SRCCOPY); continue; else if( k = cur_block & !m_flip.Finish(hWnd) ) m_flip.GetXY(m_board.GetDir(),x,y); BitBlt(m_mdc,x,y,m_ux,m_uy, m_bufdc,(k%m_nx)*m_ux,(k/m_nx)*m_uy,SRCCOPY); else BitBlt(m_mdc,j*(m_ux+(OFFSET_LINE1)+(OFFSET_LINE), i*(m_uy+(OFFSET_LINE1)+OFFSET_LINE,m_ux,m_uy, m_bufdc,x*m_ux,y*m_uy,SRCCOPY); BitBlt(hdc,0,0,m_bmp.bmWidth,m_bmp.bmHeight,m_mdc,0,0,SRCCOPY); StretchBlt(hdc,m_bmp.bmWidth,0,m_ux,m_uy, m_bufdc,0,0,m_bmp.bmWidth,m_bmp.bmHeight,SRCCOPY); return 0;int Game:Click(int mx, int my)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1