Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #include <conio.h>
- #define MAZE_BOARD_HEIGHT 10
- #define MAZE_BOARD_WIDTH 6
- #define POINT_X 4 //보드 시작좌표 x
- #define POINT_Y 2 //보드 시작좌표 y
- #define LEFT 75
- #define RIGHT 77
- #define UP 72
- #define DOWN 80
- #define DELAY 100
- #define EXIT 50
- int currunt_stage = 1;
- int maze[MAZE_BOARD_HEIGHT][MAZE_BOARD_WIDTH]={
- {'1','1','1','1','1','1'},
- {'e','0','0','0','0','1'},
- {'1','0','1','1','1','1'},
- {'1','0','0','0','1','1'},
- {'1','0','1','0','0','1'},
- {'1','0','0','1','0','1'},
- {'1','1','1','0','0','1'},
- {'1','0','1','0','1','1'},
- {'1','0','0','0','0','x'},
- {'1','1','1','1','1','1'}
- };
- int maze2[MAZE_BOARD_HEIGHT][MAZE_BOARD_WIDTH]={
- {'1','1','1','1','1','1'},
- {'e','0','0','1','1','1'},
- {'1','1','0','1','1','1'},
- {'1','1','0','1','1','1'},
- {'1','1','0','0','1','1'},
- {'1','1','0','0','1','1'},
- {'1','1','1','0','1','1'},
- {'1','1','1','0','1','1'},
- {'1','1','1','0','0','x'},
- {'1','1','1','1','1','1'}
- };
- int maze3[MAZE_BOARD_HEIGHT][MAZE_BOARD_WIDTH]={
- {'1','1','1','1','1','1'},
- {'e','0','0','0','0','1'},
- {'1','1','1','1','0','1'},
- {'1','0','0','0','0','1'},
- {'1','0','1','1','1','1'},
- {'1','0','0','0','0','1'},
- {'1','1','1','1','0','1'},
- {'1','0','0','0','0','1'},
- {'1','0','1','1','1','1'},
- {'1','x','1','1','1','1'}
- };
- void setCursor(int x,int y)
- {
- COORD pos;
- pos.X=x;
- pos.Y=y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
- }
- COORD getCursor(void)
- {
- COORD curPoint;
- CONSOLE_SCREEN_BUFFER_INFO pos;
- GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&pos);
- curPoint.X=pos.dwCursorPosition.X;
- curPoint.Y=pos.dwCursorPosition.Y;
- return curPoint;
- }
- void removeCursor(void)
- {
- CONSOLE_CURSOR_INFO cur;
- GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cur);
- cur.bVisible=0;
- SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cur);
- }
- void showBoard(void)
- {
- int x,y;
- COORD cur=getCursor();
- for(y=0; y<MAZE_BOARD_HEIGHT; y++)
- {
- for(x=0; x<MAZE_BOARD_WIDTH; x++)
- {
- setCursor(cur.X+(x*2), cur.Y+y);
- switch(currunt_stage)
- {
- case 1:
- if(maze[y][x]=='1')
- printf("■");
- if(maze[y][x]=='x')
- printf("→");
- break;
- case 2:
- if(maze2[y][x]=='1')
- printf("■");
- if(maze2[y][x]=='x')
- printf("→");
- break;
- case 3:
- if(maze3[y][x]=='1')
- printf("■");
- if(maze3[y][x]=='x')
- printf("↓");
- break;
- default:
- break;
- }
- }
- }
- setCursor(cur.X,cur.Y);
- }
- void showCharacter(void)
- {
- COORD cur=getCursor();
- printf("◎");
- setCursor(cur.X, cur.Y);
- }
- int detect(int x,int y)
- {
- int x1=0;
- int y1=0;
- // 커서 위치 얻기
- COORD cur=getCursor();
- // 미로내에서의 위치 계산.
- x1=cur.X+x;
- y1=cur.Y+y;
- x1=x1/2-2;
- y1=y1-2;
- // 미로 밖에 있느냐?
- if(!((x1 >=0 && x1 <MAZE_BOARD_WIDTH) && (y1 >=0 && y1 <MAZE_BOARD_HEIGHT)))
- {
- return 1;
- }
- switch(currunt_stage)
- {
- case 1:
- //배열을 넘어가지 않는이유?
- if(maze[y1][x1]=='1') //++
- return 1;
- //미션성공
- else if(maze[y1][x1]=='x')
- return EXIT;
- else
- return 0;
- break;
- case 2:
- if(maze2[y1][x1]=='1') //++
- return 1;
- //미션성공
- else if(maze2[y1][x1]=='x')
- return EXIT;
- else
- return 0;
- break;
- case 3:
- if(maze3[y1][x1]=='1') //++
- return 1;
- //미션성공
- else if(maze3[y1][x1]=='x')
- return EXIT;
- else
- return 0;
- break;
- default:
- break;
- }
- /*
- //배열을 넘어가지 않는이유?
- if(maze2[y1][x1]=='1')
- return 1;
- //미션성공
- else if(maze2[y1][x1]=='x')
- return EXIT;
- else
- return 0;
- */
- }
- int RemoveCharacter_Set(int x, int y)
- {
- int value=detect(x,y);
- if(value==0)
- {
- COORD cur=getCursor();
- printf(" ");
- setCursor(cur.X+x, cur.Y+y);
- }
- else if(value==EXIT)
- {
- setCursor(10,15);
- printf("ENTER를 두번 누르세요!\n\n\n\n");
- system("pause");
- currunt_stage += 1;
- return 1;
- //exit(1);
- }
- return 0;
- }
- void character_static(void)
- {
- int kb;
- int ret = 0;
- setCursor(4,3); //케릭터 시작위치
- while(1)
- {
- while(!_kbhit())
- {
- showCharacter();
- Sleep(DELAY);
- }
- kb=_getch();
- switch(kb)
- {
- case UP:
- ret = RemoveCharacter_Set(0,-1);
- break;
- case DOWN:
- ret = RemoveCharacter_Set(0,1);
- break;
- case RIGHT:
- ret = RemoveCharacter_Set(2,0);
- break;
- case LEFT:
- ret = RemoveCharacter_Set(-2,0);
- break;
- }
- if(ret) break;
- }
- }
- int main()
- {
- while(1)
- {
- system("cls");
- printf("Test\n");
- removeCursor(); //커서 깜박이 지우기
- setCursor(POINT_X, POINT_Y); //보드 시작좌표
- showBoard(); //미로판 보여주기
- character_static(); //케릭터 움직이기
- getchar();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement