1、C语言实战俄罗斯方块子函数#includeelsfkhead.h/背景数组char g_arrBackGroud2010 = 0 ;char g_arrsqare24 = 0 ;int g_nSqareID = -1;int g_nLine = -1;int g_nList = -1;int g_nScore = 0;void onpaint(HDC hdc)/函数实现 /创建兼容性DC HDC hMemDC = CreateCompatibleDC(hdc);/HMemDC一张纸的编号 HBITMAP hbitmapback= CreateCompatibleBitmap(hdc,500,6
2、00);/创建一张纸 SelectObject(hMemDC, hbitmapback);/将编号与纸关联起来 paintsqare(hMemDC); ShowSqare2(hMemDC); ShowScore(hMemDC); BitBlt(hdc, 0, 0, 500, 600, hMemDC, 0, 0, SRCCOPY);/传递 DeleteObject(hbitmapback);/释放位图 DeleteDC(hMemDC);/释放DCvoid ShowScore(HDC hMemDC) char strScore10 = 0 ; Rectangle(hMemDC, 300, 0, 5
3、00, 600); itoa(g_nScore, strScore,10); TextOut(hMemDC, 400, 10, 得分, strlen(得分); TextOut(hMemDC, 400, 32, strScore, strlen(strScore);void onCreate() srand(unsigned int)time(NULL); CreateRandomSqare(); CopySqareToBack();void paintsqare(HDC hMemDC)int i=0, j = 0; Rectangle(hMemDC, 0, 0, 300, 600); /窗口上
4、的内容,大方块 for (i = 0; i 20; i+) for (j = 0; j 10; j+) if (1 = g_arrBackGroudij) Rectangle(hMemDC, j*30, i*30, j*30+30, i*30+30); /画小格子 int CreateRandomSqare()/创建随机快 int nInedx =rand()%7; switch (nInedx) case 0:g_arrsqare00 = 1, g_arrsqare01 = 1, g_arrsqare02 = 0, g_arrsqare03 = 0; g_arrsqare10 = 0, g_
5、arrsqare11 = 1, g_arrsqare12 = 1, g_arrsqare13 = 0; g_nLine = 0; g_nList = 3; break; case 1:g_arrsqare00 = 0, g_arrsqare01 =1, g_arrsqare02 = 1, g_arrsqare03 = 0; g_arrsqare10 = 1, g_arrsqare11 = 1, g_arrsqare12 = 0, g_arrsqare13 = 0; g_nLine = 0; g_nList = 3; break; case 2:g_arrsqare00 = 1, g_arrsq
6、are01 = 0, g_arrsqare02 = 0, g_arrsqare03 = 0; g_arrsqare10 = 1, g_arrsqare11 = 1, g_arrsqare12 = 1, g_arrsqare13 = 0; g_nLine = 0; g_nList = 3; break; case 3:g_arrsqare00 = 0, g_arrsqare01 = 0, g_arrsqare02 =1, g_arrsqare03 = 0; g_arrsqare10 =1, g_arrsqare11 = 1, g_arrsqare12 = 1, g_arrsqare13 = 0;
7、 g_nLine = 0; g_nList = 3; break; case 4:g_arrsqare00 = 0, g_arrsqare01 = 1, g_arrsqare02 = 0, g_arrsqare03 = 0; g_arrsqare10 = 1, g_arrsqare11 = 1, g_arrsqare12 = 1, g_arrsqare13 = 0; g_nLine = 0; g_nList = 3; break; case 5:g_arrsqare00 = 0, g_arrsqare01 = 1, g_arrsqare02 = 1, g_arrsqare03 = 0; g_a
8、rrsqare10 = 0, g_arrsqare11 = 1, g_arrsqare12 = 1, g_arrsqare13 = 0; g_nLine = 0; g_nList = 4; break; case 6:g_arrsqare00 = 1, g_arrsqare01 = 1, g_arrsqare02 = 1, g_arrsqare03 =0; g_arrsqare10 = 0, g_arrsqare11 = 0, g_arrsqare12 = 0, g_arrsqare13 = 0; g_nLine = 0; g_nList = 3; break; g_nSqareID = nI
9、nedx; return nInedx;void CopySqareToBack( ) int i = 0, j = 0; for (i = 0; i 2; i+) for (j = 0; j = 0; i-) for (j = 0; j 10; j+) if (1 = g_arrBackGroudij) g_arrBackGroudi + 1j = g_arrBackGroudij; g_arrBackGroudij = 0; /定时器响应函数void OnTimer(HWND hwnd) HDC hdc = GetDC(hwnd);/内核对象 if (1 = CanSqaerDown()
10、& 1 = CanSqaerDown2() sqareDwon();/调用下落 g_nLine+; else Change1To2();/1变2 DestroyOneLineSqare(); if (0 = CanGameOver() /结束 KillTimer(hwnd, DEF_TIMER1); return ; CreateRandomSqare();/产生随机快 CopySqareToBack();/复制到背景 /调用显示 paintsqare(hdc); onpaint(hdc); ReleaseDC( hwnd,hdc);/内核对象释放int CanGameOver() int i
11、 = 0; for (i = 0; i 10; i+) if ( 2 = g_arrBackGroud0i) MessageBox(NULL, GameOver, 提示, MB_OK); return 0; return 1;int CanSqaerDown()/方块停在最底层,返回0不可以下落,1可以下落 int i = 0; for (i = 0; i 10; i+) if (1 = g_arrBackGroud19i) return 0; return 1;void Change1To2()/1变2 int i = 0, j = 0; for (i = 0; i 20; i+) for
12、(j = 0; j 10; j+) if (1 = g_arrBackGroudij) g_arrBackGroudij =2; /显示2void ShowSqare2(HDC hMemDC) int i = 0, j = 0; HBRUSH hOldBrush; HBRUSH hNewBrush = CreateSolidBrush(RGB(244, 38, 2); hOldBrush = (HBRUSH)SelectObject(hMemDC, hNewBrush); for (i = 0; i 20; i+) for (j = 0; j 0; i-) for (j = 0; j 10;
13、j+) if (1 = g_arrBackGroudij) if (2 = g_arrBackGroudi + 1j) return 0; return 1;void OnLeft(HWND hwnd) if (1 = CanSqaerLeft() & 1=CanSqaerLeft2() HDC hdc = GetDC(hwnd); /方块左移 sqareLeft(); g_nList-; /显示方块 onpaint(hdc); ReleaseDC(hwnd, hdc); void sqareLeft() int i = 0, j = 0; for (i = 0; i 20; i+) for
14、(j = 0; j 10; j+) if (1 = g_arrBackGroudij) g_arrBackGroudij-1 = g_arrBackGroudij; g_arrBackGroudij = 0; int CanSqaerLeft() int i = 0; for (i = 0; i 20; i+) if (1 = g_arrBackGroudi0) return 0; return 1;int CanSqaerLeft2() int i = 0, j = 0; for (i = 0; i 20; i+) for (j = 0; j 10; j+) if (1 = g_arrBac
15、kGroudij) if (2 = g_arrBackGroudij - 1) return 0; return 1;void OnRight(HWND hwnd) if (1 = CanSqaerRight() & 1 = CanSqaerRight2() HDC hdc = GetDC(hwnd); /方块右移 g_nList+; sqareRight(); onpaint(hdc); ReleaseDC(hwnd, hdc); void sqareRight() int i = 0, j = 0; for (i = 0; i = 0; j-) if (1 = g_arrBackGroud
16、ij) g_arrBackGroudij+1 = g_arrBackGroudij; g_arrBackGroudij = 0; int CanSqaerRight() int i = 0; for (i = 0; i 20; i+) if (1 = g_arrBackGroudi9) return 0; return 1;int CanSqaerRight2() int i = 0, j = 0; for (i = 0; i = 0; j-) if (1=g_arrBackGroudij) if (2 = g_arrBackGroudij+1) return 0; return 1;void
17、 OnDown(HWND hwnd) OnTimer( hwnd);void OnChangeSqare(HWND hwnd) HDC hdc = GetDC(hwnd); switch (g_nSqareID) case 0: case 1: case 2: case 3: case 4: /普通变形 if (1 = CanSqaerChangeShape() ChangeSqare(); else return; break; case 5:/正方形 return; case 6:/长条 /变形 if (CanLineSqaerChange() ChangeLineSqare(); bre
18、ak; onpaint(hdc); ReleaseDC(hwnd, hdc);void ChangeSqare() int i = 0, j = 0; int nTemp = 2; char arrSqare33 = 0 ; /背景块复制出来 for (i = 0; i 3; i+) for (j = 0; j 3; j+) arrSqareij = g_arrBackGroudg_nLine + ig_nList + j;/将背景当中的方块传递到小口袋 /变形后的方块数组复制回去 for (i = 0; i 3; i+) for (j = 0; j 3; j+) g_arrBackGroud
19、g_nLine + ig_nList + j = arrSqarenTempi; nTemp-; nTemp = 2; int CanSqaerChangeShape() int i = 0, j = 0; for (i = 0; i 3; i+) for (j = 0; j 3; j+) if (2 = g_arrBackGroudg_nLine + ig_nList + j) return 0; /if (g_nList9) / / return 0; / if (g_nList 9) g_nList = 7; return 1;void ChangeLineSqare() if (1 =
20、 g_arrBackGroudg_nLineg_nList - 1) /清零 g_arrBackGroudg_nLineg_nList - 1 = 0; g_arrBackGroudg_nLineg_nList + 1 = 0; g_arrBackGroudg_nLineg_nList + 2 = 0; if (2 = g_arrBackGroudg_nLine + 1g_nList) g_arrBackGroudg_nLine - 1g_nList = 1; g_arrBackGroudg_nLine - 2g_nList = 1; g_arrBackGroudg_nLine - 3g_nL
21、ist = 1; else if (2 = g_arrBackGroudg_nLine + 2g_nList) g_arrBackGroudg_nLine + 1g_nList = 1; g_arrBackGroudg_nLine - 1g_nList = 1; g_arrBackGroudg_nLine - 2g_nList = 1; else /元素赋值 g_arrBackGroudg_nLine - 1g_nList = 1; g_arrBackGroudg_nLine + 1g_nList = 1; g_arrBackGroudg_nLine + 2g_nList = 1; else
22、g_arrBackGroudg_nLine - 1g_nList = 0; g_arrBackGroudg_nLine + 1g_nList = 0; g_arrBackGroudg_nLine + 2g_nList = 0; if (2 = g_arrBackGroudg_nLineg_nList + 1|9=g_nList) g_arrBackGroudg_nLineg_nList - 1 = 1; g_arrBackGroudg_nLineg_nList - 2 = 1; g_arrBackGroudg_nLineg_nList - 3 = 1; g_nList = g_nList -
23、2; else if (2 = g_arrBackGroudg_nLineg_nList + 2|8= g_nList) g_arrBackGroudg_nLineg_nList + 1 = 1; g_arrBackGroudg_nLineg_nList - 1 = 1; g_arrBackGroudg_nLineg_nList - 2 = 1; g_nList = g_nList - 1; else if (2 = g_arrBackGroudg_nLineg_nList - 1 | 0 = g_nList) g_arrBackGroudg_nLineg_nList + 1 = 1; g_arrBackGroudg_nLineg_nList + 2 = 1; g_arrBackGroudg_nLineg_nList + 3 = 1; g_nList = g_nList + 1; else /元素复制 g_arrBackGroudg_nLineg_nList - 1 = 1; g_arrBackGroudg_nLineg_nList + 1 = 1; g_arrBack
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1