开发者

Visual Basic Help with my Game

I am building a game in Visual Basic called Battleship. I have 100 buttons (10 rows x 10 columns) and what I want the user to do, is be able to click on a button and a function called IsCreated (which is a Boolean) turns True for the certain button.

Two ques开发者_如何学编程tions:

1. How do I create this function ?

2. How do I make the button that is clicked have IsCreated turn from true into false ?


A very basic way would be to assign each button a number & use that in a function call from onClick(). You'd just have a global array of booleans (I used boardPosition)

Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles button1.Click
   boardPosition(1) = IsCreated(1)
End Sub

Then just have:

Protected Function IsCreated(byVal buttonNumberClicked as integer)
    If boardPosition(buttonNumberClicked) Then
       return true
    else
       return false
    End If
End Function

This is assuming you've just created 100 buttons...Not an elegant solution but it'll work.

Edit: Clean up.


I would create the buttons programatically and add them to a dictionary or list to keep track of them. You'll probably want some kind of class to hold all the information about each grid cell instead of just an array of booleans. Add the following to a blank form1.

Dim IsCreated(99) As Boolean
Dim Buttons As New Dictionary(Of String, Button)

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    For i As Integer = 0 To 99
        Dim B As New Button
        Me.Controls.Add(B)
        B.Height = 30
        B.Width = 40
        B.Left = (i Mod 10) * 41
        B.Top = (i \ 10) * 31
        B.Text = Chr((i \ 10) + Asc("A")) & i Mod 10 + 1
        Buttons.Add(B.Text, B)
        B.Tag = i
        AddHandler B.Click, AddressOf Button_Click
    Next


End Sub

Private Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim B As Button = sender
    IsCreated(B.Tag) = True
    B.BackColor = Color.Red
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜