鱼C论坛

 找回密码
 立即注册
查看: 1622|回复: 4

[已解决]问一下出现这种情况该用什么命令来编译

[复制链接]
发表于 2017-5-7 00:54:33 | 显示全部楼层 |阅读模式
10鱼币
问一下出现这种情况该用什么命令来编译
我在网上找点代码敲敲,发现敲完编译就这样
希望哪位大神能帮忙解决! QQ截图20170507003821.png @小甲鱼
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <curses.h>
  4. #include <signal.h>
  5. #include <sys/time.h>
  6. #include <fcntl.h>
  7. #include <time.h>


  8. #define INIT _POS_X         12
  9. #define INIT _POS_Y         40
  10. #define INIT_SNAKE_LENGTH   5
  11. #define MOVE_LEFT           1
  12. #define MOVE_RIGHT          2
  13. #define MOVE_UP             3
  14. #define MOVE_DOWN           4


  15. struct Snake
  16. {
  17.         int pos_x;
  18.         int pos_y;
  19.         struct Snake *pNext;
  20.         struct Snake *pPre;
  21. } *pSnakeHead,*pSnakeTail,*pSnakeTrace,*pSnakeFood;

  22. int move_dir;
  23. int is_game_over = 0;
  24. int speed[5] = {200,150,120,100,80};
  25. int snake_len = INIT_SNAKE_LENGTH;
  26. int current_score;
  27. int levelup_score[5] = {5,10,15,20,25};
  28. int level = 0;
  29. int life = 3;
  30. void init_wall();
  31. void init_snake();
  32. void show_snake(struct Snake *pSnake);
  33. void snake_move(int sig);
  34. int set_ticker(long n_msecs);
  35. void getOrder();
  36. int is_snake_dead();
  37. void put_food();
  38. void eat_food();
  39. void levelup();
  40. void over(int i);
  41. void reset_game();
  42. void delete_snake();

  43. int main (void)
  44. {
  45.         initscr();
  46.         clear();
  47.         curs_set(0);
  48.         init_wall();
  49.         init_snake();
  50.         put_food();
  51.         set_ticker(speed[level]);
  52.         noecho();
  53.         system("stty echo");
  54.         system("clear");
  55.         endwin();
  56.         exit(0);
  57. }

  58. void init_wall()
  59. {
  60.         int i;
  61.         for(i = 0; i <COLS ; ++i)
  62.         {
  63.                 move(3,i);
  64.                 addstr("*");
  65.         }

  66.         move(2,0);
  67.         printw("life: %d", life);
  68.         move (2,COLS_10);
  69.         printw("score:%2d",current_score);
  70.         move(2,COLS/2_5);
  71.         printw("level:%d",level+1);
  72.         refresh();
  73. }

  74. void init_snake()
  75. {
  76.         int i;
  77.         struct Snake *pSnakePre;
  78.         int flag=0;
  79.         pSnakeTrace = (struct Snake *)malloc(sizeof(struct Snake));
  80.         pSnakeTrace->pos_x = 0;
  81.         pSnakeTrace->pos_y = 0;
  82.         for (i = 0; i < INIT_SNAKE_LENGTH; ++i)
  83.         {
  84.                 struct Snake *pSnakeBody = (struct Snake *)malloc(sizeof(struct Snake));
  85.                 pSnakeBody->pos_x = INIT_POS_X;
  86.                 pSnakeBody->pos_y = INIT_pos_Y+INIT_SNAKE_LENGTH_i;
  87.                 if (flag == 0)
  88.                 {
  89.                         pSnakeHead = pSnakePre = pSnakeBody;
  90.                         pSnakeBody-.pPre = NULL;
  91.                         flag++;
  92.                 }
  93.                 else
  94.                 {
  95.                         pSnakePre->pNext = pSnakeBody;
  96.                         pSnakeBody->pPre = pSnakePre;
  97.                         pSnakeBody->pNext = NULL;
  98.                         pSnakePre = pSnakeBody;
  99.                 }
  100.         }
  101.         pSnakeTail = pSnakePre;
  102.         move_dir = MOVE_RIGHT;
  103.         signal(SIGALRM,snake_move);
  104.         show_snake(pSnakeHead);
  105. }

  106. int set_ticker( long n_msecs )
  107. {
  108.         struct itimerval new_timeset;
  109.         long   n_sec, n_usecs;

  110.         n_sec = n_msecs / 1000;
  111.         n_usecs = ( n_msecs % 1000 ) * 1000L ;

  112.         new_timeset.it_interval.tv_sec = n_sec;
  113.         new_timeset.it_interval.tv_usec = n_usecs;
  114.         new_timeset.it_value.tv_sec     = n_sec;
  115.         new_timeset.it_value.tv_usec    = n_usecs;

  116.         return setitimer(ITIMER_REAL, &new_timeset, NULL);
  117. }

  118. void show_snake(struct Snake *pSnake)
  119. {
  120.         while (pSnake)
  121.         {
  122.                 move(pSnake->pos_x,pSnake->pos_y);
  123.                 addstr("*");
  124.                 pSnake = pSnake->pNext;
  125.         }
  126.         move(pSnakeTrace->pos_x,pSnakeTrace->pos_y);
  127.         addstr(" ");
  128.         refresh();
  129. }

  130. void snake_move(int sig)
  131. {
  132.         noecho();
  133.         pSnakeTrace->pos_x = pSnakeTail->pos_x;
  134.         pSnakeTrace->pos_y = pSnakeTail->pos_y;
  135.         struct Snake *ptemp;
  136.         ptemp = pSnakeTail->pPre;
  137.         pSnakeTail->pPre->pNext = NULL;
  138.         pSnakeTail->pPre = NULL;
  139.         pSnakeTail->pNext = pSnakeHead;
  140.         pSnakeHead = pSnakeTail;
  141.         pSnakeTail = ptemp;

  142.         switch(move_dir)
  143.         {
  144.                 case MOVE_LEFT:
  145.                         pSnakeHead->pos_y = pSnakeHead->pNext->pos_y-1;
  146.                         pSnakeHead->pos_x = pSnakeHead->pNext->pos_x;
  147.                         break;
  148.                 case MOVE_RIGHT:
  149.                         pSnakeHead->pos_y = pSnakeHead->pNext->pos_y+1;
  150.                         pSnakeHead->pos_x = pSnakeHead->pNext->pos_x;
  151.                         break;
  152.                 case MOVE_UP:
  153.                         pSnakeHead->pos_x = pSnakeHead->pNext->pos_x-1;
  154.                         pSnakeHead->pos_y = pSnakeHead->pNext->pos_y;
  155.                         break;
  156.                 case MOVE_DOWN:
  157.                         pSnakeHead->pos_x = pSnakeHead->pNext->pos_x+1;
  158.                         pSnakeHead_>pos_y = pSnakeHead->pNext->pos-y;
  159.                         break;
  160.                 default:
  161.                         break;
  162.         }

  163.         if ((is_game_over = is_snake_dead()) != 0)
  164.         {
  165.                 over(is_game_over);
  166.         }

  167.         if ((pSnakeHead->pos_x == pSnakeFood->pos_x) && (pSnakeHead->posy == pSnakeFood->pos_y))
  168.         {
  169.                 eat_food();
  170.                 put_food();
  171.                 current_score++;
  172.                 move(2,COLS-10);
  173.                 printW("score:%2d",current_score);
  174.         }
  175.         show_snake(pSnakeHead);
  176.         if (current_score == levelup_score[level])
  177.         {
  178.                 levelup();
  179.         }
  180.         signal (SIGALRM,snake_move);
  181. }

  182. void getOrder()
  183. {
  184.         while(1)
  185.         {
  186.                 char c = getch();
  187.                 switch(c)
  188.                 {
  189.                         case 'a':
  190.                                 move_dir=MOVE_LEFT;
  191.                                 break;
  192.                         case 'd':
  193.                                 move_dir=MOVE_RIGHT;
  194.                                 break;
  195.                         case 'w':
  196.                                 move_dir=MOVE_UP;
  197.                                 break;
  198.                         case 's':
  199.                                 move_dir=MOVE_DOWN;
  200.                                 break;
  201.                         case 'q':
  202.                         system("stty echo");
  203.                         system("clear");
  204.                         endwin();
  205.                         exit(0);
  206.                         break;
  207.                         default:
  208.                         break;
  209.                 }
  210.         }
  211. }
  212. int is_snake_dead()
  213. {
  214.         struct Snake *pSnakeBody = pSnakeHead->pNext;
  215.         int k=0;
  216.         while(pSnakeBody)
  217.         {
  218.                 if((pSnakeHead->pos_x == pSnakeBody->pos_x) && (pSnakeHead->pos_y == pSnakeBody->pos_y))
  219.                 {
  220. return 2;
  221.                 }
  222.                 pSnakeBody = pSnakeBody->pNext;
  223.                 k++;
  224.         }
  225.         if ((pSnakeHead->pos_x == 3) || (pSnakeHead->pos_x == LINES) || (pSnakeHead->pos_y == 0) ||(pSnakeHead->pos_y == COLE))
  226. }
  227. return 1;
  228. }
  229. return 0;
  230. }

  231. void put_food()
  232. {
  233.         int food_pos_x,food_pos_y;
  234.         int rangex,rangey;
  235.         int flag=0;
  236.         srand(time(NULL));
  237.         rangex = LINES-4;
  238.         rangey = COLS-1;
  239.         struct Snake *pTemp = pSnakeHead;
  240.         while(1)
  241.         {
  242.                 flag = 0;
  243.                 pTemp = pSnakeHead;
  244.                 food_pos_x = rand()%rangex +4;
  245.                 food_pos_y = rand()%rangey +1;
  246.                 while(pTemp)
  247.                 {
  248.                         if ((pTemp->pos_x == food_pos_x) && (pTemp->pos_y == food_pos_y))
  249.                         {
  250.                                 flag++;
  251.                         }
  252.                 pTemp = pTemp->pNext;  
  253.                 }   
  254.                  if (flag ==0)
  255.                  {
  256.                          break;
  257.                  }
  258.         }

  259. struct Snake *pPutFood = (struct Snake *)malloc(sizeof(struct Snake));  
  260. pSnakeFood = pPutFood;  
  261. pSnakeFood->pos_x = food_pos_x;  
  262. pSnakeFood->pos_y = food_pos_y;  
  263. pSnakeFood->pPre = NULL;  
  264. pSnakeFood->pNext = NULL;  
  265. move(pSnakeFood->pos_x,pSnakeFood->pos_y);  
  266. addstr("0");  
  267. }
  268.   
  269. void eat_food()  
  270. {
  271.                pSnakeFood->pNext = pSnakeHead->pNext;  
  272.         pSnakeHead->pNext->pPre = pSnakeFood;  
  273.         pSnakeHead->pNext = NULL;  
  274.         pSnakeTail->pNext = pSnakeHead;  
  275.         pSnakeHead->pPre = pSnakeTail;  
  276.         pSnakeTail = pSnakeHead;
  277.         pSnakeHead = pSnakeFood;  
  278. }  
  279.   
  280. void levelup()  
  281. {  
  282.           if (level == 4)
  283.           {
  284.                   move(0, 1);
  285.                   addstr("finish game ,press any key to reset game");
  286.                   refresh();
  287.                   set_ticker(0);
  288.                   char c = getchar();
  289.                   level = 0;
  290.                   life = 3;
  291.                   reset_game();
  292.           }
  293.           else
  294.           {
  295.                   move(0, 1);
  296.                   addstr("level up ,press any key to contiune     ");
  297.                   refresh();
  298.                   set_ticker(0);
  299.                   char c = getchar();
  300.                   level++;
  301.                   reset_game();
  302.           }

  303. }  
  304.   
  305. void delete_snake()  
  306. {
  307.                struct Snake *pSnake;
  308.         pSnake = pSnakeHead;
  309.         while(pSnake)
  310.         {
  311.                 free(pSnake);
  312.                 pSnake = pSnake->pNext;
  313.         }
  314.                free(pSnakeFood);  
  315. }  
  316.   
  317. void reset_game()  
  318. {
  319.         current_score = 0;
  320.         delete_snake();
  321.         initscr();
  322.         clear();
  323.         init_wall();
  324.         init_snake();
  325.         put_food();
  326.         set_ticker(speed[level]);  
  327. }  
  328.   
  329. void over(int i)
  330. {
  331.         life--;
  332.         move(2,COLS/2-5);
  333.         printw("life: %d", life);
  334.         if (life == 0)
  335.         {
  336.                 move(0, 1);
  337.                 addstr("Game Over,press any key to reset game                ");
  338.                 refresh();
  339.                 life = 3;
  340.                 set_ticker(0);
  341.                 char c = getchar();
  342.                 level = 0;
  343.                 reset_game();
  344.                
  345.                 else
  346.                 {
  347.                         move(0, 1);
  348.                         if(1 == i)
  349.                         {
  350.                                 addstr("Crash the wall,press any key to contiune this level");
  351.                         }
  352.                         else if(2 == i)
  353.                         {
  354.                                 addstr("Crash itself,press any key to contiune this level   ");
  355.                                 refresh();
  356.                                 set_ticker(0);
  357.                                 char c = getchar();
  358.                                 reset_game();
  359.                         }
  360.                 }
  361.         }  
复制代码
最佳答案
2017-5-7 00:54:34
SharkYe 发表于 2017-5-7 02:07
安装好后用什么命令呢

gcc program.c -o program -lcurses
http://www.cnblogs.com/dandingyy/archive/2012/08/23/2651644.html

最佳答案

查看完整内容

gcc program.c -o program -lcurses http://www.cnblogs.com/dandingyy/archive/2012/08/23/2651644.html
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-5-7 00:54:34 | 显示全部楼层    本楼为最佳答案   
SharkYe 发表于 2017-5-7 02:07
安装好后用什么命令呢

gcc program.c -o program -lcurses
http://www.cnblogs.com/dandingyy/archive/2012/08/23/2651644.html
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-5-7 00:56:04 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-5-7 02:02:57 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-5-7 02:07:05 From FishC Mobile | 显示全部楼层
人造人 发表于 2017-5-7 02:02
需要安装 ncurses
http://wzy201109.blog.163.com/blog/static/195408078201331771031393/

安装好后用什么命令呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-24 17:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表