2016-11-20 15 views
1

所以我想創建一個濃度風格的內存匹配遊戲。但現在我只想專注於填充圖片框。每次遊戲加載時,我都希望隨機播放圖片。我的老師推薦使用控制陣列,併發送給我這個鏈接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 

末級

+0

而不是洗牌UI控制,這將需要重置位置,只是洗牌映射到他們的圖像。 'InitializeComponent()'屬於'Sub New',你不能引用任何控件,直到它們被創建,它是在Sub New運行之後 – Plutonix

+0

我不確定我是否正確理解你。是這樣的:公用Sub新板() 的InitializeComponent() 卡(0)= PictureBox1 卡(1)= PictureBox2 卡(2)= PictureBox3 卡(3)= PictureBox4 卡(4)= PictureBox5 cards(5)= PictureBox6 cards(6)= PictureBox7 etc End Sub – AlphaOne

+0

@AlphaOne請嘗試Plutonix建議的操作,然後相應編輯您的問題。 –

回答

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 
  • 該卡是從www.jfitz.com/cards/這顯然不再存在
  • 在點擊事件,代碼可以存儲在第一和第二PB卡嗒一聲使用pbFirstpbSecond
  • PickImages
    • 開始與值數組加載的文件名到陣列1-52
    • 打亂他們(你在你前面的問題洗牌是錯誤的 - 有許多Shuffle answers like this one上SO)
    • 採取一半多,因爲你需要(12對24的比賽遊戲),複製它們
  • 洗牌數組
    • 通過示出在PictureBox其中n是陣列中的點擊圖片框的上述
    • 的菲茨卡的索引具有數值組,其中1.png是ACE-黑桃,2名映射到2- myImgs(n)地圖他們黑桃等等轉換整合呃以卡片圖片文件名很簡單。
  • CoverImg是通用的you-havent-found-a-match圖片。你可以將它們留空

使用ImageLocation,而不是圖像也可以讓你輕鬆地比較,如果pbFirst具有相同的圖像作爲pbSecond

添加計時器遊戲和延誤,並最終有12卡的版本看起來像:

enter image description here

你似乎有一個窗體中的窗體聲明。我會從新的形式開始。

+0

謝謝!在編輯我的原始文章之前,我沒有看到這一點。我會玩這個。 – AlphaOne

+0

您必須將尚未編寫的方法(PickImages(),Shuffle())的調用註釋掉,但您應該可以使用通用默認圖像或根本不加載的方式加載表單。如果是這樣,請點擊複選標記,以便將其從未答覆列表中移出。你也應該參加[Tour] – Plutonix