Как я могу считать очки из случайных картинных коробок - PullRequest
1 голос
/ 12 мая 2019

Первое: я изучаю C # в школе и делаю эту игру для школьного проекта.Мне нужна действительно помощь!

Мне нужно запрограммировать змею, но я застрял в проблеме.

Она генерирует случайные яблоки на поле.Каждый раз, когда я сталкиваюсь со змеей и яблоком, она подсчитывает неправильное число для моих очков.

В качестве примера: я бегу со своей змеей к яблоку с номером 6. Он берет яблоко, но он считает число 9 до моих очков.Если я снова возьму яблоко с номером 6, оно будет считать 3 до моих очков (случайно).

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

        }
    }
}

Я ожидаю, что при столкновении эти числа яблок будут считаться с моими очками, но случайные числак моим точкам

1 Ответ

0 голосов
/ 12 мая 2019

Как только вы работаете с массивом, а затем вызываете переменные.

например,

if (picSchlangenkopf.Bounds.IntersectsWith(PicAepfel[0].Bounds) )

после того, как вы выполняете:

if (picSchlangenkopf.Bounds.IntersectsWith(picApfel3.Bounds))

С:

txtPunkte.Text = "" + punkte;

Вы перезаписываете свой текст всегда, поднимаяяблоко - это твое намерение?

Типп: пиши свой код всегда на английском языке, а также используй английские комментарии, людям, которые хотят помочь тебе понять, легче.

...