如何将PictureBox变成具有“移动功能”的类并从Form.cs调用它?

时间:2018-12-04 09:03:25

标签: c# picturebox windows-forms-designer

因此,我要尝试制作一个游戏,让您(角色)在Windows窗体上自由移动,目的是“吃掉”在屏幕上水平移动的较小角色。如果您与一个大于您的角色相交...游戏结束。

但是问题是我的程序远未生效。我已经将5个单独的pictureBoxes初始化为敌人(fish1-fish5),并在角色与敌人之间的每种可能的相交处进行了if陈述。我试图给敌人单独上课,没有任何运气。如果有人可以帮助我将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();
    }


}

        }

和我试图制造的敌人的班级

  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

0 个答案:

没有答案