我已经创建了一个纸牌游戏,其中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
如果你正在跟踪的unaccreditted后建议您所有的代码来自,你不想加载图像,只需将文件名加载到12个元素的字符串数组中即可。那么它就是'pbFirst.ImageLocation = myImgs(index)'。因为你的字符串数组只是没有路径的文件名。如果这是一场比赛,你不需要24张图片,只有12张;但是您可能希望这12个图像来自较大的池,以便不会反复使用相同的12个图像。文件名而不是图片使这个和其他事情更简单。为每个游戏填充阵列*是最难的部分。 – Plutonix