C语言课程设计俄罗斯方块源代码.doc
《C语言课程设计俄罗斯方块源代码.doc》由会员分享,可在线阅读,更多相关《C语言课程设计俄罗斯方块源代码.doc(88页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateC语言课程设计俄罗斯方块源代码C语言课程设计俄罗斯方块源代码1、 新建“.h”头文件,将“头文件”代码粘贴至其中,2、 新建“.c”源文件,将“源代码”代码粘贴到其中。3、 新建空白工程,将头文件和源代码添加进去,调试使用。/头文件/1.自定义枚举类型,定义7种形态的游戏方块typedef enum tetris_shapeZShape=0,SShape,LineSh
2、ape,TShape,SquareShape,LShape,MirroredLShapeshape;/2.函数声明/(1)操作方块函数int maxX();/取得当前方块的最大x坐标int minX();/取得当前方块的最小x坐标void turn_left();/当前方块逆时针旋转90度void turn_right();int out_of_table();void transform();int leftable();int rightable();int downable();void move_left();void move_right();/(2)操作游戏桌面的函数int add
3、_to_table();void remove_full();/(3)控制游戏函数void new_game();void run_game();void next_shape();int random(int seed);/(4)绘图函数void paint();void draw_table();/(5)其他功能函数void key_down(WPARAM wParam);void resize();void initialize();void finalize();/(6)回调函数,用来处理Windows消息LRESULT CALLBACK WndProc (HWND,UINT,WPAR
4、AM,LPARAM);/源代码/1.文件包含#include#include#include#includetetris.h/2.常量定义#define APP_NAME TETRIS#define APP_TITLE Tetris Game#define GAMEOVER GAME OVER#define SHAPE_COUNT 7#define BLOCK_COUNT 4#define MAX_SPEED 5#define COLUMS 10#define ROWS 20#define RED RGB(255,0,0)#define YELLOW RGB(255,255,0)#define
5、 GRAY RGB(128,128,128)#define BLACK RGB(0,0,0)#define WHITE RGB(255,255,255)#define STONE RGB(192,192,192)#define CHARS_IN_LINE 14#define SCORE SCORE %4d/3.全局变量定义/(1)char score_charCHARS_IN_LINE=0;/(2)char* press_enter=Press Enter key.;/(3)帮助提示信息char *help=press space or up key to transform shape.,P
6、ress left or right key to mover shape.,Press down key to speed up.,Press enter key to pause game.,Enjoy it.:-),0;/(4)枚举游戏的状态enum game_stategame_start,game_run,game_pause,game_over,state=game_start;/(5)定义方块的颜色COLORREF shape_color=RGB(255,0,0),RGB(0,255,0),RGB(0,0,255),RGB(255,255,0),RGB(0,255,255),RG
7、B(255,0,255),RGB(255,255,255);/(6)方块的7中类型int shape_coordinateSHAPE_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)得分int score=0;/(8)下一个方块shape next=0;/(9)当前方块shape current=0;/(10)当前方块的每一部分坐标int current_c
8、oordinate42=0;/(11)游戏桌面int tableROWSCOLUMS=0;/(12)当前方块的x坐标int shapex=0;/(13)当前方块的y坐标int shapey=0;/(14)方块下移速度int speed=0;/(15)每一帧开始时间clock_t start=0;/(16)每一帧结束时间clock_t finish=0;/(17)windows绘图用变量HWND gameWND;HBITMAP memBM;HBITMAP memBMOld;HDC memDC;RECT clientRC;HBRUSH blackBrush;HBRUSH stoneBrush;HB
9、RUSH shapeBrushSHAPE_COUNT;HPEN grayPen;HFONT bigFont;HFONT smallFont;/4.主要处理函数/(1)取最大坐标int maxX()int i=0;int x=current_coordinatei0;int m=x;for(i=1;iBLOCK_COUNT;i+)x=current_coordinatei0;if(mx)m=x;return m;/(2)取最小坐标int minX()int i=0;int x=current_coordinatei0;int m=x;for(i=1;ix)m=x;return m;/(3)逆时针
10、转动方块void turn_left()int i=0;int x,y;for(i=0;i4;i+)x=current_coordinatei0;y=current_coordinatei1;current_coordinatei0=y;current_coordinatei1=-x;/(4)顺时针旋转方块void turn_right()int i=0;int x,y;for(i=0;i4;i+)x=current_coordinatei0;y=current_coordinatei1;current_coordinatei0=-y;current_coordinatei1=x;/(5)检查
11、方块是否越界int out_of_table()int i=0;int x,y;for(i=0;i4;i+)x=shapex+current_coordinatei0;y=shapey+current_coordinatei1;if(x(COLUMS-1)|y(ROWS-1)return 1;if(tableyx)return 1;return 0;/(6)旋转方块void transform()if(current=SquareShape)return ;turn_right();if(out_of_table()turn_left();/(7)判断方块是否向左移动int leftable(
12、)int i=0;int x,y;for(i=0;i4;i+)x=shapex+current_coordinatei0;y=shapey+current_coordinatei1;if(x=0|tableyx-1=1)return 0;return 1;/(8)判断方块是否向右移动int rightable()int i=0;int x,y;for(i=0;i=(COLUMS-1)|tableyx+1=1)return 0;return 1;/(9)判断方块是否向下移动int downable()int i=0;int x,y;for(i=0;i=(ROWS-1)|tabley+1x=1)r
13、eturn 0;return 1;/(10)向左移动当前方块void move_left()if(leftable()shapex-;/(11)向右移动当前方块void move_right()if(rightable()shapex+;/(12)向下移动当前方块void move_down()if(downable()shapey+;elseif(add_to_table()remove_full();next_shape();elsestate=game_over;/(13)将当前方块固定到桌面上int add_to_table()int i=0;int x,y;for(i=0;i4;i+
14、)x=shapex+current_coordinatei0;y=shapey+current_coordinatei1;if(y0;i-)c=0;for(j=0;j(MAX_SPEED-speed)*100)move_down();start=clock();InvalidateRect(gameWND,NULL,TRUE);/(17)操作当前方块void next_shape()current=next;memcpy(current_coordinate,shape_coordinatenext,sizeof(int)*BLOCK_COUNT*2);shapex=(COLUMS-(maxX
15、(current)-minX(current)/2;shapey=0;next=random(SHAPE_COUNT);/(18)取随机数 int random(int seed) if(seed=0) return 0; srand(unsigned)time(NULL); return (rand()%seed); /(19)绘图 void paint() PAINTSTRUCT ps; HDC hdc; draw_table(); hdc=BeginPaint(gameWND,&ps); BitBlt(hdc,clientRC.left,clientRC.top,clientRC.rig
16、ht,clientRC.bottom,memDC,0,0,SRCCOPY); EndPaint(gameWND,&ps); /(20)绘制游戏桌面 void draw_table() HBRUSH hBrushOld; HPEN hPenOld; HFONT hFontOld; RECT rc; int x0,y0,w; int x,y,i,j; char* str; w=clientRC.bottom/(ROWS+2); x0=y0=w; FillRect(memDC,&clientRC,blackBrush); / 如果游戏是开始或结束状态 if(state=game_start|stat
17、e=game_over) memcpy(&rc,&clientRC,sizeof(RECT); rc.bottom=rc.bottom/2; hFontOld=SelectObject(memDC,bigFont); SetBkColor(memDC,BLACK); /如果游戏是开始状态,用黄色字显示游戏开始画面 if(state=game_start) str=APP_TITLE; SetTextColor(memDC,YELLOW); /如果游戏是结束状态,用红色字显示GAME OVER else str=GAMEOVER; SetTextColor(memDC,RED); DrawTex
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 课程设计 俄罗斯方块 源代码
限制150内