如何编码设置可以使得这 12 个 picturebox 的 image 每次都是随机生成 4 组 3 个一样的素材
时间: 2024-05-14 14:14:15 浏览: 31
以下是一个示例代码,可以实现上述功能:
```
Dim random As New Random() '创建一个随机数生成器
Dim images() As Image = {Image1, Image2, Image3, Image4, Image5, Image6} '存储素材的图片数组
Dim counts(images.Length - 1) As Integer '用来记录每个素材已经被使用的次数
For i As Integer = 0 To 11 '循环处理 12 个 picturebox
Dim indices() As Integer = {0, 0, 0} '用来存储随机生成的 3 个素材的索引
For j As Integer = 0 To 2 '循环生成 3 个随机索引
Dim index As Integer
Do '生成一个未曾被使用过的索引
index = random.Next(images.Length)
Loop While counts(index) >= 4
indices(j) = index
counts(index) += 1 '将该素材的使用次数加 1
Next
'将 3 个随机素材的图片合并成一张拼图,并设置为当前 picturebox 的 image
Dim combinedImage As New Bitmap(images(0).Width * 3, images(0).Height)
Dim graphics As Graphics = Graphics.FromImage(combinedImage)
For j As Integer = 0 To 2
graphics.DrawImage(images(indices(j)), j * images(0).Width, 0)
Next
PictureBoxes(i).Image = combinedImage
Next
'每次重新开始游戏时,需要将每个素材的使用次数清零
For i As Integer = 0 To counts.Length - 1
counts(i) = 0
Next
```
阅读全文