inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

묻고 답해요

174만명의 커뮤니티!! 함께 토론해봐요.

어소트락 c언어/c++ 강의 39강 중에 이해안되는 부분 질문드려요

미해결

코드에서 움직이라고 만들어주는 부분에서 코드에서 왜 움직이는지를 모르겠는데요.. 코드 완성본은 아니고 배우던부분까지 쓴건데요. MoveUp, MoveDown, MoveLeft, MoveRight 함수에서 움직이는 코드(명령어?)는 따로 없어보이는데 w,a,s,d 를 누르면 별이 왜 움직이는건가요? #include <iostream> #include <conio.h> //키를 입력하면 반응할 수 있게 해주는 해더파일(_getch) using namespace std; //플레이어(위치) 구조체 struct _tagPoint { int x; int y; }; // typedef: 타입을 재정의하는 기능이다. _tagPoint를 POINT로도 사용할 수 있게 해준다. typedef _tagPoint POINT; typedef _tagPoint* PPOINT; //참고! 하향식으로(객체지향) 인식하므로 구조체를 먼저 선언하고 함수를 선언해야 PPOINT를 사용할 수 있다. void SetMaze(char Maze[21][21], PPOINT pPlayerPos, PPOINT pStartPos, PPOINT pEndPos); void OutPut(char Maze[21][21], PPOINT pPlayerPos); void MoveUp(char Maze[21][21], PPOINT pPlayerPos); void MovePlayer(char Maze[21][21], PPOINT pPlayerPos, char cInput); void CreatBomb(char Maze[21][21], const PPOINT pPlayer, PPOINT pBombArr, int* pBombCount); void Fire(char Maze[21][21], PPOINT pPlayer, PPOINT pBombArr, int* pBombCount); int main() { //20 x 20 미로를 만들어준다.{21에는 0(null)이 들어가야 하므로 21로 설정} char strMaze[21][21] = {}; POINT tPlayerPos; POINT tStartPos; POINT tEndPos; int iBombCount = 0; int addBomb = 0; POINT tBombPos[5]; //미로를 설정한다. SetMaze(strMaze, &tPlayerPos, &tStartPos, &tEndPos); while (true) { system("cls"); //미로를 출력한다. OutPut(strMaze, &tPlayerPos); if (tPlayerPos.x == tEndPos.x && tPlayerPos.y == tEndPos.y) { cout << "도착했습니다." << endl; break; } cout << "t: 폭탄설치, u:폭탄 터트리기" << endl; cout << "w: 위, s: 아래, a: 왼쪽, d: 오른쪽, q: 종료: "; char cInput = _getch(); if (cInput == 'q' || cInput == 'Q') break; else if (cInput == 't' || cInput == 'T') CreatBomb(strMaze, &tPlayerPos, tBombPos, &iBombCount); else if (cInput == 'u' || cInput == 'U') { Fire(strMaze, &tPlayerPos, tBombPos, &iBombCount); } else MovePlayer(strMaze, &tPlayerPos, cInput); } } /* 0: 벽 1: 길 2: 시작점 3: 도착점 4: 폭탄 */ void SetMaze(char Maze[21][21], PPOINT pPlayerPos, PPOINT pStartPos, PPOINT pEndPos) { pStartPos->x = 0; pStartPos->y = 0; pEndPos->x = 19; pEndPos->y = 19; *pPlayerPos = *pStartPos; strcpy_s(Maze[0], "21100000000000000000"); strcpy_s(Maze[1], "00111111111100000000"); strcpy_s(Maze[2], "00100010000111111100"); strcpy_s(Maze[3], "01100010000000000100"); strcpy_s(Maze[4], "01000011110001111100"); strcpy_s(Maze[5], "01000000001111000000"); strcpy_s(Maze[6], "00000000001000000000"); strcpy_s(Maze[7], "00100000001111111000"); strcpy_s(Maze[8], "00100011100000001000"); strcpy_s(Maze[9], "01111110111111111000"); strcpy_s(Maze[10], "01000000000000000000"); strcpy_s(Maze[11], "01111110001111100000"); strcpy_s(Maze[12], "00000011111000111110"); strcpy_s(Maze[13], "01111110000000000010"); strcpy_s(Maze[14], "01000000000111111110"); strcpy_s(Maze[15], "01111110000100000000"); strcpy_s(Maze[16], "00000010001100000000"); strcpy_s(Maze[17], "00000010001000000000"); strcpy_s(Maze[18], "01111110001111000000"); strcpy_s(Maze[19], "11000000000001111113"); Maze = { }; } void OutPut(char Maze[21][21], PPOINT pPlayerPos) { for (int i = 0; i < 20; ++i) { for (int j = 0; j < 20; ++j) { if (Maze[i][j] == '4') { cout << "♨"; } else if (pPlayerPos->x == j && pPlayerPos->y == i)//플레이어를 띄운다. cout << "☆"; else if (Maze[i][j] == '0') cout << "■"; else if (Maze[i][j] == '1') cout << " "; //2바이트므로(■,★,◎) 두번띄운다. else if (Maze[i][j] == '2') cout << "★"; else if (Maze[i][j] == '3') cout << "◎"; } cout << endl; } } void MoveUp(char Maze[21][21], PPOINT pPlayerPos) { if (pPlayerPos->y - 1 >= 0) { //벽인지 체크한다. && 폭탄인지 체크한다. if (Maze[pPlayerPos->y - 1][pPlayerPos->x] != '0' && Maze[pPlayerPos->y - 1][pPlayerPos->x] != '4') { --pPlayerPos->y; } } } void MoveDown(char Maze[21][21], PPOINT pPlayerPos) { if (pPlayerPos->y + 1 < 20) { //벽인지 체크한다. && 폭탄인지 체크한다. if (Maze[pPlayerPos->y + 1][pPlayerPos->x] != '0' && Maze[pPlayerPos->y + 1][pPlayerPos->x] != '4') { ++pPlayerPos->y; } } } void MoveRight(char Maze[21][21], PPOINT pPlayerPos) { if (pPlayerPos->x + 1 < 20) { //벽인지 체크한다. && 폭탄인지 체크한다. if (Maze[pPlayerPos->y][pPlayerPos->x + 1] != '0' && Maze[pPlayerPos->y][pPlayerPos->x + 1] != '4') { ++pPlayerPos->x; } } } void MoveLeft(char Maze[21][21], PPOINT pPlayerPos) { if (pPlayerPos->x - 1 >= 0) { //벽인지 체크한다. && 폭탄인지 체크한다. if (Maze[pPlayerPos->y][pPlayerPos->x - 1] != '0' && Maze[pPlayerPos->y][pPlayerPos->x - 1] != '4') { --pPlayerPos->x; } } } void MovePlayer(char Maze[21][21], PPOINT pPlayerPos, char cInput) { switch (cInput) { case 'w': case 'W': MoveUp(Maze, pPlayerPos); break; case 's': case 'S': MoveDown(Maze, pPlayerPos); break; case 'a': case 'A': MoveLeft(Maze, pPlayerPos); break; case 'd': case 'D': MoveRight(Maze, pPlayerPos); break; } } //포인터 변수를 const로 생성하면 가리키는 대상의 값을 변경할 수 없다. void CreatBomb(char Maze[21][21], const PPOINT pPlayer, PPOINT pBombArr, int* pBombCount) { if (*pBombCount == 5) return; for (int i = 0; i < *pBombCount; ++i) { if (pPlayer->x == pBombArr[i].x && pPlayer->y == pBombArr[i].y) return; } pBombArr[*pBombCount] = *pPlayer; ++(*pBombCount); Maze[pPlayer->y][pPlayer->x] = '4'; } void Fire(char Maze[21][21], PPOINT pPlayer, PPOINT pBombArr, int* pBombCount) { for (int i = 0; i < *pBombCount; ++i) { Maze[pBombArr[i].y][pBombArr[i].x] = '1'; if (pBombArr[i].y - 1 >= 0) { if (Maze[pBombArr[i].y - 1][pBombArr[i].x] == '0') Maze[pBombArr[i].y - 1][pBombArr[i].x] = '1'; //플레이어가 폭탄에 맞았을때 시작점으로 보낸다. if (pPlayer->x == pBombArr[i].x && pPlayer->y == pBombArr[i].y - 1) { pPlayer->x = 0; pPlayer->y = 0; } } if (pBombArr[i].y + 1 < 20) { if (Maze[pBombArr[i].y + 1][pBombArr[i].x] == '0') Maze[pBombArr[i].y + 1][pBombArr[i].x] = '1'; //플레이어가 폭탄에 맞았을때 시작점으로 보낸다. if (pPlayer->x == pBombArr[i].x && pPlayer->y == pBombArr[i].y + 1) { pPlayer->x = 0; pPlayer->y = 0; } } if (pBombArr[i].x - 1 >= 0) { if (Maze[pBombArr[i].y][pBombArr[i].x - 1] == '0') Maze[pBombArr[i].y][pBombArr[i].x - 1] = '1'; //플레이어가 폭탄에 맞았을때 시작점으로 보낸다. if (pPlayer->x == pBombArr[i].x - 1 && pPlayer->y == pBombArr[i].y) { pPlayer->x = 0; pPlayer->y = 0; } } if (pBombArr[i].x + 1 < 20) { if (Maze[pBombArr[i].y][pBombArr[i].x + 1] == '0') Maze[pBombArr[i].y][pBombArr[i].x + 1] = '1'; //플레이어가 폭탄에 맞았을때 시작점으로 보낸다. if (pPlayer->x == pBombArr[i].x + 1 && pPlayer->y == pBombArr[i].y) { pPlayer->x = 0; pPlayer->y = 0; } } } *pBombCount = 0; }

  • 어소트락
김태왕 댓글 0 좋아요 0 조회수 266

인기 태그

인프런 TOP Writers

주간 인기글