2011-02-02 23 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. 
    } 
    } 
}