C培训第八课作业(贪食蛇雏形)
忙活了半小天,终于弄出来个四不像...发现自己的逻辑能力和代码的驾驭能力都不强...
往往出现个思路,琢磨半天也不知道用什么代码表示.
最后只好写了改,改了写的./:L
基本实现了老师交代的添加边框,上,下,左,右,回显不同符号.
还有多次按键实现蛇身变长的那个.不过不知道我这样实现的可以不...
源码:main()
{
char c={0};
int x = 40;
int y = 13;
int i=3;
char a = 0,b = 0;
while(*c != 0x30)
{
clrscr();
printf("Please enter '0' to quit.a = %d;b = %d x = %d;y = %d",a,b,x,y);
gotoxy(70,1);
printf("By: LShang");
while(i <= 80)
{
if(i<25)
{
gotoxy(i-1,2);
printf("#");
gotoxy(i-1,25);
printf("#");
gotoxy(i-1,13);
printf("-");
gotoxy(1,i);
printf("#");
gotoxy(80,i);
printf("#");
gotoxy(40,i);
printf("|");
}
else
{
gotoxy(i-1,2);
printf("#");
gotoxy(i-1,25);
printf("#");
gotoxy(i-1,13);
printf("-");
}
i++;
}
if(*c == 0x1e)
{
gotoxy(x,y);
for(i=0;i<5 && i<=abs(b);i++)
{
*(c+i+1) = *(c+i);
*(c+i) = 0xdb;
gotoxy(x,y+i);
printf("%c",*(c+i));
}
gotoxy(x,y);
printf("%c",*(c+i));
gotoxy(x,y);
}else if(*c == 0x1f)
{
gotoxy(x,y);
for(i=0;i<5 && i<=abs(b);i++)
{
*(c+i+1) = *(c+i);
*(c+i) = 0xdb;
gotoxy(x,y-i);
printf("%c",*(c+i));
}
gotoxy(x,y);
printf("%c",*(c+i));
gotoxy(x,y);
}else if(*c == 0x11)
{
gotoxy(x,y);
for(i=0;i<5 && i<=abs(a);i++)
{
*(c+i+1) = *(c+i);
*(c+i) = 0xdb;
gotoxy(x+i,y);
printf("%c",*(c+i));
}
gotoxy(x,y);
printf("%c",*(c+i));
gotoxy(x,y);
}else if(*c == 0x10)
{
gotoxy(x,y);
for(i=0;i<5 && i<=abs(a);i++)
{
*(c+i+1) = *(c+i);
*(c+i) = 0xdb;
gotoxy(x-i,y);
printf("%c",*(c+i));
}
gotoxy(x,y);
printf("%c",*(c+i));
gotoxy(x,y);
}
i = 3;
*c = getch();
if(*c == 0x48)
{
y = (y==3)?24:--y;
*c = 0x1e;
b++;
}else if(*c == 0x50)
{
y = (y == 24)?3:++y;
*c = 0x1f;
b--;
}else if(*c == 0x4b)
{
x = (x == 2)?79:--x;
*c = 0x11;
a++;
}else if(*c == 0x4d)
{
x = (x == 79)?2:++x;
*c = 0x10;
a--;
}
}
clrscr();
}
界面截图:
PS:
因为为了让蛇头正好到达虚线时身体消失.
所以在按不同方向键时候蛇身位置会不同.
源码附件:
这次在.c后缀后加.rar后缀,
仍未压缩,需重命名.
早上起来趁着头脑清醒,修改了下代码.
取消了中间的虚线,随时按反方向键都可以缩短蛇身.
然后就是蛇头可以改变方向了,
但是只能完全伸直后才能转弯.
虽然已经是尽我所能的去想了,
但是感觉并不是正确的逻辑.
因为仅仅是这一点就写了好多代码.
如果完整的贪食蛇程序,完全按这种方式写下去.
最少也要500+行代码吧....
可能是现在所能利用的东西太少了?
因为是修改代码,就是为了添加功能而写代码.
感觉很不爽.希望自己早日能够用逻辑构思出一个整体的东西出来.
修改后源码:main()
{
char c={0};
int x = 40;
int y = 13;
int i=3,j = 0;
char a = 0,b = 0;
while(*c != 0x30)
{
clrscr();
printf("Please enter '0' to quit.a = %d;b = %d x = %d;y = %d",a,b,x,y);
gotoxy(70,1);
printf("By: LShang");
while(i <= 80)
{
if(i<25)
{
gotoxy(i-1,2);
printf("#");
gotoxy(i-1,25);
printf("#");
gotoxy(1,i);
printf("#");
gotoxy(80,i);
printf("#");
}
else
{
gotoxy(i-1,2);
printf("#");
gotoxy(i-1,25);
printf("#");
}
i++;
}
if(*c == 0x1e)
{
if(a == 0)
{
gotoxy(x,y);
for(i=0;i<5 && i<=abs(b);i++)
{
*(c+i+1) = *(c+i);
*(c+i) = 0xdb;
gotoxy(x,y+i);
printf("%c",*(c+i));
}
gotoxy(x,y);
printf("%c",*(c+i));
gotoxy(x,y);
}
else
{
gotoxy(x,y);
printf("%c",*c);
if(a>0)
{
for(j=1;j<=abs(b);j++)
{
gotoxy(x,y+j);
printf("%c",0xdb);
}
for(j = a-1;j>0;j--)
{
printf("%c",0xdb);
}
a--;
gotoxy(x,y);
}else if(a<0)
{
for(j=1;j<=abs(b);j++)
{
gotoxy(x,y+j);
printf("%c",0xdb);
}
for(j = a+1;j<0;j++)
{
gotoxy(x+j,y+b);
printf("%c",0xdb);
}
a++;
gotoxy(x,y);
}
}
}else if(*c == 0x1f)
{
if(a == 0)
{
gotoxy(x,y);
for(i=0;i<5 && i<=abs(b);i++)
{
*(c+i+1) = *(c+i);
*(c+i) = 0xdb;
gotoxy(x,y-i);
printf("%c",*(c+i));
}
gotoxy(x,y);
printf("%c",*(c+i));
gotoxy(x,y);
}else
{
gotoxy(x,y);
printf("%c",*c);
if(a>0)
{
for(j=1;j<=abs(b);j++)
{
gotoxy(x,y-j);
printf("%c",0xdb);
}
for(j = a-1;j>0;j--)
{
printf("%c",0xdb);
}
a--;
gotoxy(x,y);
}else if(a<0)
{
for(j=1;j<=abs(b);j++)
{
gotoxy(x,y-j);
printf("%c",0xdb);
}
for(j = a+1;j<0;j++)
{
gotoxy(x+j,y+b);
printf("%c",0xdb);
}
a++;
gotoxy(x,y);
}
}
}else if(*c == 0x11)
{
if(b == 0)
{
gotoxy(x,y);
for(i=0;i<5 && i<=abs(a);i++)
{
*(c+i+1) = *(c+i);
*(c+i) = 0xdb;
gotoxy(x+i,y);
printf("%c",*(c+i));
}
gotoxy(x,y);
printf("%c",*(c+i));
gotoxy(x,y);
}else
{
gotoxy(x,y);
printf("%c",*c);
if(b>0)
{
for(j = 1;j<=abs(a);j++)
{
gotoxy(x+j,y);
printf("%c",0xdb);
}
for(j=b-1;j>0;j--)
{
gotoxy(x+a,y+j);
printf("%c",0xdb);
}
b--;
gotoxy(x,y);
}else if(b<0)
{
for(j = 1;j<=abs(a);j++)
{
gotoxy(x+j,y);
printf("%c",0xdb);
}
for(j=b+1;j<0;j++)
{
gotoxy(x+a,y+j);
printf("%c",0xdb);
}
b++;
gotoxy(x,y);
}
}
}else if(*c == 0x10)
{
if(b == 0)
{
gotoxy(x,y);
for(i=0;i<5 && i<=abs(a);i++)
{
*(c+i+1) = *(c+i);
*(c+i) = 0xdb;
gotoxy(x-i,y);
printf("%c",*(c+i));
}
gotoxy(x,y);
printf("%c",*(c+i));
gotoxy(x,y);
}else
{
gotoxy(x,y);
printf("%c",*c);
if(b>0)
{
for(j = 1;j<=abs(a);j++)
{
gotoxy(x-j,y);
printf("%c",0xdb);
}
for(j=b-1;j>0;j--)
{
gotoxy(x+a,y+j);
printf("%c",0xdb);
}
b--;
gotoxy(x,y);
}else if(b<0)
{
for(j = 1;j<=abs(a);j++)
{
gotoxy(x-j,y);
printf("%c",0xdb);
}
for(j=b+1;j<0;j++)
{
gotoxy(x+a,y+j);
printf("%c",0xdb);
}
b++;
gotoxy(x,y);
}
}
}
i = 3;
*c = getch();
if(*c == 0x48)
{
y = (y==3)?24:--y;
*c = 0x1e;
if(b<4)
{
b++;
}
}else if(*c == 0x50)
{
y = (y == 24)?3:++y;
*c = 0x1f;
if(b>-4)
{
b--;
}
}else if(*c == 0x4b)
{
x = (x == 2)?79:--x;
*c = 0x11;
if(a<4)
{
a++;
}
}else if(*c == 0x4d)
{
x = (x == 79)?2:++x;
*c = 0x10;
if(a>-4)
{
a--;
}
}
}
clrscr();
}
截图:
好帖居然没人顶
http://www.discuz.net/static/image/common/sigline.gif
燃文 呵呵 楼主还厉害了我都还不会 附件包坏了,直接把代码放TC里有窗口没见蛇,按0键可退出 附件包坏了,直接把代码放TC里有窗口没见蛇,按0键可退出
浮云思音 发表于 2011-3-6 08:33 https://www.chinapyg.com/images/common/back.gif
我没传压缩包,把后缀名改成.c就可以了 强大 啊 C还能做出这个啊 强大 啊 C还能做出这个啊
蓝天天蓝 发表于 2011-3-25 16:42 https://www.chinapyg.com/images/common/back.gif
这个只是一个雏形,很多东西都没实现的...
页:
[1]