2013-11-28 2 views
-1

안녕하세요 에브리 바디에 C에서 간단한 프로그램 : 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 

을하고 매개 변수를 전달하여 실행 다음과 같이

  1. 문제있다 :

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

    내가 도움이 필요 무엇 디스플레이의 시간 초과 :(

  2. 어떻게해야합니까? 화면에 타이머를 표시합니다.

  3. 게임을 시작하고 일시 중지하는 옵션을 어떻게 추가 할 수 있습니까?

  4. 어떻게 키보드 컨트롤을 추가 할 수 있습니까? 'a'를 누르면 고양이는 왼쪽으로 움직이고 'z'를 누르면 고양이가 아래로 움직입니다.

+8

TL; 코드를 문제의 원인이되는 부분으로 좁혀주십시오. –

+2

도움이 필요하면 사람들이 쉽게 도와 줄 수 있도록하십시오. 코드의 범위를 좁히고 모든 주석과 변수 이름을 영어로 번역하십시오. – kusma

+2

코드를 들여 쓰기 : 올바른 (흰색) 간격 등을 사용하십시오. 또한 문장 부호, 철자 및 문법에주의하여 읽기 쉽도록하십시오. –

답변

0

타이머 표시의 경우 커서를 Ansi escape codes (사용자가 이미 디스플레이 기능에 사용 했음)으로 놓습니다.