1、C语言小游戏俄罗斯方块代码及结果C语言小游戏俄罗斯方块代码及结果代码(结过在下面)#include #include #include #include #include #define BLKCHAR #define NUCHAR #define P_X 8#define P_Y 5#define M_X 10#define M_W 58#define M_Y 13#define M_H 11#define NEXTBLOCK_X 29#define NEXTBLOCK_Y 10#define PL_W 12#define PL_H 20#define N_S 4#define N_T 7#
2、define B_W 4#define B_H 4#define INFO_NAME_X 32#define INFO_NAME_Y 20#define INFO_LEVEL_X 32#define INFO_LEVEL_Y 23#define INFO_MARK_X 32#define INFO_MARK_Y 26#define TIME_WAIT 10#define TIME_FALL 50/curuser.level#define WIN_CWSIZE mode con cols=80 lines=30#define FILE_WELCOME welcome#define FILE_OU
3、TLINE outline#define FILE_GAMEOVER gameover#define FILE_BLOCKS blocks#define FILE_PROGRESS progress#define FILE_HELP help#define DEF_NAME NOMANEstruct tpblk int x, y; int type; int state;struct tpuser long level; long mark; char name30;curuser;int panelPL_HPL_W=0;int blocksN_TN_SB_HB_W;void gotoxy(i
4、nt x,int y) COORD c; c.X = x*2, c.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c); struct tpblk initblock() struct tpblk rdblk; rdblk.x=PL_W/2; rdblk.y=-B_H+2; rdblk.type=rand()%N_T; rdblk.state=rand()%N_S; return rdblk;void prtfiletoscr(char *file) FILE *fp; char str100; system(
5、cls); gotoxy(0,0); if(fp=fopen(file,r)=NULL) printf(打开文件:%s失败!n,file); getch(); exit(0); while(fgets(str,100,fp)!=NULL) printf(%s,str); fclose(fp);/下一个方块void prtnxtblk(struct tpblk nxtblock) int i,j; for(i=0;iB_H;i+) for(j=0;jB_W;j+) gotoxy(NEXTBLOCK_X+j,NEXTBLOCK_Y+i); printf(%s,blocksnxtblock.type
6、nxtblock.stateij?BLKCHAR:NUCHAR); /打印面板void prtpanel() int i,j; for(i=0;iPL_H;i+) gotoxy(P_X,P_Y+i); for(j=0;jx-;break; case 77: block-x+;break; case 72: case : case 13: if(block-state=N_S-1)block-state=0; else block-state+;break; case -1:/自动下落 case 80: block-y+;break; case p:/暂停 gotoxy(5,3); printf
7、(暂停中.按任意键恢复。); getch(); gotoxy(5,3); printf( ); return 2; break; case 27:/esc return -1; break; default: return 2; for(i=0;iB_H;i+) for(j=0;jtypeblock-stateij)/保证有点 if(block-x+jx+j=PL_W)return 1;/左右边界 if(block-y+i=PL_H)return 1;/是否到底 if(block-y+i=0&panelblock-y+iblock-x+j)return 1;/与其他块碰撞 return 0;v
8、oid moveblock(struct tpblk oldblock,struct tpblk newblock) int i,j; /清除原有的方块 for(i=0;iB_H;i+) for(j=0;j=0) gotoxy(P_X+oldblock.x+j,P_Y+oldblock.y+i); printf(%s,NUCHAR); /画新方块 for(i=0;iB_H;i+) for(j=0;j=0) /面板中 gotoxy(P_X+newblock.x+j,P_Y+newblock.y+i); printf(%s,BLKCHAR); void loadblocks() FILE *fp;
9、 int i,j,cx,cy,ch; /blocks初始化 if(fp=fopen(FILE_BLOCKS,rt)=NULL) gotoxy(0,0); printf(打开文件:%s失败n,FILE_BLOCKS); getch(); exit(0); for(i=0;iN_T;i+) for(j=0;jN_S;j+) for(cy=0;cyB_H;cy+) for(cx=0;cxB_W;cx+) while(ch=fgetc(fp)-48)!=0&ch!=1) if(ch=EOF) gotoxy(0,0); printf(读取文件:%s错误!n,FILE_BLOCKS); getch();e
10、xit(0); blocksijcycx=ch; fclose(fp); void saveprogress(struct tpuser user) FILE* fp; struct tpuser tmpuser10; int iffind=0,i; if(!strcmp(user.name,DEF_NAME) return; if(fp=fopen(FILE_PROGRESS,rb+)=NULL) printf(打开文件:%s失败!n,FILE_PROGRESS); getch(); exit(0); for(i=0;iffind=0&fread(&tmpuseri,sizeof(struc
11、t tpuser),1,fp);i+) if(!strcmp(user.name,tmpuseri.name) iffind=1; if(iffind) fseek(fp,-(long)sizeof(struct tpuser),1); else fseek(fp,0,2); fwrite(&user,sizeof(struct tpuser),1,fp); if(ferror(fp) printf(存档错误!忽略按Entern,FILE_PROGRESS); while(getch()!=13); fclose(fp);int onbumpbtm(struct tpblk block) in
12、t i,j,lf,nlf=0; /合并 for(i=0;iB_H;i+) for(j=0;jB_W;j+) if(blocksblock.typeblock.stateij) /游戏结束? if(block.y+i=0)return -1; /合并 panelblock.y+iblock.x+j=1; gotoxy(P_X+block.x+j,P_Y+block.y+i); printf(%s,BLKCHAR); /是否满行? for(i=0;iB_H;i+) lf=1; for(j=0;j0;i-) for(j=0;jPL_W;j+) panelij=paneli-1j; nlf+; cur
13、user.mark+=nlf*nlf; saveprogress(curuser); return lf;void prtinfo() gotoxy(INFO_NAME_X,INFO_NAME_Y); printf(%s,curuser.name); gotoxy(INFO_LEVEL_X,INFO_LEVEL_Y); printf(%d,curuser.level); gotoxy(INFO_MARK_X,INFO_MARK_Y); printf(%d,curuser.mark);void clspanel() int i,j; for(i=0;iPL_H;i+) for(j=0;jPL_W
14、;j+) panelij=0;struct tpuser loadprogress() struct tpuser user10=0; FILE* fp; int i,j,c,sel=0,alex=0; if(fp=fopen(FILE_PROGRESS,rb)=NULL) printf(打开文件:%s失败!n,FILE_PROGRESS); getch(); exit(0); for(i=M_Y;i=M_Y+M_H;i+) gotoxy(M_X/2,i); for(j=M_X/2;j=M_X/2+M_W;j+) putchar(); for(i=0;fread(&useri,sizeof(s
15、truct tpuser),1,fp);i+) gotoxy(M_X+5,M_Y+2+i); printf(NAME:%s LEVEL:%ld MARK:%ld,useri.name,useri.level,useri.mark); gotoxy(M_X+5,M_Y+2+i); printf(NEW USER); fclose(fp); gotoxy(M_X+3,M_Y+2+sel); printf(); while(1) c=getch(); switch(c) case 72:/up gotoxy(M_X+3,M_Y+2+sel); printf(); if(sel=i)sel=0; el
16、se sel+; gotoxy(M_X+3,M_Y+2+sel); printf(); break; case 27:/esc user0.level=-1; return user0; break; case 13:/enter if(sel=i)/New User gotoxy(M_X+5,M_Y+2+i+1); printf(Input Name:); scanf(%s,useri.name); for(j=0;ji&!alex;j+) if(!strcmp(userj.name,useri.name) gotoxy(M_X+5,M_Y+2+i+1); printf(%salready
17、exist!,useri.name); getch(); alex=1; if(!alex) useri.level=1L; useri.mark=0; saveprogress(useri); if(usersel.name=0|usersel.level=0|usersel.mark=100*curuser.level) clspanel(); curuser.mark=0; curuser.level+; prtinfo(); prtpanel(); /产生新的BLOCK curblock=nxtblock; nxtblock=initblock(); /画下一个方块 prtnxtblk
18、(nxtblock); if(c=80)timewait=TIME_WAIT/2; gotoxy(0,0); iskh=0; Sleep(timewait); timewait=TIME_WAIT; if(timec=TIME_FALL) c=-1; iskh=1; timec=0; else timec+; return 0;int showmenu() int i,j,c,cursel=0; gotoxy(M_X,M_Y); for(i=M_Y;i=M_Y+M_H;i+) gotoxy(M_X/2,i); for(j=M_X/2;j=M_X/2+M_W;j+) / putchar(b);
19、/ gotoxy(j,i); putchar(); gotoxy(M_X+5,M_Y+2); printf(快 速 游 戏); gotoxy(M_X+5,M_Y+4); printf(读 取 存 档); gotoxy(M_X+5,M_Y+6); printf(帮 助); gotoxy(M_X+5,M_Y+8); printf(退 出); gotoxy(M_X+3,M_Y+2+cursel*2); printf(); while(1) c=getch(); switch(c) case 72:/up gotoxy(M_X+3,M_Y+2+cursel*2); printf(); if(curse
20、l=3)cursel=0; else cursel+; gotoxy(M_X+3,M_Y+2+cursel*2); printf(); break; case 27:/esc return 3; break; case 13:/enter return cursel; break; default:; return 0;int main() int looprst=0,ch; CONSOLE_CURSOR_INFO cursor_info = 1, 0; struct tpuser tmpuser; system(color F8); SetConsoleCursorInfo(GetStdHa
21、ndle(STD_OUTPUT_HANDLE), &cursor_info); system(WIN_CWSIZE); loadblocks(); while(1) prtfiletoscr(FILE_WELCOME); if(getch()=27) break; switch(showmenu() case 0:/fast mode strcpy(curuser.name,DEF_NAME); curuser.level=1; curuser.mark=0; clspanel(); looprst=gameloop(); break; case 1:/read tmpuser=loadprogress(); if(tmpuser.level!=-1) curuser=tmpuser;