以下是一个简单的贪食蛇游戏的C#实现:\n\ncsharp\nusing System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Threading;\n\nclass Program\n{\n static void Main(string[] args)\n {\n Console.WindowHeight = 16;\n Console.WindowWidth = 32;\n int score = 0;\n int gameSpeed = 100;\n int collision = 0;\n bool isGameover = false;\n\n Random randomNumbersGenerator = new Random();\n Console.BufferHeight = Console.WindowHeight;\n int maxRight = Console.WindowWidth - 1;\n int maxLeft = Console.WindowLeft;\n int maxTop = Console.WindowTop;\n int maxBottom = Console.WindowHeight - 1;\n\n int foodLeftX = randomNumbersGenerator.Next(maxLeft + 1, maxRight - 1);\n int foodTopY = randomNumbersGenerator.Next(maxTop + 1, maxBottom - 1);\n char snakeHead = (char)2;\n int snakeHeadLeftX = Console.WindowWidth / 2;\n int snakeHeadTopY = Console.WindowHeight / 2;\n decimal sleepTime = 100;\n int direction = 0; // 0 - right, 1 - down, 2 - left, 3 - up\n List<int> snakeLeftX = new List<int>();\n List<int> snakeTopY = new List<int>();\n int foodCount = 0;\n int negativePoints = 0;\n\n for (int i = 0; i <= 5; i++)\n {\n snakeLeftX.Add(snakeHeadLeftX - i);\n snakeTopY.Add(snakeHeadTopY);\n }\n\n foreach (var leftX in snakeLeftX)\n {\n Console.SetCursorPosition(leftX, snakeHeadTopY);\n Console.Write(snakeHead);\n }\n\n Console.SetCursorPosition(foodLeftX, foodTopY);\n Console.Write("@");\n\n while (true)\n {\n if (Console.KeyAvailable)\n {\n ConsoleKeyInfo userInput = Console.ReadKey();\n if (userInput.Key == ConsoleKey.LeftArrow)\n {\n if (direction != 0)\n {\n direction = 2;\n }\n }\n if (userInput.Key == ConsoleKey.UpArrow)\n {\n if (direction != 1)\n {\n direction = 3;\n }\n }\n if (userInput.Key == ConsoleKey.RightArrow)\n {\n if (direction != 2)\n {\n direction = 0;\n }\n }\n if (userInput.Key == ConsoleKey.DownArrow)\n {\n if (direction != 3)\n {\n direction = 1;\n }\n }\n }\n\n if (snakeHeadLeftX == foodLeftX && snakeHeadTopY == foodTopY)\n {\n score++;\n foodCount++;\n if (foodCount % 5 == 0)\n {\n gameSpeed--;\n }\n sleepTime -= 0.01M;\n if (sleepTime < 10)\n {\n sleepTime = 10;\n }\n Console.SetCursorPosition(foodLeftX, foodTopY);\n Console.Write(" ");\n foodLeftX = randomNumbersGenerator.Next(maxLeft + 1, maxRight - 1);\n foodTopY = randomNumbersGenerator.Next(maxTop + 1, maxBottom - 1);\n Console.SetCursorPosition(foodLeftX, foodTopY);\n Console.Write("@");\n if (snakeLeftX.Count < 50)\n {\n for (int i = 0; i < 3; i++)\n {\n snakeLeftX.Add(-1);\n snakeTopY.Add(-1);\n }\n }\n }\n\n if (snakeLeftX.Contains(snakeHeadLeftX) && snakeTopY.Contains(snakeHeadTopY))\n {\n collision = 1;\n }\n\n if (snakeHeadLeftX >= maxRight - 1 || snakeHeadLeftX <= maxLeft || snakeHeadTopY >= maxBottom || snakeHeadTopY <= maxTop)\n {\n collision = 1;\n }\n\n if (collision == 1)\n {\n isGameover = true;\n Console.SetCursorPosition(maxRight / 2, maxBottom / 2);\n Console.WriteLine("Game Over!");\n Console.SetCursorPosition(maxRight / 2, (maxBottom / 2) + 1);\n Console.WriteLine("Your score is: " + score);\n Console.SetCursorPosition(maxRight / 2, (maxBottom / 2) + 2);\n Environment.Exit(0);\n }\n\n switch (direction)\n {\n case 0:\n snakeHeadLeftX++;\n break;\n case 1:\n snakeHeadTopY++;\n break;\n case 2:\n snakeHeadLeftX--;\n break;\n case 3:\n snakeHeadTopY--;\n break;\n }\n\n Console.Clear();\n if (!isGameover)\n {\n Console.SetCursorPosition(foodLeftX, foodTopY);\n Console.Write("@");\n }\n Console.SetCursorPosition(snakeHeadLeftX, snakeHeadTopY);\n Console.Write(snakeHead);\n if (!isGameover)\n {\n for (int i = 0; i < snakeLeftX.Count; i++)\n {\n if (snakeLeftX[i] != -1 && snakeTopY[i] != -1)\n {\n Console.SetCursorPosition(snakeLeftX[i], snakeTopY[i]);\n Console.Write("*");\n }\n }\n }\n\n if (snakeLeftX.Count > 0)\n {\n snakeLeftX.RemoveAt(0);\n snakeTopY.RemoveAt(0);\n }\n\n if (direction == 0)\n {\n snakeLeftX.Add(snakeHeadLeftX - 1);\n snakeTopY.Add(snakeHeadTopY);\n }\n if (direction == 1)\n {\n snakeLeftX.Add(snakeHeadLeftX);\n snakeTopY.Add(snakeHeadTopY - 1);\n }\n if (direction == 2)\n {\n snakeLeftX.Add(snakeHeadLeftX + 1);\n snakeTopY.Add(snakeHeadTopY);\n }\n if (direction == 3)\n {\n snakeLeftX.Add(snakeHeadLeftX);\n snakeTopY.Add(snakeHeadTopY + 1);\n }\n\n Thread.Sleep(gameSpeed);\n }\n }\n}\n\n\n这个程序使用了System.Collections.GenericSystem.Threading命名空间,包含了一个Main方法,负责游戏的初始化和主循环。游戏窗口的大小为16行32列,游戏速度和得分通过变量gameSpeedscore来控制。食物和蛇的位置通过foodLeftXfoodTopYsnakeHeadLeftXsnakeHeadTopY来表示。蛇的方向使用direction变量表示,0表示向右,1表示向下,2表示向左,3表示向上。蛇的身体使用两个List<int>来存储每个身体部分的位置。游戏中使用了Console类来进行控制台输出和控制台输入的操作。游戏主循环中使用了一个线程休眠来控制游戏速度。当游戏结束时,会在控制台输出最终得分,并退出程序。\n\n

C# 贪食蛇游戏实现 - 代码详解与游戏逻辑

原文地址: http://www.cveoy.top/t/topic/pMmo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录