cesar
cesar

Reputation: 21

Mini game forms

I'm trying to make a mini game in C# (windows forms), like space invaders. The spacecraft flies well, but I'm not getting multiple shots, I have a method timer_Tick to make the animation, when I press space to shoot, the first shot works fine, but the second shot. The first shot in this half position disappears. The shots are pictureBox and I tried List of pictureBox, but without success.

I have something like this:

    public void Form1_Load(object sender, EventArgs e)
    {
        shootP = new PictureBox();
        shootP.Image = Properties.Resources.shoot_1;
        shootP.SizeMode = PictureBoxSizeMode.Zoom;
        shootP.Size = new Size(10, 72);  
        ListShoot = new List<PictureBox>();      
        int i = -1;
    }
     private void Form1_KeyDown(object sender, KeyEventArgs e)
     {

        if (e.KeyCode == Keys.Space)
        {
        control = false;
        i++;
        ListShoot.Add(shootP);
        timeShoot.Start();
     }

      private void timeShoot_Tick(object sender, EventArgs e)
      {
        if (control == false)
        {   
            ListShoot[i].Location = new Point(spacecraft._imageBox.Location.X + 50, spacecraft._imageBox.Location.Y - 55);  // align the shoot with spacecraft
            control = true;     
        }

            ListShoot[i].Top -= 40;
      }

What am I doing wrong?

Upvotes: 2

Views: 1388

Answers (2)

Jake DeGroot
Jake DeGroot

Reputation: 56

You need to create a new image each time, like in aliassce's answer.

private void Form1_KeyDown(object sender, KeyEventArgs e)
 {
    if (e.KeyCode == Keys.Space)
    {
      control = false;
      shootP = new PictureBox();
      shootP.Image = Properties.Resources.shoot_1;
      shootP.SizeMode = PictureBoxSizeMode.Zoom;
      shootP.Size = new Size(10, 72);
      ListShoot.Add(shootP);
      timeShoot.Start();
    }
 }

Then you need to update the position of all of the images in your List. Use a foreach loop and ditch the int iterator.

private void timeShoot_Tick(object sender, EventArgs e)
  {
    if (control == false)
    {   
       foreach PictureBox shot in ListShot
       {
           shot.Location = new Point(spacecraft._imageBox.Location.X + 50, spacecraft._imageBox.Location.Y - 55);  // align the shoot with spacecraft
           shot.Top -= 40;
       }
       control = true;
    }
  }

You might have to update your code that draws the images to loop through the List as well. with something like:

foreach PictureBox shot in ListShoot
{
   draw(shot);
}

Finally you'll probably want to clean out the list every now and then to get rid of any shots that are no longer on the screen and don't need to be updated or drawn anymore.

Upvotes: 3

aliassce
aliassce

Reputation: 1197

You are adding the same PictureBox. You can try creating a new one. Add this cone into Form1_KeyDown.

    shootP = new PictureBox();
    shootP.Image = Properties.Resources.shoot_1;
    shootP.SizeMode = PictureBoxSizeMode.Zoom;
    shootP.Size = new Size(10, 72);

On the other hand you should use OOP as soon as possible

Upvotes: 1

Related Questions