c语言案例贪吃蛇,【课题研究案例】(三)贪吃蛇游戏案例

C语言的游戏案例其实有很多,网络上很多实例我们可以稍加改动作为我们的游戏案例,比如很流行的贪吃蛇,手机上,电脑单机版都有,那么我们c语言也可以写这个程序。但是如果让学生编写,确实有点难,我们可以对程序的其中一个部分知识点拿来切入课堂,下面给出了实例。这个案例里,我们可以用来讲for循环这个知识点,把程序给学生,给之前截去部分代码,让学生思考,添加部分代码,就可以运行该程序,只有掌握了知识点才能运行程序,玩这个游戏,学生的学习积极性会被激发。

#include

#include

#include

#include

int head=3,tail=0,f=0,s,t,m;

int main()

{ int i,j;

int zuobiao[2][80];

long

start;

int

direction=77;

int gamespeed;

int timeover;

int change(char qipan[30][80],int zuobiao[2][80],char

direction);

printf("auto? Y/N\n");

m=getch();

if (m=='y')

{

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<=19;i++)

for(j=0;j<=79;j++)

qipan[i][j]=' ';//初始化棋盘

qipan[1][1]=qipan[1][2]=qipan[1][3]='*';//初始化蛇的位置

qipan[1][4]='#';

printf("start\n");

printf("Input your game level,please.\

1-1000\n");

scanf("%d",&gamespeed);

s=rand() ;

t=rand()�;

qipan[s][t]='$';

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<=1000-gamespeed));

if(direction==72||direction==80)

if(s!=zuobiao[0][head])

if(s-zuobiao[0][head]<0)

direction=72;

else direction=80;

else

if(t-zuobiao[1][head]<0)

direction=75;

else direction=77;

else if(t!=zuobiao[1][head])

if(t-zuobiao[1][head]<0)

direction=75;

else direction=77;

else

if(t-zuobiao[1][head]<0)

direction=72;

else direction=80;

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");

}

if(f==1)

{

s=rand() ;

t=rand()�;

qipan[s][t]='$';

f=0;

}

}

return 0;

}

else

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=1;i<=20;i++)

qipan[i][0]='|';

for(i=1;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("start\n");

printf("Input your game level,please.\

1-1000\n");

scanf("%d",&gamespeed);

s=rand() ;

t=rand()�;

qipan[s][t]='$';

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<=1000-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");

}

if(f==1)

{

s=rand() ;

t=rand()�;

qipan[s][t]='$';

f=0;

}

}

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[1][head]-1;}

if(direction==77)

{x=zuobiao[0][head];

y=zuobiao[1][head]+1;}

if(x<0)

x=18;

else if(x>18)

x=0;

else if(y>78)

y=0;

else

if(y<0)

y=78;

if(qipan[s][t]!='$')

f=1;

qipan[zuobiao[0][tail]][zuobiao[1][tail]]=' ';

tail=(tail+1)�;

qipan[zuobiao[0][head]][zuobiao[1][head]]='*';

head=(head+1)�;

zuobiao[0][head]=x;

zuobiao[1][head]=y;

qipan[zuobiao[0][head]][zuobiao[1][head]]='#';

return 1;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值