2013-07-07 79 views
1

我是新來的Visual Basic,並試圖創建一個遊戲,用戶將會看到兩個圖像,並且需要選擇正確的一個(有點像測驗)。但是,我希望它具有挑戰性,因此,只要用戶再次播放圖像,就希望隨機配對圖像。如何讓picturebox顯示隨機圖像,也不會重複?

對於這一點,我有大約10幅圖像,這將在同一時間內提出了兩個。如果玩家點擊正確的圖像,他們可以繼續並獲得一個點,這將使用標籤記錄,兩個新圖像將取代它們,並重復該過程,但如果它們不正確,那麼會出現一條消息「不正確! 「然後他們將失去一個標記,兩個新的圖像將取代它們。

我已經創建的接口,使用一個TableLayoutPanel和兩個圖片框。我製作了一些可以生成隨機圖片的代碼,但是最終他們會重複自己,我不想要!但是,如果我在代碼末尾放置RemoveAt,他們不會重複,但是在玩家點擊圖片6次之後會出現「索引超出範圍」的錯誤,這是遊戲結束之前發生的,所以不要也不要那樣!

這裏是我到目前爲止的代碼:

第一:

Public Sub New() 
    ' This call is required by Windows Form Designer 
    InitializeComponent() 
    ' Add any initialization after the InitializeComponent() call 
    AssignImagesToSquares() 
End Sub 

Private random As New Random 

Private images = 
    New List(Of Image) From {My.Resources.Aeroplane, My.Resources.Bicycle, My.Resources.Beginner_button, My.Resources.Bird, My.Resources.Butterfly, 
          My.Resources.Cartoon_Background_Wallpaper_719574, My.Resources.cartoon_farm, My.Resources.Clock, My.Resources.Egg_Timer, 
          My.Resources.Moderate_background, My.Resources.Tree, My.Resources.Umbrella, My.Resources.Woman} 

Private Sub AssignImagesToSquares() 

    For Each Control In TableLayoutPanel1.Controls 
     Dim imageLabel = TryCast(Control, PictureBox) 
     If imageLabel IsNot Nothing Then 
      Dim randomNumber = random.Next(images.Count) 
      imageLabel.Image = images(randomNumber) 
      images.RemoveAt(randomNumber) 
     End If 
    Next 
End Sub 

Private Sub picturebox_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click, 
    PictureBox1.Click 

    Dim clickedLabel = TryCast(sender, PictureBox) 

    If clickedLabel.Image Is My.Resources.Butterfly Then 
     MsgBox("This is incorrect") 
     AssignImagesToSquares() 
    Else 
     AssignImagesToSquares() 
    End If 
End Sub 

代碼編寫的順序。使用Visual Basic 2010 Express。任何幫助非常感謝,如果你需要更多的細節,請讓我知道!我在這裏絕望!

基本上,我怎麼從重複停止圖像和阻止出現的錯誤。此外,沒有錯誤發生,但如果點擊蝴蝶圖像,然後它不顯示msgbox,所以有什麼錯?

回答

1

把你的十張圖片在一個陣列,其中洗牌,挑二的時間,五次。

+0

我也做了陣列的一部分,但我怎麼洗牌他們,讓他們隨機分配到兩個圖片框呢?非常感謝。 – KSR5

+0

不用擔心!弄清楚了。謝謝你的想法 - 工作很棒! – KSR5

1

創建自己的對象從圖像中繼承,而且還包括一個標誌,表示「拿來主義」。您也可以輕鬆包含其他控制信息。

1

數組或圖像列表將工作。只需使用標籤屬性來指示使用。也許是這樣的:

Public Sub New() 
    ' This call is required by Windows Form Designer 
    InitializeComponent() 
    ' Add any initialization after the InitializeComponent() call 
    AssignImagesToSquares() 
End Sub 

Private random As New Random 

Private images = 
    New List(Of Image) From {My.Resources.Aeroplane, _ 
          My.Resources.Bicycle, _ 
          My.Resources.Beginner_button, _ 
          My.Resources.Bird, _ 
          My.Resources.Butterfly, _ 
          My.Resources.Cartoon_Background_Wallpaper_719574, _ 
          My.Resources.cartoon_farm, _ 
          My.Resources.Clock, _ 
          My.Resources.Egg_Timer, _ 
          My.Resources.Moderate_background, _ 
          My.Resources.Tree, _ 
          My.Resources.Umbrella, _ 
          My.Resources.Woman} 

Private Sub AssignImagesToSquares() 

    For Each Control In TableLayoutPanel1.Controls 
     Dim imageLabel = TryCast(Control, PictureBox) 
     If imageLabel IsNot Nothing Then 
      Dim Done as Boolean = False 
      'Loop until it finds an image that hasn't been used. Then set Done 
      'to true to exit the while loop 
      While Not Done 
       Dim randomNumber = random.Next(images.Count-1) 
       If images(randomNumber).Tag.ToString <> "Used" Then 
        imageLabel.Image = images(randomNumber) 
        images(randomNumber).Tag = "Used" 
        Done = True 
       End If 
      End While 
     End If 
    Next 
End Sub 

Private Sub picturebox_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click, 
    PictureBox1.Click 

    Dim clickedLabel = TryCast(sender, PictureBox) 

    If clickedLabel.Image Is My.Resources.Butterfly Then 
     MsgBox("This is incorrect") 
     AssignImagesToSquares() 
    Else 
     AssignImagesToSquares() 
    End If 
End Sub 

您的索引出界失誤可能已從random.Next的上限設定爲images.Count到來。 Count總是比最高指數多一個。所以如果隨機數等於Count,你會得到錯誤。使用images.Count-1應該可以解決這個問題。