inflearn logo
강의

Course

Instructor

[MMORPG Game Development Series with C# and Unity] Part 2: Data Structures and Algorithms

Creating a map

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

175

davil3558

1 asked

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();
            }
        }
    }
}

C#

Answer 1

0

communityai8509

안녕하세요, 인프런 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();
}

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

게임개발에서 주로 어느부분에 알고리즘들이 쓰이는지 궁금합니다

0

171

2

글꼴 바꿔도 자간이 좁아 찌그러져보이시는 분들

0

88

1

NullReferenceException 예외) 같은 실수하시는분 계실까봐 남겨요

0

66

1

parent를 Pos타입으로 만든 이유

0

74

1

콘솔창에 격자가 안나옴 미로 생성 X

0

133

1

격자 생성 안됨 무한루프

0

114

1

BFS 질문

0

143

2

격자 무한 출력

0

167

2

A* 의 PriorityQueue 관련 질문입니다

0

155

2

vscode에서 원그리기

0

180

1

환결설정 강의 원 그리기

0

122

1

15-17분

0

87

1

3:16초에 근데 이렇게 해가지고 부분에 "{}"를 만들어서 자식 node들을 생성하던데 왜 중괄호로 감싸게 만드는 건가요?

0

141

2

동적 배열 관련 질문입니다!

0

209

1

Big-o 표기법에서 시간 복잡도

0

167

1

7:40에서 언급하신 색상이 날아가는 문제 이해를 못하겠습니다

0

151

1

트리구현연습 강의 질문있어요

0

142

1

Ctrl F5 하면 나오는 창은 어디서 설정할까요??

0

271

1

void CalcPathFromParent(Pos[,] parent)에 대해서

0

202

2

NullReferenceException예외가 발생했을때 어떻게 해야하나요?

0

229

1

[해결] 환경설정 강의에서 원이 이상하게 그려지는 문제

3

311

2

오른손 법칙에서 플레이어 점이 안 움직입니다

0

245

2

맵 만들기 오류

0

179

1

맵 만들기 부분 오류

0

236

1