所以我想創建一個濃度風格的內存匹配遊戲。但現在我只想專注於填充圖片框。每次遊戲加載時,我都希望隨機播放圖片。我的老師推薦使用控制陣列,併發送給我這個鏈接http://www.acthompson.net/DotNet/ControlArrays.htm使用控制陣列來洗牌圖片盒
我還是很困惑,因爲到處都是紅線,我不知道如何將圖像添加到程序中。最初,我想在構建時添加picturebox,但本教程建議在運行時進行。
InitializeComponent()和card()的下方有紅線。爲什麼?我如何添加已保存在文件中的圖像?
另外我想知道我是否需要雙類聲明?該類的名稱是'Form','Form1'是指表單本身。當我只聲明'Form1'時,它表示'類Form1和部分類Form1衝突'。我可以只聲明'Form'並且可以與表單交互嗎?
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
末級
我已經試過這和其他各種配置,但TI仍不能正常工作。我在哪裏犯了一個錯誤?
UPDATE:
所以我這個玩耍了和我有幾個問題。 1.它說道沒有聲明,所以我做了IO.Path,似乎沒關係。我不知道它是否合適,雖然 2.它說,PictureBox1等,沒有宣佈和無法訪問。 Img文件夾相同。我假設路徑文件夾我的圖片是不正確
公共類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
末級
另一個更新:
我也有我的第一個版本pictureboxes但不在這一個。不能相信我犯了這樣一個愚蠢的錯誤......這是固定的,但它仍然說我的ImgFolder是未聲明和無法訪問。順便說一句,非常感謝你的耐心和幫助。
進口System.IO
公共類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
末級
而不是洗牌UI控制,這將需要重置位置,只是洗牌映射到他們的圖像。 'InitializeComponent()'屬於'Sub New',你不能引用任何控件,直到它們被創建,它是在Sub New運行之後 – Plutonix
我不確定我是否正確理解你。是這樣的:公用Sub新板() 的InitializeComponent() 卡(0)= PictureBox1 卡(1)= PictureBox2 卡(2)= PictureBox3 卡(3)= PictureBox4 卡(4)= PictureBox5 cards(5)= PictureBox6 cards(6)= PictureBox7 etc End Sub – AlphaOne
@AlphaOne請嘗試Plutonix建議的操作,然後相應編輯您的問題。 –