2016-11-20 64 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