• 第十二天学习内容 综合应用 推箱子和对战游戏


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

    namespace day12
    {
        class Program
        {
            static void Main(string[] args)
            {

    //推箱子游戏
                int x = 1, y = 1, i = 0;
                int[, ,] dt = new int[2, 10, 10]   //定义一个二维数组用于接收地图
                    {
                       {
                        {1,1,1,1,1,1,1,1,1,1},
                        {1,4,0,0,1,0,0,0,0,1},
                        {1,0,2,0,1,0,0,0,0,1},
                        {1,0,0,1,1,0,0,0,0,1},
                        {1,0,0,1,3,0,0,0,0,1},
                        {1,0,0,1,0,0,0,0,0,1},
                        {1,0,0,1,1,1,1,0,0,1},
                        {1,0,0,0,0,0,0,0,0,1},
                        {1,0,0,0,0,0,0,0,0,1},
                        {1,1,1,1,1,1,1,1,1,1},
                       },
                       {
                        {1,1,1,1,1,1,1,1,1,1},
                        {1,0,0,4,1,0,0,0,0,1},
                        {1,1,2,0,1,0,0,0,0,1},
                        {1,1,0,1,1,0,0,0,0,1},
                        {1,1,0,1,3,0,0,0,0,1},
                        {1,1,0,1,0,0,0,0,0,1},
                        {1,0,0,1,1,1,1,0,0,1},
                        {1,0,0,0,0,0,0,0,0,1},
                        {1,1,0,0,0,0,0,0,0,1},
                        {1,1,1,1,1,1,1,1,1,1},
                       }
                      

                   
                    };
                try
                {

               

                while (true)
                {
                    int temp = 0;

                    ConsoleKeyInfo s = Console.ReadKey();  //s用于接收用户输入的键
                    if (s.Key.ToString() == "DownArrow")
                    {
                        if (dt[i, x + 1, y] == 0)//如果小人的下一步是0(空格),则两个元素交换  (下同)
                        {

                            temp = dt[i, x, y];
                            dt[i, x, y] = dt[i, x + 1, y];
                            dt[i, x + 1, y] = temp;
                            x++;
                        }
                        if (dt[i, x + 1, y] == 2 && dt[i, x + 2, y] == 0)//如果小人的下一步是箱子,箱子的下一步是空格,则三个元素进行交换 (下同)
                        {

                            temp = dt[i, x + 2, y];
                            dt[i, x + 2, y] = dt[i, x + 1, y];
                            dt[i, x + 1, y] = temp;
                            temp = dt[i, x + 1, y];
                            dt[i, x + 1, y] = dt[i, x, y];
                            dt[i, x, y] = temp;
                            x++;
                        }
                    }
                    else if (s.Key.ToString() == "UpArrow")
                    {
                        if (dt[i, x - 1, y] == 0)
                        {

                            temp = dt[i, x - 1, y];
                            dt[i, x - 1, y] = dt[i, x, y];
                            dt[i, x, y] = temp;
                            x--;
                        }
                        if (dt[i, x - 1, y] == 2 && dt[i, x - 2, y] == 0)
                        {

                            temp = dt[i, x - 2, y];
                            dt[i, x - 2, y] = dt[i, x - 1, y];
                            dt[i, x - 1, y] = temp;
                            temp = dt[i, x, y];
                            dt[i, x, y] = dt[i, x - 1, y];
                            dt[i, x - 1, y] = temp;
                            x--;
                        }
                    }
                    else if (s.Key.ToString() == "LeftArrow")
                    {
                        if (dt[i, x, y - 1] == 0)
                        {

                            temp = dt[i, x, y - 1];
                            dt[i, x, y - 1] = dt[i, x, y];
                            dt[i, x, y] = temp;
                            y--;
                        }
                        if (dt[i, x, y - 1] == 2 && dt[i, x, y - 2] == 0)
                        {

                            temp = dt[i, x, y - 2];
                            dt[i, x, y - 2] = dt[i, x, y - 1];
                            dt[i, x, y - 1] = temp;
                            temp = dt[i, x, y];
                            dt[i, x, y] = dt[i, x, y - 1];
                            dt[i, x, y - 1] = temp;
                            y--;
                            if (dt[i, x, y - 2] == 3) //如果小人的下面第二个元素是“★”
                            {

                                dt[i, x, y - 2] = 2; //让五角星变成箱子
                                dt[i, x, y - 1] = 4;// 让箱子变成小人
                                dt[i, x, y] = 0;    //让小人变成空格
                                temp = 6; //给临时变量赋一个值作为跳出循环的条件
                            }

                        }
                    }
                    else if (s.Key.ToString() == "RightArrow")
                    {
                        if (dt[i, x, y + 1] == 0)
                        {

                            temp = dt[i, x, y + 1];
                            dt[i, x, y + 1] = dt[i, x, y];
                            dt[i, x, y] = temp;
                            y++;
                        }
                        if (dt[i, x, y + 1] == 2 && dt[i, x, y + 2] == 0)
                        {

                            temp = dt[i, x, y + 2];
                            dt[i, x, y + 2] = dt[i, x, y + 1];
                            dt[i, x, y + 1] = temp;
                            temp = dt[i, x, y + 1];
                            dt[i, x, y + 1] = dt[i, x, y];
                            dt[i, x, y] = temp;
                            y++;
                        }
                    }


                    Console.Clear();  //清屏

                    ditu(dt, i);// 调用ditu函数打印地图
                    if (temp == 6)// 跳出循环
                    {
                    Console.WriteLine("恭喜您过关了!");
                    Console.WriteLine("按任意键进入下一关!");
                    Console.ReadKey();
                    x = 1; y = 3;//初始化小人的位置
                    i++;
                   
                    }

                   
                }


                }
                catch (Exception)
                {

                    Console.WriteLine("暂时只有两张图!");
                    Console.ReadKey();
                }

            }

            private static void ditu(int[,,] dt,int i)
            {
               
               
                    for (int j = 0; j < 10; j++)
                    {
                        for (int k = 0; k < 10; k++)
             {
       
       
                            if (dt[i, j,k] == 0)
                            {
                                Console.Write("  ");
                            }
                            else if (dt[i, j,k] == 1)
                            {
                                Console.Write("■");
                            }
                            else if (dt[i, j,k] == 2)
                            {
                                Console.Write("□");
                            }
                            else if (dt[i, j,k] == 3)
                            {
                                Console.Write("★");
                            }
                            else if (dt[i, j,k] == 4)
                            {
                                Console.Write("♀");
                            }
                      

                      }
                        Console.Write(" ");
                    }
                  
               
            }
        }
    }

    //  对战游戏

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

    namespace day12_zuoye
    {
        class Program

        {
            struct renwu   //新建一个renwu的结构体
            {
                public string name; //定义renwu的各种属性
                public int HP;
              
                public int defend;
                public string[] jineng;
            }
            static void Main(string[] args)
            {
                Console.ForegroundColor = ConsoleColor.Black;  //前景色改为黑色
                Console.BackgroundColor = ConsoleColor.White; //背景色改为白色
                Console.Clear(); //全屏
                renwu p1 = new renwu();
                Console.Write("请输入第一位选手的名字:");
                p1.name = Console.ReadLine();
                p1.jineng = new string[3] { "独孤九剑", "九阳神功", "大海无量" };
                renwu p2 = new renwu();
                Console.Write("请输入第二位选手的名字:");
                p2.name = Console.ReadLine();
                p2.jineng = new string[3] { "八卦掌","狮吼功","太极拳"};
                Random r = new Random();
                p1.HP = r.Next(1000) + 1000;//随机生成血量和防御值
                p1.defend = r.Next(100) ;
                p2.HP = r.Next(1000) + 1000;
                p1.defend = r.Next(100) ;
                DateTime dt = new DateTime();
                dt = DateTime.Now;  //取现在的时间
                Console.WriteLine(p1.name+" 生命力:"+p1.HP);
                Console.WriteLine(p2.name + " 生命力:" + p2.HP);
                Console.WriteLine("现在是北京时间:"+dt+" 下面为您直播"+p1.name+"与"+p2.name+"的比赛");
                Console.ReadKey();
                duizhan(ref p1, ref p2, r);  //调用对战函数
                Console.ReadLine();
            }

            private static void duizhan(ref renwu p1, ref renwu p2, Random r)
            {
                while (true)
                {// 判断哪一方获得胜利
                    if (p1.HP == 0 && p2.HP == 0)
                    {
                        Console.WriteLine(p1.name + "与" + p2.name + "打成了平局!");
                        break;
                    }
                    else if (p1.HP == 0)
                    {
                        Console.WriteLine(p2.name + "获得了胜利!");
                        break;
                    }
                    else if (p2.HP == 0)
                    {
                        Console.WriteLine(p1.name + "获得了胜利!");
                        break;
                    }
                    int dx1 = 0;
                    int j1 = r.Next(3);//定义一个随机数来确定技能的减血量
                    switch (j1)
                    {
                        case 0:
                            dx1 = r.Next(100);//技能0的减血量为0-100的随机数
                            break;
                        case 1:
                            dx1 = r.Next(100, 200);//技能1的减血量为100-200的随机数
                            break;
                        case 2:
                            dx1 = r.Next(200, 300);//技能2的减血量为2000-300的随机数
                            break;
                        default:
                            break;
                    }
                    p1.HP = p1.HP - dx1 + p1.defend; //剩余血量等于原血量减去减血量加上p1的防御值
                    if (p1.HP <= 0)
                    {
                        p1.HP = 0;  //当p1的血量为负时重新赋成0
                    }
                    Console.WriteLine(p1.name + "向" + p2.name + "使出了" + p1.jineng[j1] + "," + p2.name + "失掉生命力" + dx1 + "点");
                    Console.WriteLine();
                    System.Threading.Thread.Sleep(2000); //休眠2s
                    int dx2 = 0;
                    int j2 = r.Next(3);
                    switch (j2)
                    {
                        case 0:
                            dx2 = r.Next(100);
                            break;
                        case 1:
                            dx2 = r.Next(100, 200);
                            break;
                        case 2:
                            dx2 = r.Next(200, 300);
                            break;
                        default:
                            break;
                    }

                    p2.HP = p2.HP - dx2 + p2.defend;
                    if (p2.HP <= 0)
                    {
                        p2.HP = 0;
                    }
                    Console.WriteLine(p2.name + "向" + p1.name + "使出了" + p2.jineng[j2] + "," + p1.name + "失掉生命力" + dx2 + "点");
                    Console.WriteLine(p1.name + " 生命力:" + p1.HP);
                    Console.WriteLine(p2.name + " 生命力:" + p2.HP);
                    System.Threading.Thread.Sleep(2000);

                }
            }
        }
    }

  • 相关阅读:
    C语言经典编程例子
    C语言编程例子-判断字母是否大写
    C语言编程例子-判断偶数
    C语言编程例子-打印乘法口诀表
    C语言编程例子-特殊等式
    C语言编程例子-一元钱的兑换方案
    C语言编程例子-使用while为用户提供菜单显示
    C语言编程例子-使用while语句求n
    C语言编程例子-检查字符类型
    C语言编程例子-求最低分和最高分
  • 原文地址:https://www.cnblogs.com/William-1234/p/4336985.html
Copyright © 2020-2023  润新知