C#语言实例源码系列-游戏-实现贪吃蛇

news2024/9/23 7:28:33
专栏分享
  • 点击跳转=>Unity3D特效百例
  • 点击跳转=>案例项目实战源码
  • 点击跳转=>游戏脚本-辅助自动化
  • 点击跳转=>Android控件全解手册

👉关于作者

众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣 !!!
专注于Android/Unity和各种游戏开发技巧,以及各种资源分享(网站、工具、素材、源码、游戏等)
有什么需要欢迎私我,交流群让学习不再孤单

在这里插入图片描述

👉实践过程

😜效果

在这里插入图片描述

😜代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public static bool ifStart = false;//判断是否开始
    public static int career = 400;//移动的速度
    Snake snake = new Snake();//实例化Snake类
    int snake_W = 20;//骨节的宽度
    int snake_H = 20;//骨节的高度
    public static bool pause = false;//是否暂停游戏

    /// <summary>
    /// 绘制游戏场景
    /// </summary>
    /// <param g="Graphics">封装一个GDI+绘图图面</param>
    public void ProtractTable(Graphics g)
    {
        for (int i = 0; i <= panel1.Width / snake_W; i++)//绘制单元格的纵向线
        {
            g.DrawLine(new Pen(Color.Black, 1), new Point(i * snake_W, 0), new Point(i * snake_W, panel1.Height));
        }
        for (int i = 0; i <= panel1.Height / snake_H; i++)//绘制单元格的横向线
        {
            g.DrawLine(new Pen(Color.Black, 1), new Point(0, i * snake_H), new Point(panel1.Width, i * snake_H));
        }
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = panel1.CreateGraphics();//创建panel1控件的Graphics类
        ProtractTable(g);//绘制游戏场景
        if (!ifStart)//如是没有开始游戏
        {
            Snake.timer = timer1;
            Snake.label = label2;
            snake.Ophidian(panel1, snake_W);//初始化场地及贪吃蛇信息
        }
        else
        {
            for (int i = 0; i < Snake.List.Count; i++)//绘制蛇身
            {
                e.Graphics.FillRectangle(Snake.SolidB, ((Point)Snake.List[i]).X + 1, ((Point)Snake.List[i]).Y + 1, snake_W - 1, snake_H - 1);
            }
            e.Graphics.FillRectangle(Snake.SolidF, Snake.Food.X + 1, Snake.Food.Y + 1, snake_W - 1, snake_H - 1);//绘制食物
            if (Snake.ifGame)//如果游戏结束
                //绘制提示文本
                e.Graphics.DrawString("Game Over", new Font("宋体", 30, FontStyle.Bold), new SolidBrush(Color.DarkSlateGray), new PointF(150, 130));
        }
    }

    private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        NoviceCortrol(Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString()));
        snake.BuildFood();
        textBox1.Focus();
    }

    /// <summary>
    /// 控制游戏的开始、暂停和结束
    /// </summary>
    /// <param n="int">标识</param>
    public void NoviceCortrol(int n)
    {
        switch (n)
        {
            case 1://开始游戏
                {
                    ifStart = false;
                    Graphics g = panel1.CreateGraphics();//创建panel1控件的Graphics类
                    g.FillRectangle(Snake.SolidD, 0, 0, panel1.Width, panel1.Height);//刷新游戏场地
                    ProtractTable(g);//绘制游戏场地
                    ifStart = true;//开始游戏
                    snake.Ophidian(panel1, snake_W);//初始化场地及贪吃蛇信息
                    timer1.Interval = career;//设置贪吃蛇移动的速度
                    timer1.Start();//启动计时器
                    pause = true;//是否暂停游戏
                    label2.Text = "0";//显示当前分数
                    break;
                }
            case 2://暂停游戏
                {
                    if (pause)//如果游戏正在运行
                    {
                        ifStart = true;//游戏正在开始
                        timer1.Stop();//停止计时器
                        pause = false;//当前已暂停游戏
                    }
                    else
                    {
                        ifStart = true;//游戏正在开始
                        timer1.Start();//启动计时器
                        pause = true;//开始游戏
                    }

                    break;
                }
            case 3://退出游戏
                {
                    timer1.Stop();//停止计时器
                    Application.Exit();//半闭工程
                    break;
                }
        }
    }

    private void 初级ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (timer1.Enabled == false)//如果游戏没有开始
        {
            初级ToolStripMenuItem.Checked = false;//设置该项没有被选中
            中级ToolStripMenuItem.Checked = false;//设置该项没有被选中
            高级ToolStripMenuItem.Checked = false;//设置该项没有被选中
            ((ToolStripMenuItem)sender).Checked = true;//设置当前项被选中
            switch (Convert.ToInt32(((ToolStripMenuItem)sender).Tag.ToString()))
            {
                case 1://高级
                    {
                        career = 400;//设置移动的速度
                        break;
                    }

                case 2://中级
                    {
                        career = 200;
                        break;
                    }
                case 3://{
                        career = 50;
                        break;
                    }
            }
        }
        textBox1.Focus();//获得焦点
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        int tem_n = -1;//记录移动键值
        if (e.KeyCode == Keys.Right)//如果按→键
            tem_n = 0;//向右移
        if (e.KeyCode == Keys.Left)//如果按←键
            tem_n = 1;//向左移
        if (e.KeyCode == Keys.Up)//如果按↑键
            tem_n = 2;//向上移
        if (e.KeyCode == Keys.Down)//如果按↓键
            tem_n = 3;//向下移
        if (tem_n != -1 && tem_n != Snake.Aspect)//如果移动的方向不是相同方向
        {
            if (Snake.ifGame == false)
            {
                //如果移动的方向不是相反的方向
                if (!((tem_n == 0 && Snake.Aspect == 1 || tem_n == 1 && Snake.Aspect == 0) || (tem_n == 2 && Snake.Aspect == 3 || tem_n == 3 && Snake.Aspect == 2)))
                {
                    Snake.Aspect = tem_n;//记录移动的方向
                    snake.SnakeMove(tem_n);//移动贪吃蛇
                }
            }
        }
        int tem_p = -1;//记录控制键值
        if (e.KeyCode == Keys.F2)//如果按F2键
            tem_p = 1;//开始游戏
        if (e.KeyCode == Keys.F3)//如果按F3键
            tem_p = 2;//暂停或继续游戏
        if (e.KeyCode == Keys.F4)//如果按F4键
            tem_p = 3;//关闭游戏
        if (tem_p != -1)//如果当前是操作标识
            NoviceCortrol(tem_p);//控制游戏的开始、暂止和关闭
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        snake.SnakeMove(Snake.Aspect);//移动贪吃蛇
    }
}
partial class Form1
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.menuStrip1 = new System.Windows.Forms.MenuStrip();
        this.设置ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.初级ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.中级ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.高级ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.控制ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.开始ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.暂停F3ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.退出F4ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.panel1 = new System.Windows.Forms.Panel();
        this.timer1 = new System.Windows.Forms.Timer(this.components);
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.menuStrip1.SuspendLayout();
        this.SuspendLayout();
        // 
        // menuStrip1
        // 
        this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.设置ToolStripMenuItem,
        this.控制ToolStripMenuItem});
        this.menuStrip1.Location = new System.Drawing.Point(0, 0);
        this.menuStrip1.Name = "menuStrip1";
        this.menuStrip1.Size = new System.Drawing.Size(525, 24);
        this.menuStrip1.TabIndex = 0;
        this.menuStrip1.Text = "menuStrip1";
        // 
        // 设置ToolStripMenuItem
        // 
        this.设置ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.初级ToolStripMenuItem,
        this.中级ToolStripMenuItem,
        this.高级ToolStripMenuItem});
        this.设置ToolStripMenuItem.Name = "设置ToolStripMenuItem";
        this.设置ToolStripMenuItem.Size = new System.Drawing.Size(41, 20);
        this.设置ToolStripMenuItem.Text = "设置";
        // 
        // 初级ToolStripMenuItem
        // 
        this.初级ToolStripMenuItem.Checked = true;
        this.初级ToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked;
        this.初级ToolStripMenuItem.Name = "初级ToolStripMenuItem";
        this.初级ToolStripMenuItem.Size = new System.Drawing.Size(94, 22);
        this.初级ToolStripMenuItem.Tag = "1";
        this.初级ToolStripMenuItem.Text = "初级";
        this.初级ToolStripMenuItem.Click += new System.EventHandler(this.初级ToolStripMenuItem_Click);
        // 
        // 中级ToolStripMenuItem
        // 
        this.中级ToolStripMenuItem.Name = "中级ToolStripMenuItem";
        this.中级ToolStripMenuItem.Size = new System.Drawing.Size(94, 22);
        this.中级ToolStripMenuItem.Tag = "2";
        this.中级ToolStripMenuItem.Text = "中级";
        this.中级ToolStripMenuItem.Click += new System.EventHandler(this.初级ToolStripMenuItem_Click);
        // 
        // 高级ToolStripMenuItem
        // 
        this.高级ToolStripMenuItem.Name = "高级ToolStripMenuItem";
        this.高级ToolStripMenuItem.Size = new System.Drawing.Size(94, 22);
        this.高级ToolStripMenuItem.Tag = "3";
        this.高级ToolStripMenuItem.Text = "高级";
        this.高级ToolStripMenuItem.Click += new System.EventHandler(this.初级ToolStripMenuItem_Click);
        // 
        // 控制ToolStripMenuItem
        // 
        this.控制ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.开始ToolStripMenuItem,
        this.暂停F3ToolStripMenuItem,
        this.退出F4ToolStripMenuItem});
        this.控制ToolStripMenuItem.Name = "控制ToolStripMenuItem";
        this.控制ToolStripMenuItem.Size = new System.Drawing.Size(41, 20);
        this.控制ToolStripMenuItem.Text = "控制";
        // 
        // 开始ToolStripMenuItem
        // 
        this.开始ToolStripMenuItem.Name = "开始ToolStripMenuItem";
        this.开始ToolStripMenuItem.Size = new System.Drawing.Size(130, 22);
        this.开始ToolStripMenuItem.Tag = "1";
        this.开始ToolStripMenuItem.Text = "开始    &F2";
        this.开始ToolStripMenuItem.Click += new System.EventHandler(this.开始ToolStripMenuItem_Click);
        // 
        // 暂停F3ToolStripMenuItem
        // 
        this.暂停F3ToolStripMenuItem.Name = "暂停F3ToolStripMenuItem";
        this.暂停F3ToolStripMenuItem.Size = new System.Drawing.Size(130, 22);
        this.暂停F3ToolStripMenuItem.Tag = "2";
        this.暂停F3ToolStripMenuItem.Text = "暂停    &F3";
        this.暂停F3ToolStripMenuItem.Click += new System.EventHandler(this.开始ToolStripMenuItem_Click);
        // 
        // 退出F4ToolStripMenuItem
        // 
        this.退出F4ToolStripMenuItem.Name = "退出F4ToolStripMenuItem";
        this.退出F4ToolStripMenuItem.Size = new System.Drawing.Size(130, 22);
        this.退出F4ToolStripMenuItem.Tag = "3";
        this.退出F4ToolStripMenuItem.Text = "退出    &F4";
        this.退出F4ToolStripMenuItem.Click += new System.EventHandler(this.开始ToolStripMenuItem_Click);
        // 
        // panel1
        // 
        this.panel1.Location = new System.Drawing.Point(12, 37);
        this.panel1.Name = "panel1";
        this.panel1.Size = new System.Drawing.Size(501, 401);
        this.panel1.TabIndex = 1;
        this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
        // 
        // timer1
        // 
        this.timer1.Interval = 400;
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(245, 513);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(100, 21);
        this.textBox1.TabIndex = 2;
        this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(12, 448);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(41, 12);
        this.label1.TabIndex = 3;
        this.label1.Text = "分数:";
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Location = new System.Drawing.Point(59, 448);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(11, 12);
        this.label2.TabIndex = 4;
        this.label2.Text = "0";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(525, 469);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.panel1);
        this.Controls.Add(this.menuStrip1);
        this.MainMenuStrip = this.menuStrip1;
        this.Name = "Form1";
        this.Text = "贪吃蛇";
        this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
        this.menuStrip1.ResumeLayout(false);
        this.menuStrip1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.MenuStrip menuStrip1;
    private System.Windows.Forms.ToolStripMenuItem 设置ToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem 初级ToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem 中级ToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem 高级ToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem 控制ToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem 开始ToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem 暂停F3ToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem 退出F4ToolStripMenuItem;
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.Timer timer1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
}
/**
 * https://zhima.blog.csdn.net/
 */
class Snake
{
    public static int Condyle = 0;//设置骨节的大小
    public static int Aspect = 0;//设置方向
    public static Point[] Place = { new Point(-1, -1), new Point(-1, -1), new Point(-1, -1), new Point(-1, -1), new Point(-1, -1), new Point(-1, -1) };//设置个骨节的位置
    public static Point Food = new Point(-1, -1);//设置食物的所在点
    public static bool ifFood = false;//是否有食物
    public static bool ifGame = false;//游戏是否结束
    public static int Field_width = 0;//场地的宽度
    public static int Field_Height = 0;//场地的高度
    public static Control control;//记录绘制贪吃蛇的控件
    public static Timer timer;//记录Timer组件
    public static SolidBrush SolidB = new SolidBrush(Color.Red);//设置贪吃蛇身体的颜色
    public static SolidBrush SolidD = new SolidBrush(Color.LightCoral);//设置背景颜色
    public static SolidBrush SolidF = new SolidBrush(Color.Blue);//设置食物的颜色
    public static Label label;//记录Label控件
    public static ArrayList List = new ArrayList();//实例化ArrayList数组
    Graphics g;//实例化Graphics类

    /// <summary>
    /// 初始化场地及贪吃蛇的信息
    /// </summary>
    /// <param Con="Control">控件</param>
    /// <param condyle="int">骨节大小</param>
    public void Ophidian(Control Con, int condyle)
    {
        Field_width = Con.Width;//获取场地的宽度
        Field_Height = Con.Height;//获取场地的高度
        Condyle = condyle;//记录骨节的大小
        control = Con;//记录背景控件
        g = control.CreateGraphics();//创建背景控件的Graphics类
        SolidD = new SolidBrush(Con.BackColor);//设置画刷颜色
        for (int i = 0; i < Place.Length; i++)//绘制贪吃蛇
        {
            Place[i].X = (Place.Length - i - 1) * Condyle;//设置骨节的横坐标位置
            Place[i].Y = (Field_Height / 2) - Condyle;//设置骨节的纵坐标位置
            g.FillRectangle(SolidB, Place[i].X + 1, Place[i].Y + 1, Condyle - 1, Condyle - 1);//绘制骨节
        }
        List = new ArrayList(Place);//记录每个骨节的位置
        ifGame = false;//停止游戏
        Aspect = 0;//设置方向为右
    }

    /// <summary>
    /// 移动贪吃蛇
    /// </summary>
    /// <param n="int">标识,判断的移动的方向</param>
    public void SnakeMove(int n)
    {
        Point tem_Point = new Point(-1, -1);//定义坐标结构
        switch (n)
        {
            case 0://右移
                {
                    tem_Point.X = ((Point)List[0]).X + Condyle;//蛇头向右移
                    tem_Point.Y = ((Point)List[0]).Y;
                    break;
                }
            case 1://左移
                {
                    tem_Point.X = ((Point)List[0]).X - Condyle;//蛇头向左移
                    tem_Point.Y = ((Point)List[0]).Y;
                    break;
                }
            case 2://上移
                {
                    tem_Point.Y = ((Point)List[0]).Y - Condyle;//蛇头向上移
                    tem_Point.X = ((Point)List[0]).X;
                    break;
                }
            case 3://下移
                {
                    tem_Point.Y = ((Point)List[0]).Y + Condyle;//蛇头向下移
                    tem_Point.X = ((Point)List[0]).X;
                    break;
                }
        }
        BuildFood();//生成食物
        if (!EstimateMove(tem_Point))//如果没有向相反的方向移动
        {
            Aspect = n;//改变贪吃蛇的移动方向
            if (!GameAborted(tem_Point))//如果游戏没有结束
            {
                ProtractSnake(tem_Point);//重新绘制蛇身
                EatFood();//吃食
            }
            g.FillRectangle(SolidF, Food.X + 1, Food.Y + 1, Condyle - 1, Condyle - 1);//绘制食物
        }
    }

    /// <summary>
    /// 吃食
    /// </summary>
    public void EatFood()
    {
        if (((Point)List[0]) == Food)//如果蛇头吃到了食物
        {
            List.Add(List[List.Count - 1]);//在蛇的尾部添加蛇身
            ifFood = false;//没有食物
            BuildFood();//生成食物
            label.Text = Convert.ToString(Convert.ToInt32(label.Text) + 5);//显示当前分数
        }
    }

    /// <summary>
    /// 游戏是否失败
    /// </summary>
    /// <param GameP="Point">设置文本的显示位置</param>
    public bool GameAborted(Point GameP)
    {
        bool tem_b = false;//游戏是否结束
        bool tem_body = false;//记录蛇身是否重叠
        for (int i = 1; i < List.Count; i++)//遍历所有骨节
        {
            if (((Point)List[0]) == ((Point)List[i]))//如是骨节重叠
                tem_body = true;//游戏失败
        }
        //判断蛇头是否超出游戏场地
        if (GameP.X <= -20 || GameP.X >= control.Width - 1 || GameP.Y <= -20 || GameP.Y >= control.Height - 1 || tem_body)
        {
            //绘制游戏结束的提示文本
            g.DrawString("Game Over", new Font("宋体", 30, FontStyle.Bold), new SolidBrush(Color.DarkSlateGray), new PointF(150, 130));
            ifGame = true;//游戏结束
            timer.Stop();//停止记时器
            tem_b = true;
        }
        return tem_b;
    }

    /// <summary>
    /// 判断蛇是否向相反的方向移动
    /// </summary>
    /// <param Ep="Point">移动的下一步位置</param>
    public bool EstimateMove(Point Ep)
    {
        bool tem_bool = false;//记录蛇头是否向相反的方向移动
        if (Ep.X == ((Point)List[0]).X && Ep.Y == ((Point)List[0]).Y)//如果蛇头向相反的方向移动
            tem_bool = true;
        return tem_bool;
    }

    /// <summary>
    /// 重新绘制蛇身
    /// </summary>
    public void ProtractSnake(Point Ep)
    {
        bool tem_bool = false;//是否清除移动后的蛇身
        List.Insert(0, Ep);//根据蛇头的移动方向,设置蛇头的位置
        Point tem_point = ((Point)List[List.Count-1]);//记录蛇尾的位置
        List.RemoveAt(List.Count - 1);//移除蛇的尾部
        //使骨节向前移动一位
        for (int i = 0; i < List.Count - 1; i++)
        {
            if (tem_point == ((Point)List[i]))
                tem_bool = true;
        }
        if (!tem_bool)//清除贪吃蛇移动前的蛇尾部份
            g.FillRectangle(SolidD, tem_point.X + 1, tem_point.Y + 1, Condyle - 1, Condyle - 1);
        for (int i = 0; i < List.Count; i++)//重新绘制蛇身
        {
            g.FillRectangle(SolidB, ((Point)List[0]).X + 1, ((Point)List[0]).Y + 1, Condyle - 1, Condyle - 1);
        }
    }

    /// <summary>
    /// 生成食物
    /// </summary>
    public void BuildFood()
    {
        if (ifFood == false)//如果没有食物
        {
            Point tem_p = new Point(-1, -1);//定义坐标结构
            bool tem_bool = true;//是否计算出食物的合位置
            bool tem_b = false;//判断食物是否和蛇身重叠
            while (tem_bool)//计算食物的显示位置
            {
                tem_b = false;
                tem_p = RectFood();//随机生成食物的位置
                for (int i = 0; i < List.Count; i++)//遍历整个蛇身的位置
                {
                    if (((Point)List[i]) == tem_p)//如果食物是否和蛇身重叠
                    {
                        tem_b = true;//记录重叠
                        break;
                    }
                }
                if (tem_b == false)//如果没有重叠
                    tem_bool = false;//退出循环
            }
            Food = tem_p;//记录食物的显示位置
        }
        ifFood = true;//有食物
    }

    /// <summary>
    /// 随机生成食物的节点
    /// </summary>
    public Point RectFood()
    {
        int tem_W = Field_width / 20;//获取场地的行数
        int tem_H = Field_Height / 20;//获取场地的列数
        Random RandW = new Random();//实例化Random类
        tem_W=RandW.Next(0, tem_W - 1);//生成食物的横向坐标
        Random RandH = new Random();//实例化Random类
        tem_H = RandH.Next(0, tem_H - 1);//生成食物的纵向坐标
        Point tme_P = new Point(tem_W * Condyle, tem_H * Condyle);//生成食物的显示位置
        return tme_P;
    }

}

需要的再直接Call我下方卡片,直接发。

👉其他

📢作者:小空和小芝中的小空
📢转载说明-务必注明来源:https://zhima.blog.csdn.net/
📢这位道友请留步☁️,我观你气度不凡,谈吐间隐隐有王者霸气💚,日后定有一番大作为📝!!!旁边有点赞👍收藏🌟今日传你,点了吧,未来你成功☀️,我分文不取,若不成功⚡️,也好回来找我。

温馨提示点击下方卡片获取更多意想不到的资源。
空名先生

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/138056.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

云原生技术在离线交付场景中的实践

作者介绍&#xff1a;郭逊&#xff0c;交付部总监&#xff0c;7年运维经验&#xff0c;云原生深度爱好者软件产品只有交付到用户手中才有价值&#xff0c;本人在面向政府等 ToG 场景的软件交付领域具有数年的工作经验&#xff0c;深知其中痛点。今天借助这篇文章&#xff0c;分…

启动报名:首届“星河杯”隐私计算大赛正式上线

当前&#xff0c;隐私计算技术发展迅速&#xff0c;行业应用稳步增长&#xff0c;逐渐成为实现数据安全流通的关键技术路径之一。然而&#xff0c;隐私计算发展过程中仍面临技术应用瓶颈、行业影响有限等挑战&#xff0c;亟需加快技术攻关、提升行业影响、深化产业应用。在此背…

PyFlink1.16.0 使用说明:建表及连接Mysql数据库

PyFlink1.16.0 使用说明&#xff1a;建表及连接Mysql数据库引言安装运行环境PyFlink创建作业环境一、创建一个 Table API 批处理表环境二、创建一个 Table API 流处理表环境三、创建一个 DataStream API 数据流处理环境PyFlink建表一、从Python List对象创建一个 Table二、创建…

理解Cookie 和 Session 的工作流程

又是一年初,首先祝大家新年快乐!!!Cookie什么是Cookie?由于HTTP是一种无状态的协议, 服务器单从网络连接上是无法知道用户身份的. 这时候服务器就需要给客户端发一个cooki, 用来确认用户的身份.简单的来说, cookie就是客户端保存用户信息的一种机制, 用来记录用户的一些信息.找…

基于JAVA的数据可视化分析平台,自由制作任何您想要的数据看板,支持接入SQL、CSV、Excel、HTTP接口、JSON等

数据可视化分析平台 自由制作任何您想要的数据看板 简介 DataGear是一款数据可视化分析平台&#xff0c;自由制作任何您想要的数据看板&#xff0c;支持接入SQL、CSV、Excel、HTTP接口、JSON等多种数据源。 完整代码下载地址&#xff1a;基于JAVA的数据可视化分析平台&…

Python模块与包(八)

python学习之旅(八) &#x1f44d;查看更多可以关注查看首页或点击下方专栏目录 一.模块 (1) 什么是模块 一个Python文件,以.py 结尾,能定义函数,类和变量,也能包含可执行的代码 作用&#xff1a;我们可以认为不同的模块就是不同工具包,每一个工具包中都有各种不同的工具(如函…

Vue初识系列【2】

一 Vue入门 1.1 Vue简介 Vue 是一套用于构建用户界面的渐进式框架&#xff0c;发布于 2014 年 2 月。与其它大型框架不同的是&#xff0c;Vue 被设计为可以自底向上逐层应用。Vue 的核心库只关注视图层&#xff0c;不仅易于上手&#xff0c;还便于与第三方库&#xff08;如&a…

ZYNQ printk 缓冲区读取

之前调试kenel &#xff0c;如果kenenl崩溃会&#xff0c;通过内核system.map定位log_buf变量地址&#xff0c;给cpu复位&#xff0c;在u-boot中读取对应的物理地址&#xff0c;即可知道最终内核崩溃最后打出的消息。 我在使用 5.4.154这个内核版本&#xff0c;中没有log_buf这…

金蝶附件上传接口开发思路

1️⃣需求描述&#xff1a;需要通过调用金蝶API接口实现指定单据的附件上传。本文以收料通知单为例&#xff0c;以Java代码示例进行讲解。 tips&#xff1a;阅读本文开始前&#xff0c;希望你是一名开发者同时阅读过&#xff1a; https://vip.kingdee.com/article/872325739310…

【小知识点】为爬虫训练场项目添加 Bootstrap5 时间轴

爬虫训练场建站时间轴&#xff1a;https://pachong.vip/timeline 背景 为了便于记录爬虫训练场项目更新日志&#xff0c;所以集成该功能&#xff0c;实现效果如下所示。 特别备注一下&#xff0c;时间轴是什么&#xff1f; 时间轴是一种常用的网站布局元素&#xff0c;通常用…

Forexclub:特斯拉四季度交付车辆创纪录,你认为2023年特斯拉销量如何

周一特斯拉宣布其2022年第四季度交付了创纪录的405278辆汽车。这一数字创下了该公司的纪录&#xff0c;但低于华尔街的估计。据报道&#xff0c;报告中对交付量的普遍估计为420760。特斯拉称&#xff1a;“2022年&#xff0c;汽车交付量同比增长40%&#xff0c;达到131万辆。”…

基于Vue和SpringBoot的论文检测系统的设计与实现

作者主页&#xff1a;Designer 小郑 作者简介&#xff1a;Java全栈软件工程师一枚&#xff0c;来自浙江宁波&#xff0c;负责开发管理公司OA项目&#xff0c;专注软件前后端开发&#xff08;Vue、SpringBoot和微信小程序&#xff09;、系统定制、远程技术指导。CSDN学院、蓝桥云…

【信息论与编码 沈连丰】第七章:信息率失真理论及其应用

【信息论与编码 沈连丰】第七章&#xff1a;信息率失真理论及其应用第七章 信息率失真理论及其应用7.1 失真函数和平均失真度7.2 信息率失真函数7.3 信息率失真函数R(D)的计算7.4 保真度准则下的信源编码定理7.5 信息率失真函数与信息价值第七章 信息率失真理论及其应用 香农第…

网工、运维必备的免费在线画图工具,真的很好用!

都说一图胜千言&#xff0c;一个IT工程师如果能画的一手好图&#xff0c;无论是在方案选项、还是技术交流&#xff0c;都能快速表达自己的想法&#xff0c;让你的思路更加的直观明了&#xff1b;市面上的制图工具有很多&#xff0c;下面就推荐几款好用且免费的工具&#xff0c;…

SaaS服务最大的优势是哪些?(附免费试用)

SaaS服务十大优势 近些年来&#xff0c;SaaS&#xff08;Software-as-a-Service&#xff09;成为整个IT领域中最受欢迎的业务模型之一。由于SaaS的市场每年以近60&#xff05;的速度增长&#xff0c;因此它正在取代更传统的应用市场&#xff0c;并将在未来几年内成为主导模式。…

【非侵入式负载监测】低采样率电动汽车充电的无训练非侵入式负载监测(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

一、TTY子系统介绍

个人主页&#xff1a;董哥聊技术我是董哥&#xff0c;嵌入式领域新星创作者创作理念&#xff1a;专注分享高质量嵌入式文章&#xff0c;让大家读有所得&#xff01;文章目录1、TTY介绍2、控制台终端2.1 系统控制台2.2 当前控制台2.3 虚拟控制台3、伪终端4、串口终端5. 其它类型…

《移动安全》(10)Frida第一篇之环境搭建

0x00 前言 Frida是一款轻量级HOOK框架&#xff0c;我们在电脑上安装Frida环境后&#xff0c;还需要将frida-server上传到目标机器上运行&#xff08;需要Root&#xff09;&#xff0c;通过它来注入进程完成hook操作。本文主要讲述Frida环境的搭建。 0x01 Frida环境搭建 &…

node.js创建网站实例3

node.js访问mysql数据库并把查询结果返回给前端 1.cmd中运行&#xff1a;npm install mysql -s 2.修改api.js的代码 // 1.先引入express模块&#xff0c;express是一个函数 var express require("express") //2.执行express函数&#xff1b;用变量接收express函数…

概论_第2章_重点_随机变量函数的概率分布___定理法和分布函数法的应用

一 定义 概括地说&#xff1a; 随机变量Y是随机变量X的函数。 设g(x) 是一给定的连续函数&#xff0c; 称Yg(X) 为随机变量X的一个函数&#xff0c; Y也是一个随机变量。当X取值 时&#xff0c;Y取值 . ~~~~~~~~~~~~~~ 本文讨论连续型随机变量函数。 定理1: 设X为连续型…