C语言:贪吃蛇,用指针数组和字符型数组编写。

<p>#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#include<windows.h>
#include<time.h>
#include<conio.h>//_kbhit()
#define N 21 
int i, j;
int level=1;</p><p>void set(char stage[][2*N]);
void show(char stage[][2*N], int level);
char direct(char stage[][2*N], char *length[2*N], char *food, int now_long, char d);
void begin(char (*stage)[2*N], char **length, char *food);
void move(char stage[][2*N], char *length[2*N], char *food, int now_long);
int life_snake(char stage[][2*N], char *length[], int now_long);
void display(char stage[][2*N], char *length[2*N], char *food, int now_long);</p><p>
void set(char stage[][2*N])//初始化框架
{ 
 for(i=0; i<N; i++)
  for(j=0; j<2*N; j++)
   stage[i][j] = ' ';</p><p>  for(i=0; i<2*N; i++)
  {
   stage[0][i] = '_';
      stage[N-1][i] = '-';
  }</p><p>  for(i=1; i<N-1; i++)
  {
   stage[i][0] = '|';
      stage[i][2*N-1] = '|';
  }</p><p>}
void show(char stage[][2*N], int level) //框架展示
{
 printf("this is %d level, 方向(w, s, a, d), 空格键暂停, esc键退出\n", level);
 for(i=0; i<N; i++)
 {
  for(j=0; j<2*N; j++)
   putchar(stage[i][j]);
  putchar('\n');
 }
}
char direct(char stage[][2*N], char *length[2*N], char *food, int now_long, char d)//方向
{
 char temp;
 if( kbhit() )
 {
  if( ( temp=getch() ) == 32)
  {
   system("pause");
  }
  if( temp == 27)
  {
   system("cls");
   display(stage, length, food, now_long);
  }</p><p>  if( temp == 'a')
   d = 'a';
  if( temp == 'd')
   d = 'd';
  if( temp == 'w')
   d = 'w';
  if( temp == 's')
   d = 's';
 }
 switch(d)
 {
 case 'w':
  if(length[1] - length[2] == 2*N )//if语句不能向左时向右,向上不能向下.
   (*length)+=2*N;
  else
  (*length)-=2*N; 
  break;
 case 's':
  if(length[2] - length[1] == 2*N )
   (*length)-=2*N;
  else
  (*length)+=2*N;
  break;
 case 'd':
     if( length[2] - length[1] == 1 )
   (*length)--;
  else
   (*length)++;
  break;
 case 'a':
  if(length[1] - length[2] == 1 )
  (*length)++;
  else
   (*length)--;
  break;
 default:
  break;
 }
 return d;
 //return *length;
}
void begin(char (*stage)[2*N], char **length, char *food)
{
 set(stage);
 for( i=0; i<3; i++)
 {
  length[i] = &stage[7][2*N-1-3+i];
  *length[i] = '*';
 }
 *length[0] = '$';
 *food = <a target=_blank href="mailto:'@'">'@'</a>;
 show(stage, level);
 printf("\n\nPlease press any key to start");
 getch();
 system("cls");
}</p><p>void move(char stage[][2*N], char *length[2*N], char *food, int now_long)
{ 
 char  d;//方向
 srand( (unsigned)time(NULL) );
 loop:   d = 'a';
   now_long = 3;
  while(1)
 {
     set(stage);
  for(i=now_long-1; i>=1; i--)
  {
   length[i] = length[i-1];
   *length[i] = '*';
  }
     d = direct(stage, length, food, now_long, d); //不能direct(length, d), 一定要d = direct(length, d),因为方向可能改变
  *length[0] = '$';
  if( length[0] == food ) //头地址==食物地址
       now_long++;
  for(i=0; i<now_long; i++)
  {
   if( length[i] == food )
   {
    food = &stage[rand()%(N-2)+1][rand()%(2*N-2)+1];   
    i = -1;
   }
  }
  *food = <a target=_blank href="mailto:'@'">'@'</a>; 
  while( life_snake(stage, length, now_long) ==0 )
  {
   system("cls");
   show(stage, level); 
   printf("\nGame Over, please press any key to quit!\n\n");
   getch();
   system("cls");
   display(stage, length, food, now_long);
  }
  
  </p><p>     if( now_long == 10 )
  {
  level++;
  if(level == 5)
  {
   printf("\n全部关卡闯关成功,请按任意键退出\n");
   getch();
   system("cls");
   display(stage, length, food, now_long);
  }
  printf("\nCongratilations!, please press any key to the next level");
  getch();
  system("cls");
  begin(stage, length, food);
  goto loop; 
  }
  system("cls");  
  show(stage, level); 
  Sleep(200/level);  
  }</p><p>}</p><p>int life_snake(char stage[][2*N], char *length[], int now_long)
{
 int life;
 for(i=0; i<2*N; i++)
 {
  if( length[0] == &stage[0][i] || length[0] == &stage[N-1][i])
   life=0;
 }
 for(i=1; i<N-1; i++)
 {
  if( length[0] == &stage[i][0] || length[0] == &stage[i][2*N-1] )
   life = 0;
 }
 for(i=1; i<now_long; i++)
  if(length[0] == length[i]) 
   life = 0;
 return life;
}</p><p>void display(char stage[][2*N], char *length[2*N], char *food, int now_long)
{
 int n;
 printf("\n\n\n\t\t\t\t  贪 吃 蛇\n");
 printf("\n\n\t\t\t\t请选择难度等级\n");
 printf("\n\t\t\t 1.level = 1\t2.level = 2\n\n\t\t\t 3.level = 3\t4.level = 4\n\n");
loop: scanf("%d", &n);
 switch(n)
 {
 case 1:
  level = 1;
  system("cls");
  begin(stage, length, food);
  move(stage, length, food, now_long); 
  break;
 case 2:
  level = 2;
  system("cls");
  begin(stage, length, food);
  move(stage, length, food, now_long);
  break;
 case 3:
  level = 3;
  system("cls");
  begin(stage, length, food);
  move(stage, length, food, now_long);
  break;
 case 4:
  level = 4;
  system("cls");
  begin(stage, length, food);
  move(stage, length, food, now_long);
  break;
 default:
  printf("\n\t\t\t\t请重新选择(1~~4): ");
  goto loop;
  break; 
 }
}</p><p>int main()
{
 int now_long = 3;
 char stage[N][2*N];
 char *length[2*N];
 char *food = &stage[7][2*N-1-3-5];//char *s; s="123abc"; 
 display(stage, length, food, now_long);
 return 0;
} </p><p> </p><p> </p><p> </p><p> </p><p> </p>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值