程序设计综合实验
设计文档
惠州学院
HUIZHOU UNIVERSITY
课程名称: 程序设计综合实验
姓名:实验名称: 俄罗斯方块 学号:任课教师:
专业:班级: 计算机科学与技术1班
实验时间:
计算机科学与技术
综合实验项目:俄罗斯方块游戏
1、问题需求
(1)游戏等级:游戏分为1-10十个等级,等级越高,方块下落速度越快; (2)由方向键控制游戏:上键控制方块变形、下键控制方块下移并判断是否有消行、左键控制方块左移、右键控制方块右移;
(3)游戏积分:一次性消的行数越多加的分数越多,当消行每超过30行自动提高一个游戏等级。
2、总体设计:
(1)用数组存放方块 (2)输出地图
(3)在地图里面输出方块 (4)开始游戏 (5)方块的旋转 (6)方块是否能下落 (7)判断方块是否能下落 (8)提示下一个即将下落的方块 (9)控制方块的下落速度
(10)分成10等级,等级越高方块下落得更快 (11)消行处理 (12)游戏结束
3、详细设计设计说明:本程序运行代码如下:
#include #include #include #include \"colorConsole.h\"
#define SQUARE_COLOR FOREGROUND_RED| FOREGROUND_GREEN|FOREGROUND_INTENSITY //方块的颜色 #define up
72 #define down
80 #define left
75 #define right
77 #define esc
27 #define MAPW
//地图的宽度
#define MAPH
//地图的高度
BOOL isavailable(int a[],int x,int y,int w,int h); //判定是否能放下 void turn(int a[][4],int w,int h,int *x,int y);
//转动 int * create();
//创建方块 void init();
//初始化工作
void drawblocks(int a[],int w,int h,int x,int y,WORD wColors[],int nColors); void clearcache();
//清除键盘缓冲区 void end(); void clearsquare(int *a,int w,int h,int x,int y); void gameover(); void deletemap(int m[][MAPW],int row,int w,int h); //消除一行
int dx=30,dy=5;
//屏幕上的偏移量 int score=0,level=0;
int map[MAPH][MAPW]; int a1[4][4]={{1},{1,1,1}}; int a2[4][4]={{0,1},{1,1,1}}; int a3[4][4]={{1,1},{0,1,1}}; int a4[4][4]={{0,0,1},{1,1,1}}; int a5[4][4]={{0,1,1},{1,1}}; int a6[4][4]={{1,1,1,1}}; int a7[4][4]={{1,1},{1,1}}; int a[4][4];
int main() {
init(); int *b=NULL; b=create();
//预创建方块 int q=0; int sign,blank,x,y; while(1) {
for(int i=0;i
//复制方块
for(int j=0;j
if(a[i][j]=*(b+i*4+j)) blank=i;
y=1-blank;x=4; clearsquare(&a[0][0],4,4,13,13); b=create(); HANDLE handle; handle=initiate(); WORD wColors[1]={FOREGROUND_RED| drawblocks(b,4,4,13,13,wColors,1); wColors[0]=SQUARE_COLOR; drawblocks(&a[0][0],4,4,x,y,wColors,1); clearcache(); char string[5]; wColors[0]=FOREGROUND_RED| textout(handle,26+dx,5+dy,wColors,1,itoa(score,string,10)); textout(handle,26+dx,9+dy,wColors,1,itoa(level,string,10)); sign=1; while(sign) {
int delay=0,max_delay=100-10*level; //延迟量 while(delay
if(_kbhit()) //用if避免按住键使方块卡住 {
switch (key) { case up:
clearsquare(&a[0][0],4,4,x,y); turn(a,4,4,&x,y); draw=1; break;
int draw=0; int key=_getch(); FOREGROUND_GREEN|FOREGROUND_INTENSITY }; FOREGROUND_GREEN|FOREGROUND_INTENSITY;
case down:
}
}
}
delay=max_delay; break;
if(isavailable(&a[0][0],x-1,y,4,4)) {
}
clearsquare(&a[0][0],4,4,x,y); x--; draw=1;
case left:
break;
if(isavailable(&a[0][0],x+1,y,4,4)) {
} break; end(); break;
clearsquare(&a[0][0],4,4,x,y); x++; draw=1;
case right:
case esc:
if(draw) {
}
HANDLE handle; handle=initiate();
WORD wColors[1]={SQUARE_COLOR}; drawblocks(&a[0][0],4,4,x,y,wColors,1); draw=0;
_sleep(8);delay++; if(isavailable(&a[0][0],x,y+1,4,4)) //判断是否能下移 {
clearsquare(&a[0][0],4,4,x,y); y++;
HANDLE handle;
}
}
}
} else {
}
handle=initiate();
WORD wColors[1]={SQUARE_COLOR}; drawblocks(&a[0][0],4,4,x,y,wColors,1);
sign=0;
//标记,使跳出 while(sign) 循环,产生新方块 if(y
//是否结束 for(int i=0;i
//放下方块
for(int j=0;j
if(a[i][j]&&((i+y)
map[i+y][j+x]=a[i][j];
int full,k=0;
for(i=y;i
}
full=1;
for(int j=1;j
}
deletemap(map,i,MAPW,MAPH); k++;
q++;
score=score+k; level=min(q/30,9); if(!map[i][j]) full=0;
if(full)
//消掉一行
return EXIT_SUCCESS; BOOL isavailable(int a[],int x,int y,int w,int h) {
for(int i=max(y,1);i
}
return 0; return 1; int * create() {
} void init() {
for(int i=0;i
} for(i=0;i
} map[0][i]=-1; map[19][i]=-1; map[i][0]=-2; map[i][11]=-2;
//初始化工作
int * a=NULL; int c=rand()%7; switch(c) { case 0:
} return a; a=&a1[0][0];break; a=&a2[0][0];break; a=&a3[0][0];break; a=&a4[0][0];break; a=&a5[0][0];break; a=&a6[0][0];break; a=&a7[0][0];break; case 1: case 2: case 3: case 4: case 5: case 6:
} map[0][0]=-3; map[0][11]=-3; map[19][0]=-3; map[19][11]=-3; HANDLE handle; handle=initiate(); WORD wColors[1]={ FOREGROUND_GREEN|FOREGROUND_INTENSITY}; textout(handle,26+dx,3+dy,wColors,1,\"分数\"); textout(handle,26+dx,7+dy,wColors,1,\"等级\"); textout(handle,26+dx,11+dy,wColors,1,\"下一个方块提示\"); wColors[1]=FOREGROUND_RED|FOREGROUND_INTENSITY; drawblocks(&map[0][0],12,20,0,0,wColors,1); textout(handle,dx,dy,wColors,1,\"◇══════════◇\"); wColors[0]= FOREGROUND_GREEN|FOREGROUND_INTENSITY; textout(handle,dx-16,dy,wColors,1,\"按任意键开始\"); wColors[0]=FOREGROUND_RED|FOREGROUND_INTENSITY ; textout(handle,dx-15,dy+3,wColors,1,\"制作者\"); wColors[0]=FOREGROUND_BLUE| FOREGROUND_GREEN|FOREGROUND_INTENSITY ; textout(handle,dx-15,dy+5,wColors,1,\"赵强\"); int x=_getch(); srand(x); textout(handle,dx-16,dy,wColors,1,\"
\"); void drawblocks(int a[],int w,int h,int x,int y,WORD wColors[],int nColors) {
HANDLE handle; handle = initiate(); int temp;
for(int i=0;i
for(int j=0;j
if((temp=a[i*w+j])&&y+i>0) {
if(temp==-3)
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,\"◆\");
}
}
else if(temp==-2)
textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,\"║\"); textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,\"═\"); textout(handle,2*(x+j)+dx,y+i+dy,wColors,nColors,\"■\");
else if(temp==-1) else if(temp==1) void clearcache() {
} void end() { } void turn(int a[][4],int w,int h,int *x,int y) {
int b[4][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}}; int sign=0,line=0; for(int i=h-1;i>=0;i--) {
} for(int j=0;j
{
} line++; sign=0; if(a[i][j]) {
}
b[j][line]=a[i][j]; sign=1; exit(EXIT_SUCCESS); while(_kbhit()) { } _getch(); if(sign)
for(i=0;i
if(isavailable(&b[0][0],*x-i,y,w,h))
{
*x-=i;
for(int k=0;k
for(int j=0;j
a[k][j]=b[k][j];
break; } } void clearsquare(int *a,int w,int h,int x,int y) { HANDLE handle; handle=initiate(); WORD wColors[1]={SQUARE_COLOR}; for(int i=0;i
for(int j=0;j
if(a[i*w+j]&&i+y>0)
textout(handle,2*(x+j)+dx,y+i+dy,wColors,1,\" }
void gameover() { HANDLE handle; handle=initiate(); WORD wColors[1]={FOREGROUND_RED| FOREGROUND_GREEN}; textout(handle,7+dx,10+dy,wColors,1,\"游戏结束\"); clearcache(); _getch(); exit(EXIT_SUCCESS); }
void deletemap(int m[][MAPW],int row,int w,int h) { HANDLE handle; handle=initiate(); WORD wColors[1]={SQUARE_COLOR}; textout(handle,2+dx,row+dy,wColors,1,\"﹌﹌﹌﹌﹌﹌﹌﹌﹌﹌\");
\");
} _sleep(100); for(int i=row;i>1;i--) {
} for(i=1;i
} BOOL textout(HANDLE hOutput,int x,int y,WORD wColors[],int nColors,LPTSTR lpszString) {
DWORD cWritten;
BOOL fSucce;
COORD coord;
coord.X = x;
// start at first cell
coord.Y = y;
//
of first row
fSucce = WriteConsoleOutputCharacter(
hOutput,
// screen buffer handle
lpszString,
// pointer to source string
lstrlen(lpszString), // length of string
coord,
// first cell to write to
&cWritten);
// actual number written
if (! fSucce)
cout
for (;fSucce && coord.X
hOutput = GetStdHandle(STD_OUTPUT_HANDLE); return hOutput;
{
fSucce = WriteConsoleOutputAttribute(
hOutput,
// 屏幕缓存处理
wColors,
// pointer to source string
nColors,
// length of string
coord,
// first cell to write to
&cWritten);
// actual number written
}
if (! fSucce)
return 0; }
4、程序运行结果截图: cout
方块左移
方块右移
下一个方块开始下落
方块变形
消一行,增加1分
成功消多行
消多行后分数增加更多
消完30行后,提升一个等级
方块叠到顶端后游戏结束
5、程序使用说明:
(1)按任意键开始游戏
(2)控制方块下落位置进行消行处理
(3)成功消行后加分,当消30行之后等级升一级,最高达到10等级
(4)当产生的新方块不能再下落时,游戏结束。 (5)过程中可以按ESC直接退出游戏
6、本实验的心得体会
上个学期学习C++的时候,主要是局限于读课本,记住课本上的一些知识和概念,但很少自己亲自动手去编写代码,总是眼高手低,当真正的去编写这个俄罗斯游戏的时候就体会到编写代码真的好难,尤其是刚开始的想的时候,根本不知道如何下手。后来通过上网查询和请教一些编程比较好的同学后才有了一个基本的思路,自己又重新翻阅了一下课本,将课本上的知识又重新细致的复习了一遍,最终借助于网络和同学的帮助完成了这个实验。通过这个实验发现自己在编程方面还有很大的欠缺,编程能力很差劲,不过通过这个实验,也让我对上学期学习的C++知识有了一个很好很全面的复习,这是这个实验带给我最大的收获。必须要亲自动手多写,多看代码才能真正的学好C++。