0

I am trying to write a game on Visual Studio with C#. The aim is to click on the right picture(the one with nemo fish) as they are falling.

Until now i have managed to add images from file randomly to Pictureboxes. As time runs, the images are updated.

But the problem is the images are not dropping until the end of the panel. They are updated just where they are.

Here is a link of the program

Now I am trying to code that, the images are falling until the end of panel and there must be new coming images from behind of these images continously.

I am really new at coding, so it would be great if you explain it detailed and clear.

Thank you.

Here is a sample of my code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    PictureBox[] pic = new PictureBox[4];
    Image[] img = new Image[17];
    Random ranFoto = new Random();
    
    private void Form1_Load(object sender, EventArgs e)
    {
        panel1.Paint += new PaintEventHandler(panel1_Paint);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        for (int i = 0; i < pic.Length; i++)
        {
            pic[i].Size = new System.Drawing.Size(140, 90);
            this.pic[i].BorderStyle = BorderStyle.Fixed3D;
            pic[i].Image = img[ranFoto.Next(17)];
            panel1.Controls.Add(pic[i]);
        }
    }
    
    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        pic[0] = new PictureBox();
        pic[0].Location = new System.Drawing.Point(0, 0);
        pic[1] = new PictureBox();
        pic[1].Location = new System.Drawing.Point(140, 0);
        pic[2] = new PictureBox();
        pic[2].Location = new System.Drawing.Point(280, 0);
        pic[3] = new PictureBox();
        pic[3].Location = new System.Drawing.Point(420, 0);

        img[0] = Image.FromFile("C:\\Daten\\Fotos\\1.jpg");
        ....
        img[16] = Image.FromFile("C:\\Daten\\Fotos\\17.jpg");           
    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Enabled = true;
    }
}
  • Please ensure you read tag descriptions before you use them. I've removed your Visual Studio tags because this question doesn't relate to the Visual Studio application. – ProgrammingLlama Jan 03 '21 at 14:28
  • 1
    What happns when you run the code? It is somewhat confused. The Paint event is meant for actually drawing stuff, including images. Certainly not for adding controls anywhere of, for that matter, for moving them around. For animations use the timer.Tick event. As it stands I don't even know whether using pictureboxes is a good or bad idea. Please try to describe the goal (even) more clearly..! – TaW Jan 03 '21 at 15:04
  • 1
    Where do you think your code could make the images 'fall', ie where do you increase the y location? – TaW Jan 03 '21 at 15:07

0 Answers0