如何在纸牌游戏中翻牌?

时间:2017-01-11 11:25:49

标签: vb.net

我创建了一个纸牌游戏,其中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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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