C# 절차지향 TextRPG 만들기 01

Mr.Seo·2025년 12월 17일

C# 기초문법

목록 보기
12/15

C# TextRPG 캐릭터 생성하기


enum ClassType // 플레이어의 직업을 열거한다.
{
    None = 0,
    Knight,
    Archer,
    Mage,
}

static ClassType ChooseClass() // 캐릭터 선택창
{
    Console.WriteLine("직업을 선택하세요!");
    Console.WriteLine("[1] 기사");
    Console.WriteLine("[2] 궁수");
    Console.WriteLine("[3] 법사");

    ClassType choice = ClassType.None;
    string input = Console.ReadLine();
    switch (input)
    {
        case "1":
            choice = ClassType.Knight;
            break;
        case "2":
            choice = ClassType.Archer;
            break;
        case "3":
            choice = ClassType.Mage;
            break;
    }
    return choice;
}



static void CreatePlayer(ClassType choice, out int hp, out int attack) // out 넘겨서 진퉁으로 작업을 하겠다
    // out 진퉁으로 작업을 하려면 안에서 무조건 초기화를 해줘야 오류가 뜨지 않는다.
    // 어떤 직업을 초기화 해야 할지 알아야 하기때문에 ClassType 도 같이 인자로 받아온다.
{
    // 기사 hp : 100 attack : 10
    // 궁수 hp : 75  attack : 12
    // 법사 hp : 50  attack : 15

    hp = 0;
    attack = 0;
    switch (choice)
    {
        case ClassType.Knight:
            hp = 100;
            attack = 10;
            break;
        case ClassType.Archer:
            hp = 75;
            attack = 12;
            break;
        case ClassType.Mage:
            hp = 50;
            attack = 15;
            break;
        default:
            hp = 0;
            attack = 0;
            break;
    }
}
static void Main(string[] args)
{
    while (true)
    {
        ClassType choice = ChooseClass(); // 리턴값을 받아야함!
        if (choice != ClassType.None)
        {
            int hp;
            int attack;
            CreatePlayer(choice, out hp, out attack);

            Console.WriteLine($"HP{hp} Attack{attack}");

            // 필드로 가서 몬스터랑 싸운다
        }

    }
    
}
  • 일단 여기까지 작성후에 실행을 해보면 플레이어의 직업들이 나오고 해당 직업을 선택하면 초기화한 hp,attack 값들이 출력 되게 된다.
  • 하지만 플레이어의 능력치가 계속 늘어난다고 했을때 ChooseClass 함수에서 out 매개변수를 계속 늘려주면 가독성이 떨어지기 때문에 Player 구조체를 만들어서 관리 하도록 수정한다.
profile
프로그래머 취준생

0개의 댓글