C# 全屏label控件实现的贪吃蛇。

C# 全屏label控件实现的贪吃蛇。

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar;

namespace snake
{
    public partial class Form1 : Form
    {
        // 定义常量
        private const int BlockSize = 20; // 方块的大小
        private const int Width = 40; // 游戏界面的宽度
        private const int Height = 30; // 游戏界面的高度

        // 记录分数
        private int Score = 0;

        // 地图的二维数组
        private Label[][] Blocks = new Label[Width][];

        // 存储地图上每个方块的引用
        Label[,] map = new Label[Height, Width];

        // 贪吃蛇的移动方向
        private char moveDirection;

        // 食物的位置
        private int food_i, food_j;

        // 游戏是否失败
        private bool isFailure;

        // 初始化游戏
        private void InitializeGame()
        {
            // 将地图上的方块标记为0
            for (int i = 0; i < Height; i++)
                for (int j = 0; j < Width; j++)
                    Blocks[i][j].Tag = 0;

            // 设置初始移动方向为向右
            moveDirection = 'D';

            // 设置蛇的初始位置和长度
            Blocks[10][10].Tag = 1;
            for (int i = 1; i <= 5; i++)
            {
                Blocks[10][10 - i].Tag = i + 1;
            }

            // 在随机位置生成食物
            Random random = new Random();
            food_i = random.Next(2, Height - 5);
            food_j = random.Next(2, Width - 5);

            // 设置窗体标题
            Text = "贪吃小蛇蛇  上下左右键盘操作方向";
        }

        // 定时器的回调函数,控制游戏逻辑
        private void Timer_Tick(object sender, EventArgs e)
        {
            // 如果游戏失败,则退出
            if (isFailure)
            {
                return;
            }

            // 移动贪吃蛇
            MoveSnake();

            // 更新界面显示
            Show1();
        }

        // 更新界面显示
        private void Show1()
        {
            // 遍历地图上的每个方块
            for (int i = 0; i < Height; i++)
            {
                for (int j = 0; j < Width; j++)
                {
                    int value = (int)Blocks[i][j].Tag;

                    // 根据方块的值设置背景颜色
                    Color color = value > 0 ? Color.FromArgb(111, 111, 111) : Color.FromArgb(222, 222, 222);
                    Blocks[i][j].BackColor = color;

                    // 设置方块的位置
                    Blocks[i][j].Top = i * BlockSize;
                    Blocks[i][j].Left = j * BlockSize;
                }
            }

            // 将食物方块的背景颜色设置为红色
            Blocks[food_i][food_j].BackColor = Color.Red;
            Blocks[food_i][food_j].Top = food_i * BlockSize;
            Blocks[food_i][food_j].Left = food_j * BlockSize;

            // 如果游戏失败,停止定时器,弹出提示框
            if (isFailure)
            {
                timer1.Enabled = false;
                MessageBox.Show("Game Over~!!");
                button1.Enabled = true;
            }
        }

        // 移动贪吃蛇
        private void MoveSnake()
        {
            // 将蛇身体上的方块值加一
            for (int i = 0; i < Height; i++)
            {
                for (int j = 0; j < Width; j++)
                {
                    if ((int)Blocks[i][j].Tag > 0)
                    {
                        Blocks[i][j].Tag = (int)Blocks[i][j].Tag + 1;
                    }
                }
            }

            // 找到蛇尾和蛇头的位置
            int oldTail_i = 0, oldTail_j = 0, oldHead_i = 0, oldHead_j = 0;
            int max_value = 0;
            for (int i = 0; i < Height; i++)
            {
                for (int j = 0; j < Width; j++)
                {
                    int value = (int)Blocks[i][j].Tag;
                    if (max_value < value)
                    {
                        max_value = value;
                        oldTail_i = i;
                        oldTail_j = j;
                    }
                    if (value == 2)
                    {
                        oldHead_i = i;
                        oldHead_j = j;
                    }
                }
            }

            // 计算蛇头新的位置
            int newHead_i = oldHead_i;
            int newHead_j = oldHead_j;

            if (moveDirection == 'W')
            {
                newHead_i = oldHead_i - 1;
            }
            else if (moveDirection == 'S')
            {
                newHead_i = oldHead_i + 1;
            }
            else if (moveDirection == 'A')
            {
                newHead_j = oldHead_j - 1;
            }
            else if (moveDirection == 'D')
            {
                newHead_j = oldHead_j + 1;
            }

            // 判断游戏是否失败
            if (newHead_i >= Height || newHead_i < 0 || newHead_j >= Width || newHead_j < 0 || (int)Blocks[newHead_i][newHead_j].Tag > 0)
            {
                isFailure = true;
                return;
            }

            // 更新蛇头的位置
            Blocks[newHead_i][newHead_j].Tag = 1;

            // 判断是否吃到食物
            if (newHead_i == food_i && newHead_j == food_j)
            {
                // 在随机位置生成新的食物
                Random random = new Random();
                food_i = random.Next(2, Height - 5);
                food_j = random.Next(2, Width - 5);

                // 增加分数
                Score += 1;
                txtScore.Text = "" + Score;

            }
            else
            {
                // 更新蛇尾的位置
                Blocks[oldTail_i][oldTail_j].Tag = 0;
            }
        }

        public Form1()
        {
            InitializeComponent();

            // 创建地图上的方块并添加到窗体中
            for (int i = 0; i < Height; i++)
            {
                Blocks[i] = new Label[Width];
                for (int j = 0; j < Width; j++)
                {
                    Blocks[i][j] = new Label();
                    Blocks[i][j].Height = BlockSize;
                    Blocks[i][j].Width = BlockSize;
                    Blocks[i][j].Tag = 0;
                    Controls.Add(Blocks[i][j]);
                }
            }
        }

        // 开始按钮的点击事件
        private void button1_Click(object sender, EventArgs e)
        {
            // 初始化游戏
            InitializeGame();

            // 启动定时器,控制游戏逻辑
            timer1.Start();

            // 重置分数
            Score = 0;
            txtScore.Text = "" + Score;

            // 禁用开始按钮
            button1.Enabled = false;
        }

        // 键盘按下事件
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // 根据按键设置移动方向
            switch (e.KeyCode)
            {
                case Keys.Up:
                    if (moveDirection != 'S')
                    {
                        moveDirection = 'W';
                        MoveSnake();
                    }
                    break;
                case Keys.Left:
                    if (moveDirection != 'D')
                    {
                        moveDirection = 'A';
                        MoveSnake();
                    }
                    break;
                case Keys.Down:
                    if (moveDirection != 'W')
                    {
                        moveDirection = 'S';
                        MoveSnake();
                    }
                    break;
                case Keys.Right:
                    if (moveDirection != 'A')
                    {
                        moveDirection = 'D';
                        MoveSnake();
                    }
                    break;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ProgressBar;

namespace snake
{
    public partial class Form1 : Form
    {
        private const int BlockSize = 20;
        private const int Width = 40;
        private const int Height = 30;

        private int Score = 0;

        private Label[][] Blocks= new Label[Width][];//地图的二维数组
        Label[,] map = new Label[Height, Width];
        private char moveDirection;
        private int food_i, food_j;
        private bool isFailure;
    
 
        private void InitializeGame()
        {
            for (int i = 0; i < Height; i++)
                for (int j = 0; j<Width; j++)
                    Blocks[i][j].Tag = 0;        

            moveDirection = 'D';
            food_i = 0;
            food_j = 0;
            isFailure = false;

            Blocks[10][10].Tag =1;
            for (int i = 1; i <= 5; i++)
            {
                Blocks[10][10 - i].Tag =i + 1;
            }

            Random random = new Random();
            food_i = random.Next(2, Height - 5);
            food_j = random.Next(2, Width - 5);

         
            Text = "贪吃小蛇蛇  上下左右键盘操作方向";


        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            if (isFailure)
            {
                return;
            }

            MoveSnake();
            Show1();
        }
        private void Show1() {
            for (int i = 0; i < Height; i++)
            {
                for (int j = 0; j < Width; j++)
                {
                    int value = (int)Blocks[i][j].Tag;
                    Color color = value > 0 ? Color.FromArgb(111, 111, 111) : Color.FromArgb(222, 222, 222);
                    Blocks[i][j].BackColor = color;
                    Blocks[i][j].Top=i * BlockSize;
                    Blocks[i][j].Left= j * BlockSize;
                }
            }

            Blocks[food_i][food_j].BackColor = Color.Red;
            Blocks[food_i][food_j ].Top=food_i * BlockSize;
            Blocks[food_i][food_j ].Left= food_j * BlockSize;
            if (isFailure) { 

                timer1.Enabled = false;
                MessageBox.Show("Game Over~!!");
                button1.Enabled = true;
            }
        }
         

        private void MoveSnake()
        {
            for (int i = 0; i < Height; i++)
            {
                for (int j = 0; j < Width; j++)
                {
                    if ((int)Blocks[i][j].Tag > 0)
                    {
                        Blocks[i][j].Tag = (int)Blocks[i][j].Tag+1;
                    }
                }
            }

            int oldTail_i = 0, oldTail_j = 0, oldHead_i = 0, oldHead_j = 0;
            int max_value = 0;
            for (int i = 0; i < Height; i++)
            {
                for (int j = 0; j < Width; j++)
                {
                    int value = (int)Blocks[i][j].Tag;
                    if (max_value < value)
                    {
                        max_value = value;
                        oldTail_i = i;
                        oldTail_j = j;
                    }
                    if (value == 2)
                    {
                        oldHead_i = i;
                        oldHead_j = j;
                    }
                }
            }

            int newHead_i = oldHead_i;
            int newHead_j = oldHead_j;

            if (moveDirection == 'W')
            {
                newHead_i = oldHead_i - 1;
            }
            else if (moveDirection == 'S')
            {
                newHead_i = oldHead_i + 1;
            }
            else if (moveDirection == 'A')
            {
                newHead_j = oldHead_j - 1;
            }
            else if (moveDirection == 'D')
            {
                newHead_j = oldHead_j + 1;
            }

            if (newHead_i >= Height || newHead_i < 0 || newHead_j >= Width || newHead_j < 0 || (int)Blocks[newHead_i][newHead_j].Tag > 0)
            {
                isFailure = true;
                return;
            }

            Blocks[newHead_i][newHead_j].Tag = 1;

            if (newHead_i == food_i && newHead_j == food_j)
            {
                Random random = new Random();
                food_i = random.Next(2, Height - 5);
                food_j = random.Next(2, Width - 5);
                Score +=1;
                txtScore.Text=""+Score;

            }
            else
            {
                Blocks[oldTail_i][oldTail_j].Tag = 0;
            }
        }




        public Form1()
        {
            InitializeComponent();


            for (int i = 0; i < Height; i++)
            {
                Blocks[i] = new Label[Width];
                for (int j = 0; j<Width; j++)
                {
                    Blocks[i][j] = new Label();
                    Blocks[i][j].Height=BlockSize;
                    Blocks[i][j].Width =BlockSize;
                    Blocks[i][j].Tag = 0;
                    Controls.Add(Blocks[i][j]);

                }
            }

           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            InitializeGame();
            timer1.Start();
            Score = 0;
            txtScore.Text = ""+Score;
            button1.Enabled = false;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Up:                   
                    if (moveDirection != 'S')
                    {
                        moveDirection = 'W';
                        MoveSnake();
                    }
                    break;
                case Keys.Left:
                    if (moveDirection != 'D')
                    {
                        moveDirection = 'A';
                        MoveSnake();
                    }
                    break;
                case Keys.Down:
                    if (moveDirection != 'W')
                    {
                        moveDirection = 'S';
                        MoveSnake();
                    }
                    break;
                case Keys.Right:
                    if (moveDirection != 'A')
                    {
                        moveDirection = 'D';
                        MoveSnake();
                    }
                    break;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
            
        }
    }
}
相关推荐
若亦_Royi11 分钟前
C++ 的大括号的用法合集
开发语言·c++
资源补给站1 小时前
大恒相机开发(2)—Python软触发调用采集图像
开发语言·python·数码相机
m0_748247551 小时前
Web 应用项目开发全流程解析与实战经验分享
开发语言·前端·php
6.942 小时前
Scala学习记录 递归调用 练习
开发语言·学习·scala
FF在路上2 小时前
Knife4j调试实体类传参扁平化模式修改:default-flat-param-object: true
java·开发语言
码农君莫笑2 小时前
使用blazor开发信息管理系统的应用场景
数据库·信息可视化·c#·.net·visual studio
众拾达人3 小时前
Android自动化测试实战 Java篇 主流工具 框架 脚本
android·java·开发语言
皓木.3 小时前
Mybatis-Plus
java·开发语言
不良人天码星3 小时前
lombok插件不生效
java·开发语言·intellij-idea
源码哥_博纳软云3 小时前
JAVA同城服务场馆门店预约系统支持H5小程序APP源码
java·开发语言·微信小程序·小程序·微信公众平台