c语言贪吃蛇运行出现乱码,贪吃蛇运行程序 C语言编写

#include

#include

#include

#include

#include

#include

int  snake_len=1;//蛇的长度

int  snake_loc[50][2]={31,12};//整条蛇的位置,最长为50

int  snake_head[2]={31,12};//蛇头位置,初始值为11,12;

int  food[2];//食物位置

char snake_direction='s';

int  delay=200; //蛇每delay个时间走一步

int  eat_flag=0;//1表示吃了食物,0表示未吃

int  liv_stat=0;//1表示死了,游戏该结束了;0表示还活着

void gotoxy(int x, int y)//定位光标,x为行坐标,y为列坐标

{

COORD pos = {x,y};

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleCursorPosition(hOut, pos);

}

void hidden()//隐藏光标

{

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

CONSOLE_CURSOR_INFO cci;

GetConsoleCursorInfo(hOut,&cci);

cci.bVisible=0;//赋1为显示,赋0为隐藏

SetConsoleCursorInfo(hOut,&cci);

}

void init()//初始化

{

int i;

snake_len=1;//蛇的长度

snake_loc[0][0]=31;//整条蛇的位置

snake_loc[0][1]=12;

snake_head[0]=31;//蛇头位置,初始值为11,12;

snake_head[1]=12;

snake_direction='s';

delay=200;

eat_flag=0;

liv_stat=0;

for(i=1;i<50;i++)

{

snake_loc[i][0]=0;//整条蛇的位置

snake_loc[i][1]=0;

}

}

void create_window()//创建窗口

{

gotoxy(0,0);

printf("********************************************************************************");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *   分数:1     *");

printf("*                                                              *   按键说明:  *");

printf("*                                                              *   上:w       *");

printf("*                                                              *   下:s       *");

printf("*                                                              *   左:a       *");

printf("*                                                              *   右:d       *");

printf("*                                                              *   暂停:空格  *");

printf("*                                                              *   退出:Esc键 *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("*                                                              *               *");

printf("********************************************************************************");

}

void update_score()//更新分数

{

gotoxy(73,3);

printf("%2d",snake_len);

}

void create_food()//产生食物的位置

{

time_t t;

srand(time(&t));

while(1)

{

food[0]=rand()%62+1;//生成1~62之间的随机数,其中random函数生成0~77之间的随机数

food[1]=rand()%22+1;//生成1~22之间的随机数,其中random函数生成0~17之间的随机数

if(food[0]!=snake_head[0]&&food[1]!=snake_head[1])break;

}

gotoxy(food[0],food[1]);

printf("*");

}

void direction()

{

char keyhit=0,i;

while(kbhit()!=0)keyhit=getch();

if( ((keyhit=='a') || (keyhit=='d') || (keyhit=='w') || (keyhit=='s')) && (abs(snake_direction/16-keyhit/16)==1) )snake_direction=keyhit;

else if(keyhit==' ')

{

gotoxy(30,24);

system("pause");

gotoxy(30,24);

for(i=0;i<19;i++)printf(" ");

}

else if(keyhit==27)exit(0);

}

void state()//判定蛇死没死

{

if(snake_head[0]<1||snake_head[0]>62||snake_head[1]<1||snake_head[1]>22)liv_stat=1;

}

void eat()//判定蛇吃没吃上,并对根据方向对蛇头位置进行更新

{

switch(snake_direction)

{

case 'w': snake_head[1]--;break;

case 's': snake_head[1]++;break;

case 'a': snake_head[0]--;break;

case 'd': snake_head[0]++;break;

}

if((food[0]==snake_head[0]) && (food[1]==snake_head[1]) )

{

eat_flag=1;

switch(snake_direction)

{

case 'w': snake_head[1]--;break;

case 's': snake_head[1]++;break;

case 'a': snake_head[0]--;break;

case 'd': snake_head[0]++;break;

}

}

}

void show_snake()//更新蛇在屏幕中的位置

{

gotoxy(snake_head[0],snake_head[1]);

printf("*");

gotoxy(snake_loc[snake_len-1][0],snake_loc[snake_len-1][1]);

printf(" ");

}

void update_maxtrix()//更新存储蛇位置的数组

{

int i;

if(eat_flag!=1)

{

for(i=snake_len-1;i>0;i--)

{

snake_loc[i][0]=snake_loc[i-1][0];

snake_loc[i][1]=snake_loc[i-1][1];

}

}

else

{

snake_len++;

if(snake_len>3 && delay>100)delay-=30;

for(i=snake_len-1;i>1;i--)

{

snake_loc[i][0]=snake_loc[i-2][0];

snake_loc[i][1]=snake_loc[i-2][1];

}

snake_loc[1][0]=food[0];

snake_loc[1][1]=food[1];

eat_flag=0;

create_food();

update_score();

}

snake_loc[0][0]=snake_head[0];

snake_loc[0][1]=snake_head[1];

}

void main()

{

LOOP:

system("cls");

init();

create_window();

hidden();

create_food();

while(1)

{

int i;

Sleep(delay);

direction();

eat();

show_snake();

update_maxtrix();

state();

if(liv_stat==1)

{

for(i=1;i

{

gotoxy(snake_loc[i][0],snake_loc[i][1]);

printf(" ");

}

gotoxy(food[0],food[1]);

printf(" ");

gotoxy(30,12);

printf("Game over!\n");

gotoxy(25,13);

printf("继续请按y,退出请按n");

while(1)

{

i=getch();

if(i=='y')goto LOOP;

else if(i=='n')break;

}

break;

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值