如何用C语言写一个俄罗斯方块游戏(附评论)
# include & lttime.h & gt
# include & ltconio.h & gt
# include & ltstdio.h & gt
const int back = 34
const int speed[]={0,12,9,6,3,1 };
const int number[]={0,2,3,5,7,11,15 };
const int col[]={11,15,12,12,13,13,14 };
int rank = 1;
int level = 0;//0表示既不右也不左,1表示左,2表示右。
int类;
int pri _ kind// 7类,颜色标注,调用col[col _ kind];
int revolve = 0;//旋转状态标志
结构体
{
int x;
int y;
}当前,之前;
结构体
{
int x;
int y;
int颜色;
布尔有;
}板[15][25];
结构体
{
int vary _ x[4];
int vary _ y[4];
} vary[]={
{{0, 2, 4, 6},{0, 0, 0, 0}},{{0, 0, 0, 0},{0,-1,-2,-3}},
{{0, 2, 2, 0},{0, 0,-1,-1}},{{0,-2,-2,-4},{0 ,0,-1,-1}},
{{0, 0, 2, 2},{0,-1,-1,-2}},{{0, 2, 2, 4},{0, 0,-1,-1}},
{{0, 0,-2,-2},{0,-1,-1,-2}},{{0, 0, 2, 4},{0,-1, 0, 0}},
{{0, 0, 0, 2},{0,-1,-2,-2}},{{0, 0,-2,-4},{0,-1,-1,-1}},
{{0, 2, 2, 2},{0, 0,-1,-2}},{{0, 2, 4, 4},{0, 0, 0,-1}},
{{0,-2,-2,-2},{0, 0,-1,-2}},{{0, 0, 2, 4},{0,-1,-1,-1}},
{{0, 0, 0,-2},{0,-1,-2,-2}},{{0 ,2, 4, 2},{0, 0, 0,-1}},
{{0, 0, 2, 0},{0,-1,-1,-2}},{{0,-2, 0, 2},{0,-1,-1,-1}},
{{0, 0,-2, 0},{0,-1,-1,-2}},
};
结构体
{
int sum
int connection _ x[5];
int connection _ y[5];
}连接[]={
{2,{-2,2},{0,1}},
{1,{0},{0}},
{2,{2,-2},{0,0}},
{2,{-2,2},{0,0}},
{4,{-2,0,4,-2},{0,0,-1,1}},
{4,{-4,2,-2,4},{0,0,-1,1}},
{4,{-2,2,0,0},{0,0,0,0}},
};
void gotoxy(int x,int y)
{
坐标位置;
位置。X = x
位置。Y = y
SetConsoleCursorPosition(GetStdHandle(STD _ OUTPUT _ HANDLE),pos);
}
空白颜色(中间b)
{
HANDLE h console = GetStdHandle((STD _ OUTPUT _ HANDLE));
SetConsoleTextAttribute(h console,b);
}
void PrintGarphy()
{
颜色(1593);
int i,j;
gotoxy(2,2);
for(I = 0;我& lt26;i++)
printf("▓”);
gotoxy(2,28);
for(I = 0;我& lt26;i++)
printf("▓”);
for(I = 0;我& lt26;i++)
{
gotoxy(2,2+I);
puts("▓”);
gotoxy(34,2+I);
if(i==8||i==16)
printf("▓▓▓▓▓▓▓▓▓”);
其他
puts("▓”);
gotoxy(52,2+I);
puts("▓”);
}
int t;
FILE *fp=fopen("C:\\els.txt "," r ");
fscanf (fp," %d ",& ampt);
fclose(FP);
颜色(43);
gotoxy(37,12);
Printf("最高记录:%d ",t);
颜色(46);
gotoxy(37,14);
Puts("当前分数:0 ");
颜色(44);
gotoxy(37,16);
Puts(“系列”);
颜色(39);
gotoxy(37,20);
puts("↑↓↓→");
gotoxy(37,22);
Puts(“按空格暂停”);
gotoxy(37,24);
Puts("按ESC退出");
gotoxy(37,26);
Puts(“作家”);
颜色(背面);
for(I = 0;我& lt15;i++)
{
for(j = 0;j & lt25;j++)
{
理事会[i][j]。x = 4+2 * I;
理事会[i][j]。y = 3+j;
理事会[i][j]。having = 0;
gotoxy(board[i][j].x,板[i][j]。y);
puts(" ");
}
}
}
void GameOver()
{
颜色(391);
for(int I = 24;我& gt=0;我-)
for(int j = 0;j & lt15;j++)
{
gotoxy(32-j*2,3+I);
puts("▆”);
睡眠(5);
}
颜色(背面);
for(int j = 0;j & lt25;j++)
for(int I = 0;我& lt15;i++)
{
理事会[i][j]。having = 0;
gotoxy(4+2*i,j+3);
puts("▆”);
睡眠(5);
}
gotoxy(4216);
while(排名-)
printf(" ");
颜色(46);
gotoxy(47,14);
puts(" ");
}
void GetDepth(int *pdepth)
{
(* pdepth)= 24;
for(int j = 0;j & lt4;j++)
{
int sum = 0;
int n=(current.x+vary[kind])。vary _ x[j]-4)/2;
int m=current.y+vary[kind]。vary _ y[j]-3;
如果(m & lt0)m = 0;
for(++m;m & lt25;m++)
if(board[n][m]。having = = 0 & amp& ampn & gt= 0 & amp& ampn & lt15 & amp;& ampm & gt= 0 & amp& ampm & lt25)
sum++;
其他
打破;
if(sum & lt;(*pdepth))
(* pdepth)= sum;
}
}
void CheckFull(int *pscore)
{
int i,j;
int sum = 0;
int s[]={0,1,3,6,10 };
for(I = 0;我& lt25;i++)
{
布尔马克= 1;
for(j = 0;j & lt15;j++)
{
if(board[j][i]。有==0)
{
mark = 0;
董事会[j][i]。颜色=背面;
}
}
如果(标记)
{
sum++;
for(int t = I;t & gt0;t -)
{
gotoxy(4,3+t);
for(j = 0;j & lt15;j++)
{
if(板[j][t-1]。拥有)
{
颜色(板[j][t-1]。颜色);
printf("▆”);
董事会,董事会。color=board[j][t-1]。颜色;
董事会,董事会。having = 1;
板[j][t-1]。having = 0;
}
其他
{
颜色(背面);
printf(" ");
董事会,董事会。color=board[j][t]。颜色;
董事会,董事会。having = 0;
板[j][t-1]。having = 0;
}
}
}
}
}
(* PS core)+= s[sum];
颜色(46);
gotoxy(47,14);
printf("%d ",* PS core);
if(* PS core & gt;=140)秩= 5;
else if(* PS core & gt;=90)排名= 4;
else if(* PS core & gt;=50)秩= 3;
else if(* PS core & gt;=20)秩= 2;
else if(* PS core & gt;= 1)rank = 1;
颜色(44);
gotoxy(4216);
for(I = 0;我& lt排名与排名。& amp* pscorei++)
printf("★");
int t;
FILE *fp=fopen("C:\\els.txt "," r ");
fscanf (fp," %d ",& ampt);
fclose(FP);
if(* PS core & gt;t)
{
fp=fopen("C:\\els.txt "," w ");
fprintf (fp,“%d”,* PS core);
fclose(FP);
颜色(43);
gotoxy(47,12);
printf("%d ",* PS core);
}
fclose(FP);
}
void PrintNext(int pkind)
{
int I;
int x=43,y = 6;
颜色(34);
for(I = 0;我& lt4;i++)
{
gotoxy(x-3,y+I-2);
puts(" ");
}
pri _ kind = pkind
if(pkind==6) pkind=15,x-;
else if(pkind = = 5)pkind = 11,x-;
else if(pkind==4) pkind=7,x-;
else if(pkind==3) pkind=5,x-= 2;
else if(pkind==2) pkind=3,x+= 2;
else if(pkind==1) pkind=2,x-;
else pkind=0,x-= 3;
color(col[pri _ kind]);
for(I = 0;我& lt4;i++)
{
gotoxy(x+vary[pkind].vary_x[i],y+vary[pkind]。vary _ y[I]);
puts("▆”);
}
}
void CheckBoundary()
{
int i,n,m;
for(I = 0;我& lt4;i++)
{
m=current.x+vary[kind]。vary _ x[I];
如果(m & lt4 | | m & gt32)
{
current . x+=(m & lt;4?2:-2);
I = 0;
}
}
for(I = 0;我& lt4;I++) //分别检查四个方块。
{
m=current.x+vary[kind]。vary _ x[I];
n=current.y+vary[kind]。vary _ y[I];
m =(m-4)/2;
n-= 3;
如果(n & lt0 | | n & gt24 | |板[m][n]。拥有)
{
if(level = = 1)//左移。
current . x+= 2;//恢复
else if(level = = 2)//向右移动。
current . x-= 2;//恢复
级别= 0;
打破;
}
}
}
无效级别移动()
{
check boundary();
if(level==0)返回;
int I;
颜色(背面);
for(I = 0;我& lt4;i++)
{
if(current.y+vary[kind])。vary _ y[I]& gt;2)
{
gotoxy(before.x+vary[kind])。vary_x[i],before.y+vary[kind]。vary _ y[I]);
puts(" ");
}
}
color(col[pri _ kind]);
for(I = 0;我& lt4;i++)
{
if(current.y+vary[kind])。vary _ y[I]& gt;2)
{
gotoxy(current.x+vary[kind])。vary_x[i],current.y+vary[kind]。vary _ y[I]);
puts("▆”);
}
}
之前. x =当前. x;
}
void revolution(int * * prev _ count)//旋转函数
{
int I;
之前. x =当前. x;
before . y = current . y;
(* * prev _ count)++;
(* * prev _ count)% =连接[pri_kind]。总和;
current . x = current . x+connection[pri _ kind]。connection _ x[* * prev _ count];
current . y = current . y+connection[pri _ kind]。connection _ y[* * prev _ count];
check boundary();
for(I = 0;我& lt4;i++)
{
int m =(current . x-4+vary[number[pri _ kind]+* * prev _ count]。vary _ x[I])/2;
int n = current . y+vary[number[pri _ kind]+* * prev _ count]。vary _ y[I]-3;
if(board[m][n]。有| | m & gt14 | | m & lt;0 | | n & gt24)
{
current . x = current . x-connection[pri _ kind]。connection _ x[* * prev _ count];
current . y = current . y-connection[pri _ kind]。connection _ y[* * prev _ count];
revolve = 0;
(* * prev _ count)-;
之前. x =当前. x;
before . y = current . y;
返回;
}
}
颜色(背面);
for(I = 0;我& lt4;i++)
{
if(before.y+vary[kind])。vary _ y[I]& gt;2)
{
gotoxy(before.x+vary[kind])。vary_x[i],before.y+vary[kind]。vary _ y[I]);
puts(" ");
}
}
kind = number[pri _ kind]+* * prev _ count;
color(col[pri _ kind]);
for(I = 0;我& lt4;i++)
{
if(current.y+vary[kind])。vary _ y[I]& gt;2)
{
gotoxy(current.x+vary[kind])。vary_x[i],current.y+vary[kind]。vary _ y[I]);
puts("▆”);
}
}
睡眠(速度[排名]);
之前. x =当前. x;
before . y = current . y;
}
void choice direction(int * prev _ count)
{
int t = 50/rank;
while(t -)
{
if(_kbhit())
{
char c = _ getch();
开关(c)
{
案例0x48:// up
revolve = 1;
旋转(& ampprev _ count);
打破;
案例0x 50://向下
级别= 0;
返回;
案例0x4b: //left
之前. x =当前. x;
current . x-= 2;
level = 1;
level move();
打破;
案例0x4d://右
之前. x =当前. x;
current . x+= 2;
level = 2;
level move();
打破;
案例“”:
_ getch();
打破;
案例27:
颜色(背面);
退出(1);
默认:break
}
}
睡眠(速度[排名]);
}
}
无效直线下降(内部深度)
{
int I;
int rev _ count = 0;
之前. x =当前. x;
before . y = current . y;
while(get depth(& amp;深度),深度-)
{
选择方向(& amprev _ count);
get depth(& amp;深度);
如果(深度==0)
{
revolve = 0;
之前. x =当前. x;
before . y = current . y;
打破;
}
颜色(背面);
for(I = 0;我& lt4;i++)
{
if(before.y+vary[kind])。vary _ y[I]& gt;2)
{
gotoxy(before.x+vary[kind])。vary_x[i],before.y+vary[kind]。vary _ y[I]);
puts(" ");
}
}
current . y++;
color(col[pri _ kind]);
for(I = 0;我& lt4;i++)
{
if(current.y+vary[kind])。vary _ y[I]& gt;2)
{
gotoxy(current.x+vary[kind])。vary_x[i],current.y+vary[kind]。vary _ y[I]);
puts("▆”);
}
}
revolve = 0;
之前. x =当前. x;
before . y = current . y;
}
选择方向(& amprev _ count);
if(水平| |旋转)
{
级别= 0;
revolve = 0;
选择方向(& amprev _ count);
直降(深度);//递归调用
}
}
int main()
{
srand((无符号长整型)时间(0));
系统("模式控制列=56行=30行");
系统("颜色24 ");
int得分= 0;
FILE *fp=fopen("C:\\els.txt "," r ");
if(fp==NULL)
{
fp=fopen("C:\\els.txt "," w ");
fprintf (fp," %d ",score);
fclose(FP);
}
PrintGarphy();
kind = rand()% 7;
int深度;
while(1)
{
int pkind = rand()% 7;
print next(pkind);
current . x = 18;
current . y = 2;//盒子初始外观的坐标
get depth(& amp;深度);//初始深度
如果(深度==0)
{
game over();
得分= 0;
rank = 1;
}
pri _ kind = kind
if(kind==6) kind=15,current . x-= 2;
else if(kind = = 5)kind = 11;
else if(kind = = 4)kind = 7;
else if(kind = = 3)kind = 5;
else if(kind==2) kind=3,current . x+= 2;
else if(kind = = 1)kind = 2;
else kind=0,current . x-= 2;
直降(深度);
for(int I = 0;我& lt4;i++)
{
int m=(current.x-4+vary[kind])。vary _ x[I])/2;
int n=current.y-3+vary[kind]。vary _ y[I];
木板,木板。having = 1;
木板,木板。color = col[pri _ kind];
}
check full(& amp;分数);
kind = pkind
}
返回0;
}