首页 c语言俄罗斯方块游戏程序设计报告

c语言俄罗斯方块游戏程序设计报告

举报
开通vip

c语言俄罗斯方块游戏程序设计报告c语言俄罗斯方块游戏程序设计报告 C语言课程设计报告 主标题: C语言课程设计 副标题: 俄罗斯方块游戏 ----界面设计 姓 名: 指导教师: 院 系: 专 业: 班 级: 小组成员: 提交日期 俄罗斯方块程序设计报告 Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness,...

c语言俄罗斯方块游戏程序设计报告
c语言俄罗斯方块游戏程序设计 报告 软件系统测试报告下载sgs报告如何下载关于路面塌陷情况报告535n,sgs报告怎么下载竣工报告下载 C语言课程设计报告 主标题: C语言课程设计 副标题: 俄罗斯方块游戏 ----界面设计 姓 名: 指导教师: 院 系: 专 业: 班 级: 小组成员: 提交日期 俄罗斯方块程序设计报告 Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 一、 问题描述: 要求支持键盘操作和7种不同类型方块的旋转变换,并且界面上显示下一个方块的提示以及当前的玩家的得分,随着游戏的进行,等级越高,游戏难度越大,即方块的下落速度越快,相应的等级,等级越高,消去一行所得到的分数越高,为玩家提供了不同的选择。 二、 功能分析: 俄罗斯方块游戏需要解决的问题包括: ?按任意键开始游戏,随机产生方块并自动下移 ?用Esc键退出游戏。 ?用 键变换方块 ?用 键和 键左右移动方块 ?用 键使方块加速下移 ?用空格键使方块直接下移 ?能正确判断满行并消行、计分、定级别 ?能正确计时 ?设定游戏为不同级别,级别越高难度越大 重点: *游戏面包的数据结构:二维数组 *7种形状方块的数据结构:结构体保存每种形状方块的坐标、颜色 三、程序设计: 1、程序总体设计结构:首先初始化进入图形模式,进入欢迎界面,玩家按任意进入主菜单界面,按键进入游戏界面,键然后设置新的时钟中断。开始游戏后,进入该程序最核心的部分——处理和实现进行过程中的各种事件和函数。在处理中判断游戏是否结束,如果没有结束,则重新开始游戏,否则结束游戏。 Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 俄罗斯方块程序设计报告2016-11-24 详解如下: (1)、游戏方块预览功能。在游戏过程中,当在游戏底板中出现一个游戏方 块时,必须在游戏方块预览区域中出现下一个游戏方块,这样有利于游戏玩 家控制游戏的策略。由于在此游戏中存在19种不同的游戏方块,所以在游 戏方块预览区域中需要显示随机生成的游戏方块。 (2)、游戏方块控制功能。通过各种条件的判断,实现对游戏方块的左移、 右移、快速下移、自由下落、旋转功能,以及行满消除行的功能。 俄罗斯方块游戏 初始化图形模式 (欢迎界面) 进入菜单选项 开 游始游游 退 戏新戏戏 出 帮游排说 游 助 戏 名 明 戏 开始游戏 11Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consc计本,2,班 卢文俊 iousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 游游游游游 戏戏戏戏戏 速结显方方 度束示块块 分退更预控 数出新览制 功更功功功否 能 新能 能 能 功 能 游戏是否结束 是 恢复原有的时钟中断 关闭图形模式返回菜单 游戏执行主 流程 快递问题件怎么处理流程河南自建厂房流程下载关于规范招聘需求审批流程制作流程表下载邮件下载流程设计 图 (3)、游戏显示更新功能。在判断键值时,有左移VK_LEFT、右移VK_RIGHT、下移VK_DOWN、变形旋转VK_UP、退出VK_ESC键值的判断。当游戏方块左右移动、下落、旋转时,要清除先前的游戏方块,用新坐标重绘游戏方块。当消除满行时,要重绘游戏底板的当前状态。 (4)、游戏速度分数更新功能。在游戏玩家进行游戏过程中,需要按照一定的游戏规则给玩家计算游戏分数。比如,消除一行加10分。当游戏分数达到一定Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 俄罗斯方块程序设计报告2016-11-24 数量之后,需要给游戏者进行等级的上升,每上升一个等级,游戏方块的下落速 度将加快,游戏的难度将增加。 (5)、游戏帮助功能。玩家进入游戏后,将有对本游戏如何操作的友情提示。 主函数: void main() { InitializeGraph(); SetTimer(newtimer); /*设置新的时钟中断*/ while(1) { StartGame(); ProcessInGame(); if(GameOver()) break; bOver = FALSE; } KillTimer(); closegraph(); } 2、界面设计 分为左右两个部分: *左边为游戏面板 *右边有三部分:下一个形状提示框、速度框和计分框 3、重要数据的数据结构设计 11Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consc计本,2,班 卢文俊 iousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 1)定义方块形状: 定义如下的结构体来表示每一个形状: struct block{ int arrXY[8]; int nColor; int nNext; }; /*保存某一形状信息的结构体*/ Struct SHAPE shapes[MAX_BOX]= { 口 口口口 口口 口 口 口 口 口口口 口口 口 {0x88, 0xc0, CYAN, 1}, {0xe8, 0x0, CYAN, 2}, {0xc4, 0x40, CYAN, 3}, {0x2e, 0x0, CYAN, 0}, 口 口口 口口口 口 口 口 口 口口 口口口 口 {0x44, 0xc0, MAGENTA, 5}, {0x8e, 0x0, MAGENTA, 6}, {0xc8, 0x80, MAGENTA, 7}, {0xe2, 0x0, MAGENTA, 4}, Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 俄罗斯方块程序设计报告2016-11-24 口 口口 口口 口 口口 {0x8c, 0x40, YELLOW, 9}, {0x6c, 0x0, YELLOW, 8}, 口 口口 口口 口口 口 {0x4c, 0x80, BROWN, 11}, {0xc6, 0x0, BROWN, 10}, 口 口 口 口口口 口口 口口口 口口 口 口 口 {0x4e, 0x0, WHITE, 13}, {0x8c, 0x80, WHITE, 14}, {0xe4, 0x0, WHITE, 15}, {0x4c, 0x40, WHITE, 12}, 口 口 口 口口口口 口 {0x88, 0x88, RED, 17}, {0xf0, 0x0, RED, 16}, 11Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consc计本,2,班 卢文俊 iousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 口口 口口 {0xcc, 0x0, BLUE, 18), } 2)、定义游戏的主界面:宽10、高20的游戏板 1、 数据结构:全局数组Gameboard[12][22],1表示已有的方块,0表示这个位置空着。在10*20基础上各自加2行、2列为了便于判断形状在移动时是否到边、到底。整个屏幕的坐标系原先为640*480。在此游戏中,将16个像素定义为一个方格的边长,所以坐标系转变成为了40*30(640/16=40,480/10=30)。 2、 玩家进行游戏时,需要对游戏界面进行初始化工作。此代码被main()函数调用。主要进行的工作如下: (1) 循环调用line()函数绘制当前游戏板。 (2) 调用nScore()函数显示初始的成绩,初始成绩为0。 (3) 调用npeed()函数显示初始的速度(等级),初始速度1。 **************************************************** * 注:x,y为左上角坐标 * * m,n对应于Vertical_boxs,Horizontal_boxs * * 分别表示纵横方向上方块的个数(以方块为单位) ** BOARD_LEFT_X ,BOARD_LEFT_Y ***************************************************** 4、函数设计 1、本程序有主函数和个函数组成:本程序总共由24个函数组成。 2、函数相互作用关系见下图 、 初始化模式进 入欢迎界 win() Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 游戏介绍help () 主菜单 menu() 俄罗斯方块程序设计报告2016-11-24 初始化图形模式 InitializeGraph() 设置新的时钟中断 Interrupt() SetTimer(* (void)) (*IntProc) (void) 游戏是否结束 11Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consc计本,2,班 卢文俊 iousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability GameOver() 否 是 恢复原来的时钟中断 KillTimer() 关闭图形模型 返回到主菜单 menu() 退出程序 四、函数功能的描述: 五、 函数原型 函数功能 函数处理描述 Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 俄罗斯方块程序设计报告2016-11-24 void interrupt 新的时钟中调用(*oldtimer)()newtimer(void) 断处理函数 函数语句柄,计时器 自动加1 void SetTimer(void 指向原来时 interrupt(*IntProc)(void)) 钟中断处理 过程入口的 中断处理函 数(句柄) void KillTimer() 恢复原有的调用setvect()恢复原 时钟中断处有的时钟中断处理 理过程 过程 void InitializeGraph() 初始化图形1、调用initgraph() 模式 切换到图形模式2、 初始化若发生错误, 则返回错误密码 void 初始化游戏1、调用 InitializeGameboard() 面板以及下bar()\rectangle()等库 一形状提示函数绘图函数绘制 框、计分框和游戏界面2、调用 难度框 outtextxy()函数显示 文字 void DrawSquare(int x, int 在坐标(x,y)调用库函数bar()绘 11Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consc计本,2,班 卢文俊 iousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability y) 处画方块 制方块 void DrawBlock(int 在坐标调用DrawSqueare() BlockIndex, int sx, int (sx,sy)处绘函数绘制形状 sy,int color) 制颜色为 color的形状 int IsConflict(int 判断形状是无冲突返回0,有冲BlockIndex, int x, int y) 否存在于坐突返回1 标(x,y)处 void HandleLeft(int 按下左方向调用IsConflic()函数BlockIndex,int *x, int *y) 键时的处理判断形状是否可以 函数 左移, void HandleRight(int 按下右方向调用IsConflic()函数BlockIndex,int *x, int *y) 键时的处理判断形状是否可以 函数 右移, void HandleUp(int 按下上方向调用IsConflic()函数*BlockIndex,int *x, int 键(旋转键)判断形状是否可以*y) 时的处理函旋转, 数 int HandleDown(int 按下向下方形状在自由下落返BlockIndex,int *x, int *y) 向键或者自回0,无法下落了返 由下落时的回1,并做相应处理 处理函数 Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 俄罗斯方块程序设计报告2016-11-24 int IsLineFull(int y) 判断y行是否填满返回1,否则返 已被填满 回0 void KillLine(int y) 消去第y行 函数用于处理删除 一满行的情况。Y指 明具体哪一行为满 行,并将消去的行置 为背景色 int KillLines(int y) 消去第y行以返回消去的行数 及与第y行连 续的上面被 填满的行 int IsGameOver() 判断游戏是游戏结束返回1,否 否结束 则返回0 int GameOver() 在界面上输退出游戏返回1,否 出游戏结束则返回0 的信息,并根 据用户按键 选择决定是 否退出游戏 void StartGame() 游戏开始时1、绘制界面需要调 调用的函数 用函数 InitializeGameboard( 11Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consc计本,2,班 卢文俊 iousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability )2、接下来初始化游 戏面板的各个方块 和一些全局变量的 初值。 void ProcessInGame() 核心函数,主调用HandleUpO、 要用于处理HandleDown()、 在游戏中的HandleLeft()、 各种事件(如HandleRight()等事件 按下各种按处理函数 键) Void main() 主流程函数 主函数入口,整个游 戏的主控部分 五、运行效果 Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 俄罗斯方块程序设计报告2016-11-24 六、源代码详解 #include #include #include #include /*系统提供的头文件*/ #define TIMER 0x1c /*定义时钟中断的中断号*/ #define VK_LEFT 0x4b00/*左移键*/ #define VK_RIGHT 0x4d00/*右移键*/ #define VK_DOWN 0x5000 /*加速键*/ #define VK_UP 0x4800 /*变形键*/ #define VK_SPACE 0x3920 /*变形键*/ 11Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consc计本,2,班 卢文俊 iousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability #define VK_END 0x4f00 /*暂停键*/ #define VK_ESC 0x011b #define VK_ENTER 0x1c0d #define BSIZE 16 /*方块的边长是16个象素*/ #define MAX_SHAPE 19 /*总共有19种各形态的方块*/ #define BOARD_WIDTH 10 /*游戏面板的宽度,以方块的宽度为单位*/ #define BOARD_HEIGHT 20/*游戏面板的高度,以方块的宽度为单位*/ #define BGCOLOR BLACK /*背景色*/ #define FORECOLOR WHITE /*前景色*/ #define FALSE 0 #define TRUE 1 #define EMPTY 0 #define FILLED 1 #define BOARD_LEFT_X 10 /*游戏面板左上角的横坐标*/ #define BOARD_LEFT_Y 5 /*游戏面板左上角的纵坐标*/ /*定义全局变量*/ extern int Gameboard[BOARD_WIDTH+2][BOARD_HEIGHT+2]; extern int nCurrent_block_index ; /*当前下落的方块的索引号*/ extern int nNext_block_index ; /*下一个方块的索引号*/ extern int nSpeed, nScore; /*速度和得分*/ extern int nSpeedUpScore; /*第一次要加速需达到的分数*/ extern int bAccel, bOver; extern int nOriginX, nOriginY;/*某一形状的原点的绝对坐标*/ extern unsigned int TimerCounter; /* 计时变量,每秒钟增加18 */ struct block{ int arrXY[8]; int nColor; int nNext; }; /*保存某一形状信息的结构体*/ typedef struct block BLOCK; extern BLOCK arrayBlock[19]; void interrupt newtimer(void);/*新的时钟中断处理函数*/ void SetTimer(void interrupt(*IntProc)(void));/*设置新的时钟中断处理过程*/ Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 俄罗斯方块程序设计报告2016-11-24 void KillTimer();/*恢复原有的时钟中断处理过程*/ void InitializeGraph();/*初始化图形模式*/ void InitializeGameboard() ;/*初始化游戏面板*/ void DrawSquare(int x, int y);/*在坐标(x,y)处画方块*/ void DrawBlock(int BlockIndex, int sx, int sy,int color);/*在(sx,sy)处绘制颜色为 color的形状*/ int IsConflict(int BlockIndex, int x, int y);/*判断形状能否存在于坐标(x,y)处*/ void HandleLeft(int BlockIndex,int *x, int *y);/*左键处理函数*/ void HandleRight(int BlockIndex,int *x, int *y);/*右键处理函数*/ void HandleUp(int *BlockIndex,int *x, int *y);/*上键处理函数*/ int HandleDown(int BlockIndex,int *x, int *y);/*下键处理函数*/ int IsLineFull(int y);/*判断y行是否填满*/ void KillLine(int y);/*消去y行*/ int KillLines(int y);/*消去y行及上面被填满的行*/ int IsGameOver();/*结束游戏*/ int GameOver();/*用户自己决定是否结束游戏*/ void StartGame();/*开始游戏*/ void ProcessInGame();/*处理游戏中各种事件*/ /********************************************************** * 函数原型:void InitializeGraph() * * 传入参数:无 * * 返 回 值:无 11Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consc计本,2,班 卢文俊 iousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability * * 函数功能:初始化进入图形模式 * **********************************************************/ void InitializeGraph() { int gdriver = VGA, gmode=VGAHI, errorcode; /* 初始化图形模式*/ initgraph(&gdriver, &gmode, "c:\\turboc2"); /* 读取初始化结果 */ errorcode = graphresult(); if (errorcode != grOk) /* 错误发生 */ { printf("Graphics error: %s\n", grapherrormsg(errorcode)); printf("Press any key to halt:"); getch(); exit(1); /* 返回错误码 */ } } /********************************************************** * 函数原型:void InitializeGameboard() * * 传入参数:无 * * 返 回 值:无 * * 函数功能:初始化游戏面板以及下一形状提示框、计分框和难度框 * **********************************************************/ void InitializeGameboard() { /* 绘制游戏面板(即游戏区域)*/ setfillstyle(SOLID_FILL,BGCOLOR); bar(BSIZE*BOARD_LEFT_X,BSIZE*BOARD_LEFT_Y,BSIZE*(BOARD_LEFT_X+BOARD_WIDTH),BSIZE*(BOARD_LEFT_Y+BOARD_HEIGHT)); setcolor(WHITE); rectangle(BSIZE*BOARD_LEFT_X,BSIZE*BOARD_LEFT_Y,BSIZE*(BOARD_LEFT_X+BOARD_WIDTH),BSIZE*(BOARD_LEFT_Y+BOARD_HEIGHT)); /*绘制下一形状提示框*/ setcolor(BLUE); Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 俄罗斯方块程序设计报告2016-11-24 settextjustify(CENTER_TEXT, BOTTOM_TEXT); outtextxy(BSIZE*(25+4), BSIZE*(5+1), "next"); setfillstyle(SOLID_FILL, BGCOLOR); bar(BSIZE*(24.5+2), BSIZE*6, BSIZE*(24.5+2+5), BSIZE*(6+5)); setcolor(YELLOW); rectangle(BSIZE*(24.5+2), BSIZE*6, BSIZE*(24.5+2+5), BSIZE*(6+5)); /*绘制速度框*/ setcolor(BLUE); settextjustify(CENTER_TEXT, BOTTOM_TEXT); outtextxy(BSIZE*(25+4), BSIZE*(12+1), "level"); setfillstyle(SOLID_FILL, BGCOLOR); bar(BSIZE*25,BSIZE*13, BSIZE*(25+8), BSIZE*(13+1)); setcolor(YELLOW); rectangle(BSIZE*25,BSIZE*13, BSIZE*(25+8), BSIZE*(13+1)); setcolor(RED); settextjustify(CENTER_TEXT, BOTTOM_TEXT); outtextxy(BSIZE*(25+4), BSIZE*(13+1), "0"); /*绘制计分框*/ setcolor(BLUE); settextjustify(CENTER_TEXT, BOTTOM_TEXT); outtextxy(BSIZE*(25+4), BSIZE*(19+1), "score"); setfillstyle(SOLID_FILL, BGCOLOR); bar(BSIZE*25,BSIZE*20, BSIZE*(25+8), BSIZE*(20+1)); setcolor(YELLOW); rectangle(BSIZE*25,BSIZE*20, BSIZE*(25+8), BSIZE*(20+1)); setcolor(RED); settextjustify(CENTER_TEXT, BOTTOM_TEXT); outtextxy(BSIZE*(25+4), BSIZE*(20+1), "0"); } int Gameboard[BOARD_WIDTH+2][BOARD_HEIGHT+2]; int nCurrent_block_index;/* 当前下落的方块的索引号*/ int nNext_block_index ; /*下一个方块的索引号*/ int nSpeed, nScore; /*速度和得分*/ int nSpeedUpScore = 1000; /*第一次要加速需达到的分数*/ int bAccel, bOver; int nOriginX=5, nOriginY=1;/*某一形状的原点的绝对坐标*/ BLOCK arrayBlock[19]={ /*x1,y1,x2,y2,x3,y3,x4,y4, color, next*/ { 0,-2, 0,-1, 0, 0, 1, 0, CYAN, 1}, /* */ {-1, 0, 0, 0, 1,-1, 1, 0, CYAN, 2}, /* # */ 11Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consc计本,2,班 卢文俊 iousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability { 0,-2, 1,-2, 1,-1, 1, 0, CYAN, 3}, /* # */ {-1,-1,-1, 0, 0,-1, 1,-1, CYAN, 0}, /* ## */ { 0,-2, 0,-1, 0, 0, 1,-2,MAGENTA, 5}, /* */ {-1,-1,-1, 0, 0, 0, 1, 0,MAGENTA, 6}, /* ## */ { 0, 0, 1,-2, 1,-1, 1, 0,MAGENTA, 7}, /* # */ {-1,-1, 0,-1, 1,-1, 1, 0,MAGENTA, 4}, /* # */ {-1, 0, 0,-1, 0, 0, 1, 0,YELLOW, 9}, /* */ {-1,-1, 0,-2, 0,-1, 0, 0,YELLOW, 10}, /* */ {-1,-1, 0,-1, 0, 0, 1,-1,YELLOW, 11}, /* # */ { 0,-2, 0,-1, 0, 0, 1,-1,YELLOW, 8}, /* ### */ {-1, 0, 0,-1, 0, 0, 1,-1, BROWN, 13}, /* ## */ { 0,-2, 0,-1, 1,-1, 1, 0, BROWN, 12}, /* ## */ {-1,-1, 0,-1, 0, 0, 1, 0, WHITE, 15}, /* ## */ { 0,-1, 0, 0, 1,-2, 1,-1, WHITE, 14}, /* ## */ { 0,-3, 0,-2, 0,-1, 0, 0, RED, 17},/* # */ {-1, 0, 0, 0, 1, 0, 2, 0, RED, 16},/* # */ /* # */ /* # */ { 0,-1, 0, 0, 1,-1, 1, 0, BLUE, 18},/* ## */ /* ## */ }; /********************************************************** * 函数原型:void StartGame () * * 传入参数:无 * * 返 回 值:无 * * 函数功能:游戏开始时调用的函数,其中绘制界面需调用函数 * * InitializeGameboard(), 接下来需初始化游戏面板的 * * 各个方块和一些全局变量的初值 * **********************************************************/ void StartGame() { int i,j; Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 俄罗斯方块程序设计报告2016-11-24 /*设置游戏面板中每个方块的初始值*/ for(j=0;j<=BOARD_HEIGHT;j++) for(i=0;i(20-nSpeed*2)) if(HandleDown(nCurrent_block_index,&nOriginX,&nOriginY)) break; if(bOver) return; } } } /********************************************************** * 函数原型:void main() * * 传入参数:无 * * 返 回 值:无 * * 函数功能:入口函数,包含俄罗斯方块程序的主流程 * **********************************************************/ void main() { InitializeGraph(); SetTimer(newtimer); /*设置新的时钟中断*/ while(1) { StartGame(); ProcessInGame(); if(GameOver()) break; bOver = FALSE; } KillTimer(); closegraph(); } unsigned int TimerCounter=0; /* 计时变量,每秒钟增加18 */ /********************************************************** * 函数原型:void interrupt (*oldtimer)(void) * 11Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consc计本,2,班 卢文俊 iousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability * 传入参数:无 * * 返 回 值:无 * * 函数功能:指向原来时钟中断处理过程入口的中断处理函数指针(句柄) * **********************************************************/ void interrupt (*oldtimer)(void); /********************************************************** * 函数原型:void interrupt newtimer(void) * * 传入参数:无 * * 返 回 值:无 * * 函数功能:新的时钟中断处理函数 * **********************************************************/ void interrupt newtimer(void) { (*oldtimer)(); TimerCounter++; } /********************************************************** * 函数原型:void SetTimer(void interrupt(*)(void)) * * 传入参数:无 * * 返 回 值:无 * * 函数功能:设置新的时钟中断处理函数 * **********************************************************/ void SetTimer(void interrupt(*IntProc)(void)) { oldtimer=getvect(TIMER); disable(); setvect(TIMER,IntProc); enable(); } /********************************************************** * 函数原型:void KillTimer() * * 传入参数:无 * * 返 回 值:无 * Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 俄罗斯方块程序设计报告2016-11-24 * 函数功能:恢复原先的时钟中断处理函数 * **********************************************************/ void KillTimer() { disable(); setvect(TIMER,oldtimer); enable(); } /********************************************************** * 函数原型:void DrawSquare(int x, int y) * * 传入参数:游戏面板中的横坐标x,纵坐标y * * 返 回 值:无 * * 函数功能:在坐标(x, y)处绘制方块 * **********************************************************/ void DrawSquare(int x, int y) { if(y<1) return; bar(BSIZE*(x+9)+1,BSIZE*(y+4)+1,BSIZE*(x+10)-1,BSIZE*(y+5)-1); } /********************************************************** * 函数原型:void DrawBlock(int BlockIndex, int sx, int sy,int color) * * 传入参数:形状的索引BlockIndex,绝对横坐标x,绝对纵坐标y,颜色color * * 返 回 值:无 * * 函数功能:在坐标(sx, sy)处绘制颜色为color的形状 * **********************************************************/ void DrawBlock(int BlockIndex, int sx, int sy,int color) { int i,c; setfillstyle(SOLID_FILL, color); for(i=0;i<7;i+=2) DrawSquare(arrayBlock[BlockIndex].arrXY[i]+sx, arrayBlock[BlockIndex].arrXY[i+1]+sy); } /********************************************************** * 函数原型:int IsConflict(int BlockIndex, int x, int y) * * 传入参数:形状的索引BlockIndex,绝对横坐标x,绝对纵坐标y * 11Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consc计本,2,班 卢文俊 iousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability * 返 回 值:无冲突返回0,有冲突返回1 * * 函数功能:判断形状是否能存在于坐标(x, y)处 * **********************************************************/ int IsConflict(int BlockIndex, int x, int y) { int i; for (i=0;i<=7;i++,i++) { if (arrayBlock[BlockIndex].arrXY[i]+x<1 || arrayBlock[BlockIndex].arrXY[i]+x>10) return TRUE; if (arrayBlock[BlockIndex].arrXY[i+1]+y<1) continue; if (Gameboard[arrayBlock[BlockIndex].arrXY[i]+x][arrayBlock[BlockIndex].arrXY[i+ 1]+y]) return TRUE; } return FALSE; } /********************************************************** * 函数原型:int HandleLeft(int BlockIndex,int *x, int *y) * * 传入参数:形状的索引BlockIndex,绝对横坐标的指针*x,绝对纵坐标的 * * 指针*y * * 返 回 值:无 * * 函数功能:按下左方向键时的处理函数 * **********************************************************/ void HandleLeft(int BlockIndex,int *x, int *y) /*按下左方向键时的处理函数*/ { if(!IsConflict(BlockIndex,*x-1,*y)) { DrawBlock(BlockIndex,*x,*y,BGCOLOR); /*擦除原先的形状*/ (*x)--; DrawBlock(BlockIndex, *x, *y, arrayBlock[BlockIndex].nColor); /*绘制当 前形状*/ } } /********************************************************** Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 俄罗斯方块程序设计报告2016-11-24 * 函数原型:int HandleRight(int BlockIndex,int *x, int *y) * * 传入参数:形状的索引BlockIndex,绝对横坐标的指针*x,绝对纵坐标的 * * 指针*y * * 返 回 值:无 * * 函数功能:按下右方向键时的处理函数 * **********************************************************/ void HandleRight(int BlockIndex,int *x, int *y)/*按下右方向键时的处理函数*/ { if(!IsConflict(BlockIndex,*x+1,*y)) { DrawBlock(BlockIndex,*x,*y,BGCOLOR); /*擦除原先的形状*/ (*x)++; DrawBlock(BlockIndex, *x, *y, arrayBlock[BlockIndex].nColor); /*绘制当 前形状*/ } } /********************************************************** * 函数原型:int HandleUp(int BlockIndex,int *x, int *y) * * 传入参数:形状的索引BlockIndex,绝对横坐标的指针*x,绝对纵坐标的 * * 指针*y * * 返 回 值:无 * * 函数功能:按下上方向键(旋转键)时的处理函数 * **********************************************************/ void HandleUp(int *BlockIndex,int *x, int *y) /*按下旋转键时的处理函数*/ { int NextBlockIndex, i; static int arrayOffset[5]={0,-1,1,-2,2}; NextBlockIndex = arrayBlock[*BlockIndex].nNext; for(i=0;i<5;i++) if(!IsConflict(NextBlockIndex, *x+arrayOffset[i],*y)) { DrawBlock(*BlockIndex, *x, *y, BGCOLOR); /*擦除原先的形状*/ 11Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consc计本,2,班 卢文俊 iousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability *BlockIndex = arrayBlock[*BlockIndex].nNext; (*x) += arrayOffset[i]; DrawBlock(*BlockIndex, *x, *y, arrayBlock[*BlockIndex].nColor); /*绘制 当前形状*/ } } /********************************************************** * 函数原型:int HandleDown(int BlockIndex,int *x, int *y) * * 传入参数:形状的索引BlockIndex,绝对横坐标的指针*x,绝对纵坐标的 * * 指针*y * * 返 回 值:仍在自由下落返回0,无法下落了返回1 * * 函数功能:按下向下方向键或自由下落时的处理函数 * **********************************************************/ int HandleDown(int BlockIndex,int *x, int *y)/*按下下方向键或自由下落时的处理 函数*/ { char ScoreBuffer[10]={0},SpeedBuffer[10]={0}; int i; int NumLinesKilled=0; /*if(TimerCounter>(20-nSpeed*2))*/ { TimerCounter = 0; /*重置时钟中断*/ if(!IsConflict(BlockIndex,*x,*y+1)) /*仍在下落*/ { DrawBlock(BlockIndex,*x,*y,BGCOLOR); /*擦除原先的形状*/ (*y)++; DrawBlock(BlockIndex, *x, *y, arrayBlock[BlockIndex].nColor); /*绘 制当前形状*/ return FALSE;/*仍在下落返回FALSE*/ } else /*无法再下落了*/ { DrawBlock(BlockIndex,*x,*y,FORECOLOR); for (i=0;i<=7;i++,i++) { if ((*y)+arrayBlock[BlockIndex].arrXY[i+1]<1) continue; Gameboard[(*x)+arrayBlock[BlockIndex].arrXY[i]][(*y)+arrayBlock[BlockIndex].ar Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 俄罗斯方块程序设计报告2016-11-24 rXY[i+1]]=1; } NumLinesKilled = KillLines(*y); if(NumLinesKilled>0) { switch(NumLinesKilled) { case 1: nScore+=100; case 2: nScore+=300; case 3: nScore+=500; case 4: nScore+=800; } /*重绘计分框*/ setfillstyle(SOLID_FILL,BLACK); bar(BSIZE*25,BSIZE*20, BSIZE*(25+8), BSIZE*(20+1)); setcolor(YELLOW); rectangle(BSIZE*25,BSIZE*20, BSIZE*(25+8), BSIZE*(20+1)); itoa(nScore,ScoreBuffer, 10); setcolor(RED); settextjustify(CENTER_TEXT, BOTTOM_TEXT); outtextxy(BSIZE*(25+4), BSIZE*(20+1), ScoreBuffer); if(nScore > nSpeedUpScore) { nSpeed++; nSpeedUpScore+= nSpeed*1000; /*重绘速度框*/ setfillstyle(SOLID_FILL,BLACK); bar(BSIZE*25,BSIZE*13, BSIZE*(25+8), BSIZE*(13+1)); setcolor(YELLOW); rectangle(BSIZE*25,BSIZE*13, BSIZE*(25+8), BSIZE*(13+1)); itoa(nSpeed,SpeedBuffer,10); setcolor(YELLOW); settextjustify(CENTER_TEXT, BOTTOM_TEXT); outtextxy(BSIZE*(25+4), BSIZE*(13+1), SpeedBuffer); } } if(IsGameOver()) 11Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consc计本,2,班 卢文俊 iousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability bOver = TRUE; return TRUE; /*下落到底返回TRUE*/ } } } /********************************************************** * 函数原型:int IsLineFull(int y) * * 传入参数:纵坐标y * * 返 回 值:填满返回1,否则返回0 * * 函数功能:判断第y行是否已被填满 * **********************************************************/ int IsLineFull(int y) { int i; for(i=1;i<=10;i++) if(!Gameboard[i][y]) return FALSE; return TRUE; } /********************************************************** * void KillLine(int y) * * 传入参数:纵坐标y * * 返 回 值:无 * * 函数功能:消去第y行 * **********************************************************/ void KillLine(int y) { int i,j; for(j=y;j>=2;j--) for(i=1;i<=10;i++) { if(Gameboard[i][j]==Gameboard[i][j-1]) continue; if(Gameboard[i][j-1]==FILLED) { Gameboard[i][j]=FILLED; setfillstyle(SOLID_FILL,FORECOLOR); Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability 俄罗斯方块程序设计报告2016-11-24 } else /*Gameboard[i][j-1]==EMPTY*/ { Gameboard[i][j] = EMPTY; setfillstyle(SOLID_FILL,BGCOLOR); } DrawSquare(i,j); } } /********************************************************** * 函数原型:int KillLines(int y) * * 传入参数:纵坐标y * * 返 回 值:消去的行数 * * 函数功能:消去第y行以及与第y行连续的上面被填满的行 * **********************************************************/ int KillLines(int y) { int i, j, LinesKilled=0; for(i=0;i<4;i++) { while(IsLineFull(y)) { KillLine(y); LinesKilled++; i++; } y--; if(y<1) break; } return LinesKilled; } /********************************************************** * 函数原型:int IsGameOver() * * 传入参数:无 * * 返 回 值:游戏结束返回1,否则返回0 * * 函数功能:判断游戏是否结束 * **********************************************************/ int IsGameOver() 11Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consc计本,2,班 卢文俊 iousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability { int i; for(i=1;i<=10;i++) if(Gameboard[i][1]) return TRUE; return FALSE; } /********************************************************** * 函数原型:int GameOver() * * 传入参数:无 * * 返 回 值:退出游戏返回1,否则返回0 * * 函数功能:在界面上输出游戏结束信息,并根据用户按键选择决定是否退出 游戏* **********************************************************/ int GameOver() { int key; settextjustify(CENTER_TEXT,TOP_TEXT); /* 输出游戏结束信息 */ setcolor(RED); outtextxy(BSIZE*15,BSIZE*12,"Game Over"); setcolor(GREEN); outtextxy(BSIZE*15,BSIZE*14,"Enter : New Game"); outtextxy(BSIZE*15,BSIZE*15,"Esc : Exit"); for(;;) { while(!bioskey(1)) ; key=bioskey(0); if (key==VK_ENTER) return FALSE; /* 按下回车键,重新开始游戏 */ if (key==VK_ESC) return TRUE; /* 按下ESC键,退出游戏 */ } } Secretary must tree prison "grasp party is job, not caught party is dereliction of, caught bad party is not competent" of consciousness, effective perform caught party first responsibility people duties, seriously implementation grass-roots party work accountability
本文档为【c语言俄罗斯方块游戏程序设计报告】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_348501
暂无简介~
格式:doc
大小:122KB
软件:Word
页数:46
分类:生活休闲
上传时间:2017-10-06
浏览量:87