[C#] Project. Console_2

Lingtea_luv·2025년 3월 19일

Project

목록 보기
2/38
post-thumbnail

얼음 샛길 (Ice Path)

1. 기본 틀에 pseudo 코드 작성

static void Main(string[] args)
{
    bool gameOver = false;    // 각 스테이지에 따른 종료 조건 설정

    Start();
    // 초기 플레이어 위치
    // 목표 지점 위치
    // 맵 구현(다양하게)
    
    Step();         // 스테이지 사이 단계 페이즈, 스테이지 score 출력
    
    while(gameOver == false)
    {
        Render();  // 플레이어, 목표지점, 맵 출력
        Input();   // 입력값 방향키, WASD 할당
        Update();  // 입력값에 따른  플레이어 이동 로직 구현
    }

    End();         // 게임 종료 페이즈. 인삿말, Score 디자인
}

static void Start()
{
	// 플레이어, 목표 지점 초기 위치 데카르트 좌표계로 구현
    // 맵은 2차원 배열, 특수기호를 활용하여 설정 (데카르트 좌표와 반대 주의)
    // 카운트(score) 표기 위치도 설정
}

static void Render()
{
	// 맵 표시 함수
    // 플레이어 표시 함수
    // 목표 지점 표시 함수
    // score 표시 함수
}

static void Input()
{
	// 사용자 입력 ConsoleKey로 받기
}

static void Update()
{
	// 플레이어 이동 로직 구현 - 미끄러져서 벽에 닿을 때까지 직진
    // 게임 종료 조건 - 플레이어와 목표지점의 좌표가 같으면 종료
}

static void End()
{
	// 스테이지 클리어 멘트 출력, score초기화
}

static void End()
{
	// score 또는 마무리 멘트 출력
}

2. 초기 조건 설정

  • 데카르트 좌표 설정 : 플레이어 및 목표 지점 설정 / 배열과 순서가 반대인 것 유의할 것!

struct Position
{
    public int x;
    public int y;
}
  • 초기 조건 설정 : 플레이어, 목표 지점, score 출력 위치 설정

static void Start()
{
    Console.CursorVisible = false;    // 커서 거슬리니까 치워버려
    
    playerPos.x = 15;
    playerPos.y = 14;

    goalPos.x = 16;
    goalPos.y = 8;

    countPos.x = 20;
    countPos.y = 8;
}
  • 단계 별 맵 제작 : 아래 중괄호 내부를 특수기호와 공백으로 구현

map0 = new char[16,18]
{
}
map1 = new char[16,18]
{
}
map2 = new char[16,18]
{
}

3. 출력부 구현

static void Render()
{
    Console.SetCursorPosition(0, 0);     // 프레임 조절 기능(맵 덮어쓰기)

    PrintMap(map);
    PrintPlayer(playerPos);       
    PrintGoal(goalPos);
    PrintCount(countPos, count);
}
  • 맵 구현 : 맵을 덮어쓰는 형식이므로 맵부터 출력되도록 상단에 위치

static void PrintMap()
{
    for(int y = 0; y < map.GetLength(0); y++)
    {
        for(int x = 0; x< map.GetLength(1); x++)
        {
            Console.Write(map [y, x]);
        }
        Console.WriteLine();
    }
}
  • 오브젝트 구현 : 잘 보이기 위해 플레이어와 목표 지점은 색상 지정

static void PrintPlayer()
{
    Console.SetCursorPosition(playerPos.x, playerPos.y);
    Console.ForegroundColor = ConsoleColor.Green;
    Console.Write('P');
    Console.ResetColor();
}
static void PrintGoal()
{
    Console.SetCursorPosition(goalPos.x, goalPos.y);
    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.Write('G');
    Console.ResetColor();
}
static void PrintCount()
{
    Console.SetCursorPosition(countPos.x, countPos.y);  // score 표시 위치
    Console.WriteLine($"이동 횟수 : {count}");
}

4. 입력부 구현

static ConsoleKey Input()
{
    count++;    // 사용자 입력에 따른 score +
    return Console.ReadKey(true).Key;
}

5. 처리(로직) 구현

static void Update()
{
    Move(key,ref playerPos, map);
    gameOver = IsClear(playerPos, goalPos);
}
  • 플레이어 이동 로직 : 벽에 닿을 때까지 미끄러지도록 설정

static void Move()
{
    switch (key)
    {
        case ConsoleKey.W:
        case ConsoleKey.UpArrow:
            while (map[playerPos.y - 1,playerPos.x] != '▒')
            {
                playerPos.y--;
            }
            break;
        case ConsoleKey.A:
        case ConsoleKey.LeftArrow:
            while (map[playerPos.y, playerPos.x - 1] != '▒')
            {
                playerPos.x--;
            }
            break;
        case ConsoleKey.S:
        case ConsoleKey.DownArrow:
            while (map[playerPos.y + 1, playerPos.x] != '▒')
            {
                playerPos.y++;
            }
            break;
        case ConsoleKey.D:
        case ConsoleKey.RightArrow:
            while (map[playerPos.y, playerPos.x + 1] != '▒')
            {
                playerPos.x++;
            }
            break;
    }
}
  • 승리조건 : 플레이어와 목표 지점의 좌표가 동일한 경우 클리어하도록 설정

static bool IsClear()
{
    if(playerPos.x == goalPos.x && playerPos.y == goalPos.y)
    {
        return true;
    }
    else
    {
        return false;
    } 
}

6. 페이즈 구현 (로비, 클리어, 종료)

  • 로비 : 커서 위치 조작으로 콘솔 창 원하는 위치에 타이틀이 뜨도록 설정

static void Lobby()
{
    Console.SetCursorPosition(30, 10);
    Console.WriteLine("+-------------------------------------------+");
    Console.SetCursorPosition(30, 11);
    Console.WriteLine("|                 Ice Path                  |");
    Console.SetCursorPosition(30, 12);
    Console.WriteLine("+-------------------------------------------+\n\n");
    Console.SetCursorPosition(30, 16);
    Console.Write("          Press any key to start...");
    Console.ReadKey(true);
    Console.Clear();
}
  • 클리어 : 축하 멘트와 score가 나오도록 출력. 이후 score는 리셋

static void Step(ref int count)
{
    Console.Clear();
    Console.SetCursorPosition(30, 10);
    Console.WriteLine("+-------------------------------------------------+");
    Console.SetCursorPosition(30, 11);
    Console.WriteLine($"   해당 스테이지를 클리어하셨습니다!   Score: {count}   ");
    Console.SetCursorPosition(30, 12);
    Console.WriteLine("+-------------------------------------------------+\n\n");
    Console.SetCursorPosition(32, 16);
    Console.Write("          Press any key to continue...");
    Console.ReadKey(true);
    Console.Clear();
    count = 0;      // score 리셋
}
  • 종료 : 콘솔 창 지우고, 게임 클리어 멘트 출력

static void End(int count)
{
    Console.Clear();
    Console.SetCursorPosition(30, 10);
    Console.WriteLine("+-------------------------------------------+");
    Console.SetCursorPosition(30, 11);
    Console.WriteLine("|              Congratulation!              |");
    Console.SetCursorPosition(30, 12);
    Console.WriteLine("+-------------------------------------------+\n\n");
}

7. 소감

  • 구현하고 싶었던 것

    어렸을 때 너무 어렵게 느껴졌던 포켓몬스터의 얼음 샛길 구간을 콘솔로 제작해보고 싶었다. 실제 얼음 샛길 맵을 따왔으며 버전이 나오면서 얼음 샛길의 난이도가 너프되었기에 스테이지가 올라갈수록 구버전 맵인게 재밌는 부분.
    사실 워프와 몬스터볼을 획득하기 위한 추가 이동, 미끄러지지 않는 땅 발판 등 여러 요소를 추가하고 싶었다. 다만 시간 문제 상 디자인에 조금 더 신경썼다.

  • 버그

    얼음 샛길의 경우 이동 로직이 굉장히 단순하기 때문에 큰 버그는 나지 않았다. 다만 변수 사용에서 ref와 out의 사용 미숙으로 빨간줄이 몇 번 그어진 적이 있었다.
    이 경우 ref는 값이 선언되지 않아도 함수에 참조되어 초기화와 함께 사용이 가능하다는 것, out은 반드시 초기화 진행이 되어있어야한다는 것으로 이해해서 사용했더니 해결되었다.

  • 아쉬운 점

    일단 구현하고 싶었던 것을 못 한게 많이 아쉽긴하다. 특수 타일이나 오브젝트 등 구현하고 싶은 것은 많았으나 역시 첫 프로젝트라서 그런지 시간이 조금 부족했다. 그리고 로직이 생각보다 너무 단순해서 주말에 조금 더 난이도가 있는 것으로 콘솔(창) 게임 제작 프로젝트를 마무리 지을 예정이다.

    프로젝트 GitHub 링크

profile
뚠뚠뚠뚠

0개의 댓글