Как превратить PictureBox в класс с «движущимися функциями» и вызвать его из Form.cs? - PullRequest
0 голосов
/ 04 декабря 2018

Итак, я пытаюсь создать игру, в которой вы (персонаж) свободно перемещаетесь в форме окна, цель которой состоит в том, чтобы «съесть» более мелких персонажей, которые движутся горизонтально по экрану.Если вы пересекаетесь с персонажем, который больше вас ... Игра окончена.

Однако проблема в том, что моя программа далека от эффективности.Я инициализировал 5 отдельных PictureBox как врагов (fish1-fish5) и сделал, если утверждения для каждого возможного пересекаются между моим персонажем и врагами.Я пытался сделать отдельный класс для врагов без какой-либо удачи.Я был бы рад, если бы кто-нибудь помог мне превратить PictureBox в класс со случайными функциями скорости и размера, чтобы я мог создавать несколько «врагов» и удалять бесполезные операторы if.

 public partial class Form1 : Form

{
    SoundPlayer sndplayr = new SoundPlayer(fishes.Properties.Resources.fishysong);

    private int speed1;
    private int speed2;
    private int speed3;
    private int speed4;
    private int speed5;
    Random rnd = new Random();
    private int y1;
    private int y2;
    private int y3;
    private int y4;
    private int y5;
    private int score;
    private int patrikox;
    private int patrikoy;
    int fish1size;
    int fish2size;
    int fish3size;
    int fish4size;
    int fish5size;
    public Form1()
    {


        sndplayr.PlayLooping();


        patrikox = 1600;
        patrikoy = rnd.Next(0, 900);
        y1 = rnd.Next(0, 900);
        y2 = rnd.Next(0, 900);
        y3 = rnd.Next(0, 900);
        y4 = rnd.Next(0, 900);
        y5 = rnd.Next(0, 900);
        fish1size = rnd.Next(10, 100);
        fish2size = rnd.Next(10, 100);
        fish3size = rnd.Next(10, 100);
        fish4size = rnd.Next(10, 100);
        fish5size = rnd.Next(10, 100);


        InitializeComponent();
        score = 0;



    }
    public void timer1_Tick(object sender, EventArgs e)
    {



        fish1.Height = fish1size;
        fish1.Width = fish1size;
        fish2.Height = fish2size;
        fish2.Width = fish2size;
        fish3.Height = fish3size;
        fish3.Width = fish3size;
        fish4.Height = fish4size;
        fish4.Width = fish4size;
        fish5.Height = fish5size;
        fish5.Width = fish5size;
        speed1 += 2;
        speed2 += 4;
        speed3 += 6;
        speed4 += 8;
        speed5 += 10;
        patrikox -= 10;
        label2.Text = score.ToString();



        fish1.Location = new Point(speed1, y1);
        fish2.Location = new Point(speed2, y2);
        fish3.Location = new Point(speed3, y3);
        fish4.Location = new Point(speed4, y4);
        fish5.Location = new Point(speed5, y5);
        patriko.Location = new Point(patrikox, patrikoy);
        if (fish1.Location.X >= 1600)
        {
            speed1 -= 1600;
            fish1size = rnd.Next(10, 100);
            fish1.Height = fish1size;
            fish1.Width = fish1size;
            y1 = rnd.Next(0, 900);  
        }
        else if (fish2.Location.X >= 1600)
        {
            speed2 -= 1600;
            fish2size = rnd.Next(10, 100);
            fish2.Height = fish2size;
            fish2.Width = fish2size;
            y2 = rnd.Next(0, 900);
        }
        else if (fish3.Location.X >= 1600)
        {
            speed3 -= 1600;
            fish3size = rnd.Next(10, 100);
            fish3.Height = fish3size;
            fish3.Width = fish3size;
            y3 = rnd.Next(0, 900);
        }
        else if (fish4.Location.X >= 1600)
        {
            speed4 -= 1600;
            fish4size = rnd.Next(10, 100);
            fish4.Height = fish4size;
            fish4.Width = fish4size;
            y4 = rnd.Next(0, 900);            
        }
        else if (fish5.Location.X >= 1600)
        {
            speed5 -= 1600;
            fish5size = rnd.Next(10, 100);
            fish5.Height = fish5size;
            fish5.Width = fish5size;
            y5 = rnd.Next(0, 900);
        }
        else if (patriko.Location.X <= 0)
        {
            patrikox += 1600;
            patrikoy = rnd.Next(0, 900);
        }
        //intersection###########################################################################################
        //intersection###########################################################################################
        if (pictureBox2.Bounds.IntersectsWith(fish1.Bounds) && pictureBox2.Width >= fish1.Width)
        {
            pictureBox2.Width += 2;
            pictureBox2.Height += 2;
            score += 1;
            speed1 = 0;
            y1 = rnd.Next(0, 900);
            int fish1size = rnd.Next(10, 100);
            fish1.Height = fish1size;
            fish1.Width = fish1size;
        }
        else if (pictureBox2.Bounds.IntersectsWith(fish2.Bounds) && pictureBox2.Width >= fish2.Width)
        {
            pictureBox2.Width += 2;
            pictureBox2.Height += 2;
            score += 1;
            speed2 = 0;
            y2 = rnd.Next(0, 900);
            int fish2size = rnd.Next(10, 100);
            fish2.Height = fish2size;
            fish2.Width = fish2size;
        }
        else if (pictureBox2.Bounds.IntersectsWith(fish3.Bounds) && pictureBox2.Width >= fish3.Width)
        {
            pictureBox2.Width += 2;
            pictureBox2.Height += 2;
            score += 1;
            speed3 = 0;
            y3 = rnd.Next(0, 900);
            int fish3size = rnd.Next(10, 100);
            fish3.Height = fish3size;
            fish3.Width = fish3size;
        }
        else if (pictureBox2.Bounds.IntersectsWith(fish4.Bounds) && pictureBox2.Width >= fish4.Width)
        {
            pictureBox2.Width += 2;
            pictureBox2.Height += 2;
            score += 1;
            speed4 = 0;
            y4 = rnd.Next(0, 900);
            int fish4size = rnd.Next(10, 100);
            fish4.Height = fish4size;
            fish4.Width = fish4size;
        }
        else if (pictureBox2.Bounds.IntersectsWith(fish5.Bounds) && pictureBox2.Width >= fish5.Width)
        {
            pictureBox2.Width += 2;
            pictureBox2.Height += 2;
            score += 1;
            speed5 = 0;
            y5 = rnd.Next(0, 900);
            int fish5size = rnd.Next(10, 100);
            fish5.Height = fish5size;
            fish5.Width = fish5size;
        }


        //game over screen####################################################################################################################
        //game over screen####################################################################################################################
        else if (pictureBox2.Bounds.IntersectsWith(fish1.Bounds) && pictureBox2.Width <= fish1.Width) Hide();
        else if (pictureBox2.Bounds.IntersectsWith(fish2.Bounds) && pictureBox2.Width <= fish2.Width) Hide();
        else if (pictureBox2.Bounds.IntersectsWith(fish3.Bounds) && pictureBox2.Width <= fish3.Width) Hide();
        else if (pictureBox2.Bounds.IntersectsWith(fish4.Bounds) && pictureBox2.Width <= fish4.Width) Hide();
        else if (pictureBox2.Bounds.IntersectsWith(fish5.Bounds) && pictureBox2.Width <= fish5.Width) Hide();
        else if (pictureBox2.Bounds.IntersectsWith(patriko.Bounds)) Hide();
            Invalidate();

        }

     private void key_Down(object sender, KeyEventArgs e)
            {
                //Keypressed##########################################################################################################################
                //Keypressed##########################################################################################################################
                int x_player = pictureBox2.Location.X;
                int y_player = pictureBox2.Location.Y;

                if (e.KeyCode == Keys.Down) y_player += 10;
                else if (e.KeyCode == Keys.Up) y_player -= 10;
                else if (e.KeyCode == Keys.Left) x_player -= 10;
                else if (e.KeyCode == Keys.Right) x_player += 10;
                pictureBox2.Location = new Point(x_player, y_player);


            }
    private void Hide()
    {
        label1.Text = "Game Over";
        fish1.Hide();
        fish2.Hide();
        fish3.Hide();
        fish4.Hide();
        fish5.Hide();
        patriko.Hide();
        pictureBox2.Hide();
    }


}

        }

и класс врагов iпопытался сделать:

  class enemy : Form
{
    public int fishSpeed { get; set; }
    public int fishSize { get; set; }

    PictureBox fish = new PictureBox
    {
        Name = "lol",
        Size = new Size(fishSize, fishSize),
        Location = new Point(100, 100),
        BackColor = Color.Black

    };
    this.Controls.Add(enemy);
}

}

Буду рад, если кто-нибудь сможет помочь, спасибо заранее.Извините, если я не достаточно ясно, это мой первый раз, спрашивая о stackoverflow

...