C#实现贪吃蛇游戏

 

定义贪吃蛇和游戏逻辑

  1. 定义数据结构:创建一个类来表示贪吃蛇的每个部分(通常是一个具有X和Y坐标的结构体或类)。
  2. 定义游戏状态:包括蛇的位置、方向、长度以及食物的位置。
  3. 处理键盘输入:重写窗体的键盘事件处理函数,以便玩家可以使用键盘控制蛇的移动方向。
  4. 更新游戏状态:在每个游戏循环中,更新蛇的位置(根据当前方向和移动速度),并检查是否吃到食物、撞到墙壁或自己。
  5. 绘制游戏画面:在窗体的Paint事件中,根据当前的游戏状态绘制蛇和食物。
using System;
using System.Collections.Generic;
using System.Threading;

class Program
{
    // 游戏区域大小
    static int width = 20;
    static int height = 10;

    // 蛇的方向
    enum Direction { Up, Down, Left, Right };
    static Direction dir = Direction.Right;

    // 蛇的初始位置
    static List<int[]> snake = new List<int[]> { new int[] { 3, 1 }, new int[] { 2, 1 }, new int[] { 1, 1 } };

    // 食物的位置
    static int[] food = new int[2];

    // 游戏是否结束
    static bool gameOver = false;

    static void Main(string[] args)
    {
        Console.CursorVisible = false;
        Console.SetWindowSize(width + 1, height + 1);

        // 设置初始食物位置
        GenerateFood();

        // 游戏循环
        while (!gameOver)
        {
            Draw();
            Input();
            Move();
            CheckCollision();
            Thread.Sleep(100);
        }

        Console.SetCursorPosition(width / 2 - 4, height / 2);
        Console.WriteLine("Game Over!");
        Console.ReadKey();
    }

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

        // 画蛇
        foreach (var segment in snake)
        {
            Console.SetCursorPosition(segment[0], segment[1]);
            Console.Write("o");
        }

        // 画食物
        Console.SetCursorPosition(food[0], food[1]);
        Console.Write("X");
    }

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

    static void Move()
    {
        int[] head = new int[] { snake[0][0], snake[0][1] };
        switch (dir)
        {
            case Direction.Up:
                head[1]--;
                break;
            case Direction.Down:
                head[1]++;
                break;
            case Direction.Left:
                head[0]--;
                break;
            case Direction.Right:
                head[0]++;
                break;
        }

        // 添加新的头部
        snake.Insert(0, head);

        // 如果吃到了食物
        if (head[0] == food[0] && head[1] == food[1])
        {
            GenerateFood();
        }
        else
        {
            // 移除尾部
            snake.RemoveAt(snake.Count - 1);
        }
    }

    static void GenerateFood()
    {
        Random rnd = new Random();
        bool validPosition = false;

        // 确保食物不在蛇身上
        while (!validPosition)
        {
            food[0] = rnd.Next(0, width);
            food[1] = rnd.Next(0, height);
            validPosition = true;

            foreach (var segment in snake)
            {
                if (food[0] == segment[0] && food[1] == segment[1])
                {
                    validPosition = false;
                    break;
                }
            }
        }
    }

    static void CheckCollision()
    {
        // 检查蛇头是否碰到边界
        if (snake[0][0] < 0 || snake[0][0] >= width || snake[0][1] < 0 || snake[0][1] >= height)
        {
            gameOver = true;
            return;
        }

        // 检查蛇头是否碰到自己的身体
        for (int i = 1; i < snake.Count; i++)
        {
            if (snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1])
            {
                gameOver = true;
                return;
            }
        }
    }
}

这段代码创建了一个简单的贪吃蛇游戏,你可以在控制台中运行它。玩家可以使用WASD键来控制蛇的移动,目标是尽可能地吃到食物而不与蛇的身体相撞。

相关推荐

  1. C#实现贪吃游戏

    2024-03-20 21:28:01       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-20 21:28:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-20 21:28:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-20 21:28:01       82 阅读
  4. Python语言-面向对象

    2024-03-20 21:28:01       91 阅读

热门阅读

  1. 【前端】国际化实现过程

    2024-03-20 21:28:01       44 阅读
  2. 使用map优化双层for循环

    2024-03-20 21:28:01       38 阅读
  3. Spring容器(ApplicationContext)刷新过程

    2024-03-20 21:28:01       35 阅读
  4. Spring底层核心原理解析

    2024-03-20 21:28:01       42 阅读
  5. CSDN 新手markdown模板,画图用

    2024-03-20 21:28:01       38 阅读
  6. 13 新型网络应用(3)

    2024-03-20 21:28:01       39 阅读
  7. Rust 中Self 关键字的两种不同用法

    2024-03-20 21:28:01       44 阅读
  8. Cuckoo沙箱环境使用介绍

    2024-03-20 21:28:01       38 阅读
  9. computed

    2024-03-20 21:28:01       39 阅读