本人编程生涯中第二个程序(贪吃蛇),该代码仅限于tuorbc 2.0运行

本人编程生涯中第二个程序(贪吃蛇),该代码仅限于tuorbc 2.0运行
代码如下:
#include
#include
#include
#include
char n1[20],n2[20],n3[20];
int f1,f2,f3;
int nan1,nan2,nan3;
int score;
int x,y;
int I,J;
int K;
int A[20][50];
int head;
int F;
int N;
int ADD;
int NANDU;
void food()
{
int a,b;
if(ADD)
while(1)
{
a=random(18)+2;
b=random(38)+11;
if(A[a][b]==0) break;
}
A[a][b]=100;
ADD=0;
}
void start()
{
int i,j;
for(i=2;i<=19;i++)
for(j=11;j<=49;j++)
A[i][j]=0;
I=x=20;
J=y=3;
A[y++][x]=F++;
A[y++][x]=F++;
A[y][x]=F++;
}
void move()
{
register int i,j;
for(i=2;i<=19;i++)
for(j=11;j<=49;j++)
{
if(A[i][j]>0)
{
gotoxy(j,i);
cprintf("%c",178);
}
if(A[i][j]==0)
{
gotoxy(j,i);
cprintf(" ");
}
}
}
int key()
{
int a;
a=bioskey(0);
switch(a)
{
case 18432:a=1;break;
case 20480:a=2;break;
case 19200:a=3;break;
case 19712:a=4;break;
case 283:a=5;break;
default:a=0;
}
return a;
}
void shewei()
{
int i=I,j=J;
if(A[J][I+1]==1&&A[j][i]==9||A[j][i]==A[J][I+1]-1)
I++;
else if(A[J][I-1]==1&&A[j][i]==80||A[j][i]==A[J][I-1]-1)
I--;
else if(A[J+1][I]==1&&A[j][i]==80||A[j][i]==A[J+1][I]-1)
J++;
else if(A[J-1][I]==1&&A[j][i]==80||A[j][i]==A[J-1][I]-1)
J--;
if(ADD==0)
A[j][i]=0;
else
J=j,I=i;
}
void run(int a)
{
if(a==1)
y--;
if(a==2)
y++;
if(a==3)
x--;
if(a==4)
x++;
if(F==81) F=1;
if(A[y][x]==100) ADD=1;
if(A[y][x]!=100&&A[y][x]!=0) K=1;
A[y][x]=F++;
}
void jiemian()
{
int i;
clrscr();
textcolor(LIGHTBLUE);
gotoxy(10,1);
cprintf("%c",201);
for(i=11;i<=49;i++)
cprintf("%c",205);
cprintf("%c",187);
gotoxy(10,20);
cprintf("%c",200);
for(i=11;i<=49;i++)
cprintf("%c",205);
cprintf("%c",188);
for(i=2;i<=19;i++)
{
gotoxy(10,i);
cprintf("%c",186);
gotoxy(50,i);
cprintf("%c",186);
}
textcolor(YELLOW);
gotoxy(55,8);
cprintf("de fen: %d",score);
gotoxy(55,10);
cprintf("nan du: %d",NANDU);
}
void defen()
{
gotoxy(63,8);
if(ADD==1)
cprintf("%d",score+=5);
}
void nandu()
{
gotoxy(63,10);
cprintf("%d",NANDU);
}
void jilu()
{
FILE *fp;
int a;
textcolor(LIGHTGREEN);
fp=fopen("e://tanchise.dat","rb");
if(fp==NULL)
{
fp=fopen("e://tanchise.dat","wb+");
fprintf(fp,"%s %d,0,%s %d,0,%s %d,0","liufuduan",0,"liufuduan",0,"liufuduan",0);
}
rewind(fp);
fscanf(fp,"%s %d,%d,%s %d,%d,%s %d,%d",n1,&f1,&nan1,n2,&f2,&nan2,n3,&f3,&nan3);
a=5;
gotoxy(33,3);
cprintf("pai hang bang");
gotoxy(30,6);
cprintf("mingci xingming defen nandu");
gotoxy(30,a+=3);
cprintf("1st %-10.9s%-5d %-5d",n1,f1,nan1);
gotoxy(30,a+=3);
cprintf("2nd %-10.9s%-5d %-5d",n2,f2,nan2);
gotoxy(30,a+=3);
cprintf("3rd %-10.9s%-5d %-5d",n3,f3,nan3);
fclose(fp);
}
void gengxin()
{
FILE *fp;
if(score>f1)
{
cprintf("please input your name...");
f3=f2,f2=f1;
nan3=nan2,nan2=nan1;
n3[0]='/0',strcpy(n3,n2);
n2[0]='/0',strcpy(n2,n1);
scanf("%s",n1);
f1=score;
nan1=NANDU;
}
else if(score>f2)
{
cprintf("please input your name...");
f3=f2;
nan3=nan2;
n3[0]='/0',strcpy(n3,n2);
scanf("%s",n2);
f2=score;
nan2=NANDU;
}
else if(score>f3)
{
f3=score;
cprintf("please input your name(bu neng jia kong ge!)...");
scanf("%s",n3);
nan3=NANDU;
}
fp=fopen("e://tanchise.dat","wb");
fprintf(fp,"%s %d,%d,%s %d,%d,%s %d,%d",n1,f1,nan1,n2,f2,nan2,n3,f3,nan3);
fclose(fp);
}
void main()
{
int a;
randomize();
clrscr();
window(1,1,80,25);
jilu();
getch();
clrscr();
again:
clrscr();
ADD=1;
NANDU=1;
score=0;
head=2;
K=0;
F=1;
gotoxy(25,11);
cprintf("qing xuan zhe nandu...(1~5)");
do
{
NANDU=bioskey(0);
switch(NANDU)
{
case 561:NANDU=1;break;
case 818:NANDU=2;break;
case 1075:NANDU=3;break;
case 1332: NANDU=4;break;
case 1589: NANDU=5;break;
default:NANDU=0;
}
}while(NANDU<1||NANDU>5);
N=(6-NANDU)*10000;
jiemian();
start();
while(1)
{
do
{
food();

run(head);
shewei();
if(x>49||x<11||y>19||y<2||K==1)
goto liu;
move();
defen();

delay(N);
if(head==1||head==2)
delay(N-20000);
}while(!kbhit());
a=key();
nandu();
if(a==5) break;
else
{
if(a==1&&head!=2) head=a;
if(a==2&&head!=1) head=a;
if(a==3&&head!=4) head=a;
if(a==4&&head!=3) head=a;
}
}
liu: gotoxy(53,15);
cprintf("(T_T) you are fail!!!");
getch();
clrscr();
gengxin();
clrscr();
jilu();
getch();
clrscr();
gotoxy(25,10);
cprintf("(^-^) zai lai yi ci...(y/n)");
if((a=bioskey(0))==5497) goto again;
}


/*made by www.liufuduan.com*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值