인프런 커뮤니티 질문&답변

방재혁님의 프로필 이미지

작성한 질문수

[C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part2: 자료구조와 알고리즘

맵 만들기

창은 뜨는데 맵이 나타나지 않아요.

24.08.10 21:38 작성

·

52

0

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm
{
    class Board
    {
        const char CIRCLE = '\u25cf';   // \u25cf = ●
        public TileType[,] _tile; // 배열
        public int _size;
        
        public enum TileType
        {
            Empty,
            Wall,
        }
        public void Initialize(int size)
        {
            _tile = new TileType[size, size];
            _size = size;

            for(int y = 0; y < _size; y++)
            {
                for(int x = 0; x < _size; x++)
                {
                    if (x == 0 || x == _size - 1 || y == 0 || y == _size - 1)
                        _tile[y, x] = TileType.Wall;
                    else
                        _tile[y, x] |= TileType.Empty;
                }
            }
        }

        public void Render()
        {
            ConsoleColor prevColor = Console.ForegroundColor;

            for (int y = 0; y < _size; y++)
            {
                for (int x = 0; x < _size; x++)
                {
                    Console.ForegroundColor = GetTileColor(_tile[y, x]);
                    Console.Write(CIRCLE);    
                }
                Console.WriteLine();
            }

            Console.ForegroundColor= prevColor;
        }

        ConsoleColor GetTileColor(TileType type)
        {
            switch(type)
            {
                case TileType.Empty:
                    return ConsoleColor.Green;
                case TileType.Wall:
                    return ConsoleColor.Red;
                default:
                    return ConsoleColor.Green;
            }
        }
    }
}
using System;

namespace Algorithm
{
    class Program
    {
        static void Main(string[] args)
        {
            Board board = new Board();
            board.Initialize(25);

            Console.CursorVisible = false;  // 커서가 안 보임

            const int WAIT_TICK = 1000 / 30;
            int lastTick = 0;
            // 마지막 시간
          
            while (true)
            {
                #region 프레임 관리
                // 시스템이 시작되고 난 후 시간으로 넣음
                int currentTick = System.Environment.TickCount;
                // 1000/30이라고 쓴 이유는 ms(밀리세컨드)떄문에
                // 1/30라고 안 쓰고 1000을 곱해줘서 작성한다.
                if (currentTick - lastTick > WAIT_TICK)
                    continue;
                lastTick = currentTick;
                #endregion

                // 렌더링
                Console.SetCursorPosition(0, 0);    // 커서 위치를 (0, 0)으로 설정
                board.Render();
            }
        }
    }
}

답변 1

0

인프런 AI 인턴님의 프로필 이미지

2024. 08. 12. 21:41

안녕하세요, 인프런 AI 인턴입니다.

창은 뜨지만 맵이 나타나지 않는 문제의 경우, 주요 원인은 프레임 관리 로직에서 if (currentTick - lastTick > WAIT_TICK) 조건문이 잘못되어 있어 렌더링이 이루어지지 않습니다. 정확히는 이 조건이 참이 될 때 continue를 통해 루프를 계속 진행하게 하여 실질적으로 board.Render();가 호출되지 않게 됩니다. 이로 인해 맵이 나타나지 않고 있는 상태입니다.

해결 방법은 if (currentTick - lastTick < WAIT_TICK)으로 조건문을 수정하여 주어야 합니다. 이렇게 하면 지정된 WAIT_TICK 시간만큼 지난 후에만 board.Render();가 호출되어 화면이 갱신됩니다.

수정 후의 Program.cs 파일의 해당 부분은 다음과 같이 보일 것입니다:

while (true)
{
    #region 프레임 관리
    int currentTick = System.Environment.TickCount;
    if (currentTick - lastTick < WAIT_TICK)
        continue;
    lastTick = currentTick;
    #endregion

    // 렌더링
    Console.SetCursorPosition(0, 0);
    board.Render();
}

이 변경을 통해 프로그램이 올바르게 프레임 관리를 하여 맵이 제대로 렌더링 되길 바랍니다.