C#实现贪吃蛇

要在C#中实现贪吃蛇游戏,你可以按照以下步骤进行:

  1. 创建一个新的C#控制台应用程序项目。

  2. 在项目中创建一个Snake类,表示贪吃蛇。该类需要包含蛇的位置、长度、移动方向等信息,并提供方法来移动蛇、增长蛇的长度等操作。

  3. 创建一个Game类,表示游戏逻辑。该类需要维护游戏地图、蛇对象以及食物的位置等信息,并提供方法来更新游戏状态、处理用户输入等操作。

  4. 使用Console类来显示游戏画面和接收用户输入。你可以使用Console.SetCursorPosition()方法来设置光标位置,然后使用Console.Write()方法来绘制游戏地图、蛇和食物。

  5. 在Game类中使用定时器或循环来不断更新游戏状态,并根据用户输入来改变蛇的移动方向。每次更新游戏状态时,检查蛇是否吃到了食物,并根据情况增加蛇的长度和生成新的食物。

using System;
using System.Collections.Generic;
using System.Threading;

class SnakeGame
{
    static int width = 20; // 游戏地图宽度
    static int height = 10; // 游戏地图高度

    static bool gameover = false;
    static int score = 0;

    static int headX;
    static int headY;
    static Direction direction;

    static List<int> tailX = new List<int>();
    static List<int> tailY = new List<int>();

    static int foodX;
    static int foodY;

    static int prevX;
    static int prevY;
    static int tempX, tempY;
    enum Direction
    {
        Up,
        Down,
        Left,
        Right
    }

    static void Main(string[] args)
    {
        SetupGame();

        while (!gameover)
        {
            Draw();
            Input();
            Logic();
            Thread.Sleep(300);
        }
    }

    static void SetupGame()
    {
        Console.Title = "贪吃蛇游戏";
        Console.CursorVisible = false;

        headX = width / 2;
        headY = height / 2;
        direction = Direction.Right;

        Random random = new Random();
        foodX = random.Next(1, width - 1);
        foodY = random.Next(1, height - 1);
    }

    static void Draw()
    {
        Console.Clear();

        for (int i = 0; i < width + 2; i++)
        {
            Console.Write("#");
        }
        Console.WriteLine();

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                if (x == 0 || x == width - 1)
                    Console.Write("#");
                else if (x == headX && y == headY)
                    Console.Write("O");
                else if (x == foodX && y == foodY)
                    Console.Write("F");
                else
                {
                    bool tailPiece = false;
                    for (int i = 0; i < tailX.Count; i++)
                    {
                        if (tailX[i] == x && tailY[i] == y)
                        {
                            Console.Write("o");
                            tailPiece = true;
                        }
                    }
                    if (!tailPiece)
                        Console.Write(" ");
                }
            }
            Console.WriteLine();
        }

        for (int i = 0; i < width + 2; i++)
        {
            Console.Write("#");
        }
        Console.WriteLine();

        Console.WriteLine("得分: " + score);
    }

    static void Input()
    {

        if (Console.KeyAvailable)
        {
            ConsoleKeyInfo key = Console.ReadKey(true);
            switch (key.Key)
            {
                case ConsoleKey.W:
                    if (direction != Direction.Down)
                        direction = Direction.Up;
                    break;
                case ConsoleKey.S:
                    if (direction != Direction.Up)
                        direction = Direction.Down;
                    break;
                case ConsoleKey.A:
                    if (direction != Direction.Right)
                        direction = Direction.Left;
                    break;
                case ConsoleKey.D:
                    if (direction != Direction.Left)
                        direction = Direction.Right;
                    break;
                case ConsoleKey.Escape:
                    gameover = true;
                    break;
            }
        }
    }

    static void Logic()
    {
        if (tailX.Count > 0 && tailY.Count > 0)
        {
            prevX = tailX[0];
            prevY = tailY[0];

            tailX[0] = headX;
            tailY[0] = headY;

        }

        for (int i = 1; i < tailX.Count; i++)
        {
            tempX = tailX[i];
            tempY = tailY[i];
            tailX[i] = prevX;
            tailY[i] = prevY;
            prevX = tempX;
            prevY = tempY;
        }

        switch (direction)
        {
            case Direction.Up:
                headY--;
                break;
            case Direction.Down:
                headY++;
                break;
            case Direction.Left:
                headX--;
                break;
            case Direction.Right:
                headX++;
                break;
        }

        if (headX <= 0 || headX >= width - 1 || headY <= 0 || headY >= height)
            gameover = true;

        for (int i = 1; i < tailX.Count; i++)
        {
            if (tailX[i] == headX && tailY[i] == headY)
                gameover = true;
        }

        if (headX == foodX && headY == foodY)

        {
            score += 10;
            Random random = new Random();
            foodX = random.Next(1, width - 1);
            foodY = random.Next(1, height - 1);
            tailX.Add(prevX);
            tailY.Add(prevY);
        }
    }

}

这是一个简单的贪吃蛇游戏的C#代码示例。在游戏中,使用WASD键来控制蛇的移动方向,游戏目标是吃掉食物,得到分数,尽量避免蛇头碰到边界或自身身体。

猜你喜欢

转载自blog.csdn.net/2201_75443732/article/details/132827311
今日推荐