- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <time.h>
- #include <Windows.h>
- #define KEY_ESC 0x1B
- #define KEY_8 '8'
- #define KEY_2 '2'
- #define KEY_4 '4'
- #define KEY_6 '6'
- #define KEY_W 'w'
- #define KEY_X 'x'
- #define KEY_A 'a'
- #define KEY_D 'd'
- // border 테두리 위치
- #define BDR_L 2
- #define BDR_R 77
- #define BDR_T 3
- #define BDR_B 23
- // direction 방향
- #define UP 1
- #define DOWN 2
- #define LEFT 3
- #define RIGHT 4
- // maximum tron length 최대 길이
- #define MAXLEN 1500
- // location of tron and fuel
- // 트론과 먹이의 위치
- struct _pos{
- int x;
- int y;
- } trail[MAXLEN], fuel;
- // state of the tron
- // 트론의 상태: 방향, 길이, 속도
- int direction = RIGHT;
- int length = 2;
- int speed = 50;
- int clearsize = 10;
- void init(); // initialize
- void turn(int key); // turn
- void move(); // move the tron
- int check(); // check collisions
- void make_fuel(); // place fuel
- void draw_border(); // draw border
- void draw_bike(); // draw tron bike
- void draw_trail(); // draw trail
- void clear_trail(int n); // clear trail
- void draw_fuel(); // draw fuel
- void draw_stat();
- void gotoxy(int x, int y); // move cursor
- void showCursor(BOOL bVisible);
- void game_start();
- int main(void)
- {
- int ch;
- game_start();
- // initialize the game
- init();
- // instruction
- gotoxy(0, 0);
- puts("Left(4, A), Right(6, D), Down(2, X), Up(8, W), Quit(ESC)");
- puts("Press any key to start...");
- _getch();
- // play
- while (!(ch = check()))
- {
- if (_kbhit())
- turn(_getch());
- draw_trail();
- move();
- draw_bike();
- draw_stat();
- Sleep(speed);
- }
- // exit
- gotoxy(0, BDR_B + 1);
- switch (ch)
- {
- case 1:
- puts("Game over!");
- break;
- case 2:
- puts("No more fuel!");
- break;
- }
- return 0;
- }
- // turn the direction of the tron
- void turn(int key)
- {
- // 상하좌우 key에 따라
- // 트론의 방향(direction)을 바꾼다.
- switch (key)
- {
- case KEY_4: // left
- case KEY_A:
- if (direction != RIGHT)
- direction = LEFT;
- // TODO
- break;
- case KEY_6: // right
- case KEY_D:
- if (direction != LEFT)
- direction = RIGHT;
- // TODO
- break;
- case KEY_2: // down
- case KEY_X:
- if (direction != UP)
- direction = DOWN;
- // TODO
- break;
- case KEY_8: // up
- case KEY_W:
- if (direction != DOWN)
- direction = UP;
- // TODO
- break;
- case KEY_ESC: // quit
- gotoxy(0, BDR_B + 1);
- puts("Exit!\n");
- exit(0);
- break;
- }
- }
- // move the tron in the current direction
- void move()
- {
- /* TODO
- 트론의 현재 진행 방향(direction)에 따라
- 트론의 위치(trail)을 갱신한다.
- */
- int i;
- switch (direction)
- {
- case RIGHT:
- trail[0].x++;
- break;
- case LEFT:
- trail[0].x--;
- break;
- case UP:
- trail[0].y--;
- break;
- case DOWN:
- trail[0].y++;
- }
- }
- // collision check
- // draw tron bike
- void draw_bike()
- {
- // draw tron
- gotoxy(trail[length-1].x, trail[length-1].y);
- switch (direction)
- {
- case UP:
- putchar('^');
- break;
- case DOWN:
- putchar('v');
- break;
- case LEFT:
- putchar('<');
- break;
- case RIGHT:
- putchar('>');
- break;
- }
- }
- // draw trail
- void draw_trail()
- {
- gotoxy(trail[length - 1].x, trail[length - 1].y);
- switch (direction)
- {
- case LEFT:
- case RIGHT:
- putchar('-');
- break;
- case UP:
- case DOWN:
- putchar(':');
- break;
- }
- }
- // clear trail
- void clear_trail(int n)
- {
- int i;
- for (i = 0; i < n; i++)
- {
- gotoxy(trail[i].x, trail[i].y);
- putchar(' ');
- }
- }
- // draw fuel
- void draw_fuel()
- {
- gotoxy(fuel.x, fuel.y);
- putchar('o');
- }
- // draw stat
- void draw_stat()
- {
- gotoxy(70, 2);
- printf("]", length);
- }
- // draw border
- void draw_border()
- {
- int i;
- gotoxy(BDR_L, BDR_T);
- for (i = BDR_L; i < BDR_R + 1; i++)
- printf("#");
- for (i = BDR_T + 1; i < BDR_B; i++)
- {
- gotoxy(BDR_L, i);
- printf("#");
- gotoxy(BDR_R, i);
- printf("#");
- }
- gotoxy(BDR_L, BDR_B);
- for (i = BDR_L; i < BDR_R + 1; i++)
- printf("#");
- }
- // move cursor to (x,y)
- // upper left corner is (0,0)
- void gotoxy(int x, int y)
- {
- COORD Pos = { x, y };
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
- }
- // show cursor
- void showCursor(BOOL bVisible)
- {
- CONSOLE_CURSOR_INFO CurInfo;
- CurInfo.dwSize = 100;
- CurInfo.bVisible = bVisible;
- SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &CurInfo);
- }
프로그램 종류는 뱀게임 반대 개념인데요.. 가는 방향으로 계속 길이가 늘어나다가 먹이를 먹으면 줄어드는 방식입니다.
위쪽 이미지가 제가 실행한 이미지고 아래 이미지가 완성 원본입니다. void move쪽에서 뭔가 많이 만져야 할거같은데 만지지 못하겠습니다...
이미지 우측 상단에 있는 숫자도 길이가 증가함에 따라서 같이 증가해야 하는데 이걸 하지를 못하겠습니다...
혹시 대략적으로도 조언 부탁드릴 수 있을까요?....
혹시 코드 전체를 다 보여드려야 하나요..?
뭐가 문젠지 알겠네요. 뭔지는 내일 알려드림
전체 코드 보여주세요