C语言小游戏源代《俄罗斯方块》

C罗的意志 贡献于2013-10-14

作者 lixi  创建于2012-01-10 16:17:00   修改者lixi  修改于2012-01-10 16:17:00字数8260

文档摘要:C语言小游戏源代码《俄罗斯方块
关键词:

C语言小游戏源代码《俄罗斯方块》 #include #include #include #define ESC 27 #define UP 328 #define DOWN 336 #define LEFT 331 #define RIGHT 333 #define BLANK 32 #define BOTTOM 2 #define CANNOT 1 #define CAN 0 #define MAX 30 #define F1 315 #define ADD 43 #define EQUAL 61 #define DEC 45 #define SOUNDs 115 #define SOUNDS 83 #define PAUSEP 80 #define PAUSEp 112 void Init(); void Down(); void GoOn(); void ksdown(); void Display(int color); void Give(); int Touch(int x,int y,int dx,int dy); int GeyKey(); void Select(); void DetectFill(); void GetScores(); void Fail(); void Help(); void Quit(); void DrawBox(int x,int y,int Color); void OutTextXY(int x,int y,char *String); void DispScore(int x,int y,char Ch); void DrawNext(int Color); int Heng=12,Shu=20; /*横竖*/ int Position[MAX][MAX]; int middle[MAX][MAX]; int ActH,ActS; int Act,Staus; int i,j,k; int Wid=10; int NoPass=CAN; float Delays=15000; int BeginH=250,BeginS=7; float Seconds=0; int Scores=0; int flag=1; int Sounds=CAN; int PreAct,NextAct; int a[8][4][4][4]={{{1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0}, {1,1,1,1,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,0}, {1,1,1,1,0,0,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}, {1,1,0,0,1,1,0,0,0,0,0,0,0,0,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,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}, {0,1,1,0,1,1,0,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}, {1,1,0,0,0,1,1,0,0,0,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,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}}, {{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,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}}, {{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}}; int b[4][4]; main(int argc,char *argv[]) { if (argc!=1) { if (argv[1]!="") Heng=atoi(argv[1]); if (argv[2]!="") Shu=atoi(argv[2]); } Init(); /*初始化界面*/ PreAct=random(8); /*取得当前的方块*/ for(;;) /*以下是游戏流程*/ { NextAct=random(8); /*取得下一个方块*/ DrawNext(1); /*画出下一个方块*/ Act=PreAct; if (Heng%2==0) ActH=Heng/2; else ActH=(Heng-1)/2; ActS=0; /*方块开始从游戏空间的中间下落*/ Staus=0; /*取开始的状态*/ NoPass=CAN; /*物体可以下落*/ Give(); /*取得当前的方块*/ Display(Act+1); /*显示当前的方块,每种方块的颜色不同*/ GoOn(); /*游戏的算法精髓所在*/ PreAct=NextAct; /*方块下落完毕,取得下一个方块*/ DrawNext(0); } } void Init() { int GraphDriver=DETECT,GraphMode; registerbgidriver(EGAVGA_driver); initgraph(&GraphDriver,&GraphMode,""); if (kbhit()) Sounds=CANNOT; setcolor(1); OutTextXY(10,10,"Tetris"); OutTextXY(30,30,"Version 2.0"); OutTextXY(10,120,"Help:"); OutTextXY(20,140,"+ :Faster"); OutTextXY(20,160,"- :Slower"); OutTextXY(20,180,"Esc :Quit"); OutTextXY(20,200,"F1 :Help"); OutTextXY(10,310,"Copyright(c) 1998.2.22"); OutTextXY(10,320,"By Mr. Unique"); outtextxy(10,250,"Score: 00000"); rectangle(BeginH-3,BeginS-3,BeginH+Heng*(Wid+2)+2,BeginS+Shu*(Wid+2)+2); rectangle(BeginH-5,BeginS-5,BeginH+Heng*(Wid+2)+4,BeginS+Shu*(Wid+2)+4); rectangle(BeginH+(Heng+4)*(Wid+2)-2,BeginS+10,BeginH+(Heng+8)*(Wid+2)+2,BeginS+12+4*(Wid+2)); for (i=0;i=Delays) { Down(); Seconds=0; if (NoPass==BOTTOM) { DetectFill(); middle[ActH][ActS]=Act; if (ActS==0) Fail(); return; } } if (kbhit()) Select(); } } void Down() /*方块下降*/ { Display(0); if (Touch(ActH,ActS,0,1)==CAN) ActS++; else middle[ActH][ActS]=Act; Display(Staus+1); } int Touch(int x,int y,int dx,int dy) { NoPass=CAN; for (i=0;i<4;i++) for (j=0;j<4;j++) Position[x+dx+i][y+dy+j]+=b[i][j]; for (i=0;i1) NoPass=CANNOT; for (i=0;i<4;i++) for (j=0;j<4;j++) { Position[x+dx+i][y+dy+j]-=b[i][j]; middle[x+dx+i][y+dy+j]=Act; } if (NoPass==CANNOT && dx==0 && dy==1) { for (i=0;i<4;i++) for (j=0;j<4;j++) Position[x+i][y+j]+=b[i][j]; NoPass=BOTTOM; } return NoPass; } int GetKey(void) { int Ch,Low,Hig; Ch=bioskey(0); Low=Ch&0x00ff; Hig=(Ch&0xff00)>>8; return(Low==0?Hig+256:Low); } void Select() { int OldStaus,acts=ActS; switch(GetKey()) { case ESC :Quit();break; case DOWN :Seconds+=14500;break; case LEFT :Display(0); if (ActH>0 && Touch(ActH,ActS,-1,0)==CAN) { ActH--;} Display(Act+1);break; case RIGHT :Display(0); if (ActH300) Delays-=100;break; case DEC :if (Delays<3000) Delays+=100;break; case PAUSEP : case PAUSEp :getch();break; case SOUNDS : case SOUNDs :if (Sounds==CAN) Sounds=CANNOT; else Sounds=CAN;break; case UP :if(Act==7) { while(actsTopScore) { setcolor(1); outtextxy(470,80,"Hello !"); outtextxy(470,100,"In all the players,"); outtextxy(470,120,"You are the First !"); outtextxy(470,140,"And your score will"); outtextxy(470,160,"be the NEW RECORD !"); fseek(fp,0L,0); fprintf(fp,"%d",Scores); } fclose(fp); } setcolor(1); OutTextXY(470,220,"Are You Sure (Yes/no)?"); ch=getch(); if (ch=='y'||ch=='Y') { closegraph(); delay(20); exit(0); } setcolor(0); outtextxy(470,220,"Are You Sure (Yes/no)?"); } void OutTextXY(int x,int y,char *String) { int i=0; char a[2]; moveto(x,y); a[1]='\0'; while (*(String+i)!='\0') { a[0]=*(String+i); outtext(a); if (Sounds==CAN && a[0]!=' ') { sound(3000); delay(50); nosound(); } i++; } } void Help() { unsigned Save; void *Buf; Save=imagesize(160,120,500,360); Buf=malloc(Save); getimage(160,120,500,360,Buf); setfillstyle(1,1); bar(160,120,500,280); setcolor(0); OutTextXY(170,130," About & Help"); OutTextXY(170,150," # # # ########## # # # "); OutTextXY(170,160," # ## # # # # # # ###### ### "); OutTextXY(170,170," ########## ########## ## # # "); OutTextXY(170,180," # # # # # # # ## #### "); OutTextXY(170,190," # ## # #### ## # # # "); OutTextXY(170,200," # ## # # # # # ## # # # "); OutTextXY(170,210," # # # ## ## # ###### # # # "); OutTextXY(170,220," ## # ## # ## # # # # "); OutTextXY(170,230," # ## # #### # ## # "); OutTextXY(170,260," Good Luckly to You !!! "); getch(); putimage(160,120,Buf,0); free(Buf); } void GetScores() { int Sec10000,Sec1000,Sec100,Sec10,Sec1; setfillstyle(0,1); bar(60,250,109,260); Sec1=Scores%10; Sec10=(Scores%100-Scores%10)/10; Sec100=(Scores%1000-Scores%100)/100; Sec1000=(Scores%10000-Scores%1000)/1000; Sec10000=(Scores%100000-Scores%10000)/10000; DispScore(60,250,'0'+Sec10000); DispScore(70,250,'0'+Sec1000); DispScore(80,250,'0'+Sec100); DispScore(90,250,'0'+Sec10); DispScore(100,250,'0'+Sec1); DispScore(110,250,'0'); DispScore(120,250,'0'); } void DispScore(int x,int y,char Ch) { char a[2]; a[1]='\0'; a[0]=Ch; outtextxy(x,y,a); } void Give() { for (i=0;i<4;i++) for (j=0;j<4;j++) b[i][j]=a[Act][Staus][i][j]; } void Display(int color) { for (i=0;i<4;i++) for (j=0;j<4;j++) if (b[i][j]==1) DrawBox(ActH+i,ActS+j,color); } void DrawBox(int x,int y,int Color) { x=BeginH+x*(Wid+2); y=BeginS+y*(Wid+2); setfillstyle(1,Color); bar(x+2,y+2,x+Wid-1,y+Wid-1); if (Color==0) setcolor(9); else setcolor(Act+1); rectangle(x+4,y+4,x+Wid-4,y+Wid-4); } void DrawNext(int Color) { for (i=0;i<4;i++) for (j=0;j<4;j++) if (a[NextAct][0][i][j]==1) DrawBox(Heng+4+i,1+j,Color); } void DetectFill() { int Number,Fall,FallTime=0; for (i=Shu-1;i>=0;i--) { Number=0; for (j=0;j0;Fall--) for (j=0;j

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 3 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档