迷你游戏形式

时间:2012-10-20 19:41:52

标签: c# winforms

我正在尝试用C#(windows窗体)制作迷你游戏,就像太空入侵者一样。航天器飞得很好,但是我没有多次拍摄,我有一个方法timer_Tick来制作动画,当我按空间拍摄时,第一次拍摄效果很好,但是第二次拍摄。这一半位置的第一杆消失了。镜头为pictureBox,我尝试了pictureBox列表,但没有成功。

我有这样的事情:

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

我做错了什么?

2 个答案:

答案 0 :(得分:3)

每次都需要创建一个新图像,就像aliassce的回答一样。

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

然后,您需要更新列表中所有图像的位置。使用foreach循环并抛弃int迭代器。

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

您可能必须更新绘制图像的代码以循环遍历List。有类似的东西:

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

最后,您可能希望不时清理列表,然后摆脱屏幕上不再显示的任何镜头,不再需要更新或绘制。

答案 1 :(得分:1)

您正在添加相同的PictureBox。您可以尝试创建一个新的。 将此圆锥体添加到Form1_KeyDown

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

另一方面,你应该尽快使用OOP