2013-09-25 59 views
1

我正在製作從窗戶窗體的一側移動到另一側的小船的動畫,但是當船隻接觸到窗體的邊緣時,我必須通知用戶屏幕因爲他贏了。任何人有任何想法如何做到這一點? 這是我到目前爲止有:檢查動畫是否在Winform中的屏幕邊緣

private void playerTimer_Tick(object sender, EventArgs e) 
    { 
     for (int i = 0; i < 6; i++) 
     { 
      if (PlayersActive[i]) 
      { 
       Players[i].Move(randomNumber.Next(3,10)); 
       //HERE IS WHERE I SHOULD CHECK TO SEE IF A BIKE HAS WON 
       //If player location is at the edge of the screen 

      } 
     } 
     this.Invalidate(); 
    } 

代碼移動船隻:

private void GameForm_Paint(object sender, PaintEventArgs e) 
    { 

     Graphics g = e.Graphics; 

     // Set variables used to store logic to default values. 
     int playersStillRacing = 0; 


     for (int i = 0; i < 6; i++) 
     { 
      // Check to see if the current player is still racing. 
      if (playersRacing[i]) 
      { 
       // Incremement playersStillRacing. 
       playersStillRacing++; 

       // Set the playerPictureBox associated with this player to the new location 
       // the player is located at. 
       playerPictureBoxes[i].Image = Players[i].Image; 
       playerPictureBoxes[i].Left = Players[i].X; 
       playerPictureBoxes[i].Top = Players[i].Y; 


       g.DrawImage(playerPictureBoxes[i].Image, Players[i].X, Players[i].Y); 

      } 
     } 

    } 
+0

它涉及到你的「自行車」的長度,你如何渲染你的「自行車」(直接使用一些控件或只是繪製它) –

+0

要繪製自行車,我使用paint事件,然後使用drawImage,因爲它是一個圖片我從互聯網的圖片調整大小。 – Mirimari

+0

你如何增加Players [i] .X?如果你採用Players [i] .X值和Players [i] .Image的寬度,那麼你將能夠看到圖像是否接觸到結尾。 – MasterXD

回答

3

這會做的伎倆?我對你給我的最新信息做了一些編輯

private void playerTimer_Tick(object sender, EventArgs e) 
{ 
    for (int i = 0; i < 6; i++) 
    { 
     if (PlayersActive[i]) 
     { 
      Players[i].Move(randomNumber.Next(3,10)); 

      // EDIT BELOW in the if statement! 
      // if the edge of the picture touches the end of the screen. 
      if (Players[i].X + Players[i].Image.Width >= this.Width) 
       MessageBox.Show("Congrats! Player" + i.ToString() + "won!"); 

      //Players[i].X is the X cordinates(The length) from the left side of the winform. 
      //Players[i].Image.Width is the width of the picture ofcourse :D. 
      // if X cordinates + picture width is greater than or equal to screen width. YOU WON :D 
     } 
    } 
    this.Invalidate(); 
} 

或者你還想要別的嗎?

+0

什麼是左和寬?我認爲它是這樣的,但我的變數球員是與球員的名字陣列。 – Mirimari

+0

然後告訴我,你如何移動圖片?你能告訴我整個代碼嗎? – MasterXD