C# 开发贪吃蛇游戏

[复制链接]
发表于 2025-12-23 17:08:35 | 显示全部楼层 |阅读模式
 贪吃蛇各人都玩过,以是就不外多表明 这是一个Winfrom项目即贴既玩

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Windows.Forms;
  6. namespace SnakeGame
  7. {
  8.     public partial class Form1 : Form
  9.     {
  10.         private const int GridSize = 10; // 每个格子的尺寸
  11.         private const int WidthInGrid = 100;    // 游戏区域的宽度(格子数)
  12.         private const int HeightInGrid = 70;   // 游戏区域的高度(格子数)
  13.         private List<Rectangle> snake = new List<Rectangle>
  14.             {
  15.                 new Rectangle(100, 100, GridSize, GridSize)  // 初始化蛇的起始位置
  16.             };  // 蛇的身体部分(每个部分都是一个矩形)
  17.         private Rectangle food;         // 食物的矩形区域
  18.         private Random random;          // 用于生成随机数
  19.         private int dx, dy;             // 蛇的移动方向
  20.         private Timer timer;            // 游戏定时器
  21.         private bool isGameOver;        // 游戏是否结束
  22.         private int fastSpeed = 20;    // 按住方向键时的加速速度(定时器间隔)
  23.         private int defaultSpeed = 100; // 默认的蛇的速度(定时器间隔)
  24.         // 新增:记录蛇的当前大小
  25.         private int snakeSegmentWidth = GridSize;
  26.         private int snakeSegmentHeight = GridSize;
  27.         public Form1()
  28.         {
  29.             InitializeComponent();
  30.             Start();
  31.         }
  32.         public void Start()
  33.         {
  34.             label2.Visible = false;
  35.             label1.Visible = false;
  36.             button1.Visible = false;
  37.             this.DoubleBuffered = true; // 开启双缓冲,减少闪烁
  38.             // 根据格子数调整窗体尺寸
  39.             this.Width = WidthInGrid * GridSize + 16;  // Form的宽度
  40.             this.Height = HeightInGrid * GridSize + 39; // Form的高度
  41.             random = new Random();
  42.             timer = new Timer { Interval = defaultSpeed }; // 设置定时器初始速度
  43.             timer.Tick += Timer_Tick;
  44.             ResetGame();
  45.         }
  46.         private void ResetGame()
  47.         {
  48.             food = GenerateFood();
  49.             dx = GridSize; // 初始时蛇向右移动
  50.             dy = 0;
  51.             isGameOver = false;
  52.             timer.Start(); // 启动游戏定时器
  53.         }
  54.         private void Timer_Tick(object sender, EventArgs e)
  55.         {
  56.             if (isGameOver)
  57.             {
  58.                 timer.Stop();
  59.                 label1.Visible = true;
  60.                 label1.Text = "Game Over";
  61.                 button1.Visible = true;
  62.                 button1.Enabled = true;
  63.                 label2.Visible = true;
  64.                 return;
  65.             }
  66.             // 计算蛇头的新位置
  67.             var head = snake.First();
  68.             var newHead = new Rectangle(head.X + dx, head.Y + dy, snakeSegmentWidth, snakeSegmentHeight);
  69.             // 如果蛇头碰到墙壁或自己,游戏结束
  70.             if (newHead.X < 0 || newHead.Y < 0 || newHead.X >= WidthInGrid * GridSize || newHead.Y >= HeightInGrid * GridSize || snake.Contains(newHead))
  71.             {
  72.                 isGameOver = true;
  73.                 return;
  74.             }
  75.             if (snake.Count % 5 == 0)
  76.             {
  77.                 snakeSegmentWidth = (int)(GridSize + snake.Count);
  78.                 snakeSegmentHeight = (int)(GridSize + snake.Count);
  79.             }
  80.             // 将蛇头添加到蛇身前
  81.             snake.Insert(0, newHead);
  82.             // 如果蛇吃到食物,生成新食物并让蛇变长
  83.             if (newHead.IntersectsWith(food))
  84.             {
  85.                 food = GenerateFood();
  86.             }
  87.             else
  88.             {
  89.                 // 否则去掉蛇尾
  90.                 snake.RemoveAt(snake.Count - 1);
  91.             }
  92.             // 重新绘制游戏画面
  93.             Invalidate();
  94.         }
  95.         private Rectangle GenerateFood()
  96.         {
  97.             // 随机生成食物的位置,确保食物生成在窗体内
  98.             int x = random.Next(0, WidthInGrid) * GridSize;
  99.             int y = random.Next(0, HeightInGrid) * GridSize;
  100.             return new Rectangle(x, y, GridSize, GridSize);
  101.         }
  102.         private void button1_Click(object sender, EventArgs e)
  103.         {
  104.             label1.Visible = false;
  105.             button1.Visible = false;
  106.             button1.Enabled = false;
  107.             isGameOver = false;
  108.             label2.Visible = false;
  109.             snakeSegmentWidth = GridSize;
  110.             snakeSegmentHeight = GridSize;
  111.             snake = new List<Rectangle>
  112.             {
  113.                 new Rectangle(100, 100, GridSize, GridSize)  // 初始化蛇的起始位置
  114.             };
  115.             dx = GridSize; // 初始时蛇向右移动
  116.             dy = 0;
  117.             timer.Interval = defaultSpeed;
  118.             timer.Start();
  119.         }
  120.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  121.         {
  122.             // 控制蛇的移动方向
  123.             if (e.KeyCode == Keys.Up&& dy==0)
  124.             {
  125.                 dx = 0;
  126.                 dy = -GridSize;
  127.                 timer.Interval = fastSpeed; // 按住时加速
  128.             }
  129.             else if (e.KeyCode == Keys.Down && dy == 0)
  130.             {
  131.                 dx = 0;
  132.                 dy = GridSize;
  133.                 timer.Interval = fastSpeed; // 按住时加速
  134.             }
  135.             else if (e.KeyCode == Keys.Left && dx == 0)
  136.             {
  137.                 dx = -GridSize;
  138.                 dy = 0;
  139.                 timer.Interval = fastSpeed; // 按住时加速
  140.             }
  141.             else if (e.KeyCode == Keys.Right && dx == 0)
  142.             {
  143.                 dx = GridSize;
  144.                 dy = 0;
  145.                 timer.Interval = fastSpeed; // 按住时加速
  146.             }
  147.         }
  148.         private void Form1_Paint(object sender, PaintEventArgs e)
  149.         {
  150.             var g = e.Graphics;
  151.             // 绘制蛇身
  152.             for (int i = 0; i < snake.Count; i++)
  153.             {
  154.                 var segment = snake[i];
  155.                 // 蛇头(第一个部分)为黄色,其余为绿色
  156.                 if (i == 0)
  157.                 {
  158.                     g.FillRectangle(Brushes.Yellow, segment); // 蛇头为黄色
  159.                 }
  160.                 else
  161.                 {
  162.                     g.FillRectangle(Brushes.Lime, segment); // 蛇身为绿色
  163.                 }
  164.             }
  165.             // 绘制食物
  166.             g.FillRectangle(Brushes.Red, food);
  167.         }
  168.         private void Form1_KeyUp(object sender, KeyEventArgs e)
  169.         {
  170.             // 松开方向键时恢复到默认速度
  171.             if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
  172.             {
  173.                 timer.Interval = defaultSpeed; // 恢复原来的速度
  174.             }
  175.         }
  176.     }
  177. }
复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表