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)
        {
           
            
        }
    }
}
相关推荐
尘浮生几秒前
Java项目实战II基于微信小程序的电影院买票选座系统(开发文档+数据库+源码)
java·开发语言·数据库·微信小程序·小程序·maven·intellij-idea
hopetomorrow14 分钟前
学习路之PHP--使用GROUP BY 发生错误 SELECT list is not in GROUP BY clause .......... 解决
开发语言·学习·php
小牛itbull24 分钟前
ReactPress vs VuePress vs WordPress
开发语言·javascript·reactpress
广煜永不挂科32 分钟前
Devexpress.Dashboard的调用二义性
c#·express
请叫我欧皇i33 分钟前
html本地离线引入vant和vue2(详细步骤)
开发语言·前端·javascript
闲暇部落35 分钟前
‌Kotlin中的?.和!!主要区别
android·开发语言·kotlin
GIS瞧葩菜1 小时前
局部修改3dtiles子模型的位置。
开发语言·javascript·ecmascript
chnming19871 小时前
STL关联式容器之set
开发语言·c++
熬夜学编程的小王1 小时前
【C++篇】深度解析 C++ List 容器:底层设计与实现揭秘
开发语言·数据结构·c++·stl·list
GIS 数据栈1 小时前
每日一书 《基于ArcGIS的Python编程秘笈》
开发语言·python·arcgis