俄罗斯方块编程源代码.docx
- 文档编号:26146944
- 上传时间:2023-06-17
- 格式:DOCX
- 页数:13
- 大小:17.84KB
俄罗斯方块编程源代码.docx
《俄罗斯方块编程源代码.docx》由会员分享,可在线阅读,更多相关《俄罗斯方块编程源代码.docx(13页珍藏版)》请在冰豆网上搜索。
俄罗斯方块编程源代码
俄罗斯方块编程源代码
#include
#include
#include
#include
#definePI3.14159
/*定义全局变量*/
intcurrent_box_numb;/*保存当前方块编号*/
intCurbox_x=Sys_x+Begin_boxs_x*BSIZE,Curbox_y=Sys_y;/*x,y是保存方块的当前坐标的*/
intflag_newbox=false;/*是否要产生新方块的标记0*/
intspeed=0;/*下落速度*/
intscore=0;/*总分*/
intspeed_step=60;/*每等级所需要分数*/
voidinterrupt(*oldtimer)(void);/*指向原来时钟中断处理过程入口的中断处理函数指针*/
structBOARD/*游戏底板结构,表示每个点所具有的属性*/
{
intvar;/*当前状态只有0和1,1表示此点已被占用*/
intcolor;/*颜色,游戏底板的每个点可以拥有不同的颜色.增强美观*/
}Table_board[Vertical_boxs][Horizontal_boxs];
/*方块结构的设计*/
structSHAPE
{
charbox[2];
intcolor;/*方块的颜色*/
intnext;/*下个方块的编号*/
};
/*具体方块*/
structSHAPEshapes[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},
{0x8c,0x40,YELLOW,9},
{0x6c,0x
0,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},
{0xcc,0x0,BLUE,18},
{0x4e,0xa0,LIGHTCYAN,20},
{0x6c,0x60,LIGHTCYAN,21},
{0xae,0x40,LIGHTCYAN,22},
{0xc6,0xc0,LIGHTCYAN,19},
{0xc0,0x00,LIGHTBLUE,24},
{0x88,0x00,LIGHTBLUE,23},
{0x4c,0x00,LIGHTRED,28},
{0xc4,0x00,LIGHTRED,25},
{0xc8,0x00,LIGHTRED,26},
{0x8c,0x00,LIGHTRED,27},
{0x4e,0x40,LIGHTGRAY,29},
{0x48,0x00,LIGHTGREEN,31},
{0x84,0x00,LIGHTGREEN,30},
{0xaa,0xa0,LIGHTMAGENTA,32}
};
/*开始动画的设计*/
voidmoon()/*编辑月亮图形*/
{
intx=50,y=50,n=30;/*月亮坐标*/
setcolor(YELLOW);
setfillstyle(1,YELLOW);
ellipse(x,y,0,360,n,n);
ellipse(x+n/2,y,90,270,n,n);
floodfill(x-n+3,y,YELLOW);
setcolor(BLACK);
circle(x,y,n);/*只是一个简单的圆形*/
circle(x+n/2,y,n);
}
voidstar(intx,inty)/*编辑星星图形*/
{
inti,a;
intn=5;
intx1[5],y1[5],x2[5],y2[5];
setcolor(YELLOW);
for(i=0;isize
circle(x,y,cover++);
delay1=1000;
}
if(cover==size)
{
i=0;
x=50+rand()%550;
y=rand()%400;
cover=0;
color=rand()%16;
size=50+rand()%250;
delay1=10000;
clearviewport();
drawstar();
moon();
}
i+=n1;
delay(delay1);
}}
/*排
行榜结构*/
structpersona
{
charname[20];
intscore;
};
/*定义全局变量*/
structpersonapera[5];
unsignedintTimerCounter=0;/*定时计数器变量*/
/*新的时钟中断处理函数*/
voidinterruptnewtimer(void)
{
(*oldtimer)();/*calltheoldroutine*/
TimerCounter++;/*increasetheglobalcounter*/
}
/*设置新的时钟中断处理过程*/
voidSetTimer(voidinterrupt(*IntProc)(void))
{
oldtimer=getvect(TIMER);/*获取中断号为TIMER的中断处理函数的入口地址*/
disable();/*设置新的时钟中断处理过程时,禁止所有中断*/
setvect(TIMER,IntProc);enable();/*开启中断*/
}
/*恢复原有的时钟中断处理过程*/
voidKillTimer()
{
disable();
setvect(TIMER,oldtimer);enable();
}
voidshow_intro(intxs,intys)
{
charstemp[50];
setcolor(15);rectangle(xs-150,ys,xs-30,ys+70);sprintf(stemp,"-Roll");
stemp[0]=24;
setcolor(14);
outtextxy(xs-150,ys+10,stemp);
outtextxy(xs-150,ys+30,"Esc-Exit");
outtextxy(xs-150,ys+50,"Space-Pause");
setcolor(FgColor);
}
/*显示分数*/
voidShowScore(intscore)
{
intx,y;
charscore_str[5];/*保存游戏得分*/
setfillstyle(SOLID_FILL,BgColor);
x=LeftWin_x,y=100;
bar(x-BSIZE,y,x+BSIZE*3,y+BSIZE*3);
sprintf(score_str,"%3d",score);
outtextxy(x,y,"SCORE"),outtextxy(x,y+10,score_str);
}
/*显示速度*/
voidShowSpeed(intspeed)
{
intx,y;
charspeed_str[5];/*保存速度值*/
setfillstyle(SOLID_FILL,BgColor);
x=LeftWin_x,y=150;
bar(x-BSIZE,y,x+BSIZE*3,y+BSIZE*3);
sprintf(speed_str,"%3d",speed+1);
outtextxy(x,y,"Level"),outtextxy(x,y+10,speed_str),outtextxy(x,y+50,"Nextbox");
}
/*初始化*/
voidinitialize(intx,inty,intm,intn)
{
inti,j,oldx=x;
for(j=0;j=0;n--)/*从当前行往上看*/
{
totoal=0;
for(m=0;m=t_boardy;n--)
{
if(n=Vertical_boxs)continue;/*超过低线了*/
for(m=0;m=Vertical_boxs)continue;/*超过低线了*/
for(m=0;m=(Sys_x+BSIZE*Horizontal_boxs))return(false);/*碰到最右边了*/
elseif(Table_board[t_boardy+n][t_boardx+m].var)
{
return(false);
}
}
mask=mask/
(2);
if(mask==0)mask=128;
}
}
return(true);
}
elseif(direction==MoveDown)/*如果向下移*/
{
y=y+BSIZE;
t_boardx=(x-Sys_x)/BSIZE;
t_boardy=(y-Sys_y)/BSIZE;
mask=128;
for(n=0;n=(Sys_x+BSIZE*Horizontal_boxs))return(false);/*碰到最左边了*/
if((x+BSIZE*m)>=(Sys_x+BSIZE*Horizontal_boxs))return(false);/*碰到最右边了*/
elseif(Table_board[t_boardy+n][t_boardx+m].var)
{
return(false);
}
}
mask=mask/
(2);
if(mask==0)mask=128;
}
}
return(true);
}
else
{
return(false);
}
}
/*
*显示指定的方块
*/
voidshow_box(intx,inty,intbox_numb,intcolor)
{
inti,ii;
intls_x=x;
if(box_numb=MAX_BOX)/*指定的方块不存在*/
box_numb=MAX_BOX/2;
setfillstyle(SOLID_FILL,color);
for(ii=0;ii=i+1;j--)
pera[j]=pera[j-1];
}
pera[i]=des;
break;
}
}
}
/*显示排行榜函数*/
voidpain_boarda()
{
inti;
charstring[10];
ReadFilesa();
setfillstyle(1,9);
bar(482,227,599,239);
outtextxy(490,230,"Highestboard");
setcolor(15);
rectangle(480,225,600,240);
setcolor(8);
rectangle(481,226,601,241);
setfillstyle(1,7);
bar(475,257,610,426);
setcolor(15);
line(475,257,610,257);
line(475,257,475,426);
setcolor(8);
line(475,426,610,426);
line(610,257,610,426);
setcolor
(1);
outtextxy(480,265,"namescore");
for(i=0;ipera[4].score)
{
setfillstyle(1,9);
bar(14,14,447,419);
outtextxy(50,250,"Refueling!
YourScoreEntertheBoard!
");
outtextxy(50,270,"pleaseenteryourname:
");
gotoxy(7,19);
printf("");/*用背景色输出空格,建立姓名输入框*/
gotoxy(7,19);
scanf("%s",);
CurPerson.score=score;
CompareScorea(CurPerson);/*调用排序函数,对进榜的5人排序*/
WriteFilesa(pera);/*调用写文件函数,写入排行榜记录*/
}
pain_boarda();/*调用显示排行榜函数*/
}
voidGamePlay()
{
intGameOver=0,key,nextbox;
intCurrentaction=0;/*标记当前动作状态*/
setbkcolor(BgColor);
setcolor(FgColor);
randomize();
SetTimer(newtimer);
initialize(Sys_x,Sys_y,Horizontal_boxs,Vertical_boxs);/*初始化*/
nextbox=MkNextBox(-1);
show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color);
show_box(LeftWin_x,Curbox_y+200,nextbox,shapes[nextbox].color);
show_intro(Sys_x,Curbox_y+320);
getch();
while
(1)
{
if(bioskey
(1))
{
key=bioskey(0);
}
else
{
key=0;
}
switch(key)
{
caseLEFT:
if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveLeft))
{
EraseBox(Curbox_x,Curbox_y,current_box_numb);
Curbox_x-=BSIZE;Currentaction=MoveLeft;
}break;
caseRIGHT:
if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveRight))
{
EraseBox(Curbox_x,Curbox_y,current_box_numb);
Curbox_x+=BSIZE;
Currentaction=MoveRight;
}break;
caseDOWN:
if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveDown))
{
EraseBox(Curbox_x,Curbox_y,current_box_numb);
Curbox_y+=BSIZE;
Currentaction=MoveDown;
}
elseflag_newbox=true;break;
caseUP:
/*旋转方块*/
if(MoveAble(Curbox_x,Curbox_y,shapes[current_box_numb].next,MoveRoll))
{
EraseBox(Curbox_x,Curbox_y,current_box_numb);
current_box_numb=shapes[current_box_numb].next;
Currentaction=MoveRoll;
}break;
caseESC:
GameOver=1;
break;
caseSPACE:
getch();
break;
default:
break;
}
if(Currentaction)
{/*表示当前有动作,移动或转动*/
show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color);
Currentaction=0;
}
/*按了往下键,
但不能下移,就产生新方块*/
if(flag_newbox)
{
/*这时相当于方块到底部了,把满行清去,置0*/
ErasePreBox(LeftWin_x,Sys_y+200,nextbox);
nextbox=MkNextBox(nextbox);
show_box(LeftWin_x,Curbox_y+200,nextbox,shapes[nextbox].color);
if(!
MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveDown))/*刚一开始,游戏结束*/
{
show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color);
GameOver=1;
}
else
{flag_newbox=false;}
Currentaction=0;
}
else/*自由下落*/
{
if(Currentaction==MoveDown||TimerCounter>(8))
{
if(MoveAble(Curbox_x,Curbox_y,current_box_numb,MoveDown))
{
EraseBox(Curbox_x,Curbox_y,current_box_numb);Curbox_y+=BSIZE;
show_box(Curbox_x,Curbox_y,current_box_numb,shapes[current_box_numb].color);
}
TimerCounter=0;
}
}
if(GameOver)/*||flag_newbox==-1*/
{
printf("gameover,thankyou!
yourscoreis%d",score);
getch();
break;
}
}
getch();
KillTimer();
}
voidmain()
{
intgd=VGA,gm=VGAHI,errorcode;
intmode,level,d;
intgdriver=VGA,gmode=VGAHI;
inti,j,k;
errorcode=graphresult();
if(errorcode!
=grOk)
{
printf("\nNotice:
Graphicserror:
%s\n",grapherrormsg(errorcode));
printf("Pressanykeytoquit!
");
getch();
exit
(1);
}
initgraph(
initgraph(
drawstar();
moon();
starflower();
getch();
while
(1)
{
initgraph(
printf("\n");
printf("\n");
printf("WELCOMETOTHEGAME!
!
\n");
printf("\n");
printf("\n");
printf("MainMenu\n");
printf("\n");
printf("1-Beginthegame\n");
printf("\n");
printf("2-Highestboard\n");
printf("\n");
printf("
3-Exit\n");
scanf("%d",
if(mode==1)
{
initgraph(
GamePlay();
KillTimer();
EndPlaya();
}
elseif(mode==2)
{
initgraph(
pain_boarda();
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("print1todeleteit\n");
printf("print0toreturn\n");
scanf("%d",
if(d==1)
{
InitScoreFilesa();
pain_boarda();
}
}
else
{
intsure=0;
initgraph(
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("\n");
printf("Areyousureexitthegame?
\n");
printf("\n");
printf("1-yes2-no\n");
scanf("%d",
if(sure==1)
{
printf("seeyounexttime\n");
break;}
}
getch();
}
closegraph();
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 俄罗斯方块 编程 源代码