C-贪吃蛇-基于字符屏幕80*25 turboc 2.0

来源:互联网 发布:linux怎么删除用户组 编辑:程序博客网 时间:2024/06/05 16:39

#include<bios.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
#define MAX_LENGTH 100
#define LEFT  0x4b00
#define RIGHT 0x4d00
#define UP    0x4800
#define DOWN  0x5000
#define PAUSE 0x1970 /* p key */
#define ESC 0x11b
#define TIME_LIMIT  4   /*移动速度*/
#define SNAKE   2
#define WALL   1
#define WCOLOR  WHITE
#define BKCOLOR BLACK
#define SCOLOR WHITE
#define FOOD 15
#define FCOLOR RED
#define X 20
#define Y 2
struct snake
   {
    int x;
    int y;
   }snk[MAX_LENGTH];
int bkey;
int box[22][22];
int begin ,end,where_x,where_y,eat;
int direction = 0,dead=0,pre_direction;
void initbox()
  {
    int i,j;
    for(i=0;i<22;i++)
      for(j=0;j<22;j++)
        box[i][j]=0;
    for(i=0;i<22;i++)
      box[i][21] = box[21][i] = box[0][i] = box[i][0] = 1;
    textbackground(BKCOLOR);
    textcolor(WCOLOR);
    for(i=0;i<22;i++)
      for(j=0;j<22;j++)
       if( box[i][j]==1 ) { gotoxy(X+i,Y+j); putch(WALL); }
  }

int timeover()
 {
  static long int tm1,tm2;
  tm1=biostime(0,0l);
  if(tm1 - tm2 >TIME_LIMIT ) { tm2 = tm1; return 1;}
  return 0;
 }

void go_ahead()
{
  int mid = begin;
  if(direction == 0 )return;
  if(begin == 0 ) begin = MAX_LENGTH-1;
   else begin -= 1;
  if(direction == UP) { snk[begin].x = snk[mid].x ; snk[begin].y = snk[mid].y-1; }
  else if(direction == DOWN ){snk[begin].x = snk[mid].x ; snk[begin].y = snk[mid].y+1; }
  else if(direction == LEFT){snk[begin].x = snk[mid].x-1 ; snk[begin].y = snk[mid].y; }
  else if(direction == RIGHT){snk[begin].x = snk[mid].x+1 ; snk[begin].y = snk[mid].y; }
  if(box[snk[begin].x][snk[begin].y] == 1) dead=1;
  gotoxy(X+snk[begin].x,Y+snk[begin].y);
  textcolor(SCOLOR);
  putch(SNAKE);
  box[snk[begin].x][snk[begin].y] = 1;
  if(eat != 1)
   {  gotoxy(X+snk[end].x,Y+snk[end].y);
      textcolor(BKCOLOR);
      putch('');
      if(end == 0) end  = MAX_LENGTH-1;
      else end -= 1;
      box[snk[end].x][snk[end].y] = 0;
   }
}
void do_action()
 {
  switch(bkey)
   {
    case UP :   if(direction != DOWN)  direction = UP; break;
    case DOWN : if(direction != UP  )  direction = DOWN; break;
    case LEFT : if(direction != RIGHT) direction = LEFT; break;
    case RIGHT: if(direction != LEFT ) direction = RIGHT; break;
    case PAUSE : getch();bkey = direction; break;
    case ESC : exit(0);break;
    default : return;
   }
 }
do_eat()
{
  if((snk[begin].x == where_x) && (snk[begin].y == where_y) )
   {
     eat = 1;
     if(begin == 0 ) begin = MAX_LENGTH -1 ;
     else begin -= 1;
     snk[begin].x = where_x ;
     snk[begin].y = where_y;
   }
}

void putfood()
{
 do{
   where_x = random(20)+1;
   where_y = random(20)+1;
   }while(box[where_x][where_y]==1);
 textcolor(FCOLOR);
 gotoxy(X+where_x,Y+where_y);
 putch(FOOD);
}
main()
{
  do
  {
   dead= 0;
   bkey= 0;
   direction = 0;
   clrscr();
   textcolor(WHITE);
   gotoxy(1,1);
   printf("press direction keys for start/n");
   printf("press p for PAUSE/n");
   printf("press ESC for EXIT/n ");
   initbox();
   begin = end = MAX_LENGTH-1;
   box[9][9] = 0;
   snk[begin].x = 9;
   snk[begin].y = 9;
   gotoxy(X+snk[begin].x,Y+snk[begin].y);
   textcolor(SCOLOR);
   putch(SNAKE);
   putfood();
   eat = 0;
   do
   { do_eat();
     if(eat == 1 ) { putfood(); eat = 0; }
     while( !timeover() )
        if( bioskey(1) )
  bkey = bioskey(0);
     do_action();
     go_ahead();
   }while( !dead );
 textcolor(RED);
 gotoxy(1,24);
 printf("dou you want to try again y/n");
 do { bkey = bioskey(0); }while(bkey!=0x1579 && bkey!=0x316e);
 if(bkey == 0x316e )break;
 }while(1);
}


原创粉丝点击