Linux上的C语言简单程序

时间:2013-11-28 11:18:02

标签: c multithreading gcc

嗨evry body:p这是我的简单程序

  /*
 *         A sample game   
 *         Fahci Rochdi @KnG 
 *         Central University of MILA
 *
 *       */

#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<pthread.h>
#include<time.h>
#include<math.h>

#define xT 80
#define yT 30
char Terrain[yT][xT]; /*The terrain*/   
int xchat, ychat,xsouris, ysouris;/*The x,y coordinates of the cat (chat) and mouse (souris)*/
int X,Y;
int vsouri,vchat;/*the speed of the cat and mouse*/
int timeout; /*game duration*/
/* Start with a simple Annnimation */

char catchou[3][11];
void blink();
void normale();
void display();
void right();
void left();
////////////////////////////////////


////////////////////////////////////
void init ()
{
int i,j;
  X=30;
  Y=15;
  for (i = 0; i < Y; i++)
    {
      for (j = 0; j < X-1; j++)
    if ((i == 0) || (j == 0) || (j == (X - 2)) || (i == (Y - 1)))
      Terrain[i][j] = '*';
    else
      Terrain[i][j] = ' ';
      Terrain[i][X - 1] = '\0';
    }
  Terrain[ychat][xchat]='C';
  Terrain[ysouris][xsouris]='S';
}
////////////////////////////////////

int gameover ()
{ if(timeout<1)
    return (timeout==0);
  else 
    return ((abs (xchat - xsouris) <= 1) && (abs (ychat - ysouris) <= 1));
}
////////////////////////////////////
int timeOut()
{
    sleep(1);  
    return (timeout--);
} 

////////////////////////////////////

float distance(a)
{
return (sqrt(pow(xchat-xsouris,2)+pow(ychat-ysouris,2)));
}
////////////////////////////////////

void afficher ()
{
int i;char pause;
  while(!gameover ())
    { 
      Terrain[ychat][xchat]='C';
      Terrain[ysouris][xsouris]='S';
      system ("clear");
      for (i = 0; i < Y; i++)
    printf("%s\n", Terrain[i]);
        printf("****************************\n");
        printf("*    Xc= [ %d ] Vc [%d]    *\n",xchat,vchat);
        printf("*    Yc= [ %d ]            *\n",ychat);             
        printf("*    Xs= [ %d ] Vs [%d]    *\n",xsouris,vsouri);
        printf("*    Ys= [ %d ]            *\n",ysouris);
        printf("       %f                  *\n",distance()); 
        printf("*    TimeOut <%d>          *\n",timeOut());
        printf("****************************\n");
      usleep (100000);
        if ( pause=='p'){
           system("pause");
           printf("PAUSE\n");} 

        }


    printf("\n\t\t\t GAME OVER \n");
}

////////////////////////////////////

void *chat ()
{
  while (!gameover ())
    {

      Terrain[ychat][xchat]=' ';
      if (xchat > xsouris) xchat--;
      if (xchat < xsouris) xchat++;
      if (ychat > ysouris) ychat--;
      if (ychat < ysouris) ychat++;

switch (vchat)
{
 case 1 :
      usleep(1000000);
      break;

 case 2 :
      usleep(500000);
      break;

 case 3 :
      usleep(100000);
      break;
}
    }
}

////////////////////////////////////

void *souris ()
{
  while (!gameover ())
    {

     Terrain[ysouris][xsouris]=' ';
        if ((xsouris >= xchat) && (xsouris < X - 3)) xsouris++;
        if ((xsouris <= xchat) && (xsouris > 1)) xsouris--;
        if ((ysouris >= ychat) && (ysouris < Y - 2)) ysouris++;
        if ((ysouris <= ychat) && (ysouris > 1)) ysouris--;

  switch (vsouri)
{
 case 1 :
      usleep(1000000);
      break;

 case 2 :
      usleep(500000);
      break;

 case 3 :
      usleep(100000);
      break;
}
    }        


}

////////////////////////////////////
////////////////////////////////////

void normale(){
int i,j;
for (i=0;i<3;i++)
for (j=0;j<11;j++)
catchou[i][j]=' ';
catchou[0][3]=catchou[0][6]='/';
catchou[0][4]=catchou[0][7]='\\';
catchou[1][1]=catchou[1][9]='=';
catchou[1][2]='(';
catchou[1][8]=')';
catchou[1][4]=catchou[1][6]='o';
for (i=0;i<11;i++)
catchou[2][i]='-';
catchou[2][2]=catchou[2][8]='0';
display();
}

///////////////////////////////////////

void display(){
printf("\033[2J\033[1;1H\033[01;32m\033[01;32m ");
int i,j;
for (i=0;i<3;i++){
printf("\t");
for (j=0;j<11;j++)
printf("%c",catchou[i][j]);
printf("\n");
}
}

///////////////////////////////////////

void blink(){
catchou[1][4]=catchou[1][6]='-';
display();
}

//////////////////////////////////////
//HIS RIGHT NOT YOURS -_-
void right(){
catchou[0][2]=catchou[0][5]='/';
catchou[0][3]=catchou[0][6]='\\';
catchou[0][4]=catchou[0][7]=' ';
catchou[1][4]=catchou[1][6]=' ';
catchou[1][3]=catchou[1][5]='o';
display();
}

///////////////////////////////////////
//HIS LEFT NOT YOURS -_-
void left(){
catchou[0][4]=catchou[0][7]='/';
catchou[0][5]=catchou[0][8]='\\';
catchou[0][3]=catchou[0][6]=' ';
catchou[1][4]=catchou[1][6]=' ';
catchou[1][5]=catchou[1][7]='o';
display();
}





int main (int argc, char *argv[])
{ 
  int i,j;
  pthread_t idc, ids;

  xchat =atoi(argv[1]) ;
  ychat = atoi(argv[2]);
  xsouris =atoi(argv[3]);
  ysouris = atoi(argv[4]);
  vchat = atoi(argv[5]);
  vsouri= atoi(argv[6]);
  timeout= atoi(argv[7]);


  normale();sleep(3);

  blink();usleep(100000);normale();sleep(2);

  blink();usleep(100000);normale();sleep(1);

  left();usleep(500000);normale();sleep(1);

  right();usleep(500000);normale();sleep(2);

  blink();usleep(100000);normale();usleep(300000);

  blink();usleep(100000);normale();sleep(2);

  init ();
  pthread_create (&idc, NULL, chat, NULL);
  pthread_create (&ids, NULL, souris, NULL);
  afficher ();


}

这是一只猫试图抓住老鼠的简单游戏。 我使用以下命令编译它:

 GCC <name>.c -o <name> -lpthread -lm 

并通过传递参数执行它,如下所示:

./<name> <xcat> <ycat> <xmouse> <ymouse> <speedc> <speedm> <timeout>

以下是我需要帮助的地方:

  1. 显示屏超时出现问题:(

  2. 如何在屏幕上显示计时器。

  3. 如何添加选项以启动和暂停游戏?

  4. 如何添加键盘控件,例如按'a'将猫移动到左边,按'z'将猫移动。

1 个答案:

答案 0 :(得分:0)

对于计时器显示,将光标置于Ansi escape codes(您已在显示功能中使用它们)。