C语言课程设计俄罗斯方块源代码_第1页
C语言课程设计俄罗斯方块源代码_第2页
C语言课程设计俄罗斯方块源代码_第3页
C语言课程设计俄罗斯方块源代码_第4页
C语言课程设计俄罗斯方块源代码_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、1、 新建“.h”头文件,将“头文件”代码粘贴至其中,2、 新建“.c”源文件,将“源代码”代码粘贴到其中。3、 新建空白工程,将头文件和源代码添加进去,调试使用。/头文件/1.自定义枚举类型,定义7种形态的游戏方块typedefenumtetris_shapeZShape=0,SShape,LineShape,TShape,SquareShape,LShape,MirroredLShapeshape;/2.函数声明/(1)操作方块函数intmaxX();/取得当前方块的最大x坐标intminX();/取得当前方块的最小x坐标voidturn_left();/当前方块逆时针旋转90度voidt

2、urn_right();intout_of_table();voidtransform();intleftable();intrightable();intdownable();voidmove_left();voidmove_right();/(2)操作游戏桌面的函数intadd_to_table();voidremove_full();/(3)控制游戏函数voidnew_game();voidrun_game();voidnext_shape();intrandom(intseed);/(4)绘图函数voidpaint();voiddraw_table();/(5)其他功能函数voidke

3、y_down(WPARAMwParam);voidresize();voidinitialize();voidfinalize();/(6)回调函数,用来处理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)枚举游戏的状态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.主要处理函数/(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)逆时针转动方块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)顺时针旋转方块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)旋转方块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)创建新游戏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)取随机数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);/如果游戏是开始或结束状态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);/如果游戏是开始状态,用黄色字显示游戏开始画面if(state=game_start)str=APP_TITLE;SetTextColor(memDC,YELLOW);/如果游戏是结束状态,用红色字显示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)/如果游戏不是运行状态,按下回车键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;/如果游戏状态是运行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默认的消息处理函数处理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);/创建每个方块对应颜色的画笔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);/创建一个大字体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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论