




已阅读5页,还剩19页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
/#include stdafx.h#include /为了使用API函数 #include /为了使用定时器 #include /为了使用随机数 #include #define rand_a 4 / #define BLOCKWIDTH 20 /单个方块大小 #define NUMLINEBLOCKS 18 /行数 #define NUMCOLUMNBLOCKS 10 /列数 #define ID_TIMER 1 /定时器ID #define BLOCKSTYLES (sizeof (Blocks) / sizeof (Blocks0) /方块的种类数 /游戏区各方格顶点布尔值,代表该方格是否有方块 #define cnt_boxMax 1000bool GameClientNUMCOLUMNBLOCKSNUMLINEBLOCKS;bool GameClientRNUMCOLUMNBLOCKSNUMLINEBLOCKS;static int cF_R, cS_R, cF, cS; /随机方块图形对应的第一、二纬static int FstDimencnt_boxMax, SecDimencnt_boxMax;static int Score,Score1,Score2; /得分 /定义各方块形状,以点表示 static bool pause = false; /暂停 static POINT Block4, NextBlockcnt_boxMax4; static POINT BlockR4; int Cnt_BoxNum=0;/下落的箱子个数 int Cnt_BoxNumR=0;/下落的箱子个数 -右 bool CanDownR(POINT pt); /下落实现 void DownR(POINT pt); /判断是否可以左移 bool CanLeftR(POINT pt); /实现左移 void LeftR(POINT pt); /判断是否可以右移 bool CanRightR(POINT pt); /实现右移 void RightR(POINT pt); /判断是否可以变形 bool CanChangeR(POINT pt); /实现变形 void ChangeR(POINT pt); /消行处理以及分数结算 void DelSqureR(HWND); void GenrRand(int RandCnt); struct POINT pt4; Blocks4 = 0, 0, 1, 0, 1, 1, 1, 2, 2, 0, 0, 1, 1, 1, 2, 1, 1, 0, 1, 1, 1, 2, 2, 2, 0, 1, 1, 1, 2, 1, 0, 2, /正7 1, 0, 2, 0, 1, 1, 1, 2, 0, 1, 1, 1, 2, 1, 2, 2, 1, 0, 1, 1, 0, 2, 1, 2, 0, 0, 0, 1, 1, 1, 2, 1, /反7 1, 0, 1, 1, 1, 2, 1, 3, 0, 1, 1, 1, 2, 1, 3, 1, 1, 0, 1, 1, 1, 2, 1, 3, 0, 1, 1, 1, 2, 1, 3, 1, /1 0, 0, 1, 0, 1, 1, 2, 1, 2, 0, 1, 1, 2, 1, 1, 2, 0, 0, 1, 0, 1, 1, 2, 1, 2, 0, 1, 1, 2, 1, 1, 2, /Z 1, 0, 2, 0, 0, 1, 1, 1, 1, 0, 1, 1, 2, 1, 2, 2, 1, 0, 2, 0, 0, 1, 1, 1, 1, 0, 1, 1, 2, 1, 2, 2, /反Z 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, /田字 1, 0, 0, 1, 1, 1, 2, 1, 0, 0, 0, 1, 1, 1, 0, 2, 0, 0, 1, 0, 2, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 2 /尖头; LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) WNDCLASS wndcls; TCHAR szClassName = TEXT(Terics), szWindowName = TEXT(Akas Terics); wndcls.cbClsExtra = 0; wndcls.cbWndExtra = 0; wndcls.hbrBackground = static_cast(GetStockObject(WHITE_BRUSH); wndcls.hCursor = LoadCursor(hInstance, IDC_ARROW); wndcls.hIcon = LoadIcon(hInstance, IDI_APPLICATION); wndcls.hInstance = hInstance; wndcls.lpfnWndProc = WndProc; wndcls.lpszClassName = szClassName; wndcls.lpszMenuName = NULL; wndcls.style = CS_HREDRAW | CS_VREDRAW; RegisterClass(&wndcls); HWND hwnd = CreateWindow(szClassName, szWindowName, WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 1*(NUMCOLUMNBLOCKS + 10) * BLOCKWIDTH*2, (NUMLINEBLOCKS + 3) * BLOCKWIDTH, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, SW_SHOWNORMAL); UpdateWindow(hwnd); MSG msg; while(GetMessage(&msg, NULL, 0, 0) TranslateMessage(&msg); DispatchMessage(&msg); return msg.wParam; /随机数函数定制版,用于随机出现的方块 unsigned Random(int n); /判断是否可以下落,可以则返回true bool CanDown(POINT pt); /下落实现 void Down(POINT pt); /判断是否可以左移 bool CanLeft(POINT pt); /实现左移 void Left(POINT pt); /判断是否可以右移 bool CanRight(POINT pt); /实现右移 void Right(POINT pt); /判断是否可以变形 bool CanChange(POINT pt); /实现变形 void Change(POINT pt); /消行处理以及分数结算 void DelSqure(HWND); void winRestart(HWND hwnd); LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) POINT TericsBorder = -1+8* BLOCKWIDTH, -1, (NUMCOLUMNBLOCKS +8)* BLOCKWIDTH + 1, NUMLINEBLOCKS * BLOCKWIDTH + 1; POINT TericsBorderRight = -1+20* BLOCKWIDTH, -1, (NUMCOLUMNBLOCKS +20)* BLOCKWIDTH + 1, NUMLINEBLOCKS * BLOCKWIDTH + 1; HDC hdc; PAINTSTRUCT ps; TEXTMETRIC tm; TCHAR szNextTerics = TEXT(下一个:), / szNextTerics = TEXT(下一个:); szSCore = TEXT(得分:); static TCHAR szBufferScore5; static int cxChar, cyChar; / static POINT Block4, NextBlock4; int x, y; /static bool pause = false; /暂停 switch(message) case WM_CREATE: hdc = GetDC(hwnd); GetTextMetrics(hdc, &tm); cxChar = tm.tmAveCharWidth * 2; cyChar = tm.tmExternalLeading + tm.tmHeight; SetTimer(hwnd, ID_TIMER, 600, NULL); /初始化第一个出现的方块 cS_R=cS = Random(4); cF_R=cF = Random(BLOCKSTYLES); for(int i = 0; i 4; +i) Blocki.x = BlockscFcS.pti.x + 4; /ori:4 Blocki.y = BlockscFcS.pti.y; GameClientBlocki.xBlocki.y = true; BlockRi.x = BlockscF_RcS_R.pti.x + 4; /右侧第一个方块初始化 BlockRi.y = BlockscF_RcS_R.pti.y; GameClientRBlocki.xBlocki.y = true; / S = Random(4); /F = Random(BLOCKSTYLES);/* for(int i = 0; i 4; +i) NextBlocki.x = BlocksFS.pti.x; NextBlocki.y = BlocksFS.pti.y; */ GenrRand(cnt_boxMax); ReleaseDC(hwnd, hdc); return 0; case WM_TIMER: / cF=FstDimenCnt_BoxNum; /cS=SecDimenCnt_BoxNum; if(pause) return 0; if(CanDown(Block) Down(Block); /不能下移,需要处理消行判断(结合分数),还需要处理下一个显示,和当前显示的方块 else DelSqure(hwnd); for(int i = 0; i 4; +i) Blocki.x = NextBlockCnt_BoxNumi.x + 4; Blocki.y = NextBlockCnt_BoxNumi.y; if(GameClientBlocki.xBlocki.y) /如果游戏结束 KillTimer(hwnd, ID_TIMER); int result = MessageBox( NULL , TEXT(游戏结束,是否继续?) , TEXT(游戏提示) , MB_ICONINFORMATION|MB_YESNO); switch(result) /*注意!使用Unicode应用TEXT包围字串 case IDYES: winRestart(hwnd); break; case IDNO: DestroyWindow(hwnd); break; default: break; return 0; else GameClientBlocki.xBlocki.y = true; cF=FstDimenCnt_BoxNum; cS=SecDimenCnt_BoxNum; Cnt_BoxNum+;/方块数量+ /* if(Cnt_BoxNum=cnt_boxMax) Cnt_BoxNum=0; KillTimer(hwnd, ID_TIMER); int result = MessageBox( NULL , TEXT(游戏结束,请重新开始!) , TEXT(游戏提示) , MB_ICONINFORMATION|MB_YESNO); DestroyWindow(hwnd); */ / 右侧方块自由下落处理-start if(CanDownR(BlockR) DownR(BlockR); /不能下移,需要处理消行判断(结合分数),还需要处理下一个显示,和当前显示的方块 else DelSqureR(hwnd); for(int i = 0; i 4; +i) BlockRi.x = NextBlockCnt_BoxNumRi.x + 4; BlockRi.y = NextBlockCnt_BoxNumRi.y; if(GameClientRBlockRi.xBlockRi.y) /如果游戏结束 KillTimer(hwnd, ID_TIMER); int result = MessageBox( NULL , TEXT(游戏结束,请重新开始!) , TEXT(游戏提示) , MB_ICONINFORMATION|MB_YESNO); switch(result) /*注意!使用Unicode应用TEXT包围字串 case IDYES: winRestart(hwnd); SetTimer(hwnd, ID_TIMER, 600, NULL); break; case IDNO: DestroyWindow(hwnd); break; default: break; return 0; else GameClientRBlockRi.xBlockRi.y = true; cF_R=FstDimenCnt_BoxNumR; cS_R=SecDimenCnt_BoxNumR; Cnt_BoxNumR+;/方块数量+ /* if(Cnt_BoxNumR=cnt_boxMax) Cnt_BoxNumR=0; KillTimer(hwnd, ID_TIMER); int result = MessageBox( NULL , TEXT(游戏结束,请重新开始!) , TEXT(游戏提示) , MB_ICONINFORMATION|MB_YESNO); DestroyWindow(hwnd); */ / 右侧方块自由下落处理-end InvalidateRect(hwnd, NULL, TRUE); return 0; case WM_KEYDOWN: if(pause & wParam != VK_PAUSE) return 0; switch(wParam) case VK_LEFT: if(CanLeft(Block) Left(Block); InvalidateRect(hwnd, NULL, TRUE); break; case VK_A: if(CanLeftR(BlockR) LeftR(BlockR); InvalidateRect(hwnd, NULL, TRUE); break; case VK_RIGHT: if(CanRight(Block) Right(Block); InvalidateRect(hwnd, NULL, TRUE); break; case VK_D: if(CanRightR(BlockR) RightR(BlockR); InvalidateRect(hwnd, NULL, TRUE); break; case VK_UP: if(CanChange(Block) Change(Block); InvalidateRect(hwnd, NULL, TRUE); break; case VK_W: if(CanChangeR(BlockR) ChangeR(BlockR); InvalidateRect(hwnd, NULL, TRUE); break; case VK_DOWN: if(CanDown(Block) / Down(Block); InvalidateRect(hwnd, NULL, TRUE); break; case VK_S: if(CanDownR(BlockR) / DownR(BlockR); InvalidateRect(hwnd, NULL, TRUE); break; case VK_PAUSE: pause = !pause; break; case VK_SPACE: while(CanDownR(BlockR) / DownR(BlockR); InvalidateRect(hwnd, NULL, TRUE); break; case VK_RETURN: while(CanDown(Block) / Down(Block); InvalidateRect(hwnd, NULL, TRUE); break; default: break; return 0; case WM_CHAR: if(wParam = p) pause = !pause; else if(wParam = r) winRestart(hwnd); SetTimer(hwnd, ID_TIMER, 600, NULL); /* Score1 = 0; Score2 = 0; Cnt_BoxNum=0; Cnt_BoxNumR=0; for(int x = 0; x NUMCOLUMNBLOCKS; +x) for(int y = 0; y NUMLINEBLOCKS; +y) GameClientxy = false; GameClientRxy = false; cS_R=cS = Random(4); cF_R=cF = Random(BLOCKSTYLES); for(int i = 0; i 4; +i) Blocki.x = BlockscFcS.pti.x + 4;/ori:4 Blocki.y = BlockscFcS.pti.y; GameClientBlocki.xBlocki.y = true; BlockRi.x = BlockscF_RcS_R.pti.x + 4;/ori:4 BlockRi.y = BlockscF_RcS_R.pti.y; GameClientRBlockRi.xBlockRi.y = true; pause = false; InvalidateRect(hwnd, NULL, true); /ori:TRUE GenrRand(cnt_boxMax); */ return 0; case WM_PAINT: /if(pause) return 0; hdc = BeginPaint(hwnd, &ps); SetViewportOrgEx(hdc, BLOCKWIDTH, BLOCKWIDTH, NULL); SelectObject(hdc, GetStockObject(WHITE_BRUSH); SelectObject(hdc, GetStockObject(BLACK_PEN); /画俄罗斯方块游戏的边框 Rectangle(hdc, TericsBorder0.x, TericsBorder0.y, TericsBorder1.x, TericsBorder1.y); Rectangle(hdc, TericsBorderRight0.x, TericsBorderRight0.y, TericsBorderRight1.x, TericsBorderRight1.y); /输出下一个字符串 -Right TextOut(hdc, 1 * BLOCKWIDTH, 0, szNextTerics, lstrlen(szNextTerics); /输出得分字符串 TextOut(hdc, 1* BLOCKWIDTH, cyChar + 5 * BLOCKWIDTH, szSCore, lstrlen(szSCore); / SetTextAlign(hdc, TA_RIGHT | TA_TOP); TextOut(hdc, 1 * BLOCKWIDTH + 3 * cxChar, 2 * cyChar + 5 * BLOCKWIDTH, szBufferScore, wsprintf(szBufferScore, TEXT(%d), Score2); /输出下一个字符串 - Left TextOut(hdc, 35 * BLOCKWIDTH, 0, szNextTerics, lstrlen(szNextTerics); /输出得分字符串 TextOut(hdc, 35* BLOCKWIDTH, cyChar + 5 * BLOCKWIDTH, szSCore, lstrlen(szSCore); /SetTextAlign(hdc, TA_RIGHT | TA_TOP); TextOut(hdc, 33 * BLOCKWIDTH + 3 * cxChar, 2 * cyChar + 5 * BLOCKWIDTH, szBufferScore, wsprintf(szBufferScore, TEXT(%d), Score1); SetTextAlign(hdc, TA_LEFT | TA_TOP); SelectObject(hdc, GetStockObject(BLACK_BRUSH); SelectObject(hdc, GetStockObject(WHITE_PEN); /显示游戏区的方块 for(x = 0; x NUMCOLUMNBLOCKS; +x) for(y = 0; y NUMLINEBLOCKS; +y) if(GameClientxy) Rectangle(hdc, (x +20)* BLOCKWIDTH, y * BLOCKWIDTH, (x + 21) * BLOCKWIDTH, (y + 1) * BLOCKWIDTH); /显示游戏区的方块 -右侧 for(x = 0; x NUMCOLUMNBLOCKS; +x) for(y = 0; y NUMLINEBLOCKS; +y) if(GameClientRxy) Rectangle(hdc, (x +8)* BLOCKWIDTH, y * BLOCKWIDTH, (x + 9) * BLOCKWIDTH, (y + 1) * BLOCKWIDTH); /* /显示下一个方块区域的方块 for(int i = 0; i 4; +i) Rectangle(hdc, (NextBlocki.x + NUMCOLUMNBLOCKS + 2) * BLOCKWIDTH, NextBlocki.y * BLOCKWIDTH + cyChar, (NextBlocki.x + NUMCOLUMNBLOCKS + 3) * BLOCKWIDTH, (NextBlocki.y + 1) * BLOCKWIDTH + cyChar); */ /显示下一个方块区域的方块 for(int i = 0; i 4; +i) /-左侧 Rectangle(hdc, (NextBlockCnt_BoxNumi.x + 32) * BLOCKWIDTH, NextBlockCnt_BoxNumi.y * BLOCKWIDTH + cyChar, (NextBlockCnt_BoxNumi.x + 33) * BLOCKWIDTH, (NextBlockCnt_BoxNumi.y + 1) * BLOCKWIDTH + cyChar); for(int i = 0; i 4; +i) /-右侧 Rectangle(hdc, (NextBlockCnt_BoxNumRi.x + 2) * BLOCKWIDTH, NextBlockCnt_BoxNumRi.y * BLOCKWIDTH + cyChar, (NextBlockCnt_BoxNumRi.x + 3) * BLOCKWIDTH, (NextBlockCnt_BoxNumRi.y + 1) * BLOCKWIDTH + cyChar); EndPaint(hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage(0); return 0; return DefWindowProc(hwnd, message, wParam, lParam); /判断方块是否可以下落 bool CanDown(POINT pt) bool result = true; /将方块所在格子先假设指定为无方块 for(int i = 0; i 4; +i) GameClientpti.xpti.y = false; for(int i = 0; i 4; +i) /假如继续落下超过下底边界,返回false;或者假如该小方块下落一格已经有方块,结果为false if(pti.y + 1 = NUMLINEBLOCKS | GameClientpti.xpti.y + 1) result = false; break; /恢复方块所在格子为有方块 for(int i = 0; i 4; +i) GameClientpti.xpti.y = true; return result; bool CanDownR(POINT pt) bool result = true; /将方块所在格子先假设指定为无方块 for(int i = 0; i 4; +i) GameClientRpti.xpti.y = false; for(int i = 0; i 4; +i) /假如继续落下超过下底边界,返回false;或者假如该小方块下落一格已经有方块,结果为false if(pti.y + 1 = NUMLINEBLOCKS | GameClientRpti.xpti.y + 1) result = false; break; /恢复方块所在格子为有
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 重庆简介教学课件
- 河南省天一联考2026届高三年级开学联考物理试卷(含答案解析)
- 重庆文理学院老师课件
- 暑假预习:磁场对通电导线的作用力 -2025人教版新高二物理暑假专项提升
- 重庆培训母婴知识课件
- 建筑施工-安全培训课件-安全奖励效果分析
- 区镇智慧社区解决方案
- 醉翁亭记优翼课件
- 大模型和数据要素赋能智慧教育大数据平台解决方案
- 加拿大环球电讯宽带INTERNET分析
- 泄密案件整改报告范文
- 《报批报建工作》课件
- 船舶危险源辨识及防范措施
- 严重精神障碍患者报告卡
- 空气源热泵计算
- 学员陪跑合同模板
- 钢结构大棚安拆专项施工方案
- 消化内科护士进修总结汇报
- 人教版初中全部英语单词表(含音标)
- DL∕T 5461.11-2013 火力发电厂施工图设计文件内容深度规定 第11部分土建结构
- 《MH-T7003-2017民用运输机场安全保卫设施》
评论
0/150
提交评论