2017-01-11 144 views
0

我已經創建了一個紙牌遊戲,其中2 x 12個不同的圖像將被加載到vb格式的24個圖片框中。我的意圖是讓用戶每次翻兩張卡片,試圖找到匹配的卡片。每次遊戲加載時,都會有不同的圖片,它們將處於不同的位置。到目前爲止,我已經成功加載了遊戲卡背面的圖像,但是我無法將它們轉過來查看我的圖像是否已成功加載。 我不擔心洗牌,我只是想看看圖像是否已經加載,並且能夠一次將兩張牌翻過來。我真的很困惑,因爲我不習慣使用VB來完成這些任務,所以任何幫助都是值得讚賞的。這裏是我的代碼:如何翻轉紙牌遊戲中的紙牌?

Imports System.IO 
Public Class Board 

    ' as per stackoverflow Terms of Service 
    ' this code comes from 
    ' http://stackoverflow.com/a/40707688 

    'array of picture boxes 
    Private pBoxes As PictureBox() 
    'array of images 
    Private imgs As String() = {"1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg", "11.jpg", "12.jpg", "13.jpg", "14.jpg", "15,jpg", "16.jpg", "17.jpg", "18.jpg", "19.jpg", "20.jpg", "21.jpg", "22.jpg", "23.jpg", "24.jpg"} 
    'random number generator 
    Private RNG = New Random 
    'cover image 
    Private coverImg As String = "bg.jpg" 

    'timer 
    Private dt As DateTime 

    'turns cards 
    Private pbFirst As PictureBox 
    Private pbSecond As PictureBox 
    Private matches As Int32 = 0 

    'Folder where images are held 
    Private ImgFolder As String 


    Private Sub Board1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 


     RNG = New Random() 

     'array of picture boxes 
     pBoxes = New PictureBox() {PictureBox1, PictureBox2, PictureBox3, PictureBox4, 
     PictureBox5, PictureBox6, PictureBox7, PictureBox8, 
     PictureBox9, PictureBox10, PictureBox11, PictureBox12, PictureBox13, PictureBox14, PictureBox15, PictureBox16, PictureBox17, PictureBox18, PictureBox19, PictureBox20, PictureBox21, PictureBox22, PictureBox23, PictureBox24} 

     'where images are located 
     ImgFolder = "F: \COMPUTER SCIENCE\Test images" 

     coverImg = Path.Combine(ImgFolder, coverImg) 
     For Each p As PictureBox In pBoxes 
      p.ImageLocation = coverImg 
     Next 

     NewGame() 
    End Sub 
    'Take images from file 
    Private Sub PickImages() 

     Dim nums = Enumerable.Range(1, 12).ToArray() 

     Dim pool = nums.Concat(nums).OrderBy(Function(r) RNG.Next).ToArray() 
    End Sub 

    Private Sub Shuffle() 

    End Sub 
    ' reset everything 
    Private Sub NewGame() 

     matches = 0 
     pbFirst = Nothing 
     pbSecond = Nothing 
     ' repick, reshuffle 
     PickImages() 
     Shuffle() 

     dt = DateTime.Now 
     'tmrMain.Enabled = True 
    End Sub 
End Class 
+1

如果你正在跟蹤的unaccreditted後建議您所有的代碼來自,你不想加載圖像,只需將文件名加載到12個元素的字符串數組中即可。那麼它就是'pbFirst.ImageLocation = myImgs(index)'。因爲你的字符串數組只是沒有路徑的文件名。如果這是一場比賽,你不需要24張圖片,只有12張;但是您可能希望這12個圖像來自較大的池,以便不會反覆使用相同的12個圖像。文件名而不是圖片使這個和其他事情更簡單。爲每個遊戲填充陣列*是最難的部分。 – Plutonix

回答

0

我沒有評論點,但你是否點擊圖片將它們轉過來?如果是這樣,我認爲你只需要有一個如下的事件來加載卡片圖像的背面。

Private Sub PictureBox1_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox1.Click 
    PictureBox1.ImageLocation = ("Path to Picture of back of the card") 
    PictureBox1.Load() 
End Sub 
0

不要試圖過度覺得這 - 忘記「把他們在」簡單地改變形象:

Private Sub PictureBox1_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox1.Click, PictureBox2.Click 'etc 
    dim index as short 
    ' to do: get the index of the PictureCard 
    If sender.Image is coverImg then 
     sender.Image = imgs(index) ' in stead of 0, use the index of the picture card 
    Else 
     sender.Image = coverImage 
    End if 
End Sub