C语言小游戏俄罗斯方块代码及结果Word文档下载推荐.docx
- 文档编号:7381297
- 上传时间:2023-05-08
- 格式:DOCX
- 页数:24
- 大小:299.95KB
C语言小游戏俄罗斯方块代码及结果Word文档下载推荐.docx
《C语言小游戏俄罗斯方块代码及结果Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《C语言小游戏俄罗斯方块代码及结果Word文档下载推荐.docx(24页珍藏版)》请在冰点文库上搜索。
help"
#defineDEF_NAME"
NOMANE"
structtpblk
{
intx,y;
inttype;
intstate;
};
structtpuser
longlevel;
longmark;
charname[30];
}curuser;
intpanel[PL_H][PL_W]={0};
intblocks[N_T][N_S][B_H][B_W];
voidgotoxy(intx,inty)
COORDc;
c.X=x*2,c.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
structtpblkinitblock()
structtpblkrdblk;
rdblk.x=PL_W/2;
rdblk.y=-B_H+2;
rdblk.type=rand()%N_T;
rdblk.state=rand()%N_S;
returnrdblk;
}
voidprtfiletoscr(char*file)
FILE*fp;
charstr[100];
system("
cls"
);
gotoxy(0,0);
if((fp=fopen(file,"
r"
))==NULL)
{
printf("
打开文件:
\"
%s\"
失败!
\n"
file);
getch();
exit(0);
}
while(fgets(str,100,fp)!
=NULL)
%s"
str);
fclose(fp);
//下一个方块
voidprtnxtblk(structtpblknxtblock)
inti,j;
for(i=0;
i<
B_H;
i++)
for(j=0;
j<
B_W;
j++)
{
gotoxy(NEXTBLOCK_X+j,NEXTBLOCK_Y+i);
printf("
blocks[nxtblock.type][nxtblock.state][i][j]?
BLKCHAR:
NUCHAR);
}
//打印面板
voidprtpanel()
PL_H;
gotoxy(P_X,P_Y+i);
PL_W;
{
panel[i][j]?
//putchar(0);
//返回是否有效
intonkeyboard(intc,structtpblk*block)
{
switch(c)
case75:
block->
x--;
break;
case77:
x++;
case72:
case'
'
:
case13:
if(block->
state>
=N_S-1)block->
state=0;
elseblock->
state++;
case-1:
//自动下落
case80:
y++;
p'
//暂停
gotoxy(5,3);
暂停中...按任意键恢复。
"
"
return2;
break;
case27:
//esc
return-1;
default:
if(blocks[block->
type][block->
state][i][j])//保证有点
{
if(block->
x+j<
0||block->
x+j>
=PL_W)return1;
//左右边界
y+i>
=PL_H)return1;
//是否到底
=0&
&
panel[block->
y+i][block->
x+j])return1;
//与其他块碰撞
}
return0;
voidmoveblock(structtpblkoldblock,structtpblknewblock)
//清除原有的方块
if(blocks[oldblock.type][oldblock.state][i][j]&
oldblock.y+i>
=0)
gotoxy(P_X+oldblock.x+j,P_Y+oldblock.y+i);
printf("
NUCHAR);
//画新方块
for(i=0;
for(j=0;
{
if(blocks[newblock.type][newblock.state][i][j]&
newblock.y+i>
{//面板中
gotoxy(P_X+newblock.x+j,P_Y+newblock.y+i);
printf("
BLKCHAR);
}
voidloadblocks()
inti,j,cx,cy,ch;
//blocks初始化
if((fp=fopen(FILE_BLOCKS,"
rt"
gotoxy(0,0);
%s失败\n"
FILE_BLOCKS);
N_T;
N_S;
for(cy=0;
cy<
cy++)
for(cx=0;
cx<
cx++)
{
while((ch=fgetc(fp)-48)!
ch!
=1)
{if(ch==EOF)
{
gotoxy(0,0);
printf("
读取文件:
%s错误!
getch();
exit(0);
}
}
blocks[i][j][cy][cx]=ch;
voidsaveprogress(structtpuseruser)
FILE*fp;
structtpusertmpuser[10];
intiffind=0,i;
if(!
strcmp(user.name,DEF_NAME))return;
if((fp=fopen(FILE_PROGRESS,"
rb+"
FILE_PROGRESS);
}
iffind==0&
fread(&
tmpuser[i],sizeof(structtpuser),1,fp);
if(!
strcmp(user.name,tmpuser[i].name))
iffind=1;
if(iffind)
fseek(fp,-(long)sizeof(structtpuser),1);
else
fseek(fp,0,2);
fwrite(&
user,sizeof(structtpuser),1,fp);
if(ferror(fp))
存档错误!
忽略按Enter\n"
while(getch()!
=13);
intonbumpbtm(structtpblkblock)
inti,j,lf,nlf=0;
//合并
if(blocks[block.type][block.state][i][j])
//游戏结束?
?
if(block.y+i<
=0)return-1;
//合并
panel[block.y+i][block.x+j]=1;
gotoxy(P_X+block.x+j,P_Y+block.y+i);
//是否满行?
lf=1;
if(!
panel[block.y+i][j])
{lf=0;
if(lf)//满行
for(i=block.y+i;
i>
0;
i--)
for(j=0;
panel[i][j]=panel[i-1][j];
nlf++;
curuser.mark+=nlf*nlf;
saveprogress(curuser);
returnlf;
voidprtinfo()
gotoxy(INFO_NAME_X,INFO_NAME_Y);
printf("
curuser.name);
gotoxy(INFO_LEVEL_X,INFO_LEVEL_Y);
%d"
curuser.level);
gotoxy(INFO_MARK_X,INFO_MARK_Y);
curuser.mark);
voidclspanel()
panel[i][j]=0;
structtpuserloadprogress()
structtpuseruser[10]={0};
inti,j,c,sel=0,alex=0;
rb"
for(i=M_Y;
=M_Y+M_H;
gotoxy(M_X/2,i);
for(j=M_X/2;
=M_X/2+M_W;
putchar('
'
user[i],sizeof(structtpuser),1,fp);
gotoxy(M_X+5,M_Y+2+i);
NAME:
%sLEVEL:
%ldMARK:
%ld"
user[i].name,user[i].level,user[i].mark);
gotoxy(M_X+5,M_Y+2+i);
[NEWUSER]"
gotoxy(M_X+3,M_Y+2+sel);
◆"
while
(1)
c=getch();
switch(c)
case72:
//up
gotoxy(M_X+3,M_Y+2+sel);
"
if(sel<
=0)sel=i;
elsesel--;
break;
case80:
//down
if(sel>
=i)sel=0;
elsesel++;
case27:
user[0].level=-1;
returnuser[0];
case13:
//enter
if(sel==i)//NewUser
gotoxy(M_X+5,M_Y+2+i+1);
InputName:
scanf("
user[i].name);
for(j=0;
i&
!
alex;
if(!
strcmp(user[j].name,user[i].name))
{
gotoxy(M_X+5,M_Y+2+i+1);
alreadyexist!
getch();
alex=1;
alex)
user[i].level=1L;
user[i].mark=0;
saveprogress(user[i]);
if(user[sel].name<
=0||user[sel].level<
=0||user[sel].mark<
0)user[sel].level=-1;
returnuser[sel];
default:
;
user[0].level=-1;
returnuser[0];
intgameloop()
intiskh=0,c,val,timec=0,bumpst=0,timewait=TIME_WAIT;
structtpblkcurblock,nxtblock,tmpblock;
curblock=initblock();
nxtblock=initblock();
prtfiletoscr(FILE_OUTLINE);
prtpanel();
prtinfo();
prtnxtblk(nxtblock);
if(kbhit()||iskh)
tmpblock=curblock;
if(!
iskh)c=getch();
val=onkeyboard(c,&
tmpblock);
if(val==2)continue;
elseif(val==-1)return2;
elseif(val==0)
moveblock(curblock,tmpblock);
curblock=tmpblock;
elseif(val==1&
(c==-1||c==80))//向下撞到
bumpst=onbumpbtm(curblock);
if(bumpst==-1)return1;
else
//是否过关
if(curuser.mark>
=100*curuser.level)
clspanel();
curuser.mark=0;
curuser.level++;
prtinfo();
prtpanel();
//产生新的BLOCK
curblock=nxtblock;
nxtblock=initblock();
//画下一个方块
prtnxtblk(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;
elsetimec++;
intshowmenu()
inti,j,c,cursel=0;
gotoxy(M_X,M_Y);
//putchar('
\b'
//gotoxy(j,i);
gotoxy(M_X+5,M_Y+2);
快速游戏"
gotoxy(M_X+5,M_Y+4);
读取存档"
gotoxy(M_X+5,M_Y+6);
帮助"
gotoxy(M_X+5,M_Y+8);
退出"
gotoxy(M_X+3,M_Y+2+cursel*2);
gotoxy(M_X+3,M_Y+2+cursel*2);
if(cursel<
=0)cursel=3;
elsecursel--;
if(cursel>
=3)cursel=0;
elsecursel++;
return3;
returncursel;
intmain()
intlooprst=0,ch;
CONSOLE_CURSOR_INFOcursor_info={1,0};
structtpusertmpuser;
colorF8"
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&
cursor_info);
system(WIN_CWSIZE);
loadblocks();
prtfiletoscr(FILE_WELCOME);
if(getch()==27)break;
switch(showmenu())
case0:
//fastmode
strcpy(curuser.name,DEF_NAME);
curuser.level=1;
curuser.mark=0;
clspanel();
looprst=gameloop();
case1:
//read
tmpuser=loadprogress();
if(tmpuser.level!
=-1)
curuser=tmpuser;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 小游戏 俄罗斯方块 代码 结果
![提示](https://static.bingdoc.com/images/bang_tan.gif)