BBS上有人提问如何在控制台下实现光标的移动,自己就顺手写了一个吃豆豆的程序,然后趁热打铁也写了个贪吃蛇的程序.但是仍然有问题

1.全局变量的使用,尤其蛇身的数组只是静态定义了100个,当蛇身超过100时自然会导致程序崩溃.

2.对于下一个随即生成的豆豆坐标,随即生成后应予以判断以确保该豆豆不在蛇的身上.

不过两个都很好改,自己只是为了尽快实现图形的走动懒得改了,有兴趣的可以改改.

/*贪吃蛇程序

name:     snake.c
version:  1.0
author:   Daniel
date:     2008.7.29
*/
#include <stdio.h>
#include <stdlib.h>
#define BINGO 99
#define UP     1
#define DOWN   2
#define LEFT   3
#define RIGHT  4
#define FAIL   0
int snake_loc[100][2]={{0,0},{0,1},{0,2}};           //记录蛇身各点坐标
int score=0;                                         //记录分数
int level=1;                                         //记录等级

int move_next(int length,int direction,int *point)
{
int temp[2];
temp[0]=snake_loc[length-1][0];
temp[1]=snake_loc[length-1][1];
if(direction == UP)          //表示向上移动
{
snake_loc[length-1][0]--;
if(snake_loc[length-1][0] == -1)
return FAIL;
}
else if(direction == DOWN)          //表示向下移动
{
snake_loc[length-1][0]++;
if(snake_loc[length-1][0] == 23)
return FAIL;
}
else if(direction == LEFT)          //表示向左移动
{
snake_loc[length-1][1]--;
if(snake_loc[length-1][1] == -1)
return FAIL;
}
else if(direction == RIGHT)          //表示向右移动
{
snake_loc[length-1][1]++;
if(snake_loc[length-1][1] == 79)
return FAIL;
}

if(snake_loc[length-1][0]==point[0] && snake_loc[length-1][1]==point[1])      //表示吃到豆豆
{
snake_loc[length-1][0]=temp[0];
snake_loc[length-1][1]=temp[1];
snake_loc[length][0]=point[0];
snake_loc[length][1]=point[1];
score++;
if(score%10==0)
level++;
return BINGO;
}
else                                             //未吃到豆豆
{
int iter=1;
for(iter=1;iter<length-1;iter++)
{
snake_loc[iter-1][0]=snake_loc[iter][0];
snake_loc[iter-1][1]=snake_loc[iter][1];
}
snake_loc[length-2][0]=temp[0];
snake_loc[length-2][1]=temp[1];
}
if(crash_self(length)==0)
return FAIL;
return 1;
}int crash_self(int length)                     //自身碰撞
{
int i=0;
for(i=0;i<length-1;i++)
{
if(snake_loc[length-1][0]==snake_loc[i][0] && snake_loc[length-1][1] == snake_loc[i][1])
return 0;
}
return 1;
}

int print_picture(int *point,int length)       //终端打印
{
system("cls");
printf("您当前的分数为:%d  当前游戏等级%d 任意键退出游戏\n",score,level);
int i=0,j=0;
int picture[24][79]={{0,0}};
for(i=0;i<length;i++)
picture[snake_loc[i][0]][snake_loc[i][1]]=99;
picture[point[0]][point[1]]=99;
for(i=0;i<22;i++)
{
for(j=0;j<79;j++)
{
if(picture[i][j] == 99 )
printf("*");
else
printf(" ");
}
printf("\n");
}
}

int main()
{
int i=3;
int j=0;
for(i=3;i<100;i++)                             //init snake_point_array
for(j=0;j<2;j++)
snake_loc[i][j]=0;
int snake_length=3;                                 //init snake_length;
int point[2];
point[0]=12;
point[1]=55;
int direction=4;
while(1)
{
int key=0;
if((key=kbhit())!=0)
{
key=getch();
if(224 == key)
{
key=getch();
if(key==72)                //向上移动
{
if(direction!=2)
direction=1;
continue;
}
if(key==80)                //向下移动
{
if(direction!=1)
direction=2;
continue;
}
if(key==75)                //向左移动
{
if(direction!=4)
direction=3;
continue;
}
if(key==77)                //向右移动
{
if(direction!=3)
direction=4;
continue;
}
}
else
{
system("pause");
return 0;
}
system("pause");
return 0;
}
print_picture(point,snake_length);
int result= move_next(snake_length,direction,point);
if(result == BINGO)
{
srand(time(0));
point[0]=rand()%22;
point[1]=rand()%79;
snake_length++;
}
else if(result == FAIL)
{
system("cls");
printf("任务失败,游戏结束,");
system("pause");
return 0;
}
sleep(100/level);
}
return 0;
}

很多人装完gentoo以后运行firefox,有时候会发现需要等待5分钟左右firefox窗口才能跳出来,在emerge unmerge firefox之前,先等firefox打开一次,然后查看about:config里面的browser.startup.homepage,很可能是gentoo的主页,如果你的网络连接不了国外的话,这个就是导致慢的原因,即使你在firefox的设置中将主页设置为其他,about:config这里还是一样

如果这样不能解决,那就采用之前的先unmerge掉firefox,再装编译好的二进制包程序