基于C语言的俄罗斯方块游戏开发_第1页
基于C语言的俄罗斯方块游戏开发_第2页
基于C语言的俄罗斯方块游戏开发_第3页
基于C语言的俄罗斯方块游戏开发_第4页
基于C语言的俄罗斯方块游戏开发_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

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

文档简介

1、青 岛 农 业 大 学本 科 生 课 程 论 文论 文 题 目 基于VC语言的俄罗斯方块游戏开发 学生专业班级 2009级测控技术与仪器 学生姓名(学号) 何伟(09081322) 指 导 教 师 程强强 完 成 时 间 2011 年 9 月 11 日 基于C语言的俄罗斯方块游戏开发一 实验题目本课程设计是在学生完成计算机技术基础课程学习后进行的。学生通过本课程的实践,能进一步掌握高级语言程序设计基本概念,掌握基本的程序设计方法;通过设计一个完整的小型程序,初步掌握开发软件所需的需求定义能力、功能分解能力和程序设计能力、代码调试技能;学习编写软件设计文档;为未来的软件设计打下良好的基础。要求学

2、生掌握所学的程序设计方法的基本知识。结合实际深入理解高级语言程序设计的基本概念、原理及方法。运用所学的基础知识开发一个小型的程序,能根据问题的需要构造所需的数据结构,设计适合的算法,解决问题。掌握设计任务的具体要求,进行设计、调试软件的具体方法、步骤和技巧。对一个实际课题的软件设计有基本了解,拓展知识面,激发在此领域中继续学习和研究的兴趣,为学习后续课程做准备。二实验要求与目的设计目的:本程序旨在训练学生的基本编程能力和游戏开发技巧,熟悉C语言图形模式下的编程。本程序中涉及机构体、数组、时钟中断及绘图方面的知识。通过本程序的训练,使学生能对C语言有更深入的了解,掌握俄罗斯方块游戏开发的基本原理

3、,为将来开发出高质量的游戏软件打下坚实的基础。功能:1)游戏方块预览功能。在游戏过程中,当在游戏底板中出现一个游戏方块时,必须在游戏方块预览区域中出现下一个游戏方块,这样有利于游戏玩家控制游戏的策略。由于在此游戏中存在19种不同的游戏方块,所以在游戏方块预览区域中需要显示随机生成的游戏方块。2)游戏方块控制功能。通过各种条件的判断,实现对游戏方块的左移、右移、快速下移、自由下落、旋转功能,以及行满消除行的功能。3)游戏显示更新功能。当游戏方块左右移动、下落、旋转时,要清除先前的游戏方块,用新坐标重绘游戏方块。当消除满行时,要重绘游戏底板的当前状态。4)游戏速度分数更新功能。在游戏玩家进行游戏过

4、程中,需要按照一定的游戏规则给游戏玩家计算游戏分数。比如,消除一行加10分。当游戏分数达到一定数量之后,需要给游戏者进行等级的上升,每上升一个等级,游戏方块的下落速度将加快,游戏的难度将增加。5)游戏帮助功能。玩家进入游戏后,将有对本游戏如何操作的友情提示。三实验环境硬件:CPU-AMD AthlonXP 1800+      内存-DDR333  256M      硬盘-40G      显示

5、卡-Max440  64M   128bit      显示器-17寸彩显软件:OS-Windows XP开发工具-VC+四系统的设计思想(一) 实现程序的流程图隐藏光标初始化背景定义并开始时间游戏是否结束?是否结束是否有键入?自由下落对左右下变形暂停等进行处理显示并打印背景是否到底?产生并打印下一个方块是否消行?Allscore += scoreCase 0;Speed=25Level=0Case 9;Speed=0Level=9Case 1;Speed=20Level=1printf(&qu

6、ot;%d", level);1Y· YYNNYNNY开始;1printf("n")return 0;结束(二)具体的函数实现我先将该程序分为三个部分,即必要的输出部分、主要的函数、次要函数。必要的输出部分应包括背景的输出(包括原有方块的显示)、方块的输出、及下一方块的输出。主要函数应包括实现方块的打印、消行、自由下落、旋转、原来方块的擦除。次要函数应包括显示下一方块、计分、暂停、对方快进行涂色、难度设置等功能的实现。首先进行相关定义为下面编写程序做准备int dia_x, dia_y;/存储方块的位置,左上角int shape, change, nex

7、tshape, nextchange;/定义方块的开关与变型clock_t nowtime;/定义开始时间int BackgroundYYYXXX = 0;/定义背景并初始化int Is_Gethit = 0;/接受按键为1, 否则为0; int score = 0, Allscore = 0;/你的得分HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);/设置控制台void gotoxy(int x, int y)/设置光标位置进行必要的输出1.通过数组输出方块的所有图形int Tetris7444 = 0,0,0,0,0,1,1,0,0,1,

8、1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,/方正0,0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,1,0,/T形0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,

9、0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,/长条0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,/z0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0

10、,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0,/反Z0,0,0,0,0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,1,1,0,1,0,0,0,0,0,0,0,/J0,0,0,0,0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,/L2.打印初始

11、化背景void Init()/初始化背景int i, j;for(i = 0; i < XXX; +i)/初始化横的Background0i = 1;BackgroundYYY-1i = 1;for(j = 1; j < (YYY-1); +j)/初始化竖的Backgroundj0 = 1;BackgroundjXXX-1 = 1;srand(unsigned)time(NULL);dia_x = XXX/2-1;dia_y = 1;Dis_Back();Score();shape = rand()%7;change = rand()%4;NextTetris();进行主要函数的编

12、写1.打印方块void Dis_Dia(int dia_x, int dia_y, int shape, int change)/打印方块SetColor(shape);int i, j;for(j = 0; j < 4; +j)for(i = 0; i < 4; +i)if(Tetrisshapechangeji = 1)gotoxy(2*dia_x + 2*j, dia_y + i);printf("");2.用随机函数将下一方块打印出来(随机函数为rand()直接调用,随机产生下一个方块)void NextTetris()/设置下一个方块并打印出来goto

13、xy(XXX*2+5, YYY-21);printf("The next tetris:");Clear_Dia(XXX+5, YYY-20, nextshape, nextchange); nextshape = rand()%7;nextchange = rand()%4;Dis_Dia(XXX+5, YYY-20, nextshape, nextchange);3.消行函数void Disappear()/消行int jj, ii, j, i, CanDis, num = 0;score = 0;for(j = (YYY - 2); j > 0; -j)CanD

14、is = 1;X:for(i = 1; i < (XXX - 1); +i)if(Backgroundji != 1)CanDis = 0;if(CanDis = 1)for(jj = j; jj > 1; -jj)/调整背景数组for(ii = 1; ii < (XXX - 1); +ii)Backgroundjjii = Backgroundjj-1ii;+num;goto X;if(num)system("cls");Dis_Back();/重新打印背景score = num*num;4.擦出方块函数(当方块下落到下一时刻时,上一时刻的方块必须进行擦

15、出,以及在显示下一方块时也要调用该函数。)void Clear_Dia(int dia_x, int dia_y, int shape, int change)/擦除方块int i, j;for(j = 0; j < 4; +j)for(i = 0; i < 4; +i)if(Tetrisshapechangeji = 1)gotoxy(2*dia_x + 2*j, dia_y + i);printf(" ");5设置光标位置(为了控制方块出现的位置,以及定位其他输出显示的位置)void gotoxy(int x, int y)/设置光标位置COORD coor

16、d;coord.X = x;coord.Y = y;SetConsoleCursorPosition(hConsole, coord); 6.对相关键的处理()void Getkbhit()/对上下左右按键的处理if(kbhit()/如果有按键刚进入按键处理switch(getch()case 71:case'p':Pause();break;case 72:case 'w':if(change + 1) > 3)if(Dete(dia_x, dia_y, shape, 0)Clear_Dia(dia_x, dia_y, shape, change);ch

17、ange = 0;Dis_Dia(dia_x, dia_y, shape, change);elseif(Dete(dia_x, dia_y, shape, change + 1)Clear_Dia(dia_x, dia_y, shape, change);Dis_Dia(dia_x, dia_y, shape, +change);break;case 's':case 80:if(Dete(dia_x, dia_y + 1, shape, change)Clear_Dia(dia_x, dia_y, shape, change);Dis_Dia(dia_x, +dia_y,

18、shape, change);break;case 'a':case 75:if(Dete(dia_x - 1, dia_y, shape, change)Clear_Dia(dia_x, dia_y, shape, change);Dis_Dia(-dia_x, dia_y, shape, change);break;case 'd':case 77:if(Dete(dia_x + 1, dia_y, shape, change)Clear_Dia(dia_x, dia_y, shape, change);Dis_Dia(+dia_x, dia_y, shap

19、e, change);break;default :break;if(clock() - nowtime) >= 1000 && Dete(dia_x, dia_y + 1, shape, change)/自动下落Clear_Dia(dia_x, dia_y, shape, change);Dis_Dia(dia_x, +dia_y, shape, change);nowtime = clock();至此函数的主要功能已经实现,下面我们队该程序的其他功能进行完善。1.下一方块并打印出来void NextTetris()/设置下一个方块并打印出来gotoxy(XXX*2+5

20、, YYY-21);printf("The next tetris:");Clear_Dia(XXX+5, YYY-20, nextshape, nextchange); nextshape = rand()%7;nextchange = rand()%4;Dis_Dia(XXX+5, YYY-20, nextshape, nextchange);2对游戏进行计分并对难度进行设置.void Score()/分数系统Allscore += score;SetColor(5);gotoxy(XXX*2+5, YYY-15);printf("score:");

21、gotoxy(XXX*2+5, YYY-13);printf("%d", Allscore);switch(Allscore/100) case 0:speed=25;break; case 1:speed=20;break; case 2:speed=17;break; case 3:speed=15;break; case 4:speed=12;break; case 5:speed=10;break; case 6:speed=7;break; case 7:speed=5;break; case 8:speed=2;break; case 9:speed=0;bre

22、ak; gotoxy(XXX*2+5, YYY-11);printf("level:");if(speed=25)level=0;if(speed=20)level=1;if(speed=17)level=2;if(speed=15)level=3;if(speed=12)level=4;if(speed=10)level=5;if(speed=7)level=6;if(speed=5)level=7;if(speed=2)level=8;if(speed=0)level=9;gotoxy(XXX*2+5, YYY-9);printf("%d", lev

23、el);3.对方快进行着色void SetColor(int shape)/设置方块颜色SetConsoleTextAttribute(hConsole, shape+9);4.暂停void Pause()/暂停 char c;doc=getch(); while(c!='p');五源代码#include <stdio.h>#include <windows.h>#include <stdlib.h>#include <conio.h>#include <time.h>#define XXX 14 /方框的横轴大小#d

24、efine YYY 25 /方框的竖轴大小int speed;nt level;int Tetris7444 = 0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,/方正0,0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,0,

25、0,0,1,1,0,0,1,0,/T形0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,/长条0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,/z0,0,0,0,0,1,1,0,1,1,

26、0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0,/反Z0,0,0,0,0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,1,1,0,1,0,0,0,0,0,0,0,/J0,0,0,0,0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0,1,0,0,0,1,1,1,0,0

27、,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,/L;/方块数组 int dia_x, dia_y;/存储方块的位置,左上角int shape, change, nextshape, nextchange;/定义方块的开关与变型clock_t nowtime;/定义开始时间int BackgroundYYYXXX = 0;/定义背景并初始化int Is_Gethit = 0;/接受按键为1, 否则为0; int score = 0, Allscore = 0;/你的得分HANDLE hConsole =

28、GetStdHandle(STD_OUTPUT_HANDLE);/设置控制台void gotoxy(int x, int y)/设置光标位置COORD coord;coord.X = x;coord.Y = y;SetConsoleCursorPosition(hConsole, coord);void SetColor(int shape)/设置方块颜色SetConsoleTextAttribute(hConsole, shape+9);void Clear_Dia(int dia_x, int dia_y, int shape, int change)/擦除方块int i, j;for(j

29、 = 0; j < 4; +j)for(i = 0; i < 4; +i)if(Tetrisshapechangeji = 1)gotoxy(2*dia_x + 2*j, dia_y + i);printf(" ");void Pause()/暂停 char c;doc=getch(); while(c!='p');void Dis_Dia(int dia_x, int dia_y, int shape, int change)/打印方块SetColor(shape);int i, j;for(j = 0; j < 4; +j)for(i

30、= 0; i < 4; +i)if(Tetrisshapechangeji = 1)gotoxy(2*dia_x + 2*j, dia_y + i);printf("");void Backdrop(int dia_x, int dia_y, int shape, int change)/将方块融入背景int j, i;for(j = 0; j < 4; +j)for(i = 0; i < 4; +i)if(Tetrisshapechangeji = 1)Backgrounddia_y + idia_x + j = 1;void Dis_Back()/显示

31、背景int j, i;for(j = 0; j < YYY; +j)for(i = 0; i < XXX; +i)if(Backgroundji = 1)gotoxy(2*i, j);printf("");int Dete(int dia_x, int dia_y, int shape, int change)/判断是否非法int j, i;for(j = 0; j < 4; +j)for(i = 0; i < 4; +i)if(Backgrounddia_y + idia_x + j = 1) && (Tetrisshapechan

32、geji = 1)return 0;/返回0代表非法了return 1;/返回1代表不非法int Is_GameOver()/判断游戏结束了没int j;for(j = 1; j < XXX-1; +j)if(Background2j = 1)return 0;/返回0代表 死掉了return 1;/返回1代表还活着void NextTetris()/设置下一个方块并打印出来gotoxy(XXX*2+5, YYY-21);printf("The next tetris:");Clear_Dia(XXX+5, YYY-20, nextshape, nextchange)

33、; nextshape = rand()%7;nextchange = rand()%4;Dis_Dia(XXX+5, YYY-20, nextshape, nextchange);void Disappear()/消行int jj, ii, j, i, CanDis, num = 0;score = 0;for(j = (YYY - 2); j > 0; -j)CanDis = 1;X:for(i = 1; i < (XXX - 1); +i)if(Backgroundji != 1)CanDis = 0;if(CanDis = 1)for(jj = j; jj > 1;

34、-jj)/调整背景数组for(ii = 1; ii < (XXX - 1); +ii)Backgroundjjii = Backgroundjj-1ii;+num;goto X;if(num)system("cls");Dis_Back();/重新打印背景score = num*num;void Score()/分数系统Allscore += score;SetColor(5);gotoxy(XXX*2+5, YYY-15);printf("score:");gotoxy(XXX*2+5, YYY-13);printf("%d"

35、, Allscore);switch(Allscore/100) case 0:speed=25;break; case 1:speed=20;break; case 2:speed=17;break; case 3:speed=15;break; case 4:speed=12;break; case 5:speed=10;break; case 6:speed=7;break; case 7:speed=5;break; case 8:speed=2;break; case 9:speed=0;break; gotoxy(XXX*2+5, YYY-11);printf("leve

36、l:");if(speed=25)level=0;if(speed=20)level=1;if(speed=17)level=2;if(speed=15)level=3;if(speed=12)level=4;if(speed=10)level=5;if(speed=7)level=6;if(speed=5)level=7;if(speed=2)level=8;if(speed=0)level=9;gotoxy(XXX*2+5, YYY-9);printf("%d", level);void Init()/初始化背景int i, j;for(i = 0; i &l

37、t; XXX; +i)/初始化横的Background0i = 1;BackgroundYYY-1i = 1;for(j = 1; j < (YYY-1); +j)/初始化竖的Backgroundj0 = 1;BackgroundjXXX-1 = 1;srand(unsigned)time(NULL);dia_x = XXX/2-1;dia_y = 1;Dis_Back();Score();shape = rand()%7;change = rand()%4;NextTetris();void Getkbhit()/对上下左右按键的处理if(kbhit()/如果有按键刚进入按键处理swi

38、tch(getch()case 71:case'p':Pause();break;case 72:case 'w':if(change + 1) > 3)if(Dete(dia_x, dia_y, shape, 0)Clear_Dia(dia_x, dia_y, shape, change);change = 0;Dis_Dia(dia_x, dia_y, shape, change);elseif(Dete(dia_x, dia_y, shape, change + 1)Clear_Dia(dia_x, dia_y, shape, change);Dis

39、_Dia(dia_x, dia_y, shape, +change);break;case 's':case 80:if(Dete(dia_x, dia_y + 1, shape, change)Clear_Dia(dia_x, dia_y, shape, change);Dis_Dia(dia_x, +dia_y, shape, change);break;case 'a':case 75:if(Dete(dia_x - 1, dia_y, shape, change)Clear_Dia(dia_x, dia_y, shape, change);Dis_Dia(-dia_x, dia_y, shape, change);break;case 'd':case 77:if(Dete(dia_x + 1, dia_y, shape, change)Clear_Dia(dia_x, dia_y, shape, change);Dis_Dia(+dia_x, dia_y, shape, change);break;default :break;if(clock() - nowtime) >= 1000 && Dete

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论