C游戏编程从入门到精通 c游戏编程( 四 )


zuobiao[0][tail]=1;zuobiao[1][tail]=1;zuobiao[0][1]=1;zuobiao[1][1]=2;zuobiao[0][2]=1;zuobiao[1][2]=3;zuobiao[0][head]=1;zuobiao[1][head]=4;
/*处理棋盘*/
char qipan[20][80];//定义棋盘
for(i=0;i<20;i++)
for(j=0;j<80;j++)
qipan[i][j]=’ ‘;//初始化棋盘
for(i=0;i<80;i++)
qipan[0][i]=’_’;
for(i=0;i<20;i++)
qipan[i][0]=’|’;
for(i=0;i<20;i++)
qipan[i][79]=’|’;
for(i=0;i<80;i++)
qipan[19][i]=’_’;
qipan[1][1]=qipan[1][2]=qipan[1][3]=’*’;//初始化蛇的位置
qipan[1][4]=’#’;
printf(“This is a game of a SNAKE.\nGOOD LUCK TO YOU !\n”);
printf(“Input your game speed,please.(e.g.300)\n”);
scanf(“%d”,&gamespeed);
while(direction!=’q’)
system(“cls”);
for(i=0;i<20;i++)//打印出棋盘
for(j=0;j<80;j++)
printf(“%c”,qipan[i][j]);
timeover=1;
start=clock();
while(!kbhit()&&(timeover=clock()-start<=gamespeed));
if(timeover)
getch();
direction=getch();
else
direction=direction;
if(!(direction==72||direction==80||direction==75||direction==77))
return 0;
system(“cls”);
printf(“GAME OVER!\n”);
if(!change(qipan,zuobiao,direction))
direction=’q’;
system(“cls”);
printf(“GAME OVER!\n”);
return 0;
int change(char qipan[20][80],int zuobiao[2][80],char direction)
int x,y;
if(direction==72)
x=zuobiao[0][head]-1;y=zuobiao[1][head];
if(direction==80)
x=zuobiao[0][head]+1;y=zuobiao[1][head];
if(direction==75)
x=zuobiao[0][head];y=zuobiao[0][head]-1;
if(direction==77)
x=zuobiao[0][head];y=zuobiao[1][head]+1;
if(x==0||x==18||y==78||y==0)
return 0;
if(qipan[x][y]!=’ ‘)
return 0;
qipan[zuobiao[0][tail]][zuobiao[1][tail]]=’ ‘;
tail=(tail+1)%80;
qipan[zuobiao[0][head]][zuobiao[1][head]]=’*’;
head=(head+1)%80;
zuobiao[0][head]=x;
zuobiao[1][head]=y;
qipan[zuobiao[0][head]][zuobiao[1][head]]=’#’;
return 1;
}“猜数字小游戏”,每个数字后按空格,最后按回车确认
#include
#include
#include
int a[4],b[4];
int count=0;? //计算猜测次数
void csh( );? //初始化
void start( );? //开始游戏
int main( )
{ csh( );
start( );
void csh( )? //初始化
{ printf(“\n\n猜? 数? 字? 小? 游? 戏\n\n”);
? printf(“猜四个数字,如数字与顺序都正确记为A,数字正确位置不对记为B.\n”);
void start( )? //开始游戏
{int m,n;? //m是完全猜对的个数,n是顺序不对的个数
while(1)
{srand((unsigned)time(NULL));? //初始化随机数发生器srand( )
while(1) { for(int i=0;i<4;i++) a[i]=rand( )%10; ?//rand( )函数每次随机产生一个0-9的数
if( (a[3]!=a[2]&&a[3]!=a[1]&&a[3]!=a[0])&&
(a[2]!=a[1]&&a[2]!=a[0])&&a[1]!=a[0] ) break; }? //4个随机数各自不相等
printf(“请依次输入4个一位整数:\n\n”);
while(1)
{for(int i=0;i<4;i++) scanf(“%d”,&b[i]);
printf(“你输入的是:%d? %d? %d? %d “,b[0],b[1],b[2],b[3]);
m=0;n=0;
for(int i=0;i<4;i++)
{for(int j=0;j<4;j++)
{ if(b[i]==a[j]&&i==j)m=m+1; if(b[i]==a[j]&&i!=j)n=n+1; }
count=count+1;
printf(“%dA? %dB你试了%d次\n”,m,n,count);
if(m==4)break;
if(count==8){ count=0; break; }
printf(“\n”);
if(m==4)printf(“你猜对了(^-^)! 就是:%d %d %d %d\n”,a[0],a[1],a[2],a[3]);

相关经验推荐