[C# project actual combat] Console game - Dragon Quest (1)

insert image description here

Junxi_'s personal homepage

No matter how far you go, don't forget the original intention when you set off

C/C++ game development

Hello, Mina-san, this is Jun Xi_, who has recently started to officially enter the right track of learning game development, and wants to share the knowledge and experience he has learned by writing a blog. This is the purpose of creating this column. I hope these small independent C# projects can be helpful to you who are making games. After all, the best way to learn game development is to make a game yourself! !

foreword

  • This column is mainly to share some pure C# project development process. If there is a game developed with Unity later, there will be a special column. At the same time, because any independent project is very complicated, even a simple small project needs to be completed at least It needs 4~500 lines of code, so most of the independent projects in this column will be divided into different sections to explain in batches, so that firstly, the sections can be split to reduce the difficulty of project development, and secondly, it is also easy for everyone to understand and follow the blog to get started design.
  • For projects that are being updated, the relevant links will be placed at the end of the blog after the updated content, and I will also update it as soon as possible when opening a new project for your convenience! !
  • At the same time, at the beginning of each project, the flow chart of this project will be placed at the beginning, which is convenient for everyone to search. Of course, I hope that after studying for a period of time, you can read this The project has been realized, if you can do this, then congratulations, you are really getting started.

1. Flowchart of the Warrior Rescuing the Princess

insert image description here

2. Project realization

1. Game flow

  • The general flow of the game is that the player comes to the princess to save the princess by defeating the dragon. If the challenge is successful and the princess is rescued, the game wins, and if unfortunately defeated by the dragon, the game ends.
  • start interface
    insert image description here
  • game interface
    insert image description here
  • rescue the princess
    insert image description here
  • game over screen
    insert image description here
  • Although the sparrow is small, it has all internal organs. The production of the game is to start from this simple project step by step. Let's start our study without talking nonsense! !

2. Project Analysis

  • The project is mainly divided into several parts, scene setting, setting and switching of the start interface, map design of the game interface and character initialization, player movement logic and game battle logic, logic of rescuing the princess, entry after victory or failure The setting of the interface is completed.
  • Today, we mainly talk about some initial settings and the layout of the start interface.

3. Initial settings

  • First of all, we need to complete some basic settings.
  • Since we are a small game with a console window, we need to set the size of the console window and the buffer first
  int w = 60;
  int h = 40;
  Console.SetWindowSize(w, h);//控制台窗口大小
  Console.SetBufferSize(w, h);//缓冲区大小
  • In fact, the first parameter passed in by these two functions is the set width, and the second passed in parameter is the set height
    insert image description here
  • At this time, there is still a blinking cursor on your computer, we need to hide the cursor
//隐藏光标
 Console.CursorVisible = false;//false为隐去,true为显示
  • Ok, after doing the above basic settings, let's set up the start interface

4. Start interface

  • Before we start, let's think about it, our game has three interfaces, how do we switch between different interfaces?
  • Here, we can consider using switch to group, use different conditions to distinguish different interfaces, and then realize different functions of different interfaces in different cases. In this way, when switching between different interfaces, we only need to change the conditions to let the program enter different cases.
int nowSceneID = 1;
//不同的场景进行不同的逻辑处理
switch (nowSceneID)
{
    
    
	//开始界面
    case 1:
        break;
     //游戏界面
    case 2:
        break;
     //结束界面
    case 3:
        break;
    default:
        break;
}
  • In this way, we can switch between different scenes by changing the value of nowScene in the code
  • At the same time, we should pay attention that the game cannot be exited actively, but only through the player's input, so we might as well put the above logic into an infinite loop, so that we can control the exit of the game through the control loop.

insert image description here

  • Let's realize the function of our start interface in this case 1
  • Our start interface, we must first have our game name
case 1:
  Console.Clear();//清屏
  Console.SetCursorPosition(w/2-5,10);//设置输入坐标
  Console.WriteLine("勇士斗恶龙");
  • First introduce the SetCursorPosition function. The coordinate axis (0, 0) of our console coordinates is in the upper left corner, so the input position for increasing the size of the incoming parameter is right and down. Regarding the setting of the coordinates, I believe that if you are smart, you can try it yourself and understand it. principle
    insert image description here

  • Note: If you have been flashing the screen here because you have been clearing the screen in a loop, it is a normal phenomenon, don’t panic, it will be resolved later, let’s continue

  • Well, the next step is the most complicated part of the start interface. How can we press the W key and the S key to light up "Start Game" or "End Game" to remind the player of the current options?
    insert image description here

  • Let’s analyze it here. In fact, the logic is very simple. First of all, when we first enter, we let the program enter the scene where the start game is on and the exit game is not on. When the player presses "S", it switches to "exit game". " is on, and then press "W" to switch back to the situation where "Start Game" is on.

  • Hey, isn't it a little bit similar to the scene switching logic just now? That's right, this can also be achieved by combining while with switch! !

 //当前选项的编号
 int nowSelIndex = 0;
 //因为要输入 我们可以构造一个开始界面的死循环
 //专门用来处理 开始场景相关的逻辑
 while (true)
 {
    
    
     
     //显示内容检测输入
     //设置光标位置,再显示内容
     Console.SetCursorPosition(w / 2 - 4, 12);
     //根据当前选择的编号来决定是否变色
     Console.ForegroundColor = nowSelIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;//三目操作符结合设置字体颜色的函数
     Console.Write("开始游戏");
     Console.SetCursorPosition(w / 2 - 4, 14);
     Console.ForegroundColor = nowSelIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
     Console.Write("退出游戏");
     //检查玩家输入的键并且不会在控制台上显示输入内容
     char input = Console.ReadKey(true).KeyChar;
     switch (input)
     {
    
    
         case 'W':
         case 'w':
             nowSelIndex = 0;

             break;
         case 'S':
         case 's':
             nowSelIndex = 1;

             break;
      
     }

    

 }
  • It is very simple logic, mark by the value of nowSelIndex, the initial value is 0, if you press the "S" key, let nowSelIndex = 1, at this time, start the game as white, exit the game as red, want to switch to start the game as red and exit If the game is white, just press the "W" key.
  • Here's what you need to pay attention to:
  //检查玩家输入的键并且不会在控制台上显示输入内容
     char input = Console.ReadKey(true).KeyChar;
  • When the value in the brackets of ReadKey is true, the input value will not be displayed on the console, and at the same time, the screen will be cleared every time we enter the next loop, and the running of the game will not be affected. This has achieved our goal, let's move on to the next step.
  • At the same time, after we realize the mark reminder of "Start Game" and "Exit Game", it's time to make a choice. Here we press "J" to proceed to the next step. When "Start Game" is highlighted, press J to switch to Game scene, highlight when exiting the game, press J to exit the program.
 case 'j':
 case 'J':
     if(nowSelIndex == 0)
     {
    
    
         //1.改变场景ID,进入游戏场景
         nowSceneID = 2;
         //2.要退出内层循环while
         isQuitWhile = true;
     }
     else
     {
    
    
         //关闭控制台
         Environment.Exit(0);
     }
     break;
   if (isQuitWhile == true)
    break;
  • Here we define a bool type isQuitWhile to achieve the purpose of switching scenes. Since the breaks here are all in the switch, we can only exit the switch, so we need to use this bool type variable to exit the inner loop and enter the outer layer. The purpose of re-selection in the switch, and since we changed the value of nowSceneID at this time, we can naturally switch to the game scene.

5. The module source code

  • Since the above description is split step by step, the source code of this part is provided below to help you understand the content of this part
namespace 勇士斗恶龙
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            int w = 60;
            int h = 40;
            Console.SetWindowSize(w, h);
            Console.SetBufferSize(w, h);
            //隐藏光标
            Console.CursorVisible = false;
            int nowSceneID = 1;
            while (true)
            {
    
    
                //不同的场景进行不同的逻辑处理

                switch (nowSceneID)
                {
    
    
                    case 1:
                        Console.Clear();
                       
                        Console.SetCursorPosition(w / 2 - 5, 10);
                        Console.WriteLine("勇士斗恶龙");
                        //当前选项的编号
                        int nowSelIndex = 0;
                        //因为要输入 我们可以构造一个开始界面的死循环
                        //专门用来处理 开始场景相关的逻辑
                        while (true)
                        {
    
    
                            //用一个标识用来退出此循环
                            bool isQuitWhile = false;
                            //显示内容检测输入
                            //设置光标位置,再显示内容
                            Console.SetCursorPosition(w / 2 - 4, 12);
                            //根据当前选择的编号来决定是否变色
                            Console.ForegroundColor = nowSelIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.Write("开始游戏");
                            Console.SetCursorPosition(w / 2 - 4, 14);
                            Console.ForegroundColor = nowSelIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.Write("退出游戏");
                            //检查玩家输入的键并且不会在控制台上显示输入内容
                            char input = Console.ReadKey(true).KeyChar;
                            switch (input)
                            {
    
    
                                case 'W':
                                case 'w':
                                    nowSelIndex = 0;

                                    break;
                                case 'S':
                                case 's':
                                    nowSelIndex = 1;

                                    break;
                                case 'j':
                                case 'J':
                                    if (nowSelIndex == 0)
                                    {
    
    
                                        //1.改变场景ID
                                        nowSceneID = 2;
                                        //2.要退出内层循环while
                                        isQuitWhile = true;
                                    }
                                    else
                                    {
    
    
                                        //关闭控制台
                                        Environment.Exit(0);
                                    }
                                    break;
                            }

                            if (isQuitWhile == true)
                                break;

                        }
                        
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    default:
                        break;
                }
            }
        }
    }
}


Summarize

  • This is the end of today's content. Generally speaking, the content of this part is relatively simple. You might as well follow the blogger and try it yourself. After all, many problems can only be discovered by yourself. If you have any questions, please point them out in the comment area or private message me! !
  • The following content will be updated soon, if you are interested, you may wish to pay attention to the following content if you miss it! ! (A small preview, then we will start the content of our game interface, this part is the real highlight)

It is not easy for new bloggers to create. If you feel that the content of the article is helpful to you, you may wish to read it three times before leaving. Your support is my motivation to update! ! !

**(Ke Li asks you to support the blogger three times in a row!!! Click the comment below to like and collect to help Ke Li)**

insert image description here

Guess you like

Origin blog.csdn.net/syf666250/article/details/132654969
Recommended