weixin_43691257 2019-06-27 10:23 采纳率: 100%
浏览 310
已采纳

C#WINform的贪吃蛇程序,请问如何改初始长度?

代码是网上找的,我没找到设置初始长度的代码,能帮我看下吗

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1 //蛇身初始状态,吃的算法
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void Form1_Load(object sender, EventArgs e)
    {

    }
    public class 蛇
    {
        public static int D;
        public static int s=2;
    }
    void Add_food()       //添加食物
    {
        Label label = new Label();
        label.Name = "Lab" + 蛇.s;
        label.BackColor = System.Drawing.Color.Red;
        label.Size = new System.Drawing.Size(10, 10);
        label.Margin = new System.Windows.Forms.Padding(0);
        label.ForeColor = System.Drawing.Color.Red;
        label.AutoSize = false;
        label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        Random rd = new Random();
    tag1:
        label.Location = new System.Drawing.Point(rd.Next(0, 30) * 10, rd.Next(0, 30) * 10);
    for (int i = 1; i < 蛇.s; i++)
        {
            Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; if (label.Location == lo.Location) goto tag1;
        }

        this.Controls.Add(label);
        label.SendToBack();
        panel1.SendToBack();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Add_food();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (蛇.D == 1) { System.Windows.Forms.SendKeys.Send("{S}"); return; }
        if (蛇.D == 2) { System.Windows.Forms.SendKeys.Send("{W}"); return; }
        if (蛇.D == 3) { System.Windows.Forms.SendKeys.Send("{A}"); return; }
        if (蛇.D == 4) { System.Windows.Forms.SendKeys.Send("{D}"); return; };
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.S && 蛇.D != 2)
        {
            timer1.Enabled = false;
            蛇.D = 1;
            Label lb = (Label)this.Controls.Find("Lab" + 蛇.s, true)[0];
            Label lbl = (Label)this.Controls.Find("Lab" + (蛇.s - 1), true)[0];
            Point[,] Lo = new Point[101, 101];
            for (int i = 1; i <= 蛇.s; i++)
            {
                Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; Lo[i, i] = new Point(lo.Location.X, lo.Location.Y);
            }
            Label le = new Label();
            le.Visible = false;

            if (e.KeyCode == Keys.S && 蛇.s > 2 && lbl.BackColor != System.Drawing.Color.Red && lab1.Location.Y != 290)
            {
                for (int i = 2; i < 蛇.s; i++)
                {
                    Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; lo.Location = Lo[i - 1, i - 1];
                }
            }
            if (lb.Location == new System.Drawing.Point(lab1.Location.X, lab1.Location.Y + 10))
            {
                lb.BackColor = System.Drawing.Color.Black;
                le.Location = new System.Drawing.Point(lab1.Location.X, lab1.Location.Y);
                lab1.Location = new System.Drawing.Point(lab1.Location.X, lab1.Location.Y + 10);
                lb.Location = le.Location;
                this.Controls.Remove(le);
                蛇.s++;
                Add_food();
                timer1.Enabled = true;
                return;
            }
            for (int i = 1; i < 蛇.s; i++)
            {
                Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; if (lo.Location == new System.Drawing.Point(lab1.Location.X, lab1.Location.Y + 10)) { timer1.Enabled = false; MessageBox.Show("游戏结束!", "提示"); return; }; ;
            }

            if (e.KeyCode == Keys.S && lab1.Location.Y == 290) 
            { 
                timer1.Enabled = false;
                MessageBox.Show("游戏结束,本次得分为:" + Convert.ToString(蛇.s-1), "提示");
                Application.Exit();
            };
                lab1.Top = lab1.Top + 10;
                timer1.Enabled = true;
            }
         if (e.KeyCode == Keys.W && 蛇.D != 1)
        {
            timer1.Enabled = false;
            蛇.D = 2;
            Label lb = (Label)this.Controls.Find("Lab" + 蛇.s, true)[0];
            Label lbl = (Label)this.Controls.Find("Lab" + (蛇.s - 1), true)[0];
            Point[,] Lo = new Point[101, 101];
            for (int i = 1; i <= 蛇.s; i++)
            {
                Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; Lo[i, i] = new Point(lo.Location.X, lo.Location.Y);
            }
            Label le = new Label();
            le.Visible = false;

        if (e.KeyCode == Keys.W && 蛇.s > 2 && lbl.BackColor != System.Drawing.Color.Red && lab1.Location.Y != 0)
            {
                for (int i = 2; i < 蛇.s; i++)
                {
                    Label lo = (Label)this.Controls.Find("lab" + i, true)[0]; lo.Location = Lo[i - 1, i - 1];
                }
            }
            if (lb.Location == new System.Drawing.Point(lab1.Location.X, lab1.Location.Y - 10))
            {
                lb.BackColor = System.Drawing.Color.Black;
                le.Location = new System.Drawing.Point(lab1.Location.X, lab1.Location.Y);
                lab1.Location = new System.Drawing.Point(lab1.Location.X, lab1.Location.Y - 10);
                lb.Location = le.Location;
                this.Controls.Remove(le);
                蛇.s++;
                Add_food();
                timer1.Enabled = true;
                return;
            }
            for (int i = 1; i < 蛇.s; i++)
            {
                Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; 
                if (lo.Location == new System.Drawing.Point(lab1.Location.X, lab1.Location.Y - 10))
                { 
                    timer1.Enabled = false;
                    MessageBox.Show("游戏结束,本次得分为:" + Convert.ToString(蛇.s-1), "提示");
                    Application.Exit();
                }; 
            }

            if (e.KeyCode == Keys.W && lab1.Location.Y == 0) 
            { 
                timer1.Enabled = false;
                MessageBox.Show("游戏结束,本次得分为:" + Convert.ToString(蛇.s-1), "提示");
                Application.Exit();
            };
            lab1.Top = lab1.Top - 10;
            timer1.Enabled = true;
            }
           if (e.KeyCode == Keys.A && 蛇.D != 4)
           {
                timer1.Enabled = false;
                蛇.D = 3;
                Label lb = (Label)this.Controls.Find("Lab" + 蛇.s, true)[0];
                Label lbl = (Label)this.Controls.Find("Lab" + (蛇.s - 1), true)[0];
                Point[,] Lo = new Point[101, 101];
                for (int i = 1; i <= 蛇.s; i++)
                {
                    Label lo = (Label)this.Controls.Find("Lab" + i, true)[0];
                    Lo[i, i] = new Point(lo.Location.X, lo.Location.Y);
                 }
                Label le = new Label();
                le.Visible = false;

            if (e.KeyCode == Keys.A && 蛇.s > 2 && lbl.BackColor != System.Drawing.Color.Red && lab1.Location.X != 0)
            {
                for (int i = 2; i < 蛇.s; i++)
                {
                    Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; lo.Location = Lo[i - 1, i - 1];
                }
            }
            if (lb.Location == new System.Drawing.Point(lab1.Location.X - 10, lab1.Location.Y))
            {
                lb.BackColor = System.Drawing.Color.Black;
                le.Location = new System.Drawing.Point(lab1.Location.X, lab1.Location.Y);
                lab1.Location = new System.Drawing.Point(lab1.Location.X - 10, lab1.Location.Y);
                lb.Location = le.Location;
                this.Controls.Remove(le);
                蛇.s++;
                Add_food();
                timer1.Enabled = true;
                return;
            }
            for (int i = 1; i < 蛇.s; i++)
            {
                Label lo = (Label)this.Controls.Find("Lab" + i, true)[0];
                if (lo.Location == new System.Drawing.Point(lab1.Location.X - 10, lab1.Location.Y)) 
                { 
                    timer1.Enabled = false;
                    MessageBox.Show("游戏结束,本次得分为:" + Convert.ToString(蛇.s-1), "提示");
                    Application.Exit();
                }; 
            }

                if (e.KeyCode == Keys.A && lab1.Location.X == 0) 
                { 
                    timer1.Enabled = false; MessageBox.Show("游戏结束,本次得分为:"+Convert.ToString(蛇.s-1), "提示");
                    Application.Exit();
                 };
                 lab1.Left = lab1.Left - 10;
                 timer1.Enabled = true;
            }
        if (e.KeyCode == Keys.D && 蛇.D != 3)
        {
            timer1.Enabled = false;
            蛇.D = 4;
            Label lb = (Label)this.Controls.Find("Lab" + 蛇.s, true)[0];
            Label lbl = (Label)this.Controls.Find("Lab" + (蛇.s - 1), true)[0];
            Point[,] Lo = new Point[101, 101];
            for (int i = 1; i <= 蛇.s; i++)         
            {
                Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; Lo[i, i] = new Point(lo.Location.X, lo.Location.Y);
            }
            Label le = new Label();
            le.Visible = false;

            if (e.KeyCode == Keys.D && 蛇.s > 2 && lbl.BackColor != System.Drawing.Color.Red && lab1.Location.X != 290)
            {
                for (int i = 2; i < 蛇.s; i++)
                {
                    Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; lo.Location = Lo[i - 1, i - 1];
                }
            }
            if (lb.Location == new System.Drawing.Point(lab1.Location.X + 10, lab1.Location.Y))
            {
                lb.BackColor = System.Drawing.Color.Black;
                le.Location = new System.Drawing.Point(lab1.Location.X, lab1.Location.Y);
                lab1.Location = new System.Drawing.Point(lab1.Location.X + 10, lab1.Location.Y);
                lb.Location = le.Location;
                this.Controls.Remove(le);
                蛇.s++;
                Add_food();
                timer1.Enabled = true;
                return;
            }
            for (int i = 1; i < 蛇.s; i++)
            {
                Label lo = (Label)this.Controls.Find("Lab" + i, true)[0]; if (lo.Location == new System.Drawing.Point(lab1.Location.X + 10, lab1.Location.Y)) { timer1.Enabled = false; MessageBox.Show("游戏结束!", "提示"); return; }; ;
            }

            if (e.KeyCode == Keys.D && lab1.Location.X == 290)
            { 
                timer1.Enabled = false; 
                MessageBox.Show("游戏结束,本次得分为:"+Convert.ToString(蛇.s-1), "提示");
                Application.Exit();
            };
            lab1.Left = lab1.Left + 10;
            timer1.Enabled = true;
        }
    }

    private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Add_food();
    }

    private void 简单ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        timer1.Interval = 150;
    }

    private void 困难ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        timer1.Interval = 20;
    }

    private void 一般ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        timer1.Interval = 80;
    }

    private void 暂停ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(1000);
        MessageBox.Show("继续?", "");
    }

    private void 结束ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}

}

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-06-27 12:10
    关注

    蛇.s 就是长度,你修改下试试看

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误