2017-01-11 6 views
0

私はカードゲームを作成しました。ここでは、2 x 12の異なる画像がVBフォームの24個の画像ボックスに読み込まれます。私の意図は、一度に2枚のカードを裏返して、一致するペアを見つけることです。ゲームがロードされるたびに、異なる画像が表示され、異なる位置に表示されます。これまでのところ、カードの裏側のイメージをゲームに正常にロードしましたが、イメージが正常に読み込まれたかどうか確認することはできません。 私はまだそれらをシャッフルすることについて心配していません、私はちょうど画像が読み込まれていると、一度に2枚のカードを裏返すことができるようにしたいです。このようなタスクに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

をすべてのコードが来ます画像をロードしたくない場合は、ファイル名を12要素の文字列配列にロードするだけです。それは単純に 'pbFirst.ImageLocation = myImgs(index)'です。つまり、文字列配列はパスのないファイル名だけです。マッチゲームの場合は、24枚の画像をプールする必要はありません。ちょうど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

試してみて、これは過剰だと思いません - 単に画像変更「それらをひっくり返し」忘れる:あなたはunaccredittedポスト上のアドバイスに従っている場合

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 
関連する問題