发新话题
打印

[转载]泡泡堂单机两人对战斗版C语言源代码

[转载]泡泡堂单机两人对战斗版C语言源代码

信息来源:C语言基地
复制内容到剪贴板
代码:
/*

          泡泡堂(单机两人对站版,编译器:TC 2.0)

使用方法:

      双击PAOPAO.EXE运行,如果不是全屏的话,请按Alt+Enter键使它变成全屏。
   
      玩家一:黄色的笑脸
      向上: w
      向下: s
      向左: a
      向右: d
      放泡泡:空格

      玩家二:红色的笑脸
      向上: UP(方向键)
      向下: DOWN
      向左: LEFT
      向右: RIGHT
      放泡泡:回车

      退出:
           ESC

      o: 增加泡泡个数
      上下方向的箭头: 增加泡泡的威力。

      人物被泡泡炸后就不能动了,过一段时间才能恢复,如果在这段时间被另外一个玩家碰了的话,就输了。

编程思想:

        在游戏循环中不端检查按键,对控制键执行相关操作,对其他键则忽略处理。
      对于还没有爆炸的泡泡(或刚爆炸还没有擦除的泡泡)用“队列”存储。每
      循环一次就检查“队头”元素的时间到了没有,时间到了就让它爆炸(或擦除爆炸
      痕迹)。爆炸时还要检查其爆炸范围内有没有其他的泡泡,有的话就引爆它。

                                                  游戏策划: 王宸博
                                                  游戏编程: 王德浩
                                                    2004年3月17日

                                                               */

3月24日,增加了一些注释和空格。

/*-------------------------------------------------
  paopaodang.c -- a demo of paopaotang game
          (c) wonderful,wangchengbo 2004
-------------------------------------------------*/

/**********************************************************/
/* 程序中用到的库函数所在头文件应用 #include 命令包含进来 */

#include
#include
#include
#include
#include
#include
#include
#include

/**********************************************************/


#define TIME_DELAY 3/* 人物移动的延时*/
#define WIN_DELAY 18
#define MAXQSIZE  100
#define PAO_TIME  50 /*放泡泡后,等待泡泡爆炸的时间*/
#define BLAST_TIME 10 /*爆炸消失的时间*/
#define PAOMAN_DELAY 60 /* 人被炸后恢复需要的时间 */

/*              键盘中断函数(借用一个TC做的CS游戏中的)       */
#define KEY_Y 0x15
#define KEY_N 0x31



#define KEY_A 0x1E
#define KEY_D 0x20
#define KEY_S 0x1f
#define KEY_W 0x11
#define KEY_ESC 0x01
#define KEY_ENTER 0x1c
#define KEY_SPACE 0x39
#define KEY_UP 0x48
#define KEY_LEFT 0x4b
#define KEY_RIGHT 0x4d
#define KEY_DOWN 0x50

#define PLAY1UP KEY_W
#define PLAY1LEFT KEY_A
#define PLAY1DOWN KEY_S
#define PLAY1RIGHT KEY_D
#define PLAY1FIRE KEY_SPACE

#define PLAY2UP KEY_UP
#define PLAY2DOWN KEY_DOWN
#define PLAY2LEFT KEY_LEFT
#define PLAY2RIGHT KEY_RIGHT
#define PLAY2FIRE KEY_ENTER



void InstallKeyboard(void);
void ShutDownKeyboard(void);
void far interrupt NewInt9(void);
int GetKey(int ScanCode);

char key_state[128],key_pressed[128];
void interrupt far (*OldInt9Handler)();





void InstallKeyboard(void)       /*键盘中断程序*/
{
int i;
for(i=0;i<128;i++)
key_state[i ]=key_pressed[i ]=0;
OldInt9Handler = getvect(9);      /*中断向量值*/
setvect(9,NewInt9);           /*中断程序NewInt9地址存入指定的中断向量表中INT 09H*/
}


void ShutDownKeyboard(void)
{
setvect(9,OldInt9Handler);
}


void far interrupt NewInt9(void)
{
unsigned char ScanCode,temp;
ScanCode=inportb(0x60);
temp=inportb(0x61);
outportb(0x61,temp | 0x80);
outportb(0x61,temp & 0x7f);
if(ScanCode&0x80)
  {
   ScanCode&=0x7f;
   key_state[ScanCode]=0;
  }
else
  {
   key_state[ScanCode]=1;
   key_pressed[ScanCode]=1;
  }
outportb(0x20,0x20);
}


int GetKey(int ScanCode)
{
int res;
res=key_state[ScanCode]|key_pressed[ScanCode];
key_pressed[ScanCode]=0;
return res;
}



/*>>>>>>>>>>>> Man function -- copyright Wonderful and WangChengBo <<<<<<<<<<<<<*/
typedef enum
{
   PLAY1=1, PLAY2
}which_play;


typedef enum
{
   CAN,
   CANNOT
}MOVE;

typedef struct
{
   int x, y;       /*the position of the man */
   which_play which;   /* play1 or play2?*/
   MOVE can_move;    /* can man move? */
   int len;       /*paopao&#39;s length*/
   int pao_num;       /* how many pao can the man fire?*/
   int old_time;    /* 人被泡泡炸后,恢复的时间 */
}man;

/*画人函数*/
DrawMan(man m)
{
   gotoxy(m.x+15, m.y+6);
   if (m.which == PLAY1)
   {
      textcolor(YELLOW);
   }
   else
   {
      textcolor(LIGHTRED);
   }
   putch(2);
}

/*画空心的人的函数*/
DrawPaoMan(man m)
{
   gotoxy(m.x+15, m.y+6);
   if (m.which == PLAY1)
   {
      textcolor(YELLOW);
   }
   else
   {
      textcolor(LIGHTRED);
   }
   putch(1);
}

/*擦除人的函数*/
EraseMan(man m)
{
   gotoxy(m.x+15, m.y+6);
   textcolor(BLACK);
   putch(&#39; &#39;);
}

/*画泡泡的函数*/
DrawPao(int x, int y)
{
   gotoxy(x+15, y+6);
   textcolor(LIGHTBLUE);
   putch(&#39;O&#39;);
}

/*画爆炸的#号的函数*/
DrawBlast1(int x, int y)
{
   gotoxy(x+15, y+6);
   textcolor(LIGHTBLUE);
   putch(&#39;#&#39;);
}



   
/*游戏开头显示的字符*/
char name[8][26] =
{
" O  O       O  O  ",
"  O OOOOOO    O OOOOOO",
"   OOO O       OOO O",
" O  O O O    O  O O O",
"  O  OOO O    O  OOO O",
"   O  OO       O  OO",
" O  O   O   O  O   O",
"O   OOOOOO   O   OOOOOO"
};
int Name_X = 26;
int Name_Y = 8;

void DrawBegin()
{
   int x, y;
   for (y=0; y   {
      for(x=0; x      {
        gotoxy(x+21, y+5);
        textcolor(LIGHTBLUE);
        putch(name[y][x]);
      }
   }

   gotoxy(19, 20);
   printf("Press any key to Enter the game!");
}
/*游戏地图,#为墙壁,@为箱子,试着自己改改*/
char map1[8][17] =
{"#################",
"# @@@@  @ #@# ##",
"# #@#@#@  @@@  #",
"#  @@@  @@ #@#@##",
"#@#@#@#@  @@@@@@#",
"#@@@@@  @@#@#@##",
"#@#@#@#@@  @@@@@#",
"#################"};
int Map_X = 17;
int Map_Y = 8;

char map[8][17];


void DrawMap()
{

      int x, y;
   for (y=0; y   {
      for(x=0; x      {
        gotoxy(x+15, y+6);
        if(map[y][x] == &#39;#&#39;)
        {
          textcolor(GREEN);
          putch(219);
        }
       else if(map[y][x] == &#39;@&#39;)
       {
          textcolor(BROWN);
          putch(178);
       }
       else
           putch(&#39; &#39;);
      }
   }
   textcolor(BLUE);
   gotoxy(48, 4);
   printf("Player1 key:");
      gotoxy(48, 5);
   printf("  UP----w");
      gotoxy(48, 6);
   printf("  DOWN--s");
      gotoxy(48, 7);
   printf("  LEFT--a");
      gotoxy(48, 8);
   printf("  RIGHT-d");
      gotoxy(48, 9);
   printf("  FIRE--space");
   gotoxy(48, 11);
      printf("Player2 key:");
      gotoxy(48, 12);
   printf("  UP----up");
   gotoxy(48, 13);
      printf("  DOWN--down");
   gotoxy(48, 14);
      printf("  LEFT--left");
   gotoxy(48, 15);
      printf("  RIGHT-right");
   gotoxy(48, 16);
      printf("  FIRE--ENTER");
   gotoxy(48, 18);
      printf("exit game:");
      gotoxy(48, 19);
   printf("  ESC");

   gotoxy(38, 2);
   textcolor(LIGHTRED);
   putch(&#39;P&#39;);
   putch(&#39;A&#39;);
   putch(&#39;O&#39;);
   putch(&#39;P&#39;);
   putch(&#39;A&#39;);
   putch(&#39;O&#39;);

}

/*炸箱子后,出宝物的函数*/
Treasure(int x, int y)
{
   int i;
   
   i = random(15);

   if (i > 10)
   {
      switch (i)
      {
      case 11:
      case 12:
        map[y][x] = &#39;O&#39;;
        gotoxy(x+15, y+6);
        textcolor(YELLOW);
        putch(&#39;o&#39;);
        break;
      case 13:
      case 14:
        map[y][x] = &#39;-&#39;;
        gotoxy(x+15, y+6);
        textcolor(YELLOW);
        putch(18);
        break;
      default:
        break;
      }
   }
   else
   {
      map[y][x] = &#39; &#39;;
      gotoxy(x+15, y+6);
      putch(&#39; &#39;);
   }

}

/*人物m向上移动的函数*/
int MoveUp(man *m, man *p)
{
   if (map[m->y - 1][m->x] == &#39;#&#39; || map[m->y - 1][m->x] == &#39;@&#39;
      || map[m->y - 1][m->x] == &#39;o&#39; || m->can_move == CANNOT)
   {
      return 1;
   }

   EraseMan(*m);

   if (map[m->y][m->x] == &#39;o&#39;)
   {
      DrawPao(m->x, m->y);
   }


   --(m->y);
   DrawMan(*m);

   switch (map[m->y][m->x])
   {
   case &#39;a&#39;:
   case &#39;b&#39;:
      m->can_move = CANNOT;
      m->old_time = clock();
      break;
   case &#39;O&#39;:
      ++(m->pao_num);
      map[m->y][m->x] = &#39; &#39;;
      break;
   case &#39;-&#39;:
      ++(m->len);
      map[m->y][m->x] = &#39; &#39;;
      break;
   default:
      break;
   }

   if (m->x == p->x && m->y == p->y && p->can_move == CANNOT)
   {
      gotoxy(36, 3);
      printf("Play%d Win!!", m->which);
      return 0;
   }

   return 1;
}
/*人物m向上移动的函数*/
int MoveUp(man *m, man *p)
{
   if (map[m->y - 1][m->x] == &#39;#&#39; || map[m->y - 1][m->x] == &#39;@&#39;
      || map[m->y - 1][m->x] == &#39;o&#39; || m->can_move == CANNOT)
   {
      return 1;
   }

   EraseMan(*m);

   if (map[m->y][m->x] == &#39;o&#39;)
   {
      DrawPao(m->x, m->y);
   }


   --(m->y);
   DrawMan(*m);

   switch (map[m->y][m->x])
   {
   case &#39;a&#39;:
   case &#39;b&#39;:
      m->can_move = CANNOT;
      m->old_time = clock();
      break;
   case &#39;O&#39;:
      ++(m->pao_num);
      map[m->y][m->x] = &#39; &#39;;
      break;
   case &#39;-&#39;:
      ++(m->len);
      map[m->y][m->x] = &#39; &#39;;
      break;
   default:
      break;
   }

   if (m->x == p->x && m->y == p->y && p->can_move == CANNOT)
   {
      gotoxy(36, 3);
      printf("Play%d Win!!", m->which);
      return 0;
   }

   return 1;
}

/*人物m向下移动的函数*/
int MoveDown(man *m, man *p)
{
   if (map[m->y + 1][m->x] == &#39;#&#39; || map[m->y + 1][m->x] == &#39;@&#39;
      || map[m->y + 1][m->x] == &#39;o&#39; || m->can_move == CANNOT)
   {
      return 1;
   }
   EraseMan(*m);

   if (map[m->y][m->x] == &#39;o&#39;)
   {
      DrawPao(m->x, m->y);
   }


   ++(m->y);
   DrawMan(*m);
   
   switch (map[m->y][m->x])
   {
   case &#39;a&#39;:
   case &#39;b&#39;:
      m->can_move = CANNOT;
      m->old_time = clock();
      break;
   case &#39;O&#39;:
      ++(m->pao_num);
      map[m->y][m->x] = &#39; &#39;;
      break;
   case &#39;-&#39;:
      ++(m->len);
      map[m->y][m->x] = &#39; &#39;;
      break;
   default:
      break;
   }
   
   if (m->x == p->x && m->y == p->y && p->can_move == CANNOT)
   {
      gotoxy(36, 3);
      printf("Play%d Win!!", m->which);
      return 0;
   }

   return 1;
      
      
}

/*人物m向左移动的函数*/
int MoveLeft(man *m, man *p)
{
   if (map[m->y][m->x - 1] == &#39;#&#39; || map[m->y][m->x - 1] == &#39;@&#39;
      || map[m->y][m->x - 1] == &#39;o&#39; || m->can_move == CANNOT)
   {
      return 1;
   }
   EraseMan(*m);

   if (map[m->y][m->x] == &#39;o&#39;)
   {
      DrawPao(m->x, m->y);
   }


   --(m->x);
   DrawMan(*m);

   switch (map[m->y][m->x])
   {
   case &#39;a&#39;:
   case &#39;b&#39;:
      m->can_move = CANNOT;
      m->old_time = clock();
      break;
   case &#39;O&#39;:
      ++(m->pao_num);
      map[m->y][m->x] = &#39; &#39;;
      break;
   case &#39;-&#39;:
      ++(m->len);
      map[m->y][m->x] = &#39; &#39;;
      break;
   default:
      break;
   }

   if (m->x == p->x && m->y == p->y && p->can_move == CANNOT)
   {
      gotoxy(36, 3);
      printf("Play%d Win!!", m->which);
      return 0;
   }

   return 1;
}

/*人物m向右移动的函数*/
int MoveRight(man *m, man *p)
{
   if (map[m->y][m->x + 1] == &#39;#&#39; || map[m->y][m->x + 1] == &#39;@&#39;
      || map[m->y][m->x + 1] == &#39;o&#39; || m->can_move == CANNOT)
   {
      return 1;
   }

   EraseMan(*m);

   if (map[m->y][m->x] == &#39;o&#39;)
   {
      DrawPao(m->x, m->y);
   }


   ++(m->x);
   DrawMan(*m);

   switch (map[m->y][m->x])
   {
   case &#39;a&#39;:
   case &#39;b&#39;:
      m->can_move = CANNOT;
      m->old_time = clock();
      break;
   case &#39;O&#39;:
      ++(m->pao_num);
      map[m->y][m->x] = &#39; &#39;;
      break;
   case &#39;-&#39;:
      ++(m->len);
      map[m->y][m->x] = &#39; &#39;;
      break;
   default:
      break;
   }

   if (m->x == p->x && m->y == p->y && p->can_move == CANNOT)
   {
      gotoxy(36, 3);
      printf("Play%d Win!!", m->which);
      return 0;
   }

   return 1;
}

/*>>>>>>>>>>> paopao functions -- copyright wonderful and WangChengBo <<<<<<<<<<<<*/
/*泡泡的数据结构*/
typedef struct
{
   int x;
   int y;
   int original_time;
   int len;
   which_play which;
}PAO;

/*--------------------------------------------------------------*/
/*以下为链表队列*/
typedef PAO QElemType;

typedef struct QNode
{
   QElemType data;
   struct QNode *next;
}QNode, *QueuePtr;

typedef struct
{
   QueuePtr  front; /*队头,最早出队列的一端*/
   QueuePtr  rear; /*队尾,允许插入的一端*/
}LinkQueue;

void InitalQueue(LinkQueue *Q)
{
   Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode));
   Q->front->next = NULL;
}


int EnQueue(LinkQueue *Q, QElemType e)
{
   QueuePtr p;
   p = (QueuePtr)malloc(sizeof(QNode));

   if (p == NULL)
   {
      return 0;
   }

   p->data = e;
   p->next = NULL;
   Q->rear->next = p;
   Q->rear = p;
   return 1;
}

int QueueHead(LinkQueue *Q, QElemType *e)
{
   if (Q->front == Q->rear)
   {
      return 0;
   }

   *e = Q->front->next->data;
   return 1;
}

int FindElemDelete(LinkQueue *Q, QElemType *e, int x, int y)
{
   QueuePtr p, q = Q->front;

   for (p = Q->front->next; p != 0; p = p->next)
   {
      if (p->data.x == x && p->data.y == y)
      {
        q->next = p->next;
        *e = p->data;
        
        if (p == Q->rear)
        {
           Q->rear = q;
        }

        free(p);
        return 1;
      }
      else
      {
        q = p;
      }
   }
   return 0;
}


int DeQueue(LinkQueue *Q)
{
   QueuePtr p;

   if (Q->front == Q->rear)
   {
      return 0;
   }

   p = Q->front->next;
   Q->front->next = p->next;

   if (Q->rear == p)
   {
      Q->rear = Q->front;
   }
   free(p);
   return 1;
}


void DestroyQueue(LinkQueue *Q)
{
   while (Q->front)
   {
      Q->rear = Q->front->next;
      free(Q->front);
      Q->front = Q->rear;
   }
}

/*放泡泡的函数*/
void Paopao(man *m, LinkQueue *Q)
{
   PAO pao;
   if (m->can_move == CANNOT || m->pao_num == 0)
   {
      return;
   }
   DrawPaoMan(*m);
   map[m->y][m->x] = &#39;o&#39;;
   pao.x = m->x;
   pao.y = m->y;
   pao.original_time = clock();
   pao.len = m->len;
   pao.which = m->which;

   --m->pao_num;

   /*入队列 */
   EnQueue(Q, pao);
}

/*泡泡爆炸的函数*/
void PaoBlast(PAO pao, man *p1, man *p2, LinkQueue *Q, LinkQueue *B)
{
   int i = 1, end = 0;
   PAO e;

   switch (pao.which)
   {
      case PLAY1:
        ++(p1->pao_num);
        break;
      case PLAY2:
        ++(p2->pao_num);
        break;
   }

   DrawBlast1(pao.x, pao.y);
   map[pao.y][pao.x] = &#39;b&#39;;
   
   if (p1->x == pao.x && p1->y == pao.y)
   {
      DrawPaoMan(*p1);
      p1->can_move = CANNOT;
      p1->old_time = clock();
   }
   else if (p2->x == pao.x && p2->y == pao.y)
   {
      DrawPaoMan(*p2);
      p2->can_move = CANNOT;
      p2->old_time = clock();
   }
   
   /*--------------------Up----------------------*/
   for (; i <= pao.len; ++i)
   {
      end = 0;
      
      switch (map[pao.y-i][pao.x])
      {
      case &#39;#&#39;:
        end = 1;
        break;

      case &#39; &#39;:
        DrawBlast1(pao.x, pao.y-i);
        map[pao.y-i][pao.x] = &#39;b&#39;;
        break;

      case &#39;@&#39;:
        end = 1;
        DrawBlast1(pao.x, pao.y-i);
        map[pao.y-i][pao.x] = &#39;a&#39;;
        break;

      case &#39;o&#39;:
        if (FindElemDelete(Q, &e, pao.x, pao.y-i) == 1)
        {
           e.original_time = clock();
           EnQueue(B, e);
           PaoBlast(e, p1, p2, Q, B);
        }
        break;
      
      default:
        break;
      }
   
      if (p1->x == pao.x && p1->y == pao.y-i)
      {
        DrawPaoMan(*p1);
        p1->can_move = CANNOT;
        p1->old_time = clock();
      }
      else if (p2->x == pao.x && p2->y == pao.y-i)
      {
        DrawPaoMan(*p2);
        p2->can_move = CANNOT;
        p2->old_time = clock();
      }
      
      if (end == 1)
      {
        break;
      }
   }
   
   /*----------------------Down------------------------*/
   for (i=1; i <= pao.len; ++i)
   {
      end = 0;
      
      switch (map[pao.y+i][pao.x])
      {
      case &#39;#&#39;:
        end = 1;
        break;

      case &#39; &#39;:
        DrawBlast1(pao.x, pao.y+i);
        map[pao.y+i][pao.x] = &#39;b&#39;;
        break;   

      case &#39;@&#39;:
        end = 1;
        DrawBlast1(pao.x, pao.y+i);
        map[pao.y+i][pao.x] = &#39;a&#39;;
        break;

      case &#39;o&#39;:
        if (FindElemDelete(Q, &e, pao.x, pao.y+i) == 1)
        {
           e.original_time = clock();
           EnQueue(B, e);
           PaoBlast(e, p1, p2, Q, B);
        }
        break;
      
      default:
        break;
      }

      if (p1->x == pao.x && p1->y == pao.y+i)
      {
        DrawPaoMan(*p1);
        p1->can_move = CANNOT;
        p1->old_time = clock();
      }
      else if (p2->x == pao.x && p2->y == pao.y+i)
      {
        DrawPaoMan(*p2);
        p2->can_move = CANNOT;
        p2->old_time = clock();
      }


      if (end == 1)
      {
        break;
      }
   }

   /*---------------------Left-------------------------*/
   for (i=1; i <= pao.len; ++i)
   {
      
      end = 0;
      switch (map[pao.y][pao.x-i])
      {
      case &#39;#&#39;:
        end = 1;
        break;
      
      case &#39; &#39;:
        DrawBlast1(pao.x-i, pao.y);
        map[pao.y][pao.x-i] = &#39;b&#39;;
        break;

      case &#39;@&#39;:
        end = 1;
        DrawBlast1(pao.x-i, pao.y);
        map[pao.y][pao.x-i] = &#39;a&#39;;
        break;

      case &#39;o&#39;:
        if (FindElemDelete(Q, &e, pao.x-i, pao.y) == 1)
        {
           e.original_time = clock();
           EnQueue(B, e);
           PaoBlast(e, p1, p2, Q, B);
        }
        break;

      default:
        break;
      }

      if (p1->x == pao.x-i && p1->y == pao.y)
      {
        DrawPaoMan(*p1);
        p1->can_move = CANNOT;
        p1->old_time = clock();
      }
      else if (p2->x == pao.x-i && p2->y == pao.y)
      {
        DrawPaoMan(*p2);
        p2->can_move = CANNOT;
        p2->old_time = clock();
      }

      if (end == 1)
      {
        break;
      }
   }
/*----------------------Right-----------------------*/
   for (i=1; i <= pao.len; ++i)
   {
      
      end = 0;
      switch (map[pao.y][pao.x+i])
      {
      case &#39;#&#39;:
        end = 1;
        break;
      
      case &#39; &#39;:
        DrawBlast1(pao.x+i, pao.y);
        map[pao.y][pao.x+i] = &#39;b&#39;;
        break;

      case &#39;@&#39;:
        end = 1;

        DrawBlast1(pao.x+i, pao.y);
        map[pao.y][pao.x+i] = &#39;a&#39;;
        break;

      case &#39;o&#39;:
        if (FindElemDelete(Q, &e, pao.x+i, pao.y) == 1)
        {
           e.original_time = clock();
           EnQueue(B, e);
           PaoBlast(e, p1, p2, Q, B);
        }
        break;

      default:
        break;
      }
   
      if (p1->x == pao.x+i && p1->y == pao.y)
      {
        DrawPaoMan(*p1);
        p1->can_move = CANNOT;
        p1->old_time = clock();
      }
      else if (p2->x == pao.x+i && p2->y == pao.y)
      {
        DrawPaoMan(*p2);
        p2->can_move = CANNOT;
        p2->old_time = clock();
      }

      if (end == 1)
      {
        break;
      }
   }
}

/*擦除爆炸痕迹的函数*/
void EraseBlast(PAO pao, man *p1, man *p2)
{
   int i = 1, end = 0;
   gotoxy(pao.x+15, pao.y+6);
   putch(&#39; &#39;);
   map[pao.y][pao.x] = &#39; &#39;;

   if (p1->x == pao.x && p1->y == pao.y)
   {
      DrawPaoMan(*p1);
   }
   else if (p2->x == pao.x && p2->y == pao.y)
   {
      DrawPaoMan(*p2);
   }

   /*----------------------UP-------------------*/
   for (; i <= pao.len; ++i)
   {
      end = 0;

      switch (map[pao.y-i][pao.x])
      {
      case &#39;b&#39;:
        map[pao.y-i][pao.x] = &#39; &#39;;
        gotoxy(pao.x+15, pao.y-i+6);
        putch(&#39; &#39;);
        break;
      case &#39;a&#39;:
        Treasure(pao.x, pao.y-i);
      case &#39;#&#39;:
        end = 1;
        break;
      default:
        break;
      }

      if (p1->x == pao.x && p1->y == pao.y-i  && p1->can_move == CANNOT)
      {
        DrawPaoMan(*p1);
      }
      else if (p2->x == pao.x && p2->y == pao.y-i && p2->can_move == CANNOT)
      {
        DrawPaoMan(*p2);
      }

      if (end == 1)
      {
        break;
      }

   }

      /*---------------DOWN-----------------*/
   for (i=1; i <= pao.len; ++i)
   {
      end = 0;
      switch (map[pao.y+i][pao.x])
      {
      case &#39;b&#39;:
        map[pao.y+i][pao.x] = &#39; &#39;;
        gotoxy(pao.x+15, pao.y+i+6);
        putch(&#39; &#39;);
        break;
      case &#39;a&#39;:
        Treasure(pao.x, pao.y+i);
      case &#39;#&#39;:
        end = 1;
        break;
      default:
        break;
      }

      if (p1->x == pao.x && p1->y == pao.y+i && p1->can_move == CANNOT)
      {
        DrawPaoMan(*p1);
      }
      else if (p2->x == pao.x && p2->y == pao.y+i && p2->can_move == CANNOT)
      {
        DrawPaoMan(*p2);
      }

      if (end == 1)
      {
        break;
      }
   }
      /*---------------LEFT-----------------*/
   for (i=1; i <= pao.len; ++i)
   {
      end = 0;
      switch (map[pao.y][pao.x-i])
      {
      case &#39;b&#39;:
        map[pao.y][pao.x-i] = &#39; &#39;;
        gotoxy(pao.x+15-i, pao.y+6);
        putch(&#39; &#39;);
        break;
      case &#39;a&#39;:
        Treasure(pao.x-i, pao.y);
      case &#39;#&#39;:
        end = 1;
        break;
      default:
        break;
      }

      if (p1->x == pao.x-i && p1->y == pao.y && p1->can_move == CANNOT)
      {
        DrawPaoMan(*p1);
        p1->can_move = CANNOT;
      }
      else if (p2->x == pao.x-i && p2->y == pao.y && p2->can_move == CANNOT)
      {
        DrawPaoMan(*p2);
        p2->can_move = CANNOT;
      }

      if (end == 1)
      {
        break;
      }
   }

      /*-------------RIGHT------------------*/
   for (i=1; i <= pao.len; ++i)
   {
      end = 0;
      switch (map[pao.y][pao.x+i])
      {
      case &#39;b&#39;:
        map[pao.y][pao.x+i] = &#39; &#39;;
        gotoxy(pao.x+15+i, pao.y+6);
        putch(&#39; &#39;);
        break;
      case &#39;a&#39;:
        Treasure(pao.x+i, pao.y);
      case &#39;#&#39;:
        end = 1;
        break;
      default:
        break;
      }

      if (p1->x == pao.x+i && p1->y == pao.y && p1->can_move == CANNOT)
      {
        DrawPaoMan(*p1);
        p1->can_move = CANNOT;
      }
      else if (p2->x == pao.x+i && p2->y == pao.y && p2->can_move == CANNOT)
      {
        DrawPaoMan(*p2);
        p2->can_move = CANNOT;
      }

      if (end == 1)
      {
        break;
      }
   }
}


/*主函数*/
int main()
{
   int END = 0;
   int x, y;
   man play1 = {1, 1, PLAY1, CAN, 1, 1}, play2 = {14, 1, PLAY2, CAN, 1, 1};
   LinkQueue PaoQueue, BlastQueue;
   int iCurrentTime, iOldTime;
   PAO PaoHead, BlastHead;


   for (y=0; y<Map_Y; ++y)
   {
      for (x=0; x<Map_X; ++x)
      {
        map[y][x] = map1[y][x];
      }
   }

   clrscr();
   randomize();
   
   /*显示游戏开始画面*/
   DrawBegin();
   getch();
   clrscr();
   /*开始游戏,先画地图*/
   DrawMap();
   /*画出两个人来*/
   DrawMan(play1);
   DrawMan(play2);

   /*初始化泡泡队列和爆炸队列*/
   InitalQueue(&PaoQueue);
   InitalQueue(&BlastQueue);

   InstallKeyboard();
   iOldTime = clock();
   /*游戏主循环——接受按键并执行相关操作*/
   while (END != 1)
   {

      if (GetKey(KEY_ESC))
      {  
        END=1;
      }
      /*---------Play1--------------*/
      if (GetKey(PLAY1UP))
      {
        if (!MoveUp(&play1, &play2))
        {
           while (clock() - iOldTime <= WIN_DELAY);
           END = 1;
        }
      }
      if (GetKey(PLAY1DOWN))
      {
        if (!MoveDown(&play1, &play2))
        {
           while (clock() - iOldTime <= WIN_DELAY);
           END = 1;
        }
      }
      if (GetKey(PLAY1LEFT))
      {
        if (!MoveLeft(&play1, &play2))
        {
           while (clock() - iOldTime <= WIN_DELAY);
           END = 1;
        }
      }
      if (GetKey(PLAY1RIGHT))
      {
        if (!MoveRight(&play1, &play2))
        {
           while (clock() - iOldTime <= WIN_DELAY);
           END = 1;
        }
      }
      if (GetKey(PLAY1FIRE))
      {
        Paopao(&play1, &PaoQueue);
      }
      /*---------Play2--------------*/
      if (GetKey(PLAY2UP))
      {
        if (!MoveUp(&play2, &play1))
        {
           while (clock() - iOldTime <= WIN_DELAY);
           END = 1;
        }
      }
      if (GetKey(PLAY2DOWN))
      {
        if (!MoveDown(&play2, &play1))
        {
           while (clock() - iOldTime <= WIN_DELAY);
           END = 1;
        }
      }
      if (GetKey(PLAY2LEFT))
      {
        if (!MoveLeft(&play2, &play1))
        {
           while (clock() - iOldTime <= WIN_DELAY);
           END = 1;
        }
      }
      if (GetKey(PLAY2RIGHT))
      {
        if (!MoveRight(&play2, &play1))
        {
           while (clock() - iOldTime <= WIN_DELAY);
           END = 1;
        }
      }
      if (GetKey(PLAY2FIRE))
      {
        Paopao(&play2, &PaoQueue);
      }

      /*---------Ok! we check if the pao is blast!----------------*/
      /*循环一次就检查泡泡的爆炸时间到了没有*/

      iCurrentTime = clock();

      if (QueueHead(&PaoQueue, &PaoHead))
      {
        if ((iCurrentTime - PaoHead.original_time) >= PAO_TIME)
        {
           DeQueue(&PaoQueue);
           map[PaoHead.y][PaoHead.x] = &#39; &#39;;
           gotoxy(PaoHead.x+15, PaoHead.y+6);
           putch(&#39; &#39;);
           /* blast */
           PaoBlast(PaoHead, &play1, &play2, &PaoQueue, &BlastQueue);
           PaoHead.original_time = iCurrentTime;
           EnQueue(&BlastQueue, PaoHead);
        }
      }

      /*检查是不是应该擦除爆炸痕迹了*/
      if (QueueHead(&BlastQueue, &BlastHead))
      {
        if ((iCurrentTime - BlastHead.original_time) >= BLAST_TIME)
        {
           DeQueue(&BlastQueue);
           /*-- Erase the blast --*/
           EraseBlast(BlastHead, &play1, &play2);
        }
      }

      /*检查人物被炸后,有没有到恢复时间*/
      if (play1.can_move == CANNOT)  
      {
        if (iCurrentTime - play1.old_time >= PAOMAN_DELAY)
        {
           play1.can_move = CAN;
           DrawMan(play1);
        }
      }
      
      if (play2.can_move == CANNOT)  
      {
        if (iCurrentTime - play2.old_time >= PAOMAN_DELAY)
        {
           play2.can_move = CAN;
           DrawMan(play2);
        }
      }

      /*拖延一段时间*/
      while (clock() - iOldTime < TIME_DELAY);
      iOldTime = clock();

      /*如果按了ESC键或游戏结素,询问要不要重新开始*/
      if (END == 1)
      {
        gotoxy(34, 3);
        textcolor(WHITE);
        printf("Play again?(y/n)");

        while (1)
        {
           
           if (GetKey(KEY_Y))
           {

              DestroyQueue(&PaoQueue);
              DestroyQueue(&BlastQueue);

              InitalQueue(&PaoQueue);
              InitalQueue(&BlastQueue);

              for (y=0; y<Map_Y; ++y)
              {
                for (x=0; x<Map_X; ++x)
                {
                   map[y][x] = map1[y][x];
                }
              }

              play1.x = 1;
              play1.y = 1;
              play1.which = PLAY1;
              play1.can_move = CAN;
              play1.len = 1;
              play1.pao_num = 1;
              play2.x = 14;
              play2.y = 1;
              play2.which = PLAY2;
              play2.can_move = CAN;
              play2.len = 1;
              play2.pao_num = 1;

              textcolor(LIGHTBLUE);
              clrscr();
              DrawMap();

              DrawMan(play1);
              DrawMan(play2);

              END = 0;
              break;
           }
           
           else if (GetKey(KEY_N))
           {
              break;
           }
        }

      }

   }
   ShutDownKeyboard();
   DestroyQueue(&PaoQueue);
   DestroyQueue(&BlastQueue);

   return 0;
}
曾几何时,有人对我说:装B遭雷劈。我说:去你妈的。于是,这个人又对我说:如果再说脏话,上帝会惩罚你的。我说:我操上帝。结论:彪悍的人生不需要上帝。

TOP

发新话题