c++实现拼图游戏.doc

上传人:精*** 文档编号:830808 上传时间:2023-09-06 格式:DOC 页数:23 大小:264KB
下载 相关 举报
c++实现拼图游戏.doc_第1页
第1页 / 共23页
c++实现拼图游戏.doc_第2页
第2页 / 共23页
c++实现拼图游戏.doc_第3页
第3页 / 共23页
c++实现拼图游戏.doc_第4页
第4页 / 共23页
c++实现拼图游戏.doc_第5页
第5页 / 共23页
点击查看更多>>
资源描述

1、目录1 课程设计简介51.1 课程设计的目的51.2 课程设计内容52课程设计实现过程62.1系统结构框图62.2模块流程图62.3关键代码62.4主要数据结构62.5测试与评价63设计总结74参考文献8整个写完后使用更新域(在目录上点击右键选择更新域)确定各部分的页码!.23.1 课程设计简介(宋体,三号)1.1 课程设计的目的(宋体,小三)通过c+来实现拼图游戏,用户可以使用鼠标和键盘来控制游戏(正文字体要求为宋体,字号为小四,行间距20磅,每段前空2字符)1.2 课程设计内容(宋体小三)数据结构,将整个所有图像块记为一个二维数组m_board,写了一个Board类来控制图像的移动和修改。

2、Flip类是用来实现这个功能的,其中定义了移动一格的帧数、速度等信息。然后整个游戏是一个类Game。算法及其他,基本上全是小算法,有一点就是游戏开始时必须将所有的图像块置乱,但是如果是随机打乱就不能保证这个游戏一定有可行解,这个问题的讨论可以参见8数码问题,这种问题的有解是需要条件的。至于如何保证这个游戏有解,我用了最笨的方法,就是进行逆操作,一开始随机对原图进行操作,循环一定次数以后的图像就是置乱的图像了,但是这种方法有一个缺陷,当图像块非常多时,比如20*20,这种方法只能置乱图像的一部分,而另一部分却和原图差不多。为了消除这个问题,对每种操作(比如向上移动)重复执行随机次,这样就基本上解

3、决的前面的问题了。其实这个问题可以用数学方法来解决,又快又省力,但是自己对这一部分不是很了解,只能用笨方法了。关于平滑运动 ,只要在图像变换的时候插入几帧图像就好了,利用定时器来不断显示,写这个图像滑动效果很纠结,细节的问题考虑的比较多,一个好的数据结构的支持是必不可少的。(正文字体要求为宋体,字号为小四,行间距20磅,每段前空2字符)2课程设计实现过程(宋体,三号)(要求写详细过程)(正文字体要求为宋体,字号为小四,行间距20磅,每段前空2字符)该部分必须包括:系统结构框图、各模块流程图、关键代码、主要的数据结构、测试与评价等2.12.3头文件(宋体,小三)#include stdh.hcl

4、ass Boardprivate:int m_bMAXN_BOARDMAXN_BOARD;/ empty block position : ex ey/ old empty block position oex,oeyint m_ex,m_ey;int m_oex,m_oey;int m_dir;/ reverse-direction of empty blockbool m_bMove;/ if the board movedint m_nx,m_ny;int m_nBlock;public:Board();int Create();bool Check();/ check if the g

5、ame winint 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();int Down();int Left();int Right();int Click(int , int);/ when User click the pictureint Init();#

6、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 pixels are moved per m_timeint m_time;/ uSecondsint m_dir;/direction of flipint m_cx,m_cy;/ current pos of block

7、int m_sx,m_sy;/ source pos of blockint m_dx,m_dy;/ destination pos of blockint GetXY(int , int& , int&);int GetTime();int SetXY(int ,int ,int ,int );int SetTime(int );int SetCnt(int ,int ,int );int SetDir(int);bool Finish(HWND );/ check if the flipping over;#include stdh.h#include Flip.h#include Boa

8、rd.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_hBmp;HBITMAP m_hMemBmp;BITMAP m_bmp;HDC m_mdc,m_bufdc;/double cacheHWND m_hWnd;public:Game();int SetLevel(int ,int );int LoadBmp(HINSTANCE,int

9、);int Init(HWND);/ Init the gameint Create(HWND);int Paint(HWND);int Click(int , int);/User clicked the pictureint Key(WPARAM);/User push down the keyboardint InitFlip();/ init the m_flip and then flipping start.bool CheckWin();int Release(HWND );/NO_DEPENDENCIES/ Microsoft Visual C+ generated inclu

10、de file./ Used by res.rc/#define IDB_AC 103#define IDB_BH 104/ Next default values for new objects/ #ifdef APSTUDIO_INVOKED#ifndef APSTUDIO_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#e

11、ndif#endif#pragma once#include #include #include using namespace std;const int MAXN_BOARD = 10;const int CLIENT_X = 1024;const int CLIENT_Y = 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

12、_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:Create()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 =

13、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:GetMat(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

14、_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_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

15、/ to make sure must have a solution, we/ have to simulate The-Inverse-Action of the user./Then nStep 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 i

16、s huge, then method would/ only change a small area of all. Avoiding this I add a new variable/ 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;

17、 +i)for(int j = 0; j m_nx; +j)m_bij = id;+id;m_bm_ny-1m_nx-1 = -1;m_oey = m_ey;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

18、:Move(int nx, int ny)m_oex = m_ex;m_oey = m_ey;m_ex = nx;m_ey = ny;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_b

19、Move = true;return 0;int Board:Down()m_bMove = false;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-

20、1 = -1;-m_ex;m_dir = DIR_RIGHT;m_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)retur

21、n -1;if(abs(x-m_ex) + 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(HWN

22、D 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;m_board.Ini

23、t();MoveWindow(hWnd,0,0,m_bmp.bmWidth+m_ux+m_nx*OFFSET_LINE+20,m_bmp.bmHeight+20,TRUE);/ initialize flipm_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 Game:LoadBmp(HINSTANCE

24、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_X,CLIENT_Y);SelectObject(m_mdc,m_

25、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,/m_ux-2*OFFSET_LINE,m_uy-2*OFFSET_LINE,/m_

26、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);elseBitBlt(m_mdc,j*(m_ux+(OFFSET_LINE1)+(OFFSET_LINE),i*(m_uy+(OFFSET_LINE1)+OFFSET_LINE,

27、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)m_board.ResetMove();int x,y;x = mx/m_ux;y = my/m_uy;m_board.Click(

28、x,y);return 0;int Game:Key(WPARAM wParam)m_board.ResetMove();switch(wParam)case VK_UP:m_board.Down(); break;case VK_DOWN: m_board.Up(); break;case VK_LEFT: m_board.Right(); break;case VK_RIGHT: m_board.Left(); break;if(m_board.CheckMove()/ if board moved.InitFlip();SetTimer(m_hWnd,0,m_flip.GetTime()

29、,0);return 0;int Game:InitFlip()/ source x&y/ destination x&yint sx,sy,dx,dy;sx = m_board.GetEX()*(m_ux+(OFFSET_LINE1)+OFFSET_LINE;sy = m_board.GetEY()*(m_uy+(OFFSET_LINE1)+OFFSET_LINE;dx = m_board.GetOEX()*(m_ux+(OFFSET_LINE1)+OFFSET_LINE;dy = m_board.GetOEY()*(m_uy+(OFFSET_LINE1)+OFFSET_LINE;m_fli

30、p.SetXY(sx,sy,dx,dy);/ set directionm_flip.SetDir(m_board.GetDir();return 0;bool Game:CheckWin()if(m_board.Check()return true;return false;int Game:Release(HWND hWnd)ReleaseDC(hWnd,m_mdc);ReleaseDC(hWnd,m_bufdc);DeleteObject(m_hBmp);DeleteObject(m_hMemBmp);return 0;#include Flip.hint Flip:GetXY(int

31、dir, int& x, int& y)if(m_dir = DIR_UP)m_cy -= m_uy;if(m_cy m_dy)m_cy = m_dy;else if(m_dir = DIR_LEFT)m_cx -= m_ux;if(m_cx m_dx)m_cx = m_dx;x = m_cx;y = m_cy;return 0;int Flip:GetTime()return m_time;int Flip:SetXY(int sx,int sy, int dx, int dy)m_sx = sx;m_sy = sy;m_dx = dx;m_dy = dy;m_cx = sx;m_cy =

32、sy;return 0;int Flip:SetTime(int t)m_time = t;return 0;int Flip:SetCnt(int cnt, int bx, int by)m_cnt = cnt;m_ux = bx/cnt;m_uy = by/cnt;return 0;int Flip:SetDir(int dir)m_dir = dir;return 0;bool Flip:Finish(HWND hWnd)if(m_cx = m_dx & m_cy = m_dy)KillTimer(hWnd,0);return true;return false;#include #in

33、clude PingTu.h#include resource.hLRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;/ my datas /HINSTANCE ghInst;Game game;/int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow) static TCHAR szAppName = TEXT (WinApp) ; HWND hWnd ; MSG msg ; WNDCLASS wndcl

34、ass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ;wndclass.cbWndExtra = 0 ;wndclass.hInstance = hInstance ;wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;wndclass.hbrBackground = (HBRUSH) Ge

35、tStockObject (WHITE_BRUSH) ;wndclass.lpszMenuName = NULL ;wndclass.lpszClassName= szAppName ;if (!RegisterClass (&wndclass)return 0 ;ghInst = hInstance;hWnd = CreateWindow( szAppName, / window class nameTEXT (Win32), / window captionWS_OVERLAPPEDWINDOW, / window styleCW_USEDEFAULT,/ initial x positi

36、onCW_USEDEFAULT,/ initial y position800,/ initial x size600,/ initial y sizeNULL, / parent window handleNULL, / window menu handlehInstance, / program instance handleNULL) ; / creation parametersShowWindow (hWnd, iCmdShow) ;UpdateWindow (hWnd) ;while (GetMessage (&msg, NULL, 0, 0)TranslateMessage (&

37、msg) ;DispatchMessage (&msg) ;return msg.wParam ; LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)PAINTSTRUCT ps ;HDC hdc;RECT rect ;TCHAR str10;switch (message)case WM_KEYDOWN:game.Key(wParam);game.Paint(hWnd);if(game.CheckWin()MessageBox(hWnd,TEXT(You Win!),NULL,NUL

38、L);game.Init(hWnd);break;case WM_LBUTTONDOWN:game.Click(LOWORD(lParam),HIWORD(lParam);game.Paint(hWnd);if(game.CheckWin()MessageBox(hWnd,TEXT(You Win!),NULL,NULL);game.Init(hWnd);return 0;case WM_CREATE:game.Create(hWnd);game.LoadBmp(ghInst,IDB_BH);game.SetLevel(4,4);game.Init(hWnd);return 0 ;case WM_TIMER:;case WM_PAINT:hdc = BeginPaint (hWnd, &ps) ;game.Paint(hWnd);EndPaint (hWnd, &ps) ;return 0 ;case WM_DESTROY:game.Release(hWnd);PostQuitMessage (0) ;return 0 ;return DefWindowProc (hWnd, message, wParam, lParam) ;(正文字体要求为宋体,字号为小四,行间距20磅,每段前空2字符)2.3资源文件

展开阅读全文
相关资源
相关搜索
资源标签

当前位置:首页 > 学术论文 > 毕业设计

版权声明:以上文章中所选用的图片及文字来源于网络以及用户投稿,由于未联系到知识产权人或未发现有关知识产权的登记,如有知识产权人并不愿意我们使用,如有侵权请立即联系:2622162128@qq.com ,我们立即下架或删除。

Copyright© 2022-2024 www.wodocx.com ,All Rights Reserved |陕ICP备19002583号-1 

陕公网安备 61072602000132号     违法和不良信息举报:0916-4228922