[C# Actual Combat] Console Game Dragon Quest (3) - Rescue the Princess and End Interface

Insert image description here

Junxi_'s personal homepage

No matter how far you go, never forget your original intention when you started the journey

C/C++ game development

Hello, Mina-san, this is Junxi_. Recently, I have officially started to learn game development. I want to share the knowledge and experience I have learned by writing a blog. This is the purpose of this column. I hope these independent C# small 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! !

Preface

  • Today we will continue to talk about the third and final part of Dragon Quest, the logic of defeating the dragon to rescue the princess and the related logic of the ending interface.
  • Let's put the overall flow chart of our game here first
    Insert image description here

After defeating the dragon

  • We will talk about the players who failed to pass the Dragon Trial and entered and exit the game interface and who successfully rescued the princess and entered and exit the game together. First, let’s talk about how to rescue the princess.

rescue princess

  • If the warrior is brave enough and lucky enough to defeat the dragon, we can go and rescue the princess. First of all, we have a condition here. When the BOSS's HP has not been reduced to 0, the princess cannot appear on the game screen. Yes, only when the HP of the BOSS is reduced to 0, can we mark the position of the princess on the game screen. At the same time, since we have defeated the dragon, we should no longer let the icon representing the BOSS continue to appear on the screen. These, we discussed in the last chapter It has been used in the game logic, but since the focus of the last chapter was on player movement and combat logic, we did not go into details. Let’s review it through this part of the code.
 
      //hp>0时,boss活着才绘制
       if (bossHp > 0)
  {
    
    
      Console.SetCursorPosition(bossX, bossY);
      Console.ForegroundColor = bossColor;
      Console.Write(bossIcon);
    }
  #region 6 公主显示
  //公主显示
   else
  {
    
    
     Console.SetCursorPosition(princessX,princessY);
	Console.ForegroundColor = princessColor;
 	Console.Write(princessIcon);
 }
    #endregion
//擦除BOSS
 else if(bossHp<=0)
 {
    
    
     //去营救公主
     //boss擦除
     Console.SetCursorPosition(bossX, bossY);
     Console.Write("  ");
     isFight = false;
     continue;
     
 }
  • For the initialization of the princess, you only need to set the position of the princess, the icon of the princess and the color of the icon, so I won’t repeat them here. In addition, this is for the purpose of explaining the split. It is not actually a coherent piece of code. Finally, I will release the complete source code, so that everyone can understand the content of this part.

How to rescue the princess

  • We are a small console game. At the same time, I am just telling you a big game framework here, so everything is kept simple. Here we come to the princess and press the J key to rescue the princess (of course, the rescue of the princess here can also be designed It's more complicated. For example, the princess's position can be a random number. Every time you rescue her, she will move to another place. Only three rescues are considered successful, etc. In short, these places can be customized according to your own ideas. There is no specific requirements)
case 'j':
case 'J':
 //判断是否在公主身边
 else if (((playerX == princessX && playerY == princessY - 1) || (playerX == princessX && playerY == princessY + 1) ||
     (playerX == princessX - 2 && princessY == playerY) || (playerX == princessX + 2 && playerY == princessY)) && bossHp <= 0)
 {
    
     
     nowSceneID = 3;//修改场景选择ID进入退出界面
 	nowEndIndex = "恭喜你救出公主";//这个提示语在之后结束界面会用到
 
 	//跳出while循环,回到主循环
 	isOver = true;
     
 }
 break;
 }
 if (isOver)
{
    
    
    //此时的break与while配对
    break;
}
  • Since this is in a while loop, we need to re-enter the switch statement of scene selection, so an isOver is defined to assist us in exiting the loop and re-selecting the scene.

game end interface

  • There are two situations when entering the game end interface. The first situation is that the princess has been rescued. As mentioned above, the second situation is that our warriors failed to defeat the dragon. At this time, we should also enter the game end interface.
//在这判断玩家或者怪物是否死亡,如果死了,继续后面的流程
if(playerHp<=0)
{
    
    
    //游戏结束
    //游戏结束画面
    isOver = true;
    nowSceneID = 3;
	nowEndIndex = "很遗憾你被恶龙击败了";
	endSay = 1;
    break;
}
  • To explain, the nowEndIndex and endSay here also have a nowEndIndex after the successful rescue of the princess above. The definition at that time was "Congratulations on rescuing the princess." When we enter the end interface, we need to give the player a prompt whether it failed or failed. Successfully rescued the princess. This is a different sentence printed on the end interface to remind the player of the game result. At the same time, the number of words in these two paragraphs of prompts is different. In order to ensure that these two paragraphs can be displayed in the center, we defined an endSay. Use its value to determine where our text needs to be printed.
  • For others, the end interface and the start interface are very similar, let's take a look at the relevant code directly
    case 3:
        Console.Clear();
        int nowEndSelIndex = 0;
        
        while (true)
        {
    
    
            bool EndQuitWhile = false;
            Console.SetCursorPosition(w / 2 - 4, 5);

            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("GameOver");
            if(endSay==1)
            Console.SetCursorPosition(w / 2 - 8,7);
            else
            Console.SetCursorPosition(w / 2 - 6,7);
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.Write(nowEndIndex);
            Console.ForegroundColor=nowEndSelIndex==0?ConsoleColor.Red:ConsoleColor.White;
            Console.SetCursorPosition(w / 2 - 4, 10);
            Console.Write("继续游戏");
            Console.ForegroundColor = nowEndSelIndex == 0 ? ConsoleColor.White : ConsoleColor.Red;
            Console.SetCursorPosition(w / 2 - 4, 12);
            Console.Write("退出游戏");
            char endInput=Console.ReadKey(true).KeyChar;
            switch (endInput)
            {
    
    
                case 'w':
                case 'W':
                    Console.Clear();
                    nowEndSelIndex = 0;
                    break;
                case 's':
                case 'S':
                    Console.Clear();
                    nowEndSelIndex = 1;
                    break;
                case 'j':
                case 'J':
                    if (nowEndSelIndex == 1)
                        Environment.Exit(0);
                    else
                    {
    
    
                        nowSceneID = 1;
                        EndQuitWhile = true;
                    }
                    break;

            }
            if(EndQuitWhile)
            {
    
    
                break;
            }

        }
        break;
}
  • When the player presses the "J" key when exiting the game and highlighting it, he will exit the game directly. When the player wants to continue the game, we have to switch the game scene to the start game interface, so the EndQuitWhile here is also used to assist us in restarting the game. Entering the game scene switching cycle, as for the others, it is exactly the same as the start interface, so I won’t go into details.

The source code of the overall game

  • Okay, that’s pretty much it for this small console game. Here we provide you with the source code and screenshots of the game for your convenience to check and try it out.
using System;
using System.Diagnostics;
using System.Runtime.Intrinsics.X86;

namespace 王子救公主
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            //隐藏光标
            Console.CursorVisible = false;
            //设置舞台的大小
            int w = 60;
            int h = 40;
            Console.SetWindowSize(w,h);
            Console.SetBufferSize(w,h);
            //当前场景的编号
            int nowSceneID = 1;
            string nowEndIndex ="";
            int endSay = 0;
            while (true)
            {
    
    
                //不同的场景进行不同的逻辑处理
                switch (nowSceneID)
                {
    
    
                    case 1:
                        Console.Clear();
                        #region  1 开始界面
                        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;

                        }
                    #endregion
                        break;
                    case 2:
                        Console.Clear();
                        #region 2 红墙
                        Console.ForegroundColor = ConsoleColor.Red;
                        //画墙
                        //设为红色
                        int i = 0;
                        //横墙
                        for(i = 0; i < w;i+=2)
                        {
    
    
                            Console.SetCursorPosition(i, 0);
                            Console.Write('■');
                            Console.SetCursorPosition(i, h-1);
                            Console.Write('■');
                            Console.SetCursorPosition(i, h-9);
                            Console.Write('■');
                        }
                        //竖墙
                        int j = 0;
                        for (j = 0; j <h; j++)
                        {
    
    
                            Console.SetCursorPosition(0, j);
                            Console.Write('■');
                            Console.SetCursorPosition(w-2,j);
                            Console.Write('■');
                          
                        }
                        #endregion
                        #region
                        #region 3 Boss属性相关
                        int bossX = 24;
                        int bossY = 15;
                        int bossAtkMin = 7;
                        int bossAtkMax = 13;
                        int bossHp = 100;
                        string bossIcon = "●";
                        //申明一个颜色变量
                        ConsoleColor bossColor = ConsoleColor.Red;
                        #endregion
                        
                        #region 4 玩家属性相关
                        int playerX = 4;
                        int playerY = 5;
                        int playerAtkMin = 7;
                        int playerAtkMax = 13;
                        int playerHp = 100;
                        string playerIcon = "●";
                        ConsoleColor playerColor = ConsoleColor.Green;
                        //公主相关
                        int princessX = 24;
                        int princessY = 5;
                        string princessIcon = "▲";
                        ConsoleColor princessColor = ConsoleColor.Blue;
                        char playerInput;

                        #endregion
                        #region 5 玩家战斗相关
                        bool isFight = false;
                        //游戏结束
                        bool isOver=false;
                        #endregion
                        //游戏场景的死循环 专门用来 检测玩家的输入相关的循环
                        while (true)
                        {
    
    
                           
                            //画出玩家
                            Console.SetCursorPosition(playerX, playerY);
                            Console.ForegroundColor= playerColor;
                            Console.Write(playerIcon);
                            //不停的输入wasd键 都可以控制它移动                          
                            
                                //hp>0时,boss活着才绘制
                                if (bossHp > 0)
                                {
    
    
                                    Console.SetCursorPosition(bossX, bossY);
                                    Console.ForegroundColor = bossColor;
                                    Console.Write(bossIcon);
                                }
                                #region 6 公主显示
                                //公主显示
                                else
                                {
    
    
                                    Console.SetCursorPosition(princessX, princessY);
                                    Console.ForegroundColor = princessColor;
                                    Console.Write(princessIcon);

                                }
                                #endregion
                                Console.SetCursorPosition(playerX, playerY);
                                Console.ForegroundColor= playerColor;
                                Console.Write(playerIcon);
                                //玩家输入
                                playerInput = Console.ReadKey(true).KeyChar;
                                if(isFight)
                                {
    
    
                                    //如果是战斗状态
                                    //只会处理j键
                                    if(playerInput=='j'||playerInput=='J')
                                    {
    
    
                                        //在这判断玩家或者怪物是否死亡,如果死了,继续后面的流程
                                        if(playerHp<=0)
                                        {
    
    
                                            //游戏结束
                                            //游戏结束画面
                                            isOver = true;
                                            nowSceneID = 3;
                                        nowEndIndex = "很遗憾你被恶龙击败了";
                                        endSay = 1;
                                            break;
                                        }
                                        else if(bossHp<=0)
                                        {
    
    
                                            //去营救公主
                                            //boss擦除
                                            Console.SetCursorPosition(bossX, bossY);
                                            Console.Write("  ");
                                            isFight = false;
                                            continue;
                                            
                                        }

                                        //玩家打怪物
                                        Random r= new Random();
                                        int atk = r.Next(playerAtkMin,playerAtkMax);
                                        bossHp -= atk;
                                        Console.ForegroundColor=ConsoleColor.Green;
                                        Console.SetCursorPosition(2, h - 7);
                                        Console.Write("                                            ");
                                        if (bossHp <= 0)
                                        {
    
    
                                            
                                            //擦除
                                            Console.SetCursorPosition(2, h - 6);
                                            Console.Write("                                            ");
                                            Console.SetCursorPosition(2, h - 7);
                                            Console.Write("                                            ");
                                            Console.SetCursorPosition(2, h - 8);
                                            Console.Write("                                            ");
                                            Console.SetCursorPosition(2, h - 8);
                                            Console.Write("你发动了致命一击造成了{0},恭喜你击败了恶龙", atk);
                                           
                                            Console.ForegroundColor = ConsoleColor.Red;
                                            Console.SetCursorPosition(2, h - 7);
                                            Console.Write("快去营救公主!!!,按J键继续");
                                            
                                            
                                        }
                                        else
                                        {
    
    
                                            Console.SetCursorPosition(2, h - 7);
                                            Console.Write("勇士对恶龙造成了{0}点伤害,此时恶龙还有{1}血", atk, bossHp);
                                            //怪兽打玩家
                                            atk = r.Next(bossAtkMin, bossAtkMax);
                                            playerHp -= atk;
                                            Console.ForegroundColor = ConsoleColor.Red;
                                            Console.SetCursorPosition(2, h - 6);
                                            Console.Write("                                         ");
                                            
                                            //Boss打死玩家
                                            if(playerHp<=0)
                                            {
    
    
                                                
                                                Console.SetCursorPosition(2, h - 6);
                                                Console.Write("很遗憾,你未能战胜恶龙");
                                                
                                            }
                                            else
                                            {
    
    
                                                Console.SetCursorPosition(2, h - 6);
                                                Console.Write("恶龙对勇士造成了{0}点伤害,此时你还有{1}血", atk, playerHp);
                                            }
                                        }
                                       
                                    }

                                }
                                else
                                {
    
    
                                    #region 6 玩家移动相关
                                    //擦除

                                    Console.SetCursorPosition(playerX, playerY);
                                    Console.Write("  ");

                                    switch (playerInput)
                                    {
    
    
                                        //贯穿
                                        case 'a':
                                        case 'A':
                                            playerX -= 2;
                                            if (playerX < 2)
                                                playerX = 2;
                                            else if (playerX == bossX && playerY == bossY && bossHp > 0)
                                                playerX += 2;
                                            else if (playerX == princessX && playerY == princessY && bossHp <= 0)
                                                playerX += 2;
                                            break;
                                        case 'w':
                                        case 'W':
                                            playerY--;
                                            if (playerY < 1)
                                                playerY++;
                                            else if (playerX == bossX && playerY == bossY && bossHp > 0)
                                                playerY++;
                                            else if (playerX == princessX && princessY == playerY && bossHp <= 0)
                                                playerY++;

                                            break;
                                        case 's':
                                        case 'S':

                                            playerY++;
                                            if (playerY > h - 10)
                                                playerY--;
                                            else if (playerX == bossX && playerY == bossY && bossHp > 0)
                                                playerY--;
                                            else if (playerX == princessX && playerY == princessY && bossHp <= 0)
                                                playerY--;

                                            break;
                                        case 'd':
                                        case 'D':
                                            playerX += 2;
                                            if (playerX > w - 4)
                                                playerX -= 2;
                                            else if (playerX == bossX && playerY == bossY && bossHp > 0)
                                                playerX -= 2;
                                            else if (playerX == princessX && playerY == princessY && bossHp <= 0)
                                                playerX -= 2;
                                            break;
                                        case 'j':
                                        case 'J':
                                            //开始战斗
                                            if (((playerX == bossX && playerY == bossY - 1) || (playerX == bossX && playerY == bossY + 1) ||
                                                (playerX == bossX - 2 && playerY == bossY) || (playerX == bossX + 2 && playerY == bossY)) && bossHp > 0)
                                            {
    
    
                                                isFight = true;
                                                //可以开始战斗了
                                                Console.SetCursorPosition(2, h - 8);
                                                Console.ForegroundColor = ConsoleColor.White;
                                                Console.Write("你开始和恶龙战斗了,按J键继续");


                                            }
                                            //判断是否在公主身边
                                            else if (((playerX == princessX && playerY == princessY - 1) || (playerX == princessX && playerY == princessY + 1) ||
                                                (playerX == princessX - 2 && princessY == playerY) || (playerX == princessX + 2 && playerY == princessY)) && bossHp <= 0)
                                            {
    
     
                                                nowSceneID = 3;
                                            nowEndIndex = "恭喜你救出公主";
                                            
                                            //跳出while循环,回到主循环
                                            isOver = true;
                                                
                                            }
                                                break;
                                    }
                                }
                                if (isOver)
                                {
    
    
                                    //此时的break与while配对
                                    break;
                                }


                            }                       
                         
                        break;
                   
                    case 3:
                        Console.Clear();
                        int nowEndSelIndex = 0;
                        
                        while (true)
                        {
    
    
                            bool EndQuitWhile = false;
                            Console.SetCursorPosition(w / 2 - 4, 5);

                            Console.ForegroundColor = ConsoleColor.White;
                            Console.Write("GameOver");
                            if(endSay==1)
                            Console.SetCursorPosition(w / 2 - 8,7);
                            else
                            Console.SetCursorPosition(w / 2 - 6,7);
                            Console.ForegroundColor = ConsoleColor.DarkRed;
                            Console.Write(nowEndIndex);
                            Console.ForegroundColor=nowEndSelIndex==0?ConsoleColor.Red:ConsoleColor.White;
                            Console.SetCursorPosition(w / 2 - 4, 10);
                            Console.Write("继续游戏");
                            Console.ForegroundColor = nowEndSelIndex == 0 ? ConsoleColor.White : ConsoleColor.Red;
                            Console.SetCursorPosition(w / 2 - 4, 12);
                            Console.Write("退出游戏");
                            char endInput=Console.ReadKey(true).KeyChar;
                            switch (endInput)
                            {
    
    
                                case 'w':
                                case 'W':
                                    Console.Clear();
                                    nowEndSelIndex = 0;
                                    break;
                                case 's':
                                case 'S':
                                    Console.Clear();
                                    nowEndSelIndex = 1;
                                    break;
                                case 'j':
                                case 'J':
                                    if (nowEndSelIndex == 1)
                                        Environment.Exit(0);
                                    else
                                    {
    
    
                                        nowSceneID = 1;
                                        EndQuitWhile = true;
                                    }
                                    break;

                            }
                            if(EndQuitWhile)
                            {
    
    
                                break;
                            }

                        }
                        break;
                }
            }
            


            }
    }
    #endregion

}

#endregion

Insert image description here
Insert image description here

Insert image description here
Insert image description here
Insert image description here

Summarize

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/132761437