C#小游戏初探

C#尝试做点小游戏练练手

在这里插入图片描述在这里插入图片描述在这里插入图片描述

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

namespace lesson59
{
    class Program
    {
        static void Main(string[] args)
        {
            int w = 50;
            int h = 30;
            string info="";
            #region 1 控制台基础设置
            Console.CursorVisible = false;
            Console.SetWindowSize(w, h);
            Console.SetBufferSize(w+2, h+2);


            #endregion

            #region 2 多场景
            int nowSceneID = 1;
            while (true)
            {
                switch (nowSceneID)
                {
                    #region 3 开始场景
                    case 1:
                        Console.Clear();
                        Console.SetCursorPosition(w / 2 - 4, 4);
                        Console.Write("营救公主");
                        int nowSelIndex = 0;
                        bool isExit = false;
                        while (true)
                        {

                            Console.ForegroundColor = nowSelIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.SetCursorPosition(w / 2 - 4, 7);
                            Console.Write("开始游戏");
                            Console.ForegroundColor = nowSelIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.SetCursorPosition(w / 2 - 4, 8);
                            Console.Write("退出游戏");

                            char input = Console.ReadKey(true).KeyChar;
                            switch (input)
                            {
                                case 'W':
                                case 'w':
                                    --nowSelIndex;
                                    if (nowSelIndex <0)
                                    {
                                        nowSelIndex = 0;
                                    }
                                    break;

                                case 'S':
                                case 's':
                                    ++nowSelIndex;
                                    if (nowSelIndex > 1)
                                    {
                                        nowSelIndex = 1;
                                    }
                                    break;
                                case 'J':
                                case 'j':
                                    if (nowSelIndex==0)
                                    {
                                        nowSceneID = 2;
                                        isExit = true;
                                    }else
                                    { Environment.Exit(0); }                                   
                                    break;
                            }
                            if (isExit)
                            {
                                break;
                            }
                        }
                        break;
                    #endregion

                    #region 4 游戏场景
                    case 2:
                        Console.Clear();
                        Console.ForegroundColor = ConsoleColor.Red;
                        for (int i = 0; i < w; i += 2)
                        {
                            Console.SetCursorPosition(i, 0);
                            Console.Write("■");
                            Console.SetCursorPosition(i, h - 1);
                            Console.Write("■");
                            Console.SetCursorPosition(i, h - 6);
                            Console.Write("■");
                        }

                        for (int i = 0; i < h; i++)
                        {
                            Console.SetCursorPosition(0, i);
                            Console.Write("■");
                            Console.SetCursorPosition(w - 2, i);
                            Console.Write("■");
                        }
                        int bossX = 24;
                        int bossY = 15;
                        int bossAtkMin = 7;
                        int bossAtkMax = 13;
                        int bossHp = 100;
                        string bossIcon = "■";
                        ConsoleColor bossColor = ConsoleColor.Green;

                        int playerX = 4;
                        int playerY = 5;
                        int playerAtkMin = 8;
                        int playerAtkMax = 12;
                        int playerHp = 100;
                        string playerIcon = "●";
                        ConsoleColor playerColor = ConsoleColor.Yellow;

                        int princessX = 24;
                        int princessY = 5;
                        string princessIcon = "★";
                        ConsoleColor princessColor = ConsoleColor.Blue;

                        char playerinput;
                        bool isFight = false;
                        bool isOver = false;
                        while (true)
                        {
                            
                            if (bossHp > 0)
                            {
                                Console.SetCursorPosition(bossX, bossY);
                                Console.ForegroundColor = bossColor;
                                Console.Write(bossIcon);
                            }else
                            {
                                Console.SetCursorPosition(princessX, princessY);
                                Console.ForegroundColor = princessColor;
                                Console.Write(princessIcon);

                            }
                            Console.SetCursorPosition(playerX, playerY);
                            Console.ForegroundColor = playerColor;
                            Console.Write(playerIcon);
                            playerinput = Console.ReadKey(true).KeyChar;
                            Console.SetCursorPosition(playerX, playerY);
                            Console.Write("  ");

                            if (isFight)
                            {
                                if (playerinput=='J' || playerinput == 'j')
                                {
                                    if (playerHp<=0)
                                    {
                                        nowSceneID = 3;
                                        break;
                                    }
                                    if (bossHp <= 0)
                                    {
                                        Console.SetCursorPosition(bossX,bossY);
                                        Console.Write("  ");
                                        isFight = false;
                                        
                                    }
                                    Random r = new Random();
                                    int atk = r.Next(playerAtkMin,playerAtkMax);
                                    bossHp -= atk;
                                    Console.ForegroundColor = ConsoleColor.Green;
                                    Console.SetCursorPosition(2, h - 4);
                                    Console.Write("                                      ");
                                    Console.SetCursorPosition(2, h - 4);
                                    Console.Write("攻击怪物伤害={0},BOSS剩余={1}", atk, bossHp);
                                    if (bossHp>0)
                                    {
                                         atk = r.Next(bossAtkMin, bossAtkMax);
                                        playerHp -= atk;
                                        Console.ForegroundColor = ConsoleColor.Yellow;
                                        Console.SetCursorPosition(2, h - 3);
                                        Console.Write("                                      ");
                                        if (playerHp<=0)
                                        { 
                                            Console.SetCursorPosition(2, h - 3);
                                            Console.Write("你挂了");
                                            info = "LOWSER";
                                        }
                                        else
                                        {
                                            Console.SetCursorPosition(2, h - 3);
                                            Console.Write("攻击player伤害={0},player剩余={1}", atk, playerHp);
                                        }
                                    }else
                                    {
                                        Console.SetCursorPosition(2, h - 5);
                                        Console.Write("                                      ");
                                        Console.SetCursorPosition(2, h - 4);
                                        Console.Write("                                      ");
                                        Console.SetCursorPosition(2, h - 3);
                                        Console.Write("                                      ");
                                        Console.SetCursorPosition(2, h - 5);
                                        Console.Write("恭喜你打死了monster");
                                        Console.SetCursorPosition(2, h - 4);
                                        Console.Write("前往公主身边按J键,救公主");
                                        
                                    }
                                }
                            }else
                            {
                                #region 玩家移动逻辑
                                
                                switch (playerinput)
                                {

                                    case 'W':
                                    case 'w':
                                        playerY--;
                                        if (playerY < 1)
                                        {
                                            playerY = 1;
                                        }
                                        else if (playerX == bossX && playerY == bossY && bossHp > 0) { playerY++; }
                                        else if (playerX == princessX && playerY == princessY && bossHp <= 0) { playerY++; }
                                        break;
                                        
                                    case 'S':
                                    case 's':
                                        playerY++;
                                        if (playerY > h - 7)
                                        {
                                            playerY = h - 7;
                                        }
                                        else if (playerX == bossX && playerY == bossY && bossHp > 0) { playerY--; }
                                        else if (playerX == princessX && playerY == princessY && bossHp <= 0) { playerY--; }
                                        break;
                                    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 'D':
                                    case 'd':
                                        playerX += 2;
                                        if (playerX > w - 4)
                                        {
                                            playerX = w - 4;
                                        }
                                        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 ||
                                            playerY == bossY && playerX == bossX - 2 ||
                                            playerY == bossY && playerX == bossX + 2) && bossHp > 0)

                                        {
                                            isFight = true;
                                            Console.SetCursorPosition(2, h - 5);
                                            Console.ForegroundColor = ConsoleColor.White;
                                            Console.Write("开始战斗了,按J继续!");
                                            Console.SetCursorPosition(2, h - 4);
                                            Console.Write("玩家血量={0}", playerHp);
                                            Console.SetCursorPosition(2, h - 3);
                                            Console.Write("monster血量={0}", bossHp);
                                        }
                                        if ((playerX == princessX && playerY == princessY - 1 ||
                                            playerX == princessX && playerY == princessY + 1 ||
                                            playerY == princessY && playerX == princessX - 2 ||
                                            playerY == princessY && playerX == princessX + 2) && bossHp <= 0)

                                        {
                                            Console.SetCursorPosition(2, h - 5);
                                            Console.Write("                                      ");
                                            Console.SetCursorPosition(2, h - 4);
                                            Console.Write("                                      ");
                                            Console.SetCursorPosition(2, h - 3);
                                            Console.Write("                                      ");
                                            Console.SetCursorPosition(2, h - 5);
                                            Console.ForegroundColor = ConsoleColor.White;
                                            Console.Write("营救公主成功!");
                                            nowSceneID = 3;
                                            isOver = true;
                                            info = "营救成功";
                                        }
                                        break;
                                }
                                #endregion
                            }

                            if (isOver)
                            {
                                break;
                            }
                        }
                        break;
                    #endregion

                    #region 5 结束场景
                    case 3:
                        Console.Clear();
                        Console.SetCursorPosition(w / 2 - 4, 4);
                        Console.Write(info);
                        nowSelIndex = 0;
                        isExit = false;
                        while (true)
                        {

                            Console.ForegroundColor = nowSelIndex == 0 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.SetCursorPosition(w / 2 - 4, 7);
                            Console.Write("继续游戏");
                            Console.ForegroundColor = nowSelIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
                            Console.SetCursorPosition(w / 2 - 4, 8);
                            Console.Write("退出游戏");

                            char input = Console.ReadKey(true).KeyChar;
                            switch (input)
                            {
                                case 'W':
                                case 'w':
                                    --nowSelIndex;
                                    if (nowSelIndex < 0)
                                    {
                                        nowSelIndex = 0;
                                    }
                                    break;

                                case 'S':
                                case 's':
                                    ++nowSelIndex;
                                    if (nowSelIndex > 1)
                                    {
                                        nowSelIndex = 1;
                                    }
                                    break;
                                case 'J':
                                case 'j':
                                    if (nowSelIndex == 0)
                                    {
                                        nowSceneID = 1;
                                        isExit = true;
                                    }
                                    else
                                    { Environment.Exit(0); }
                                    break;
                            }
                            if (isExit)
                            {
                                break;
                            }
                        }
                        break;
                    #endregion

                }
                
            }
            #endregion

        }
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sky6595418

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值