所以我想创建一个集中式记忆匹配游戏。但是现在我想专注于填充图片框。我希望每次加载游戏时都以随机顺序对照片进行洗牌。我的老师建议使用控件数组,并将此链接发送给我http://www.acthompson.net/DotNet/ControlArrays.htm
我仍然很困惑,因为到处都是红线,我不知道如何将图像添加到程序中。最初我想在构建时添加图片框,但本教程建议在运行时进行。
InitializeComponent()和card()下面有红线。为什么?如何添加我保存在文件中的图像?
另外我想知道我是否需要双级声明?类的名称是“Form”,“Form1”是指表单本身。当我刚刚声明'Form1'时,它表示'类Form1和部分类Form1冲突'。我可以直接声明'表单',它可以与表单进行交互吗?
tldr;如何使用可以在每次游戏运行时洗牌的图片框填充控件数组?
提前谢谢你。到目前为止我的代码:
Public Class Form
Public Class Form1
Inherits System.Windows.Forms.Form
Dim cards(23) As PictureBox
Public Class Form
Public Class Form1
Inherits System.Windows.Forms.Form
Dim cards(23) As PictureBox
Sub New()
InitializeComponent()
cards = 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}
End Sub
End Class
结束班
我已经尝试了这个以及其他各种配置但是仍然无法正常工作。我在哪里弄错了?
更新:
所以我玩过这个,我遇到了一些问题。 1.它说没有声明Path所以我做了IO.Path,这似乎没问题。我不知道它是否合适 2.它表示PictureBox1等未声明且无法访问。 Img文件夹也一样。我想我的图像文件夹的路径不正确
Public Class Form1
'picture boxes
Private pBoxes As PictureBox()
'images
Private imgs As String()
'random number generator
Private rNum As Random
'cover image
Private coverImg As String = "bg.jpeg"
'timer
Private dt As DateTime
'turns cards
Private pbFirst As PictureBox
Private pbSecond As PictureBox
Private matches As Int32 = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim ImgFolder As String
rNum = New Random()
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 stored
ImgFolder = IO.Path.Combine(Environment.GetFolderPath("H:\Test images"))
coverImg = IO.Path.Combine(ImgFolder, coverImg)
For Each p As PictureBox In pBoxes
p.ImageLocation = coverImg
Next
'NewGame()
End Sub
'Private Sub NewGame()
' reset everything that matters
'matches = 0
'pbFirst = Nothing
'pbSecond = Nothing
' repick, reshuffle
'PickImages()
'Shuffle()
'dt = DateTime.Now
'tmrMain.Enabled = True
'End Sub
结束班
又一次更新:
我的第一个版本确实有彩盒,但不是这个。无法相信我犯了这么愚蠢的错误......这是固定的,但它仍然说我的ImgFolder是未申报和无法访问的。顺便说一下,非常感谢你们的耐心和帮助。
Imports System.IO
Public Class Form1
'array of picture boxes
Private pBoxes As PictureBox()
'array of images
Private imgs As String()
'random number generator
Private rNum As Random
'cover image
Private coverImg As String = "bg.jpeg"
'timer
Private dt As DateTime
'turns cards
Private pbFirst As PictureBox
Private pbSecond As PictureBox
Private matches As Int32 = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
rNum = New Random()
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 you keep YOUR images
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
'Private Sub NewGame()
' reset everything that matters
'matches = 0
'pbFirst = Nothing
'pbSecond = Nothing
' repick, reshuffle
'PickImages()
'Shuffle()
'dt = DateTime.Now
'tmrMain.Enabled = True
'End Sub
结束班
答案 0 :(得分:1)
您可能不希望在启动时将匹配图像加载到PictureBox
es中 - 它们应该从相同的图像开始直到它们被选中,不是吗?您还希望避免实际使用图像 - 如果您创建/加载图像,您还要负责处理它们。使用ImageLoacation
属性指定文件的路径。
此游戏版使用一副牌中的图像。这只是一个启动,因为这似乎是家庭作业。
Public Class frmGame
Private pbs As PictureBox()
Private myImgs As String() ' img file paths
Private RNG As Random
Private ImgFolder As String
Private CoverImg As String = "b1fv.png"
' crude timer
Private dt As DateTime
' turn elements - first and second pbs clicked,
' matches so you know when it is Game Over
Private pbFirst As PictureBox
Private pbSecond As PictureBox
Private matches As Int32 = 0
Private Sub frmGame_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RNG = New Random()
pbs = New PictureBox() {PictureBox1, PictureBox2, PictureBox3, PictureBox4,
PictureBox5, PictureBox6, PictureBox7, PictureBox8,
PictureBox9, PictureBox10, PictureBox11, PictureBox12}
' where you keep YOUR images
ImgFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"DevGraphics", "Cards")
CoverImg = Path.Combine(ImgFolder, CoverImg)
For Each p As PictureBox In pbs
p.ImageLocation = CoverImg
Next
NewGame()
End Sub
Private Sub NewGame()
' reset verything that matters
matches = 0
pbFirst = Nothing
pbSecond = Nothing
' repick, reshuffle
PickImages()
Shuffle()
dt = DateTime.Now
tmrMain.Enabled = True
End Sub
End Class
pbFirst
和pbSecond
PickImages
会将文件名加载到数组中
myImgs(n)
中显示PictureBox
来映射它们,其中n是上面数组中单击的图片框的索引1.png
是Ace-Spades,2个映射到2 -sdesdes等,所以将整数转换为卡片图像文件名很简单。CoverImg
是您通常找不到匹配的图像。你可以把它们留空使用ImageLocation
而不是图片,您还可以轻松比较pbFirst
与pbSecond
具有相同的图像
为游戏和延迟添加计时器,最终12卡版本可能如下所示:
您似乎在表单中有表单声明。我将从一个新表格开始。