俄罗斯方块c语言程序带注释.docx
《俄罗斯方块c语言程序带注释.docx》由会员分享,可在线阅读,更多相关《俄罗斯方块c语言程序带注释.docx(12页珍藏版)》请在冰豆网上搜索。
![俄罗斯方块c语言程序带注释.docx](https://file1.bdocx.com/fileroot1/2022-11/20/8b940526-24f7-4934-87f3-53b48cc774de/8b940526-24f7-4934-87f3-53b48cc774de1.gif)
俄罗斯方块c语言程序带注释
//#include
//#include
#include
#include
#include
#include
usingnamespacestd;
#definekey_left75
#definekey_right77
#definekey_up72
#definekey_down80
#definespace32
/*全局变量。
方块。
背景数组*/
intbackground[20][10];
/*intfang_kuai[2][2]={1,1,
1,1,};*/
intfang_kuai[7][4][4][4]={
1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,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,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0,1,1,1,0,1,0,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,0,0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,
1,0,0,0,1,1,0,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0,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,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,0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,1,1,0,
1,1,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,0,1,1,0,1,1,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,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,
};/*方块模板*/
intshape,dir,next_shape,next_dir;
structposition
{
intx;
inty;
}now_position;/*纪录现在方块的位置*/
intspeed_level=1;
intfall_to_land,be_lined;/*判断方块是否着地的*/
/*----------------------------------------*/
voidclscreen();
voidsetxy(intx,inty);
voiddisplay_back();
voiddisplay_interface();
voiddisplay_fk(intx,inty,intshape,intdir);
voidinit();
//intgetkey();/*取键盘的扫描码*/
voidgetinput();
voidm_sound(intfreq,inttime);
voideras_fk(intx,inty,intshape,intdir);/*擦除方块*/
voidfk_down(boolquickly=false);
boolis_illegal(intx,inty,intchanged_dir);/*判断方块移动后坐标是否非法,非法1,合法0*/
intget_random(intn);/*生成一个0----n-1的随机数*/
voidcheck_line();/*检查是否有行可以消去,如果有就消去*/
voidgame_over();
/*----------------------------------*/
intmain()
{
inti;
init();
while(true)
{
display_back();
now_position.x=10;
now_position.y=2;/*复原初始坐标*/
be_lined=0;/*一个方块刚出现时没有成行*/
shape=next_shape;
dir=next_dir;
next_shape=get_random(7);
next_dir=get_random(4);
eras_fk(55,5,shape,dir);/*擦去前一个方块*/
display_fk(55,5,next_shape,next_dir);/*显示下一个方块*/
display_fk(now_position.x,now_position.y,shape,dir);/*显示目前方块*/
fall_to_land=0;
while(!
fall_to_land)
{
for(i=15000-1500*speed_level;i>0;i--)
{
getinput();
}/*接受键盘输入*/
fk_down();/*方块自动下落*/
}
//m_sound(350,500);/*块落下的声音*/
check_line();/*检查有没有可消去的行*/
if(be_lined)
m_sound(250,1000);
else
m_sound(350,500);
}
return0;
}
/*------------------------------------*/
voidinit()
{
inti,j;
for(i=0;i<20;i++)
for(j=0;j<10;j++)
background[i][j]=0;/*背景数组赋初值0*/
next_shape=(get_random(7)+5)/7;
next_dir=(get_random(4)+2)/4;/*预先产生一个方块*/
display_interface();/*画界面*/
}
voiddisplay_interface()
{
clscreen();
setxy(40,5);
printf("TheNext:
");
setxy(1,1);
}
/*-----------------------------------------*/
voiddisplay_back()/*显示背景*/
{
inti,j;
for(i=0;i<20;i++)
{
for(j=0;j<10;j++)
{
setxy(2*(j+1),i+2);/*数组下标到屏幕坐标的变换*/
if(!
background[i][j])/*背景数组中值为0*/
printf("");
else
printf("[]");
}
}
}
/*---------------------------------------*/
voiddisplay_fk(intx,inty,intshape,intdir)/*显示方块*/
{
inti,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(fang_kuai[shape][dir][i][j])
{
setxy((x+2*j),(y+i));/*结合现在方块的位置*/
printf("[]");
}
}
}
}
/*-----------------------------------*/
voidgetinput()
{
if(GetAsyncKeyState(VK_LEFT)){
if(!
is_illegal(now_position.x-2,now_position.y,dir))
{
eras_fk(now_position.x,now_position.y,shape,dir);
now_position.x=now_position.x-2;
display_fk(now_position.x,now_position.y,shape,dir);
Sleep(1000);
}
else
m_sound(440,500);
/*如果没有遇阻,方块位置左移一格*/
}elseif(GetAsyncKeyState(VK_RIGHT)){
if(!
is_illegal(now_position.x+2,now_position.y,dir))
{
eras_fk(now_position.x,now_position.y,shape,dir);
now_position.x=now_position.x+2;
display_fk(now_position.x,now_position.y,shape,dir);
Sleep(1000);
}
else
m_sound(440,500);
/*如果没有遇阻,方块位置右移一格*/
}elseif(GetAsyncKeyState(VK_UP)){
if(!
is_illegal(now_position.x,now_position.y,(dir+1)%4))
{
eras_fk(now_position.x,now_position.y,shape,dir);
dir=(dir+1)%4;
display_fk(now_position.x,now_position.y,shape,dir);
Sleep(1000);
}
}elseif(GetAsyncKeyState(VK_DOWN)){
fk_down(true);
}elseif(GetAsyncKeyState(VK_SPACE)){
game_over();
}
}
/*------------------------------------*/
voidm_sound(intfreq,inttime)
{
Beep(freq,time);
}
/*--------------------------------*/
voideras_fk(intx,inty,intshape,intdir)/*擦除方块*/
{
inti,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(fang_kuai[shape][dir][i][j])
{
setxy((x+2*j),(y+i));/*结合现在方块的位置*/
printf("");
}
}
}
}
/*----------------------------------------------------*/
voidfk_down(boolquickly)/*方块下落*/
{
inti,j,x,y;
if(!
is_illegal(now_position.x,now_position.y+1,dir))/*下落没有阻碍*/
{
eras_fk(now_position.x,now_position.y,shape,dir);
now_position.y=now_position.y+1;
display_fk(now_position.x,now_position.y,shape,dir);
if(quickly)
{
Sleep(10);
}else
{
Sleep(1000);
}
}
else/*不可再下落了*/
{
/*方块并入背景*/
x=now_position.x;
y=now_position.y;/*x,y表示方便*/
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(fang_kuai[shape][dir][i][j]==1)
background[(y-2+i)][(x/2-1+j)]=1;
}
}
fall_to_land=1;/*告诉主程序方块着地*/
}
}
/*-----------------------------------------------------*/
boolis_illegal(intx,inty,intchanged_dir)
{
inti,j;
boolillegal=false;
/*先判断有没有出界,如果有x,y在界外并且此x,y处方块数组为1,就返回1*/
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(fang_kuai[shape][changed_dir][i][j]==1&&((x+j*2)>21||(x+j*2)<2||(y+i)>21||(y+i)<2))
illegal=true;
/*再判断是否有原方块阻碍*/
if(!
illegal)
{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(fang_kuai[shape][changed_dir][i][j]==1&&background[(y-2+i)][(x/2-1+j)]==1)
illegal=true;/*有阻碍返回一*/
}
returnillegal;
}
/*-----------------------------------------------------*/
intget_random(intn)
{
intx;
Sleep(500);
/*
randomize();
x=random(n);
*/
srand((unsigned)time(NULL));
x=rand()%n;
return(x);
}
/*--------------------------------------------------------------*/
voidcheck_line()/*待修改*/
{
/*检查背景数组*/
inti,j,k;
intline,n;
for(i=19;i>=0;i--)
{
k=1;/*开始默认成行*/
for(j=9;j>=0;j--)
{
if(background[i][j]==0)/*不成行的条件*/
k=0;
}
if(k==1)/*如果成行*/
{
be_lined=1;/*成行标志*/
for(line=i;line>0;line--)
for(n=9;n>=0;n--)
background[line][n]=background[line-1][n];/*逐次下移一行*/
i=i+1;/*把下一次待检的行重新设定*/
}
}
}
/*--------------------------------------------------------------*/
voidgame_over()
{
clscreen();
setxy(35,1);
printf("GAMEOVER");
exit(0);
}
voidclscreen()
{
system("cls");
}
voidsetxy(intx,inty)
{
HANDLEhConsole=GetStdHandle(STD_OUTPUT_HANDLE);
COORDplace;
place.X=x;
place.Y=y;
SetConsoleCursorPosition(hConsole,place);
}
/*ColorsdefinedforSetColor(int)*/
enum{
BLACK=0,
DARK_BLUE=1,
DARK_GREEN=2,
TEAL=3,
DARK_RED=4,
DARK_PURPLE=5,
GOLD=6,
GREY=7,
DARK_WHITE=8,
BLUE=9,
GREEN=10,
CYAN=11,
RED=12,
PURPLE=13,
YELLOW=14,
WHITE=15
};
voidSetColor(constintforeground,constintbackground){
intColor=foreground+(background*16);
HANDLEhConsole=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole,Color);
return;
}
你把这段拿回去看哈吧,这是一个俄罗斯方块的程序,就是在控制台运行的,就是用你所说的纯数字和字符所运行,你现在只能在dos下显示那些纯数字的东西,是因为你很多技术性的手法,你还不会,但你学习到一定程度后,你就会有所领悟,也不要太心急,当你练好内功后,修炼招数,应该会很快的!
希望对你有帮助