广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C#游戏开发之实现贪吃蛇游戏
  • 402
分享到

C#游戏开发之实现贪吃蛇游戏

C#实现贪吃蛇游戏C#贪吃蛇游戏C#贪吃蛇 2023-01-04 12:01:30 402人浏览 薄情痞子
摘要

目录实践过程效果代码实践过程 效果 代码 public partial class FORM1 : Form { public Form1() {

实践过程

效果

代码

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;
}

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;
    }

}

到此这篇关于C#游戏开发之实现贪吃蛇游戏的文章就介绍到这了,更多相关C#贪吃蛇游戏内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C#游戏开发之实现贪吃蛇游戏

本文链接: https://www.lsjlt.com/news/176624.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C#游戏开发之实现贪吃蛇游戏
    目录实践过程效果代码实践过程 效果 代码 public partial class Form1 : Form { public Form1() { ...
    99+
    2023-01-04
    C#实现贪吃蛇游戏 C#贪吃蛇游戏 C#贪吃蛇
  • C#实现贪吃蛇小游戏
    本文实例为大家分享了C#实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 之前一直想写一个贪吃蛇小游戏,上个周末终于有时间做了一个,现在和大家分享。 界面 界面比较简单,一个按钮...
    99+
    2022-11-13
  • C语言实现贪吃蛇小游戏开发
    本文实例为大家分享了C语言实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 程序介绍 代码 #include<stdafx.h>            //vc自带...
    99+
    2022-11-13
    C语言 贪吃蛇
  • Android开发之经典游戏贪吃蛇
    前言 这款游戏实现的思路和源码参考了Google自带的Snake的例子,其中修改了一些个人认为还不够完善的地方,加入了一些新的功能,比如屏幕上的方向操作盘,暂停按钮,开始按钮,...
    99+
    2022-06-06
    android开发 贪吃蛇 Android
  • C++实现简易贪吃蛇游戏
    C++实现建议贪吃蛇(不会闪屏幕) 使用vs2013完成。记录踏上游戏开发的道路。 效果图 代码 // 2021.7.24.1贪吃蛇.cpp : 定义控制台应用程序的入口点。 ...
    99+
    2022-11-12
  • 利用C/C++实现贪吃蛇游戏
    利用C/C++实现贪吃蛇 (注意:本文章仅供参考,第一次写博客还请多多指教。理解本文章需要easyx和c++等基础知识,并且需要了解贪吃蛇游戏机制) 贪吃蛇机制介绍 相信绝大多数人都...
    99+
    2022-11-12
  • python实现贪吃蛇游戏
    文章目录 1、效果2、实现过程3、代码 1、效果 2、实现过程 导入 Pygame 和 random 模块。初始化 Pygame。设置游戏界面大小、背景颜色和游戏标题。定义颜色常量。...
    99+
    2023-09-29
    python 游戏 pygame
  • Java实现贪吃蛇游戏
    下面是一个简单的Java实现贪吃蛇游戏的示例代码:```javaimport javax.swing.*;import java.a...
    99+
    2023-08-09
    Java
  • JavaScript实现贪吃蛇游戏
    本文实例为大家分享了JavaScript实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下 通过JavaScript,我们可以实现贪吃蛇游戏,具体功能如下: (1)通过按上下左右键来...
    99+
    2022-11-12
  • QT实现贪吃蛇游戏
    为了熟悉QT的相关知识,我用了大约8个小时的时间用QT再次写了一遍贪吃蛇。 因为QT的机制和平时写的程序流程不同,所以程序中可能没有遵守代码规范。 运行效果: 程序内除了实现贪吃蛇...
    99+
    2022-11-12
  • pygame实现贪吃蛇游戏
    本文实例为大家分享了pygame实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下 为了简化起见,游戏素材暂定为两张简单的图片(文中用的是30*30)。大家很方便就能制作。 背景也...
    99+
    2022-11-12
  • C++实现简单贪吃蛇小游戏
    本文实例为大家分享了C++实现简单贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 1 贪吃蛇游戏原理 1.1 构造蛇身:定义一个坐标数组,存放的是蛇的每一节蛇身所在的坐标位置。 1...
    99+
    2022-11-12
  • 怎么用C++实现贪吃蛇游戏
    这篇文章给大家分享的是有关怎么用C++实现贪吃蛇游戏的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。1976年,Gremlin平台推出了一款经典街机游戏Blockade。游戏中,两名玩家分别控制一个角色在屏幕上移动...
    99+
    2023-06-25
  • 用JS实现贪吃蛇游戏
    本文实例为大家分享了JS实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下 效果图: 完整代码如下: html: <!DOCTYPE html> <html la...
    99+
    2022-11-13
  • JS实现贪吃蛇小游戏
    目录一、初始化结构二、渲染蛇的颜色 三、蛇的运动四、蛇死亡的判定方式 蛇有两种判定死亡的方式五、食物的创建六、蛇吃食物边长七、开始游戏功能八、暂停/继续游戏功能页面效果: 贪吃蛇游...
    99+
    2022-11-12
  • Python turtle实现贪吃蛇游戏
    本文实例为大家分享了Python turtle实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下 # Simple Snake Game in Python 3 for Beginners import tu...
    99+
    2022-06-02
    python turtle 贪吃蛇
  • pygame实现贪吃蛇小游戏
    本文实例为大家分享了pygame实现贪吃蛇小游戏的具体代码,供大家参考,具体内容如下 由于这段时间实在是太聊了,没什么事做,游戏也玩腻了,所以玩起来pygame。pygame是真的容...
    99+
    2022-11-12
  • Android 2d游戏开发之贪吃蛇基于surfaceview
    前两个游戏是基于View游戏框架的,View游戏框架只适合做静止的,异步触发的游戏,如果做一直在动的游戏,View的效率就不高了,我们需要一种同步触发的游戏框架,也就是surface...
    99+
    2022-11-12
  • C++入门指南之贪吃蛇游戏的实现
    目录参考贪吃蛇游戏程序框架绘制游戏地图和蛇小蛇向右移动控制小蛇4个方向移动时间控制的改进失败判断与显示添加食物完整代码总结参考 《C和C++游戏趣味编程》 贪...
    99+
    2022-11-12
  • 如何用Silverlight开发贪吃蛇游戏
    今天就跟大家聊聊有关如何用Silverlight开发贪吃蛇游戏,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。介绍使用 Silverlight 3.0(c#) 开发一个贪吃蛇...
    99+
    2023-06-17
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作