1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <time.h>
  5. #include <Windows.h>
  6.  
  7. #define KEY_ESC         0x1B
  8. #define KEY_8           '8'
  9. #define KEY_2           '2'
  10. #define KEY_4           '4'
  11. #define KEY_6           '6'
  12. #define KEY_W           'w'
  13. #define KEY_X           'x'
  14. #define KEY_A           'a'
  15. #define KEY_D           'd'
  16. // border 테두리 위치
  17. #define BDR_L           2
  18. #define BDR_R           77
  19. #define BDR_T           3
  20. #define BDR_B           23
  21. // direction 방향
  22. #define UP                      1
  23. #define DOWN            2
  24. #define LEFT            3
  25. #define RIGHT           4
  26. // maximum tron length 최대 길이
  27. #define MAXLEN          1500
  28.  
  29.  
  30. // location of tron and fuel
  31. // 트론과 먹이의 위치
  32. struct _pos{
  33.         int x;
  34.         int y;
  35. } trail[MAXLEN], fuel;
  36.  
  37.  
  38. // state of the tron
  39. // 트론의 상태: 방향, 길이, 속도
  40. int direction = RIGHT;
  41. int length = 2;
  42. int speed = 50;
  43. int clearsize = 10;
  44.  
  45. void init();                                    // initialize
  46. void turn(int key);                             // turn
  47. void move();                                    // move the tron
  48. int check();                                    // check collisions
  49. void make_fuel();                               // place fuel
  50. void draw_border();                             // draw border
  51. void draw_bike();                               // draw tron bike
  52. void draw_trail();                              // draw trail
  53. void clear_trail(int n);                // clear trail
  54. void draw_fuel();                               // draw fuel
  55. void draw_stat();
  56. void gotoxy(int x, int y);              // move cursor
  57. void showCursor(BOOL bVisible);
  58. void game_start();
  59.  
  60.  
  61. int main(void)
  62. {
  63.         int ch;
  64.         game_start();
  65.         // initialize the game
  66.         init();
  67.  
  68.         // instruction
  69.         gotoxy(0, 0);
  70.         puts("Left(4, A), Right(6, D), Down(2, X), Up(8, W), Quit(ESC)");
  71.         puts("Press any key to start...");
  72.        
  73.         _getch();
  74.  
  75.         // play
  76.         while (!(ch = check()))
  77.         {
  78.                 if (_kbhit())
  79.                         turn(_getch());
  80.                
  81.                 draw_trail();
  82.                 move();
  83.                 draw_bike();
  84.  
  85.                 draw_stat();
  86.                
  87.                 Sleep(speed);
  88.         }
  89.  
  90.         // exit
  91.         gotoxy(0, BDR_B + 1);
  92.         switch (ch)
  93.         {
  94.         case 1:
  95.                 puts("Game over!");
  96.                 break;
  97.         case 2:
  98.                 puts("No more fuel!");
  99.                 break;
  100.         }
  101.         return 0;
  102. }
  103.  
  104. // turn the direction of the tron
  105. void turn(int key)
  106. {
  107.         // 상하좌우 key에 따라
  108.         // 트론의 방향(direction)을 바꾼다.
  109.  
  110.         switch (key)
  111.         {
  112.         case KEY_4:             // left
  113.         case KEY_A:
  114.                 if (direction != RIGHT)
  115.                         direction = LEFT;
  116.                 // TODO
  117.  
  118.                 break;
  119.         case KEY_6:             // right
  120.         case KEY_D:
  121.                 if (direction != LEFT)
  122.                         direction = RIGHT;
  123.                 // TODO
  124.  
  125.                 break;
  126.         case KEY_2:             // down
  127.         case KEY_X:
  128.                 if (direction != UP)
  129.                         direction = DOWN;
  130.                 // TODO
  131.  
  132.                 break;
  133.         case KEY_8:             // up
  134.         case KEY_W:
  135.                 if (direction != DOWN)
  136.                         direction = UP;
  137.                 // TODO
  138.  
  139.                 break;
  140.         case KEY_ESC:   // quit
  141.                 gotoxy(0, BDR_B + 1);
  142.                 puts("Exit!\n");
  143.  
  144.                 exit(0);
  145.                 break;
  146.         }
  147. }
  148.  
  149.  
  150. // move the tron in the current direction
  151. void move()
  152. {
  153.         /* TODO
  154.         트론의 현재 진행 방향(direction)에 따라
  155.         트론의 위치(trail)을 갱신한다.
  156.        
  157.         */
  158.         int i;
  159.        
  160.         switch (direction)
  161.         {
  162.         case RIGHT:
  163.                
  164.                 trail[0].x++;
  165.                 break;
  166.         case LEFT:
  167.                 trail[0].x--;
  168.                 break;
  169.         case UP:
  170.                 trail[0].y--;
  171.                 break;
  172.         case DOWN:
  173.                 trail[0].y++;
  174.         }
  175.  
  176. }
  177.  
  178.  
  179. // collision check


  180. // draw tron bike
  181. void draw_bike()
  182. {
  183.         // draw tron
  184.         gotoxy(trail[length-1].x, trail[length-1].y);
  185.         switch (direction)
  186.         {
  187.         case UP:
  188.                 putchar('^');
  189.                 break;
  190.         case DOWN:
  191.                 putchar('v');
  192.                 break;
  193.         case LEFT:
  194.                 putchar('<');
  195.                 break;
  196.         case RIGHT:
  197.                 putchar('>');
  198.                 break;
  199.         }
  200. }
  201.  
  202.  
  203. // draw trail
  204. void draw_trail()
  205. {
  206.         gotoxy(trail[length - 1].x, trail[length - 1].y);
  207.  
  208.         switch (direction)
  209.         {
  210.         case LEFT:
  211.         case RIGHT:
  212.                 putchar('-');
  213.                 break;
  214.         case UP:
  215.         case DOWN:
  216.                 putchar(':');
  217.                 break;
  218.         }
  219. }
  220.  
  221.  
  222. // clear trail
  223. void clear_trail(int n)
  224. {
  225.         int i;
  226.  
  227.         for (i = 0; i < n; i++)
  228.         {
  229.                 gotoxy(trail[i].x, trail[i].y);
  230.                 putchar(' ');
  231.         }
  232.  
  233. }
  234.  
  235.  
  236. // draw fuel
  237. void draw_fuel()
  238. {
  239.         gotoxy(fuel.x, fuel.y);
  240.         putchar('o');
  241. }
  242.  
  243.  
  244. // draw stat
  245. void draw_stat()
  246. {
  247.         gotoxy(70, 2);
  248.         printf("]", length);
  249. }
  250.  
  251.  
  252. // draw border
  253. void draw_border()
  254. {
  255.         int i;
  256.         gotoxy(BDR_L, BDR_T);
  257.         for (i = BDR_L; i < BDR_R + 1; i++)
  258.                 printf("#");
  259.  
  260.         for (i = BDR_T + 1; i < BDR_B; i++)
  261.         {
  262.                 gotoxy(BDR_L, i);
  263.                 printf("#");
  264.                 gotoxy(BDR_R, i);
  265.                 printf("#");
  266.         }
  267.  
  268.         gotoxy(BDR_L, BDR_B);
  269.         for (i = BDR_L; i < BDR_R + 1; i++)
  270.                 printf("#");
  271.  
  272. }
  273.  
  274.  
  275. // move cursor to (x,y)
  276. // upper left corner is (0,0)
  277. void gotoxy(int x, int y)
  278. {
  279.         COORD Pos = { x, y };
  280.         SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
  281. }
  282.  
  283.  
  284. // show cursor
  285. void showCursor(BOOL bVisible)
  286. {
  287.         CONSOLE_CURSOR_INFO CurInfo;
  288.         CurInfo.dwSize = 100;
  289.         CurInfo.bVisible = bVisible;
  290.         SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &CurInfo);
  291. }


프로그램 종류는 뱀게임 반대 개념인데요..  가는 방향으로 계속 길이가 늘어나다가 먹이를 먹으면 줄어드는 방식입니다.

위쪽 이미지가 제가 실행한 이미지고 아래 이미지가 완성 원본입니다. void move쪽에서 뭔가 많이 만져야 할거같은데 만지지 못하겠습니다...

이미지 우측 상단에 있는 숫자도 길이가 증가함에 따라서 같이 증가해야 하는데 이걸 하지를 못하겠습니다...

혹시 대략적으로도 조언 부탁드릴 수 있을까요?....