使蛇游戏成为计分器并提高移动速度

时间:2017-04-22 14:51:03

标签: c#

所以我做了这个简单的蛇游戏,我作为一个学校项目的指南制作现在我想通过添加一个分数计数器来改善它,并在每次获得食物帮助时提高蛇的移动速度
这是我目前遇到的问题 enter image description here

namespace Snake
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WindowHeight = 32;
            Console.WindowWidth = 64;
            int screenwidth = Console.WindowWidth;
            int screenheight = Console.WindowHeight;
            Random randomnummer = new Random();
            int emtiaz = 5;
            int bakht = 0;
            pixel gz = new pixel();
            gz.xpos = screenwidth / 2;
            gz.ypos = screenheight / 2;
            gz.rangsafe = ConsoleColor.Red;
            string movement = "RIGHT";
            List<int> xposlijf = new List<int>();
            List<int> yposlijf = new List<int>();
            int berryx = randomnummer.Next(0, screenwidth);
            int berryy = randomnummer.Next(0, screenheight);
            DateTime tijd = DateTime.Now;
            DateTime tijd2 = DateTime.Now;
            string buttonpressed = "no";
            while (true)
            {
                Console.Clear();
                if (gz.xpos == screenwidth - 1 || gz.xpos == 0 || gz.ypos == screenheight - 1 || gz.ypos == 0)
                {
                    bakht = 1;
                }
                for (int i = 0; i < screenwidth; i++)
                {
                    Console.SetCursorPosition(i, 0);
                    Console.Write("*");
                }
                for (int i = 0; i < screenwidth; i++)
                {
                    Console.SetCursorPosition(i, screenheight - 1);
                    Console.Write("*");
                }
                for (int i = 0; i < screenheight; i++)
                {
                    Console.SetCursorPosition(0, i);
                    Console.Write("*");
                }
                for (int i = 0; i < screenheight; i++)
                {
                    Console.SetCursorPosition(screenwidth - 1, i);
                    Console.Write("v");
                }
                Console.ForegroundColor = ConsoleColor.Green;
                if (berryx == gz.xpos && berryy == gz.ypos)
                {
                    emtiaz++;
                    berryx = randomnummer.Next(1, screenwidth - 2);
                    berryy = randomnummer.Next(1, screenheight - 2);
                }
                for (int i = 0; i < xposlijf.Count(); i++)
                {
                    Console.SetCursorPosition(xposlijf[i], yposlijf[i]);
                    Console.Write("*");
                    if (xposlijf[i] == gz.xpos && yposlijf[i] == gz.ypos)
                    {
                        bakht = 1;
                    }
                }
                if (bakht == 1)
                {
                    break;
                }
                Console.SetCursorPosition(gz.xpos, gz.ypos);
                Console.ForegroundColor = gz.rangsafe;
                Console.Write("*");
                Console.SetCursorPosition(berryx, berryy);
                Console.ForegroundColor = ConsoleColor.DarkBlue;
                Console.Write("*");
                tijd = DateTime.Now;
                buttonpressed = "no";
                while (true)
                {
                    tijd2 = DateTime.Now;
                    if (tijd2.Subtract(tijd).TotalMilliseconds > 500) { break; }
                    if (Console.KeyAvailable)
                    {
                        ConsoleKeyInfo toets = Console.ReadKey(true);
                        if (toets.Key.Equals(ConsoleKey.UpArrow) && movement != "DOWN" && buttonpressed == "no")
                        {
                            movement = "UP";
                            buttonpressed = "yes";
                        }
                        if (toets.Key.Equals(ConsoleKey.DownArrow) && movement != "UP" && buttonpressed == "no")
                        {
                            movement = "DOWN";
                            buttonpressed = "yes";
                        }
                        if (toets.Key.Equals(ConsoleKey.LeftArrow) && movement != "RIGHT" && buttonpressed == "no")
                        {
                            movement = "LEFT";
                            buttonpressed = "yes";
                        }
                        if (toets.Key.Equals(ConsoleKey.RightArrow) && movement != "LEFT" && buttonpressed == "no")
                        {
                            movement = "RIGHT";
                            buttonpressed = "yes";
                        }
                    }
                }
                xposlijf.Add(gz.xpos);
                yposlijf.Add(gz.ypos);
                switch (movement)
                {
                    case "UP":
                        gz.ypos--;
                        break;
                    case "DOWN":
                        gz.ypos++;
                        break;
                    case "LEFT":
                        gz.xpos--;
                        break;
                    case "RIGHT":
                        gz.xpos++;
                        break;
                }
                if (xposlijf.Count() > emtiaz)
                {
                    xposlijf.RemoveAt(0);
                    yposlijf.RemoveAt(0);
                }
            }
            Console.SetCursorPosition(screenwidth / 5, screenheight / 2);
            Console.WriteLine("bazi ra bakhti, emtiaz: " + emtiaz);
            Console.SetCursorPosition(screenwidth / 5, screenheight / 2 + 1);
        }
        class pixel
        {
            public int xpos { get; set; }
            public int ypos { get; set; }
            public ConsoleColor rangsafe { get; set; }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

看看以下2段代码

if (tijd2.Subtract(tijd).TotalMilliseconds > 500)

制作变量并使用它而不是500 比在下面的块中使用该变量

if (berryx == gz.xpos && berryy == gz.ypos)
{
  emtiaz++;
  berryx = randomnummer.Next(1, screenwidth - 2);
  berryy = randomnummer.Next(1, screenheight - 2);
}

并测试并进行更改并查看您的游戏行为,它将指导您尝试执行的操作。 你的变量emtiaz已经计算了蛇获取食物的次数