2011-02-02 39 views
0

我想编写一个应用程序做以下工作:问题移动在C#中的许多图像

我们有15个不同的位置(即我们提出他们点(X1,Y1)的第一位置,点(X1,Y2 )用于第二个位置,以及最后一个位置的点(x1,y15))。

100图像必须在此位置(具有特定顺序)与pictureBox一起显示。 (如点(Xc,Yc)),然后垂直移动等等......

当图像到达特定点(例如点(Xm,Ym))时, )),我们决定它必须继续移动或者必须销毁(概率为20%)(这意味着我们可以在该初始位置创建下一个图像)。

例如,使用pictureBox1在位置(Xi,Yi)创建图像。然后,不允许在位置(Xi,Yi)创建更多图像,直到pictureBox1销毁或返回到其初始位置。

我至今写的是:

  • 创建15的位置。

  • 移动一个图像到达点(Xc,Yc)。

我有一些问题:

  • 我用一个定时器来移动图像。但我想移动100张图片。 (从每个15个位置开始,我们创建图像并移动它们直到我们销毁它们,然后创建下一个图像)。 那我该做什么?每个位置使用15个计时器?!但是如何?

  • 在特定点(例如点(Xk,Yk)),图像必须停止移动随机秒,然后继续移动。我应该怎么做?用另一个计时器?!但是,如何?

  • 当图像到达点(Xc,Yc)并且我们决定销毁它时,我的代码中什么也没有发生......我不知道为什么!

我已经加我想HERE

的图片这是到目前为止我的代码:

public partial class Form1 : Form 
{ 

    Random r = new Random(); 

    // falg to prevent create just one image at each location 
    private Boolean[] createNext = {true,true,true,true,true,true,true, 
            true,true,true,true,true,true,true,true}; 

    private void setImage() 
    { 
     int i = 1 + r.Next() % 15; 

     while (createNext[i] != true) 
      i = 1 + r.Next() % 15; 

     switch (i) 
     { 
      case 1: pictureBox1.ImageLocation = "grin.png"; 
       break; 
      case 2: pictureBox2.ImageLocation = "grin.png"; 
       break; 
      case 3: pictureBox3.ImageLocation = "grin.png"; 
       break; 
      case 4: pictureBox4.ImageLocation = "grin.png"; 
       break; 
      case 5: pictureBox5.ImageLocation = "grin.png"; 
       break; 
      case 6: pictureBox6.ImageLocation = "grin.png"; 
       break; 
      case 7: pictureBox7.ImageLocation = "grin.png"; 
       break; 
      case 8: pictureBox8.ImageLocation = "grin.png"; 
       break; 
      case 9: pictureBox9.ImageLocation = "grin.png"; 
       break; 
      case 10: pictureBox10.ImageLocation = "grin.png"; 
       break; 
      case 11: pictureBox11.ImageLocation = "grin.png"; 
       break; 
      case 12: pictureBox12.ImageLocation = "grin.png"; 
       break; 
      case 13: pictureBox13.ImageLocation = "grin.png"; 
       break; 
      case 14: pictureBox14.ImageLocation = "grin.png"; 
       break; 
      case 15: pictureBox15.ImageLocation = "grin.png"; 
       break; 
     } 
    } 

    private int k = 0; 
    private Boolean destroy = true; 

    // timer that move pictureBox1 
    void timer_Tick(object sender, EventArgs e) 
    { 
     k++; 

     int x = pictureBox1.Location.X; 
     int y = pictureBox1.Location.Y; 

     if (k <= 150)//go right 300 in 150 ticks 
      pictureBox1.Location = new Point(x + 2, y); 
     else if (k <= 300) 
     { 
      if (y < 200) 
       pictureBox1.Location = new Point(x, y + 1); 
      if (y > 200) 
       pictureBox1.Location = new Point(x, y - 1); 
     } 

     else if (k <= 400) 
      pictureBox1.Location = new Point(x + 2, y); 

     else if (k <= 550) 
     { 
      if (destroy == false) 
       pictureBox1.Location = new Point(x + 2, y); 
      if (destroy == true) 
       pictureBox1.Location = new Point(x, y - 3); 
     } 

     else if (k <= 650) 
      pictureBox1.Location = new Point(x, y - 1); 

     else if (k <= 850) 
      pictureBox1.Location = new Point(x - 2, y); 

     else if (k <= 950) 
      pictureBox1.Location = new Point(x, y + 1); 

     else 
      timer1.Stop(); 
    } 

    public Form1() 
    { 
     InitializeComponent(); 

     for (int i = 0; i < 15; i++) 
     { 
      setImage(); 
      timer1.Start(); 
      timer1.Interval = 15; 
      timer1.Tick += new EventHandler(timer_Tick); 
     } 
    } 

请帮我完成这个程序。

在此先感谢。

+0

你是否使用XNA和代表图像作为2D精灵? – 2011-02-02 11:36:51

回答

0

您应该只需使用一个计时器,并创建一个表示您的移动图框的类的实例。 (这个类将包含它将要到达的点,命令第一个点始终是你的下一个目的地,并且它将包含picturebox ..等等)。

enum Action 
{ 
    None, 
    Stop, 
    CheckDestroy 

} 
class InterestingPoint 
{ 
    Point m_Point; 
    Action m_Action; 
} 

class MovingImage 
{ 
public bool DestroyMe {get; private set; } 

PictureBox m_PictureBox; 
List<InterestingPoint> m_PointsToVisit; 
int m_StoppedUntil = 0; 

MovingImage(...) 
{ 
    // Constructor 
} 

void Update(int k) 
{ 
    if(m_StoppedUntil > k) return; 

    //Move m_PictureBox towards m_PointsToVisit.First 
    //when m_PictureBox.Location == m_PointToVisit.First, check if the InterestingPoint 
    //has a action you need to handle, 
    //like if the action is stop, you set m_StoppedUntil to k+ the number of frames you 
    //want the picturebox to stay still. (m_StoppedUntil = k+10 => doesnt update for 10 frames) 
    //if the action is checkdestroy, see if it shall be destroyed and set DestroyMe to true 
} 

所以,你初始化MovingImage对象有一个图片和点应该移动到列表中,这些点包含当它到达那里执行对象的动作。

将您的MovingImages保存在某种列表中,然后遍历它们以更新它们。 在你的Timer_Tick你做类似的东西

void timer_Tick(object sender, EventArgs e) 
{ 
    k++ 
    foreach(MovingImage m in m_MyMovingImages) 
    { 
    m.Update(k); 
    if(m.DestroyMe) 
    { 
     //Destroy it. 
    } 
    } 
}