2016-01-11 83 views
0

我正在嘗試開發戰艦遊戲,並使用圖片框創建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 

產生一個隨機數後,盒(VALUE4,value41,假).backgoundimage = shipimage僅示出了在圖片框一個woodenimage而不是採取4的PictureBox。

+0

在'For'循環您的'船隻「方法,你永遠不會使用'i'變量。不應該將它用作「Box」方法的參數之一嗎? –

+0

您可以將它們從單元格幾何體中構建出來嗎?所以範圍(「A5」),頂部到範圍(「a10」),頂部範圍(「A5」)。頂部給你5船?和尺寸關閉,所以b.left-a.left =寬度等 –

+0

你可以寫我的實際代碼 – Jenny

回答

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