C# 如何从随机图片框中计算点数

C# 如何从随机图片框中计算点数,c#,picturebox,C#,Picturebox,第一:我在学校学习C#,我正在为一个学校项目做这个游戏。我真的需要帮助 我必须为snake编程,但我遇到了一个问题 它在田间随机生成苹果。每次当我与蛇和苹果发生碰撞时,我的点数都是错误的 例如: 我带着我的蛇向6号苹果跑去。它拿走了苹果,但它把数字9算到了我的分数上。如果我再拿一个数字为6的苹果,我的点数是3(随机) 公共部分类表单1:表单 { List PicAepfel=新列表(); PictureBox picApfel1=新PictureBox(); PictureBox picApfe

第一:我在学校学习C#,我正在为一个学校项目做这个游戏。我真的需要帮助

我必须为snake编程,但我遇到了一个问题

它在田间随机生成苹果。每次当我与蛇和苹果发生碰撞时,我的点数都是错误的

例如: 我带着我的蛇向6号苹果跑去。它拿走了苹果,但它把数字9算到了我的分数上。如果我再拿一个数字为6的苹果,我的点数是3(随机)

公共部分类表单1:表单
{
List PicAepfel=新列表();
PictureBox picApfel1=新PictureBox();
PictureBox picApfel2=新PictureBox();
PictureBox picApfel3=新PictureBox();
PictureBox picApfel4=新PictureBox();
PictureBox picApfel5=新PictureBox();
PictureBox picApfel6=新PictureBox();
PictureBox picApfel7=新PictureBox();
PictureBox picApfel8=新PictureBox();
PictureBox picApfel9=新PictureBox();
PictureBox picApfel10=新PictureBox();
List bilderAepfel=新列表();
私有int xrichtong=5;
私家车总里程=0;
私人整数倒计时=20;
私有int punkte=0;
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
picapfel.Add(picApfel1);
picapfel.Add(picApfel2);
PicAepfel.Add(picApfel3);
picapfel.Add(picApfel4);
picapfel.Add(picApfel5);
picapfel.Add(picApfel6);
Picapfel.Add(picApfel7);
Picapfel.Add(picApfel8);
Picapfel.Add(picApfel9);
generiereAepfel();
}
私有void btnStart_单击(对象发送方,事件参数e)
{
tmrSpiel.Start();
tmrCountdown.Start();
lblAktion.Text=“Sammle innerhalb von 20 Sekunden die meisten Punkte”;
}
私有void tmrSpiel_Tick(对象发送方,事件参数e)
{
lblAktion.Text=“Los!”;
picSchlangenkopf.Location=新点(picSchlangenkopf.Location.X+xrichtong,picSchlangenkopf.Location.Y+YRichtung);
nehmeAepfelAuf();
if(picSchlangenkopf.Location.X>=pnlSpielfeld.Width-picSchlangenkopf.Width)
{
tmrSpiel.Stop();
tmrCountdown.Stop();
lblAktion.Text=“Spiel vorbei!”;
}
else if(picSchlangenkopf.Location.X<0)
{
tmrSpiel.Stop();
tmrCountdown.Stop();
lblAktion.Text=“Spiel vorbei!”;
}
else if(picSchlangenkopf.Location.Y<0)
{
tmrSpiel.Stop();
tmrCountdown.Stop();
lblAktion.Text=“Spiel vorbei!”;
}
else if(picSchlangenkopf.Location.Y>=pnlSpielfeld.Height-picSchlangenkopf.Height)
{
tmrSpiel.Stop();
tmrCountdown.Stop();
lblAktion.Text=“Spiel vorbei!”;
}
}
受保护的覆盖布尔ProcessDialogKey(Keys keyData)
{
if(keyData==Keys.Up)
{
xrichtong=0;
YRichtung=-5;
返回true;
}
else if(keyData==Keys.Down)
{
xrichtong=0;
YRichtung=5;
返回true;
}
else if(keyData==Keys.Right)
{
xrichtong=5;
YRichtung=0;
返回true;
}
else if(keyData==Keys.Left)
{
xrichtong=-5;
YRichtung=0;
返回true;
}
返回base.ProcessDialogKey(keyData);
}
私有void btnReset_单击(对象发送者,事件参数e)
{
pnlSpielfeld.Controls.Clear();
pnlSpielfeld.Controls.Add(picSchlangenkopf);
picSchlangenkopf.位置=新点(306173);
lblAktion.Text=“Sammle innerhalb von 20 Sekunden die meisten Punkte”;
倒计时=20;
txtCountdown.Text=”“+倒计时;
picSchlangenkopf.位置=新点(306173);
generiereAepfel();
t平均间隔=100;
punkte=0;
txtPunkte.Text=”“+punkte;
tmrSpiel.Stop();
tmrCountdown.Stop();
}
私有void tmrCountdown_Tick_1(对象发送方,事件参数e)
{
倒计时--;
txtCountdown.Text=”“+倒计时;
如果(倒计时==0)
{
tmrSpiel.Stop();
tmrCountdown.Stop();
lblAktion.Text=“Deine Zeit is abgelaufen”;
}
}
public void generiereAepfel()
{
随机rnd=新随机();
Random zahl=新的Random();
foreach(picApfel中的var picApfel)
{
对于(int i=0;i<10;i++)
{
int r=rnd.Next(0,9);
picApfel.Location=新点(rnd.Next(8600),rnd.Next(5450));
picApfel.Name=$“picApfel{r}”;
picApfel.尺寸=新尺寸(26,26);
picApfel.BackgroundImageLayout=ImageLayout.Stretch//https://stackoverflow.com/questions/21959490/picturebox-backgroundimagelayout-change-fails
开关(picApfel.Name)
{
“皮卡菲尔”案:
picApfel.BackgroundImage=Properties.Resources.Apfel1;
打破
案例“picApfel2”:
 public partial class Form1 : Form
    {

        List<PictureBox> PicAepfel = new List<PictureBox>();
        PictureBox picApfel1 = new PictureBox();
        PictureBox picApfel2 = new PictureBox();
        PictureBox picApfel3 = new PictureBox();
        PictureBox picApfel4 = new PictureBox();
        PictureBox picApfel5 = new PictureBox();
        PictureBox picApfel6 = new PictureBox();
        PictureBox picApfel7 = new PictureBox();
        PictureBox picApfel8 = new PictureBox();
        PictureBox picApfel9 = new PictureBox();
        PictureBox picApfel10 = new PictureBox();

        List<Bitmap> bilderAepfel = new List<Bitmap>();


        private int XRichtung = 5;
        private int YRichtung = 0;
        private int countdown = 20;
        private int punkte = 0;



        public Form1()
        {
            InitializeComponent();

        }


        private void Form1_Load(object sender, EventArgs e)
        {
            PicAepfel.Add(picApfel1);
            PicAepfel.Add(picApfel2);
            PicAepfel.Add(picApfel3);
            PicAepfel.Add(picApfel4);
            PicAepfel.Add(picApfel5);
            PicAepfel.Add(picApfel6);
            PicAepfel.Add(picApfel7);
            PicAepfel.Add(picApfel8);
            PicAepfel.Add(picApfel9);

            generiereAepfel();
        }


        private void btnStart_Click(object sender, EventArgs e)
        {
            tmrSpiel.Start();
            tmrCountdown.Start();
            lblAktion.Text = "Sammle innerhalb von 20 Sekunden die meisten Punkte";

        }


        private void tmrSpiel_Tick(object sender, EventArgs e)
        {

            lblAktion.Text = "Los Los Los!";
            picSchlangenkopf.Location = new Point(picSchlangenkopf.Location.X + XRichtung, picSchlangenkopf.Location.Y + YRichtung);
            nehmeAepfelAuf();


            if (picSchlangenkopf.Location.X >= pnlSpielfeld.Width - picSchlangenkopf.Width)
            {
                tmrSpiel.Stop();
                tmrCountdown.Stop();
                lblAktion.Text = "Spiel vorbei!";

            }
            else if (picSchlangenkopf.Location.X < 0)
            {
                tmrSpiel.Stop();
                tmrCountdown.Stop();
                lblAktion.Text = "Spiel vorbei!";
            }
            else if (picSchlangenkopf.Location.Y < 0)
            {
                tmrSpiel.Stop();
                tmrCountdown.Stop();
                lblAktion.Text = "Spiel vorbei!";
            }
            else if (picSchlangenkopf.Location.Y >= pnlSpielfeld.Height - picSchlangenkopf.Height)
            {
                tmrSpiel.Stop();
                tmrCountdown.Stop();
                lblAktion.Text = "Spiel vorbei!";
            }



        }
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Up)
            {
                XRichtung = 0;
                YRichtung = -5;
                return true;
            }
            else if (keyData == Keys.Down)
            {
                XRichtung = 0;
                YRichtung = 5;
                return true;
            }
            else if (keyData == Keys.Right)
            {
                XRichtung = 5;
                YRichtung = 0;
                return true;
            }
            else if (keyData == Keys.Left)
            {
                XRichtung = -5;
                YRichtung = 0;
                return true;
            }
            return base.ProcessDialogKey(keyData);
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            pnlSpielfeld.Controls.Clear();
            pnlSpielfeld.Controls.Add(picSchlangenkopf);
            picSchlangenkopf.Location = new Point(306, 173);
            lblAktion.Text = "Sammle innerhalb von 20 Sekunden die meisten Punkte";
            countdown = 20;
            txtCountdown.Text = "" + countdown;
            picSchlangenkopf.Location = new Point(306, 173);
            generiereAepfel();
            tmrSpiel.Interval = 100;
            punkte = 0;
            txtPunkte.Text = "" + punkte;
            tmrSpiel.Stop();
            tmrCountdown.Stop();



        }


        private void tmrCountdown_Tick_1(object sender, EventArgs e)
        {
            countdown--;
            txtCountdown.Text = "" + countdown;

            if(countdown == 0)
            {
                tmrSpiel.Stop();
                tmrCountdown.Stop();
                lblAktion.Text = "Deine Zeit ist abgelaufen";
            }
        }

        public void generiereAepfel()
        {

            Random rnd = new Random();
            Random zahl = new Random();

            foreach (var picApfel in PicAepfel)
            {
                for (int i = 0; i < 10; i++)
                {
                int r = rnd.Next(0, 9);

                picApfel.Location = new Point(rnd.Next(8, 600), rnd.Next(5, 450));
                picApfel.Name = $"picApfel{r}";



                picApfel.Size = new Size(26, 26);
                picApfel.BackgroundImageLayout = ImageLayout.Stretch; //https://stackoverflow.com/questions/21959490/picturebox-backgroundimagelayout-change-fails


                    switch (picApfel.Name)
                    {
                        case "picApfel":
                            picApfel.BackgroundImage = Properties.Resources.Apfel1;
                            break;
                        case "picApfel2":
                            picApfel.BackgroundImage = Properties.Resources.Apfel2;
                            break;
                        case "picApfel3":
                            picApfel.BackgroundImage = Properties.Resources.Apfel3;
                            break;
                        case "picApfel4":
                            picApfel.BackgroundImage = Properties.Resources.Apfel4;
                            break;
                        case "picApfel5":
                            picApfel.BackgroundImage = Properties.Resources.Apfel5;
                            break;
                        case "picApfel6":
                            picApfel.BackgroundImage = Properties.Resources.Apfel6;
                            break;
                        case "picApfel7":
                            picApfel.BackgroundImage = Properties.Resources.Apfel7;
                            break;
                        case "picApfel8":
                            picApfel.BackgroundImage = Properties.Resources.Apfel8;
                            break;
                        case "picApfel9":
                            picApfel.BackgroundImage = Properties.Resources.Apfel9;
                            break;
                    }
                }
                pnlSpielfeld.Controls.Add(picApfel);
            }



            /* for (int i = 0; i < 11; i++)
             {
                 Random rnd = new Random();
                 int RandomZahlBild = rnd.Next(1, 9);
                 picApfel.Location = new Point(rnd.Next(8, 420), rnd.Next(5, 250));
                 picApfel = new PictureBox();
                 picApfel.Size = new Size(26, 26);
                 picApfel.BackgroundImageLayout = ImageLayout.Stretch;

                 for(int k = 0; k < 11; k++)
                 {

                     int test = rnd.Next(1, 9);
                     picApfel.Name = $"picApfel{test}";
                     pnlSpielfeld.Controls.Add(picApfel);
                     picApfel.BackgroundImage = Properties.Resources.Apfel1;


                 }
             }*/

            /*
            for (int i = 0; i < 10; i++)
            {
                int r = rnd.Next(PicAepfel.Count);
                pnlSpielfeld.Controls.Add(PicAepfel[r]);
            }*/




            /* Random rnd = new Random();
                 int RandomZahlBild = rnd.Next(1, 9);
                 picApfel1.BackgroundImage = Properties.Resources.Apfel1;
                 pnlSpielfeld.Controls.Add(picApfel1);
                 picApfel1.Location = new Point(rnd.Next(8, 420), rnd.Next(5, 250));
                 picApfel1.Size = new Size(26, 26);
                 picApfel1.BackgroundImageLayout = ImageLayout.Stretch;

                 picApfel2.BackgroundImage = Properties.Resources.Apfel2;
                 pnlSpielfeld.Controls.Add(picApfel2);
                 picApfel2.Location = new Point(rnd.Next(8, 420), rnd.Next(5, 250));
                 picApfel2.Size = new Size(26, 26);
                 picApfel2.BackgroundImageLayout = ImageLayout.Stretch;

                 picApfel3.BackgroundImage = Properties.Resources.Apfel3;
                 pnlSpielfeld.Controls.Add(picApfel3);
                 picApfel3.Location = new Point(rnd.Next(8, 420), rnd.Next(5, 250));
                 picApfel3.Size = new Size(26, 26);
                 picApfel3.BackgroundImageLayout = ImageLayout.Stretch;

                 picApfel4.BackgroundImage = Properties.Resources.Apfel4;
                 pnlSpielfeld.Controls.Add(picApfel4);
                 picApfel4.Location = new Point(rnd.Next(8, 420), rnd.Next(5, 250));
                 picApfel4.Size = new Size(26, 26);
                 picApfel4.BackgroundImageLayout = ImageLayout.Stretch;

                 picApfel5.BackgroundImage = Properties.Resources.Apfel5;
                 pnlSpielfeld.Controls.Add(picApfel5);
                 picApfel5.Location = new Point(rnd.Next(8, 420), rnd.Next(5, 250));
                 picApfel5.Size = new Size(26, 26);
                 picApfel5.BackgroundImageLayout = ImageLayout.Stretch;

                 picApfel6.BackgroundImage = Properties.Resources.Apfel6;
                 pnlSpielfeld.Controls.Add(picApfel6);
                 picApfel6.Location = new Point(rnd.Next(8, 420), rnd.Next(5, 250));
                 picApfel6.Size = new Size(26, 26);
                 picApfel6.BackgroundImageLayout = ImageLayout.Stretch;

                 picApfel7.BackgroundImage = Properties.Resources.Apfel7;
                 pnlSpielfeld.Controls.Add(picApfel7);
                 picApfel7.Location = new Point(rnd.Next(8, 420), rnd.Next(5, 250));
                 picApfel7.Size = new Size(26, 26);
                 picApfel7.BackgroundImageLayout = ImageLayout.Stretch;

                 picApfel8.BackgroundImage = Properties.Resources.Apfel8;
                 pnlSpielfeld.Controls.Add(picApfel8);
                 picApfel8.Location = new Point(rnd.Next(8, 420), rnd.Next(5, 250));
                 picApfel8.Size = new Size(26, 26);
                 picApfel8.BackgroundImageLayout = ImageLayout.Stretch;

                 picApfel9.BackgroundImage = Properties.Resources.Apfel9;
                 pnlSpielfeld.Controls.Add(picApfel9);
                 picApfel9.Location = new Point(rnd.Next(8, 420), rnd.Next(5, 250));
                 picApfel9.Size = new Size(26, 26);
                 picApfel9.BackgroundImageLayout = ImageLayout.Stretch;

                 picApfel10.BackgroundImage = Properties.Resources.Apfel10;
                 pnlSpielfeld.Controls.Add(picApfel10);
                 picApfel10.Location = new Point(rnd.Next(8, 420), rnd.Next(5, 250));
                 picApfel10.Size = new Size(26, 26);
                 picApfel10.BackgroundImageLayout = ImageLayout.Stretch;*/





        }

        private void btnPunkteListe_Click(object sender, EventArgs e)
        {
            txtListe.Text += txtPunkte.Text + Environment.NewLine; 
        }



        public void nehmeAepfelAuf()
        {



            if (picSchlangenkopf.Bounds.IntersectsWith(picApfel1.Bounds) )
                {
                punkte = punkte + 1;
                txtPunkte.Text = "" + punkte;
                picApfel1.BackgroundImage = null;
                picApfel1.Location = new Point(-500, -500);
                tmrSpiel.Interval = tmrSpiel.Interval -5;
                }

           if (picSchlangenkopf.Bounds.IntersectsWith(picApfel2.Bounds))
            {
                punkte = punkte + 2;
                txtPunkte.Text = "" + punkte;
                picApfel2.BackgroundImage = null;
                picApfel2.Location = new Point(-500, -500);
                tmrSpiel.Interval = tmrSpiel.Interval -5;
            }

            if (picSchlangenkopf.Bounds.IntersectsWith(picApfel3.Bounds))
            {
                punkte = punkte + 3;
                txtPunkte.Text = "" + punkte;
                picApfel3.BackgroundImage = null;
                picApfel3.Location = new Point(-500, -500);
                tmrSpiel.Interval = tmrSpiel.Interval -5;
            }

            if (picSchlangenkopf.Bounds.IntersectsWith(picApfel4.Bounds))
            {
                punkte = punkte + 4;
                txtPunkte.Text = "" + punkte;
                picApfel4.BackgroundImage = null;
                picApfel4.Location = new Point(-500, -500);
                tmrSpiel.Interval = tmrSpiel.Interval -5;
            }

            if (picSchlangenkopf.Bounds.IntersectsWith(picApfel5.Bounds))
            {
                punkte = punkte + 5;
                txtPunkte.Text = "" + punkte;
                picApfel5.BackgroundImage = null;
                picApfel5.Location = new Point(-500, -500);
                tmrSpiel.Interval = tmrSpiel.Interval - 5;
            }

            if (picSchlangenkopf.Bounds.IntersectsWith(picApfel6.Bounds))
            {
                punkte = punkte + 6;
                txtPunkte.Text = "" + punkte;
                picApfel6.BackgroundImage = null;
                picApfel6.Location = new Point(-500, -500);
                tmrSpiel.Interval = tmrSpiel.Interval - 10;
            }

            if (picSchlangenkopf.Bounds.IntersectsWith(picApfel7.Bounds))
            {
                punkte = punkte + 7;
                txtPunkte.Text = "" + punkte;
                picApfel7.BackgroundImage = null;
                picApfel7.Location = new Point(-500, -500);
                tmrSpiel.Interval = tmrSpiel.Interval - 10;
            }

            if (picSchlangenkopf.Bounds.IntersectsWith(picApfel8.Bounds))
            {
                punkte = punkte + 8;
                txtPunkte.Text = "" + punkte;
                picApfel8.BackgroundImage = null;
                picApfel8.Location = new Point(-500, -500);
                tmrSpiel.Interval = tmrSpiel.Interval - 10;
            }

            if (picSchlangenkopf.Bounds.IntersectsWith(picApfel9.Bounds))
            {
                punkte = punkte + 9;
                txtPunkte.Text = "" + punkte;
                picApfel9.BackgroundImage = null;
                picApfel9.Location = new Point(-500, -500);
                tmrSpiel.Interval = tmrSpiel.Interval - 10;
            }

            if (picSchlangenkopf.Bounds.IntersectsWith(picApfel10.Bounds))
            {
                punkte = punkte + 10;
                txtPunkte.Text = "" + punkte;
                picApfel10.BackgroundImage = null;
                picApfel10.Location = new Point(-500, -500);
                tmrSpiel.Interval = tmrSpiel.Interval - 10;
            }

        }
    }
}
if (picSchlangenkopf.Bounds.IntersectsWith(PicAepfel[0].Bounds) )
if (picSchlangenkopf.Bounds.IntersectsWith(picApfel3.Bounds))
txtPunkte.Text = "" + punkte;