c语言游戏.docx
《c语言游戏.docx》由会员分享,可在线阅读,更多相关《c语言游戏.docx(40页珍藏版)》请在冰豆网上搜索。
c语言游戏
#include"graphics.h"/*头文件*/
#include"time.h"
#include"stdlib.h"
#include"bios.h"
#include"dos.h"
#include"stdio.h"
#defineESC0x11b/*键盘扫描码*/
#defineUP0x4800
#defineDOWN0x5000
#defineLEFT0x4b00
#defineF10x3b00
#defineRIGHT0x4d00
#defineYES0x1579
#defineNO0x316e
#defineRESTART0x1372
structdiamond/*记录每种方块每种状态的信息*/
{
intx[4];
inty[4];
intstart_x;
intstart_y;
intcolor;
structdiamond*next;
};
intgrid[12][23];/*记录所有格子的状态(0)没有方块
(1)有固定方块
(2)有活动方块*/
intx;/*活动方块所在位置*/
inty;
intlevel;/*游戏难度*/
intcount;/*计数器*/
intscore;/*得分*/
structdiamond*nowinfo;/*当前活动方块*/
structdiamond*nextinfo;/*下一个方块*/
intcolor;/*画格子的颜色*/
intbackcolor;/*游戏区域背景色*/
voidshow_interface(intmode);/*显示游戏界面*/
voidshow_copsign(inttopx,inttopy,intsize,intcolor);/*显示公司标志--恒基伟业*/
voidshow_intro(intxs,intys);/*显示软件介绍区*/
/*voidprint();测试用函数*/
voidscandel();/*扫描所有格子看是否有可消除行*/
voidshow_down();/*方块下落后的下一个状态*/
voidshow_next();/*方块翻转后的下一个状态*/
voidshow_left();/*方块向左移动后的下一个状态*/
voidshow_right();/*方块向右移动后的下一个状态*/
voidinterrupt(*oldtimer)();/*指向未安装前的中断向量,即函数指针,指向一段可执行的代码*/
voidinstall();/*安装新的中断向量,即将中断服务程序安装到中断向量表中*/
voidinterruptnewtimer();/*中断服务程序*/
structdiamond*get_diamond();/*随机得到一个方块*/
structdiamond*create_I();/*函数用来构造各种类形方块的环形链表*/
structdiamond*create_T();/*返回链表中随机一个状态的指针*/
structdiamond*create_L();
structdiamond*create_J();
structdiamond*create_Z();
structdiamond*create_N();
structdiamond*create_H();
voiddelinfo(structdiamond*info);/*释放当前方块所占用的空间*/
voidaddtobuffer(intc);/*向键盘缓冲区中增加一个DOWN*/
/*voidclrkey();调用dos中断清空键盘缓冲区,未使用此方法.*/
voidshowsubwin(structdiamond*next);/*在小窗口显示下一个方块*/
voidshowscore(intscoreget);/*显示分数*/
voidstartset();/*初始化游戏*/
main()
{
intdriver=VGA;
intmode=VGAHI;
inti;/*计数器*/
intj;
intkey;/*记录键盘扫描码*/
initgraph(&driver,&mode,"");/*初始化*/
color=8;
backcolor=7;
level=1;
score=298;
count=0;
show_interface(9);/*显示界面*/
setfillstyle(SOLID_FILL,1);
bar(0,465,640,480);
outtextxy(5,469,"Pressanykeytostart...");
getch();
setfillstyle(SOLID_FILL,9);
bar(0,465,640,480);
showscore(0);/*显示分数*/
randomize();
nowinfo=get_diamond();/*得到一个当前方块*/
oldtimer=getvect(0x1c);
install(newtimer);
for(i=0;i<=21;i++)/*初始化grid[12][23]*/
{
for(j=1;j<=10;j++)
grid[j]=0;
}
for(i=0;i<=22;i++)
{
grid[0]=1;
grid[11]=1;
}
for(i=0;i<=11;i++)
{
grid[22]=1;
}
x=nowinfo->start_x;/*初始化方块位置*/
y=nowinfo->start_y;
nextinfo=get_diamond();/*得到下一个方块*/
showsubwin(nextinfo);
for(;;)
{
key=bioskey(0);/*得到键盘扫描码*/
if(key)
{
switch(key)
{
caseDOWN:
{
show_down();
break;
}
caseUP:
{
show_next();
break;
}
caseLEFT:
{
show_left();
break;
}
caseRIGHT:
{
show_right();
break;
}
caseRESTART:
{
install(oldtimer);
setfillstyle(SOLID_FILL,1);
bar(0,465,640,480);
outtextxy(5,469,"Areyousuretorestart(Y/N)...");
for(;;)
{
key=bioskey(0);/*得到键盘扫描码*/
if(key==YES)
{
startset();
setfillstyle(SOLID_FILL,9);
bar(0,465,640,480);
break;
}
if(key==NO)
{
setfillstyle(SOLID_FILL,9);
bar(0,465,640,480);
install(newtimer);
break;
}
}
break;
}
/*caseF1:
{
print();
break;
}
*/
caseESC:
{
install(oldtimer);
setfillstyle(SOLID_FILL,1);
bar(0,465,640,480);
outtextxy(5,469,"Areyousuretoexit(Y/N)...");
for(;;)
{
key=bioskey(0);/*得到键盘扫描码*/
if(key==YES)
{
closegraph();
exit(0);
}
if(key==NO)
{
setfillstyle(SOLID_FILL,9);
bar(0,465,640,480);
install(newtimer);
break;
}
}
}
}
}
}
}
structdiamond*get_diamond()
{
structdiamond*now;
switch(random(7)+1)
{
case1:
{
now=create_I();
returnnow;
}
case2:
{
now=create_T();
returnnow;
}
case3:
{
now=create_L();
returnnow;
}
case4:
{
now=create_J();
returnnow;
}
case5:
{
now=create_Z();
returnnow;
}
case6:
{
now=create_N();
returnnow;
}
case7:
{
now=create_H();
returnnow;
}
}
}
voidshow_interface(intfill_mode)
{
inti;
setbkcolor(9);
setcolor(color);
setfillstyle(SOLID_FILL,backcolor);
bar(100,60,300,420);
bar(360,60,440,140);
rectangle(100,60,300,420);
rectangle(96,56,304,424);
rectangle(360,60,440,140);
rectangle(356,56,444,144);
setfillstyle(fill_mode,14);
floodfill(97,57,color);
floodfill(397,57,color);
setcolor
(1);
rectangle(96,56,304,424);
setcolor(color);
for(i=80;i<=400;i+=20)
{
line(100,i,300,i);
}
for(i=120;i<=280;i+=20)
{
line(i,60,i,420);
}
for(i=80;i<=120;i+=20)
{
line(360,i,440,i);
}
for(i=380;i<=420;i+=20)
{
line(i,60,i,140);
}
show_intro(360,180);
show_copsign(475,320,40,1);
setfillstyle(SOLID_FILL,1);
setcolor(14);
rectangle(420,405,534,417);
floodfill(421,406,14);
outtextxy(422,408,"HI-TECHWEALTH");
}
voidshow_copsign(inttopx,inttopy,intsize,intcolor)
{
inthalfsize,qtrsize;
intxadd,xdel,yadd1,yadd2;
halfsize=0.5*size;
qtrsize=0.25*size;
xadd=topx+size;
xdel=topx-size;
yadd1=topy+size;
yadd2=topy+2*size;
setcolor(color);
line(topx,topy,xdel,yadd1);
line(xdel,yadd1,topx,yadd2);
line(topx,yadd2,xadd,yadd1);
line(xadd,yadd1,topx,topy);
rectangle(topx-halfsize,topy+halfsize,topx+halfsize,yadd1+halfsize);
setfillstyle(SOLID_FILL,color);
floodfill(topx,topy+1,color);
floodfill(xdel+1,yadd1,color);
floodfill(topx,yadd2-1,color);
floodfill(xadd-1,yadd1,color);
rectangle(topx-halfsize,yadd1-qtrsize,topx-0.75*halfsize,yadd1+qtrsize);
floodfill(topx-halfsize+1,yadd1-qtrsize+1,color);
rectangle(topx-qtrsize,yadd1-halfsize,topx+qtrsize,yadd1-0.25*halfsize);
floodfill(topx-qtrsize+1,yadd1-halfsize+1,color);
rectangle(topx+0.75*halfsize,yadd1-qtrsize,topx+halfsize,yadd1+qtrsize);
floodfill(topx+0.75*halfsize+1,yadd1-qtrsize+1,color);
rectangle(topx-qtrsize,yadd1+0.25*halfsize,topx+qtrsize,yadd2-halfsize);
floodfill(topx-qtrsize+1,yadd1+0.25*halfsize+1,color);
setcolor(14);
line(topx,topy-1,xdel-1,yadd1);
line(xdel-1,yadd1,topx,yadd2+1);
line(topx,yadd2+1,xadd+1,yadd1);
line(xadd+1,yadd1,topx,topy-1);
setfillstyle(SOLID_FILL,14);
floodfill(topx,yadd1,color);
}
voidshow_intro(intxs,intys)
{
charstemp[20];
setcolor(15);
rectangle(xs-3,ys-3,xs+239,ys+115);
line(xs-3,ys+26,xs+239,ys+26);
line(xs-3,ys+72,xs+239,ys+72);
outtextxy(xs,ys,"Level:
");
outtextxy(xs,ys+15,"Score:
");
sprintf(stemp,"-Roll-Downwards");
stemp[0]=24;
stemp[7]=25;
outtextxy(xs,ys+30,"Help:
");
setcolor(14);
outtextxy(xs+40,ys+30,stemp);
outtextxy(xs+40,ys+45,"<-TurnLeft>-TurnRight");
outtextxy(xs+40,ys+60,"Esc-ExitR-Restart");
outtextxy(xs,ys+75,"RussiaDiamondsVer1.0");
outtextxy(xs,ys+90,"CopyRightByGZcleverboy");
outtextxy(xs,ys+105,"Email:
lihongx2007@");
}
structdiamond*create_I()
{
structdiamond*info;
structdiamond*first;
first=(structdiamond*)malloc(sizeof(structdiamond));
info=(structdiamond*)malloc(sizeof(structdiamond));
first->next=info;
info->next=first;
first->x[0]=0;
first->y[0]=0;
first->x[1]=-1;
first->y[1]=0;
first->x[2]=1;
first->y[2]=0;
first->x[3]=2;
first->y[3]=0;
first->start_x=5;
first->start_y=3;
first->color=2;
info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=-1;
info->x[2]=0;
info->y[2]=1;
info->x[3]=0;
info->y[3]=2;
info->start_x=5;
info->start_y=1;
info->color=2;
if(random
(2)==0){returnfirst;}
else{returnfirst->next;}
}
structdiamond*create_T()
{
structdiamond*info;
structdiamond*first;
structdiamond*last;
inti;
info=(structdiamond*)malloc(sizeof(structdiamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=-1;
info->y[1]=0;
info->x[2]=0;
info->y[2]=-1;
info->x[3]=1;
info->y[3]=0;
info->start_x=5;
info->start_y=3;
info->color=4;
first=info;
last=info;
info=(structdiamond*)malloc(sizeof(structdiamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=1;
info->x[2]=0;
info->y[2]=-1;
info->x[3]=1;
info->y[3]=0;
info->start_x=5;
info->start_y=2;
info->color=4;
last->next=info;
last=info;
info=(structdiamond*)malloc(sizeof(structdiamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=-1;
info->y[1]=0;
info->x[2]=1;
info->y[2]=0;
info->x[3]=0;
info->y[3]=1;
info->start_x=5;
info->start_y=2;
info->color=4;
last->next=info;
last=info;
info=(structdiamond*)malloc(sizeof(structdiamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=1;
info->x[2]=0;
info->y[2]=-1;
info->x[3]=-1;
info->y[3]=0;
info->start_x=5;
info->start_y=2;
info->color=4;
last->next=info;
last=info;
last->next=first;
for(i=0;i<=random(4);i++)
{
first=first->next;
}
returnfirst;
}
structdiamond*create_L()
{
structdiamond*info;
structdiamond*first;
structdiamond*last;
inti;
info=(structdiamond*)malloc(sizeof(structdiamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=1;
info->x[2]=0;
info->y[2]=-1;
info->x[3]=1;
info->y[3]=1;
info->start_x=5;
info->start_y=2;
info->color=5;
first=info;
last=info;
info=(structdiamond*)malloc(sizeof(structdiamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=-1;
info->y[1]=0;
info->x[2]=1;
info->y[2]=0;
info->x[3]=-1;
info->y[3]=1;
info->start_x=5;
info->start_y=2;
info->color=5;
last->next=info;
last=info;
info=(structdiamond*)malloc(sizeof(structdiamond));
info->x[0]=0;
info->y[0]=0;
info->x[1]=0;
info->y[1]=1;
info->x[2]=0;
info->y[2]=-1;
info->x[3]=-1;
info->y[3]=-1;
info->start_x=5;
info->start_y=2;
info->color=5;
last->next=info;
last=info;
info=(structdiamond*)malloc(sizeof(structdiamond));
info->x[0]=0;
i