C#控制台小游戏吃豆人

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

namespace 吃豆人__20180817__1
{
class Vection2
{
public int x;
public int y;
}

class Program
{
    static void Main(string[] args)
    {
        rd = new Random();
        int a = 0;
        int b = 0;
        int[,] num = new int[,]
        {
            {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
            {1,0,0,3,0,0,0,0,0,0,0,0,0,0,1 },
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
            {1,0,0,0,0,0,0,0,0,0,0,3,0,0,1 },
            {1,0,0,0,0,0,0,3,0,0,0,0,0,0,1 },
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
            {1,0,0,3,0,0,0,0,0,0,0,0,0,0,1 },
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
            {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1 },
            {1,0,0,0,0,0,2,0,0,0,0,0,0,0,1 },
            {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }
         };
        for (int i = 0; i < num.GetLength(0); i++)
        {
            for (int j = 0; j < num.GetLength(1); j++)
            {
                if (num[i, j] == 0 || num[i, j] == 5)
                {
                    Console.Write("  ");
                }
                if (num[i, j] == 1)
                {
                    Console.Write("■");
                }
                if (num[i, j] == 2)
                {
                    a = i;
                    b = j;
                    Console.Write("◎");
                }
                if (num[i, j] == 3)
                {
                    Console.Write("※");
                }
                if (num[i, j] == 4)
                {
                    Console.Write("·");
                }
                if (num[i, j] == 8)
                {
                    Console.Write("●");
                }

            }
            Console.WriteLine();

        }
        Primary(num,a,b);


        Console.ReadKey();

    }

    //每次更新坐标后刷新
    public static void Show(int[,] num)
    {
        for (int i = 0; i < num.GetLength(0); i++)
        {
            for (int j = 0; j < num.GetLength(1); j++)
            {
                if (num[i, j] == 0 || num[i, j] == 5)
                {
                    Console.Write("  ");
                }
                if (num[i, j] == 1)
                {
                    Console.Write("■");
                }
                if (num[i, j] == 2||num[i,j]==6)
                {
                    Console.Write("◎");
                }
                if (num[i, j] == 3||num[i,j]==7)
                {
                    Console.Write("※");
                }
                if (num[i, j] == 4)
                {
                    Console.Write("·");
                }
                if (num[i,j]==8)
                {
                    Console.Write("●");
                }

            }
            Console.WriteLine();

        }


    }

    //主方法
    public static void Primary(int[,] num ,int a,int b)
    {
        List<Vection2> list = new List<Vection2>();//存豆子

        for (int i = 0; i < 5; i++)
        {
            Vection2 vect = RandemBeans(num);
            num[vect.x, vect.y] = 4;
            list.Add(vect);

        }
        int tump = 0;//控制步数
        int tum = 0;//积分
        int nump = 0;
        int smp = 10;
        int monsterNUM = 0;
        bool bo2 = false;
        bool bo3 = false;
        while (true)
        {
            for (int i = 0; i < num.GetLength(0); i++)
            {
                bool bo4 = false;
                for (int j = 0; j < num.GetLength(1); j++)
                {
                    if (num[i, j] == 8)
                    {
                        bo3 = true;
                        bo4 = true;
                        break;
                    }
                    if (i==num.GetLength(0)-1&&j==num.GetLength(1)-1)
                    {
                        bo3 = false;
                    }
                }
                if (bo4==true)
                {
                    break;
                }
            }

            if (tum!=0&&tum== smp&&bo3==false)
            {
                Vection2 vect = RandemBeans(num);
                num[vect.x, vect.y] = 8;
                smp += 10;
                bo3 = true;
            }

            if (bo2 == true)
            {
                if (tump >= nump)
                {
                    bo2 = false;
                    for (int i = 0; i < num.GetLength(0); i++)
                    {
                        for (int j = 0; j < num.GetLength(1); j++)
                        {
                            if (num[i, j] == 6)
                            {
                                num[i, j] = 2;
                            }
                        }
                    }
                }
            }


            for (int i = 0; i < num.GetLength(0); i++)
            {
                for (int j = 0; j < num.GetLength(1); j++)
                {
                    if (num[i, j] == 2 )
                    {
                        int nums = 0;
                        for (int i1 = 0; i1 < num.GetLength(0); i1++)
                        {
                            for (int j1 = 0; j1 < num.GetLength(1); j1++)
                            {
                                if (num[i1, j1] == 3 || num[i1, j1] == 7)
                                {
                                    nums++;
                                }
                            }
                        }
                        if (nums<monsterNUM)
                        {
                            Console.WriteLine("游戏结束");
                            GameOver();
                        }
                    }

                }

            }
           monsterNUM = 0;
            for (int i = 0; i < num.GetLength(0); i++)
            {
                for (int j = 0; j < num.GetLength(1); j++)
                {
                    if (num[i,j]==3||num[i,j]==7)
                    {
                        monsterNUM++;
                    }
                }
            }



            bool bo1 = true;
                num = MonsterVections(num);

                for (int i = 0; i < num.GetLength(0); i++)
                {
                    for (int j = 0; j < num.GetLength(1); j++)
                    {
                        if (num[i, j] == 7)
                        {
                            bo1 = false;
                            break;
                        }
                    }
                }

                if (bo1 == true)
                {
                    int n = BeansAmount(num);
                    if (n < 5)
                    {
                        for (int i = 0; i < 5 - n; i++)
                        {
                            Vection2 vect = RandemBeans(num);
                            num[vect.x, vect.y] = 4;
                            list.Add(vect);
                        }
                    }
                }

            bool bo5 = false;
            for (int i = 0; i < num.GetLength(0); i++)
            {
                for (int j = 0; j < num.GetLength(1); j++)
                {
                    if (num[i,j]==2)
                    {
                        bo5 = true;
                        break;
                    }
                    if (i==num.GetLength(0)-1&&j==num.GetLength(1))
                    {
                        Console.WriteLine("玩家死亡");
                        GameOver();
                    }
                }
                if (bo5==true)
                {
                    break;
                }
            }


                int x = 0;
                int y = 0;
                Console.WriteLine();

                char ch = Console.ReadKey().KeyChar;
                Console.WriteLine();
                if (ch == 'w')
                {
                    y = -1;
                }
                if (ch == 's')
                {
                    y = 1;
                }
                if (ch == 'a')
                {
                    x = -1;
                }
                if (ch == 'd')
                {
                    x = 1;
                }

                if (num[a + y, b + x] == 1 || num[a + y, b + x] == 3|| num[a + y, b + x] == 7)
                {
                    continue;
                }
                else
                {
                    if (num[a + y, b + x] == 4)
                    {
                        tum++;
                        for (int i = 0; i < list.Count; i++)
                        {
                            if (list[i].x == a + y && list[i].y == b + x)
                            {
                                list.RemoveAt(i);
                                break;
                            }
                        }
                    }
                    if (num[a + y, b + x] == 5)
                    {
                        bo2 = true;
                        nump = tump + 10;
                        for (int i = 0; i < num.GetLength(0); i++)
                        {
                            for (int j = 0; j < num.GetLength(1); j++)
                            {
                                if (num[i, j] == 2)
                                {
                                    num[a, b] = 0;
                                    num[a + y, b + x] = 6;
                                }
                            }
                        }
                    }
                    else
                    {
                        num[a, b] = 0;
                        num[a + y, b + x] = 2;
                    }


                    Show(num);
                    Console.Clear();
                    Show(num);
                    Console.WriteLine("积分:{0}", tum);

                    a += y;
                    b += x;
                    tump++;
                }
            }


    }

    //随机生成豆子方法
    public static Vection2 RandemBeans(int[,] num)
    {
        Random rd = new Random();
        while (true)
        {
            int xx  = rd.Next(0,num.GetLength(1));
            int yy = rd.Next(0,num.GetLength(0));
            if (num[yy,xx]==0)
            {

                return new Vection2() {x=yy,y=xx };

            }
        }

    }

    //判断豆子数目
    public static int BeansAmount(int[,] num)
    {
        int n = 0;
        for (int i = 0; i < num.GetLength(0); i++)
        {
            for (int j = 0; j < num.GetLength(1); j++)
            {
                if (num[i,j]==4)
                {
                    n++;
                }
            }
        }
        return n;
    }

    //获取怪物坐标
    public static int[,] MonsterVections(int[,] num)
    {
        List<Vection2> list = new List<Vection2>();

        for (int i = 0; i < num.GetLength(0); i++)
        {
            for (int j = 0; j < num.GetLength(1); j++)
            {
                if (num[i, j] == 3||num[i,j]==7)
                {
                    list.Add(new Vection2() { x = i, y = j });
                }
            }
        }

        for (int i = 0; i < list.Count; i++)
        {
            num = MonsterTrack(list[i].x, list[i].y, num);
        }

        return num;
    }

    static Random rd;
    //创建怪物移动轨迹
    public static int[,] MonsterTrack(int a,int b,int[,] num)
    {          


        while (true)
        {
            int tmp = rd.Next(1, 5);//生成随机数1,2,3,4,用于控制方向;1向上,2向下,3向左,4向右

            if (tmp==1)
            {

                if (num[a - 1, b] == 1 || num[a - 1, b] == 3||num[a - 1, b] == 7|| num[a - 1, b] == 6 || num[a - 1, b] == 8)
                {
                    break;
                }
                else if (num[a - 1, b] == 4)
                {
                    num[a, b] = 0;
                    num[a - 1, b] = 7;
                    return num;
                }
                else
                {
                    if (num[a , b] == 3)
                    {
                        num[a, b] = 0;
                        num[a - 1, b] = 3;
                        return num;
                    }
                    else if (num[a, b] == 7)
                    {
                        num[a, b] = 4;
                        num[a - 1, b] = 3;
                        return num;
                    }
                }


            }
            if (tmp == 2)
            {
                //if (num[a + 1, b] == 1|| num[a + 1, b] == 3)
                //{
                //    break;
                //}
                //else
                //{
                //    num[a, b] = 0;
                //    num[a + 1, b] = 3;
                //    return num;
                //}
                if (num[a + 1, b] == 1 || num[a + 1, b] == 3 || num[a + 1, b] == 7 || num[a + 1, b] == 6 || num[a + 1, b] == 8)
                {
                    break;
                }
                else if (num[a + 1, b] == 4)
                {
                    num[a, b] = 0;
                    num[a + 1, b] = 7;
                    return num;
                }
                else
                {
                    if (num[a, b] == 3)
                    {
                        num[a, b] = 0;
                        num[a + 1, b] = 3;
                        return num;
                    }
                    else if (num[a, b] == 7)
                    {
                        num[a, b] = 4;
                        num[a + 1, b] = 3;
                        return num;
                    }
                }
            }

            if (tmp == 3)
            {
                //if (num[a , b-1] == 1|| num[a, b - 1] == 3)
                //{
                //    break;
                //}
                //else
                //{
                //    num[a, b] = 0;
                //    num[a, b - 1] = 3;
                //    return num;
                //}
                if (num[a, b - 1] == 1 || num[a, b - 1] == 3 || num[a, b - 1] == 7 || num[a, b - 1] == 6 || num[a, b - 1] == 8)
                {
                    break;
                }
                else if (num[a, b - 1] == 4)
                {
                    num[a, b] = 0;
                    num[a, b - 1] = 7;
                    return num;
                }
                else
                {
                    if (num[a, b] == 3)
                    {
                        num[a, b] = 0;
                        num[a, b - 1] = 3;
                        return num;
                    }
                    else if (num[a, b] == 7)
                    {
                        num[a, b] = 4;
                        num[a, b - 1] = 3;
                        return num;
                    }
                }


            }

            if (tmp == 4)
            {
                //if (num[a , b+1] == 1||num[a, b + 1] == 3)
                //{
                //    break;
                //}
                //else
                //{
                //    num[a, b] = 0;
                //    num[a, b + 1] = 3;
                //    return num;
                //}
                if (num[a, b + 1] == 1 || num[a, b + 1] == 3 || num[a, b + 1] == 7 || num[a, b + 1] == 6 || num[a, b + 1] == 8)
                {
                    break;
                }
                else if (num[a, b + 1] == 4)
                {
                    num[a, b] = 0;
                    num[a, b + 1] = 7;
                    return num;
                }
                else
                {
                    if (num[a, b] == 3)
                    {
                        num[a, b] = 0;
                        num[a, b + 1] = 3;
                        return num;
                    }
                    else if (num[a, b] == 7)
                    {
                        num[a, b] = 4;
                        num[a, b + 1] = 3;
                        return num;
                    }
                }

            }
        }

        return num;

    }

    //游戏结束
    public static void GameOver()
    {
        // Environment.Exit(0);
        Environment.Exit(0);
    }

}

}

猜你喜欢

转载自blog.csdn.net/qq_42016542/article/details/82108102