c语言小游戏俄罗斯方块代码及结果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 NEX...
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
#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_OUTLINE "outline" #define FILE_GAMEOVER "gameover" #define FILE_BLOCKS "blocks" #define FILE_PROGRESS "progress" #define FILE_HELP "help"
#define DEF_NAME "NOMANE"
struct tpblk
{
int x, y;
int type;
int state;
};
struct tpuser
{
long level;
long mark;
char name[30];
}curuser;
int panel[PL_H][PL_W]={0}; int blocks[N_T][N_S][B_H][B_W];
void gotoxy(int x,int y) {
COORD c;
c.X = x*2, c.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTP
UT_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 str[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)
printf("%s",str);
fclose(fp);
}
//下一个方块
void prtnxtblk(struct tpblk nxtblock)
{
int i,j;
for(i=0;ix--;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("暂停中...按任意键恢复。");
getch();
gotoxy(5,3);
printf(" ");
return 2;
break;
case 27://esc
return -1;
break;
default:
return 2;
}
for(i=0;itype][block->state][i][j])//保证有点
{
if(block->x+j<0||block->x+j>=PL_W)return
1;//左右边界
if(block->y+i>=PL_H)return 1;//是否到底
if(block->y+i>=0&&panel[block->y+i][block->x+j])retur
n 1;//与其他块碰撞
}
return 0;
}
void moveblock(struct tpblk oldblock,struct tpblk
newblock)
{
int i,j;
//清除原有的方块
for(i=0;i=0)
{
gotoxy(P_X+oldblock.x+j,P_Y+oldblock.y+i);
printf("%s",NUCHAR);
}
}
//画新方块
for(i=0;i=0)
{//面板中
gotoxy(P_X+newblock.x+j,P_Y+newblock.y+i);
printf("%s",BLKCHAR);
}
}
}
void loadblocks()
{
FILE *fp;
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;i0;i--)
for(j=0;j=i)sel=0;
else sel++;
gotoxy(M_X+3,M_Y+2+sel); printf("?");
break;
case 27://esc
user[0].level=-1;
return user[0];
break;
case 13://enter
if(sel==i)//New User
{
gotoxy(M_X+5,M_Y+2+i+1);
printf("Input Name:");
scanf("%s",user[i].name);
for(j=0;j=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;
}
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');
// 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(cursel<=0)cursel=3;
else cursel--;
gotoxy(M_X+3,M_Y+2+cursel*2); printf("?");
break;
case 80://down
gotoxy(M_X+3,M_Y+2+cursel*2); printf(" ");
if(cursel>=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(GetStdHandle(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;
clspanel();
looprst=gameloop();
}
break;
case 2://help
prtfiletoscr(FILE_HELP);
getch();
break;
case 3://esc
break;
}
if(looprst==1)
{
prtfiletoscr(FILE_GAMEOVER);
gotoxy(10,3);
printf("[%s] [LEVEL %ld]
[MARK %ld]",curuser.name,curuser.level,curuser.mark);
while((ch=getch())!=27&&ch!=13);
looprst=0;
}
}
return 0;
}
主界面
选择界面
运 行 界 面
帮助界面
本文档为【c语言小游戏俄罗斯方块代码及结果】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。