版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、1、 新建“.h”頭文件,將“頭文件”代碼粘貼至其中,2、 新建“.c”源文件,將“源代碼”代碼粘貼到其中。3、 新建空白工程,將頭文件和源代碼添加進去,調(diào)試使用。/頭文件/1.自定義枚舉類型,定義7種形態(tài)的游戲方塊typedefenumtetris_shapeZShape=0,SShape,LineShape,TShape,SquareShape,LShape,MirroredLShapeshape;/2.函數(shù)聲明/(1)操作方塊函數(shù)intmaxX();/取得當前方塊的最大x坐標intminX();/取得當前方塊的最小x坐標voidturn_left();/當前方塊逆時針旋轉(zhuǎn)90度voidt
2、urn_right();intout_of_table();voidtransform();intleftable();intrightable();intdownable();voidmove_left();voidmove_right();/(2)操作游戲桌面的函數(shù)intadd_to_table();voidremove_full();/(3)控制游戲函數(shù)voidnew_game();voidrun_game();voidnext_shape();intrandom(intseed);/(4)繪圖函數(shù)voidpaint();voiddraw_table();/(5)其他功能函數(shù)voidke
3、y_down(WPARAMwParam);voidresize();voidinitialize();voidfinalize();/(6)回調(diào)函數(shù),用來處理Windows消息LRESULTCALLBACKWndProc(HWND,UINT,WPARAM,LPARAM);/源代碼/1.文件包含#include<windows.h>#include<time.h>#include<stdio.h>#include"tetris.h"/2.常量定義#defineAPP_NAME"TETRIS"#defineAPP_TITL
4、E"TetrisGame"#defineGAMEOVER"GAMEOVER"#defineSHAPE_COUNT7#defineBLOCK_COUNT4#defineMAX_SPEED5#defineCOLUMS10#defineROWS20#defineREDRGB(255,0,0)#defineYELLOWRGB(255,255,0)#defineGRAYRGB(128,128,128)#defineBLACKRGB(0,0,0)#defineWHITERGB(255,255,255)#defineSTONERGB(192,192,192)#defi
5、neCHARS_IN_LINE14#defineSCORE"SCORE%4d"/3.全局變量定義/(1)charscore_charCHARS_IN_LINE=0;/(2)char*press_enter="PressEnterkey."/(3)幫助提示信息char*help="pressspaceorupkeytotransformshape.","Pressleftorrightkeytomovershape.","Pressdownkeytospeedup.","Pressent
6、erkeytopausegame.","Enjoyit.:-)",0;/(4)枚舉游戲的狀態(tài)enumgame_stategame_start,game_run,game_pause,game_over,state=game_start;/(5)定義方塊的顏色COLORREFshape_color=RGB(255,0,0),RGB(0,255,0),RGB(0,0,255),RGB(255,255,0),RGB(0,255,255),RGB(255,0,255),RGB(255,255,255);/(6)方塊的7中類型intshape_coordinateSHAPE
7、_COUNTBLOCK_COUNT2=0,1,0,0,-1,0,-1,1,0,-1,0,0,1,0,1,1,0,-1,0,0,0,1,0,2,-1,0,0,0,1,0,0,1,0,0,1,0,0,1,1,1,-1,-1,0,-1,0,0,0,1,1,-1,0,-1,0,0,0,1;/(7)得分intscore=0;/(8)下一個方塊shapenext=0;/(9)當前方塊shapecurrent=0;/(10)當前方塊的每一部分坐標intcurrent_coordinate42=0;/(11)游戲桌面inttableROWSCOLUMS=0;/(12)當前方塊的x坐標intshapex=0;/
8、(13)當前方塊的y坐標intshapey=0;/(14)方塊下移速度intspeed=0;/(15)每一幀開始時間clock_tstart=0;/(16)每一幀結束時間clock_tfinish=0;/(17)windows繪圖用變量HWNDgameWND;HBITMAPmemBM;HBITMAPmemBMOld;HDCmemDC;RECTclientRC;HBRUSHblackBrush;HBRUSHstoneBrush;HBRUSHshapeBrushSHAPE_COUNT;HPENgrayPen;HFONTbigFont;HFONTsmallFont;/4.主要處理函數(shù)/(1)取最大坐
9、標intmaxX()inti=0;intx=current_coordinatei0;intm=x;for(i=1;i<BLOCK_COUNT;i+)x=current_coordinatei0;if(m<x)m=x;returnm;/(2)取最小坐標intminX()inti=0;intx=current_coordinatei0;intm=x;for(i=1;i<BLOCK_COUNT;i+)x=current_coordinatei0;if(m>x)m=x;returnm;/(3)逆時針轉(zhuǎn)動方塊voidturn_left()inti=0;intx,y;for(i=
10、0;i<4;i+)x=current_coordinatei0;y=current_coordinatei1;current_coordinatei0=y;current_coordinatei1=-x;/(4)順時針旋轉(zhuǎn)方塊voidturn_right()inti=0;intx,y;for(i=0;i<4;i+)x=current_coordinatei0;y=current_coordinatei1;current_coordinatei0=-y;current_coordinatei1=x;/(5)檢查方塊是否越界intout_of_table()inti=0;intx,y;
11、for(i=0;i<4;i+)x=shapex+current_coordinatei0;y=shapey+current_coordinatei1;if(x<0|x>(COLUMS-1)|y>(ROWS-1)return1;if(tableyx)return1;return0;/(6)旋轉(zhuǎn)方塊voidtransform()if(current=SquareShape)return;turn_right();if(out_of_table()turn_left();/(7)判斷方塊是否向左移動intleftable()inti=0;intx,y;for(i=0;i<
12、;4;i+)x=shapex+current_coordinatei0;y=shapey+current_coordinatei1;if(x<=0|tableyx-1=1)return0;return1;/(8)判斷方塊是否向右移動intrightable()inti=0;intx,y;for(i=0;i<4;i+)x=shapex+current_coordinatei0;y=shapey+current_coordinatei1;if(x>=(COLUMS-1)|tableyx+1=1)return0;return1;/(9)判斷方塊是否向下移動intdownable()
13、inti=0;intx,y;for(i=0;i<4;i+)x=shapex+current_coordinatei0;y=shapey+current_coordinatei1;if(y>=(ROWS-1)|tabley+1x=1)return0;return1;/(10)向左移動當前方塊voidmove_left()if(leftable()shapex-;/(11)向右移動當前方塊voidmove_right()if(rightable()shapex+;/(12)向下移動當前方塊voidmove_down()if(downable()shapey+;elseif(add_to
14、_table()remove_full();next_shape();elsestate=game_over;/(13)將當前方塊固定到桌面上intadd_to_table()inti=0;intx,y;for(i=0;i<4;i+)x=shapex+current_coordinatei0;y=shapey+current_coordinatei1;if(y<0|tableyx=1)return0;tableyx=1;return1;/(14)刪除填滿的行voidremove_full()intc=0;inti,j;for(i=ROWS-1;i>0;i-)c=0;for(j
15、=0;j<COLUMS;j+)c+=tableij;if(c=COLUMS)memmove(table1,table0,sizeof(int)*COLUMS*i);memset(table0,0,sizeof(int)*COLUMS);score+;speed=(score/100)%MAX_SPEED;i+;elseif(c=0)break;/(15)創(chuàng)建新游戲voidnew_game()memset(table,0,sizeof(int)*COLUMS*ROWS);start=clock();next=random(SHAPE_COUNT);score=0;speed=0;/(16)
16、運行游戲voidrun_game()finish=clock();if(finish-start)>(MAX_SPEED-speed)*100)move_down();start=clock();InvalidateRect(gameWND,NULL,TRUE);/(17)操作當前方塊voidnext_shape()current=next;memcpy(current_coordinate,shape_coordinatenext,sizeof(int)*BLOCK_COUNT*2);shapex=(COLUMS-(maxX(current)-minX(current)/2;shape
17、y=0;next=random(SHAPE_COUNT);/(18)取隨機數(shù)intrandom(intseed)if(seed=0)return0;srand(unsigned)time(NULL);return(rand()%seed);/(19)繪圖voidpaint()PAINTSTRUCTps;HDChdc;draw_table();hdc=BeginPaint(gameWND,&ps);BitBlt(hdc,clientRC.left,clientRC.top,clientRC.right,clientRC.bottom,memDC,0,0,SRCCOPY);EndPaint
18、(gameWND,&ps);/(20)繪制游戲桌面voiddraw_table()HBRUSHhBrushOld;HPENhPenOld;HFONThFontOld;RECTrc;intx0,y0,w;intx,y,i,j;char*str;w=clientRC.bottom/(ROWS+2);x0=y0=w;FillRect(memDC,&clientRC,blackBrush);/如果游戲是開始或結束狀態(tài)if(state=game_start|state=game_over)memcpy(&rc,&clientRC,sizeof(RECT);rc.botto
19、m=rc.bottom/2;hFontOld=SelectObject(memDC,bigFont);SetBkColor(memDC,BLACK);/如果游戲是開始狀態(tài),用黃色字顯示游戲開始畫面if(state=game_start)str=APP_TITLE;SetTextColor(memDC,YELLOW);/如果游戲是結束狀態(tài),用紅色字顯示GAMEOVERelsestr=GAMEOVER;SetTextColor(memDC,RED);DrawText(memDC,str,strlen(str),&rc,DT_SINGLELINE|DT_CENTER|DT_BOTTOM);S
20、electObject(memDC,hFontOld);hFontOld=SelectObject(memDC,smallFont);rc.top=rc.bottom;rc.bottom=rc.bottom*2;if(state=game_over)SetTextColor(memDC,YELLOW);sprintf(score_char,SCORE,score);DrawText(memDC,score_char,strlen(score_char),&rc,DT_SINGLELINE|DT_CENTER|DT_TOP);SetTextColor(memDC,STONE);DrawT
21、ext(memDC,press_enter,strlen(press_enter),&rc,DT_SINGLELINE|DT_CENTER|DT_VCENTER);SelectObject(memDC,hFontOld);return;/桌面上殘留的方塊hBrushOld=SelectObject(memDC,stoneBrush);for(i=0;i<ROWS;i+)for(j=0;j<COLUMS;j+)if(tableij=1)x=x0+j*w;y=y0+i*w;Rectangle(memDC,x,y,x+w+1,y+w+1);SelectObject(memDC,h
22、BrushOld);/畫當前的方塊hBrushOld=SelectObject(memDC,shapeBrushcurrent);for(i=0;i<4;i+)x=x0+(current_coordinatei0+shapex)*w;y=y0+(current_coordinatei1+shapey)*w;if(x<x0|y<y0)continue;Rectangle(memDC,x,y,x+w+1,y+w+1);SelectObject(memDC,hBrushOld);/畫桌面上的表格線hPenOld=SelectObject(memDC,grayPen);for(i=0
23、;i<=ROWS;i+)MoveToEx(memDC,x0,y0+i*w,NULL);LineTo(memDC,x0+COLUMS*w,y0+i*w);for(i=0;i<=COLUMS;i+)MoveToEx(memDC,x0+i*w,y0,NULL);LineTo(memDC,x0+i*w,y0+ROWS*w);SelectObject(memDC,hPenOld);/畫玩家得分x0=x0+COLUMS*w+3*w;y0=y0+w;hFontOld=SelectObject(memDC,smallFont);SetTextColor(memDC,YELLOW);sprintf(
24、score_char,SCORE,score);TextOut(memDC,x0,y0,score_char,strlen(score_char);/畫下一個方塊y0+=w;SetTextColor(memDC,STONE);TextOut(memDC,x0,y0,"NEXT",4);x0+=w;y0+=2*w;hBrushOld=SelectObject(memDC,shapeBrushnext);for(i=0;i<4;i+)x=x0+shape_coordinatenexti0*w;y=y0+shape_coordinatenexti1*w;Rectangle(
25、memDC,x,y,x+w+1,y+w+1);SelectObject(memDC,hBrushOld);/打印幫助信息x0=(COLUMS+2)*w;y0+=4*w;SetTextColor(memDC,GRAY);i=0;while(helpi)TextOut(memDC,x0,y0,helpi,strlen(helpi);y0+=w;i+;SelectObject(memDC,hFontOld);/(21)處理按鍵voidkey_down(WPARAMwParam)/如果游戲不是運行狀態(tài),按下回車鍵if(state!=game_run)if(wParam=VK_RETURN)switch
26、(state)casegame_start:next_shape();state=game_run;break;casegame_pause:state=game_run;break;casegame_over:new_game();next_shape();state=game_run;break;/如果游戲狀態(tài)是運行elseswitch(wParam)caseVK_SPACE:caseVK_UP:transform();break;caseVK_LEFT:move_left();break;caseVK_RIGHT:move_right();break;caseVK_DOWN:move_d
27、own();break;caseVK_RETURN:state=game_pause;break;InvalidateRect(gameWND,NULL,TRUE);/(22)改變窗口大小voidresize()HDChdc;LOGFONTlf;hdc=GetDC(gameWND);GetClientRect(gameWND,&clientRC);SelectObject(memDC,memBMOld);DeleteObject(memBM);memBM=CreateCompatibleBitmap(hdc,clientRC.right,clientRC.bottom);memBMOl
28、d=SelectObject(memDC,memBM);DeleteObject(bigFont);memset(&lf,0,sizeof(LOGFONT);lf.lfWidth=(clientRC.right-clientRC.left)/CHARS_IN_LINE;lf.lfHeight=(clientRC.bottom-clientRC.top)/4;lf.lfItalic=1;lf.lfWeight=FW_BOLD;bigFont=CreateFontIndirect(&lf);DeleteObject(smallFont);lf.lfHeight=clientRC.b
29、ottom/(ROWS+2);lf.lfWidth=lf.lfHeight/2;lf.lfItalic=0;lf.lfWeight=FW_NORMAL;smallFont=CreateFontIndirect(&lf);ReleaseDC(gameWND,hdc);/(23)處理消息LRESULTCALLBACKWndProc(HWNDhwnd,UINTmessage,WPARAMwParam,LPARAMlParam)switch(message)caseWM_SIZE:resize();return0;caseWM_ERASEBKGND:return0;caseWM_PAINT:p
30、aint();return0;caseWM_KEYDOWN:key_down(wParam);return0;caseWM_DESTROY:PostQuitMessage(0);return0;/其他消息用Windows默認的消息處理函數(shù)處理returnDefWindowProc(hwnd,message,wParam,lParam);/(24)初始化voidinitialize()LOGFONTlf;HDChdc;inti;hdc=GetDC(gameWND);GetClientRect(gameWND,&clientRC);memDC=CreateCompatibleDC(hdc)
31、;memBM=CreateCompatibleBitmap(hdc,clientRC.right,clientRC.bottom);memBMOld=SelectObject(memDC,memBM);blackBrush=CreateSolidBrush(BLACK);stoneBrush=CreateSolidBrush(STONE);/創(chuàng)建每個方塊對應顏色的畫筆for(i=0;i<SHAPE_COUNT;i+)shapeBrushi=CreateSolidBrush(shape_colori);grayPen=CreatePen(PS_SOLID,1,GRAY);memset(&a
32、mp;lf,0,sizeof(LOGFONT);/創(chuàng)建一個大字體lf.lfWidth=(clientRC.right-clientRC.left)/CHARS_IN_LINE;lf.lfHeight=(clientRC.bottom-clientRC.top)/4;lf.lfItalic=0;lf.lfWeight=FW_NORMAL;smallFont=CreateFontIndirect(&lf);ReleaseDC(gameWND,hdc);/(25)釋放資源voidfinalize()inti=0;DeleteObject(blackBrush);DeleteObject(stoneBrush);for(i=0;i<SHAPE_COUNT;i+)DeleteObject(shapeBrushi);DeleteObjec
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 醫(yī)師事跡范文(5篇)
- 《環(huán)保節(jié)能培訓》課件
- 辦公室產(chǎn)品展會市場分析報告
- 辦公環(huán)境中小學語文學習的價值
- 《次施工準備工作》課件
- 農(nóng)業(yè)科技系統(tǒng)在醫(yī)療健康領域的創(chuàng)新應用
- 2025建筑工程分包合同
- 2025附條件贈與合同 標準版模板全
- 2025中國銀行勞動合同范本
- 卷煙配件行業(yè)深度研究報告
- 2024年安徽省廣播電視行業(yè)職業(yè)技能大賽(有線廣播電視機線員)考試題庫(含答案)
- 山東省濟南市濟陽區(qū)三校聯(lián)考2024-2025學年八年級上學期12月月考語文試題
- 糖尿病酮酸癥中毒
- 《玉米種植技術》課件
- 2023年聊城市人民醫(yī)院招聘備案制工作人員筆試真題
- Unit 6 Food Lesson 1(說課稿)-2024-2025學年人教精通版(2024)英語三年級上冊
- 東北師大附屬中學2025屆高一物理第一學期期末質(zhì)量檢測試題含解析
- 收費站微笑服務培訓
- GB/T 44570-2024塑料制品聚碳酸酯板材
- 雨的形成課件教學課件
- 金蛇納瑞2025年公司年會通知模板
評論
0/150
提交評論