使用图片框在战舰游戏中制作船只

时间:2016-01-11 15:04:49

标签: vb.net

我正在尝试开发Battleship游戏,我正在使用picturebox创建一个10x10的网格。我设法制作了这个网格,但我仍然坚持制造船只。  我不知道如何制造船只(正如你在我的代码中看到的那样,船只将显示为木质图像)。就像一艘船将连续4个图片框,另一个将连续3个图片框。我想生成一个随机数来为船舶起点,基于这一点,我需要把船放在网格上。

我的代码如下:

Public Class GameScvb
    Dim oceanimage As Image = My.Resources.oceanimage
    Dim missimage As Image = My.Resources.missimage
    Dim hitimage As Image = My.Resources.hitimage
    Dim shipimage As Image = My.Resources.woodenimage

    Private Function Box(ByVal X As Integer, ByVal Y As Integer, ByRef IsEnemy As Boolean) As PictureBox
        If IsEnemy Then
            Select Case X
                Case 0
                    Return Choose(Y + 1, PA1, PA2, PA3, PA4, PA5, PA6, PA7, PA8, PA9, PA10)
                Case 1
                    Return Choose(Y + 1, PB1, PB2, PB3, PB4, PB5, PB6, PB7, PB8, PB9, PB10)
                Case 2
                    Return Choose(Y + 1, PC1, PC2, PC3, PC4, PC5, PC6, PC7, PC8, PC9, PC10)
                Case 3
                    Return Choose(Y + 1, PD1, PD2, PD3, PD4, PD5, PD6, PD7, PD8, PD9, PD10)
                Case 4
                    Return Choose(Y + 1, PE1, PE2, PE3, PE4, PE5, PE6, PE7, PE8, PE9, PE10)
                Case 5
                    Return Choose(Y + 1, PF1, PF2, PF3, PF4, PF5, PF6, PF7, PF8, PF9, PF10)
                Case 6
                    Return Choose(Y + 1, PG1, PG2, PG3, PG4, PG5, PG6, PG7, PG8, PG9, PG10)
                Case 7
                    Return Choose(Y + 1, PH1, PH2, PH3, PH4, PH5, PH6, PH7, PH8, PH9, PH10)
                Case 8
                    Return Choose(Y + 1, PI1, PI2, PI3, PI4, PI5, PI6, PI7, PI8, PI9, PI10)
                Case 9
                    Return Choose(Y + 1, PJ1, PJ2, PJ3, PJ4, PJ5, PJ6, PJ7, PJ8, PJ9, PJ10)
            End Select
        Else
            Select Case X
                Case 0
                    Return Choose(Y + 1, SA1, SA2, SA3, SA4, SA5, SA6, SA7, SA8, SA9, SA10)
                Case 1
                    Return Choose(Y + 1, SB1, SB2, SB3, SB4, SB5, SB6, SB7, SB8, SB9, SB10)
                Case 2
                    Return Choose(Y + 1, SC1, SC2, SC3, SC4, SC5, SC6, SC7, SC8, SC9, SC10)
                Case 3
                    Return Choose(Y + 1, SD1, SD2, SD3, SD4, SD5, SD6, SD7, SD8, SD9, SD10)
                Case 4
                    Return Choose(Y + 1, SE1, SE2, SE3, SE4, SE5, SE6, SE7, SE8, SE9, SE10)
                Case 5
                    Return Choose(Y + 1, SF1, SF2, SF3, SF4, SF5, SF6, SF7, SF8, SF9, SF10)
                Case 6
                    Return Choose(Y + 1, SG1, SG2, SG3, SG4, SG5, SG6, SG7, SG8, SG9, SG10)
                Case 7
                    Return Choose(Y + 1, SH1, SH2, SH3, SH4, SH5, SH6, SH7, SH8, SH9, SH10)
                Case 8
                    Return Choose(Y + 1, SI1, SI2, SI3, SI4, SI5, SI6, SI7, SI8, SI9, SI10)
                Case 9
                    Return Choose(Y + 1, SJ1, SJ2, SJ3, SJ4, SJ5, SJ6, SJ7, SJ8, SJ9, SJ10)
            End Select
        End If
        Return Nothing
    End Function

    Private Sub InitialLayout()
        ' draw game board
        For i = 0 To 9
            For j = 0 To 9
                Box(i, j, True).BackgroundImage = oceanimage 'true indicates enemy grid / i - row / j coloumn
                Box(i, j, False).BackgroundImage = oceanimage 'false indicates my grid
            Next
        Next
        Me.Refresh()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitialLayout()
        ships()
    End Sub

    Public Sub ships()
        Dim rnd As New Random
        Dim value4 As Integer
        Dim value41 As Integer

        value4 = rnd.Next(0, 7)
        value41 = value4 + 3

        'create ship with 4 space
        For i = value4 To value41
            Box(value4, value41, False).BackgroundImage = shipimage
        Next
    End Sub
End Class

生成一个随机数后,Box(value4,value41,false).backgoundimage = shipimage只显示图片框中的一个木制图像而不是4个图片框。

1 个答案:

答案 0 :(得分:0)

这样的东西,只是为了垂直的船,我已经显示了可选参数,这是你定义海/船等的地方......

Sub x()

dim newShip as shape
set newShip=draw_ship Range("c5"), 10

End Sub

Function draw_ship(rngStart As Excel.Range, _
                        intSize As Integer, _
                        Optional strImage As String = "", _
                        Optional blnVertical As Boolean = True) As Shape

Dim ship As Shape

Set draw_ship = ActiveSheet.Shapes.AddShape( _
            msoShapeRectangle, _
            rngStart.Left, _
            rngStart.Top, _
            rngStart.Offset(0, 1).Left - rngStart.Left, _
            rngStart.Offset(intSize + 1, 0).Top - rngStart.Top)

End Function