代码之家  ›  专栏  ›  技术社区  ›  User0123456789

如何使用Visual Basic(windows窗体)创建网格/游戏板

  •  -1
  • User0123456789  · 技术社区  · 7 年前

    我目前正在进行一个项目,我正在创建一个名为围棋的游戏。这是一个关于地盘的棋盘游戏,尽管规则很简单,但很难掌握。如果您想了解更多信息,请单击此处: https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwjbjOTh_tfXAhVjIcAKHQuxCEgQFggoMAA&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FGo_(game)&usg=AOvVaw3jJDO24LghMzmB1WUxB2t_

    我使用Visual Basic来编程这个游戏,更具体地说是用vb编程,而不是用C或任何其他语言,使用windows窗体作为面向对象的游戏。

    我在网上搜索了如何编写这个程序板,但找不到任何对我来说简单易懂的东西(我的编程技能不是很高级)。主要的问题是,我需要通过点击空白区域来记录用户在黑板上放置他们的碎片或石头的位置,因此使用2D阵列。然而,石头被放置在 十字路口 而不是线之间的空间。

    我找不到任何人试图这样做的例子。我只能想到创建/使用一张图板的图片,将其导入windows窗体,并在单击时覆盖一个不可见的2D阵列,单击的地方会出现一个黑色/白色的石头。这是我的另一个问题;如何在单击的表单中显示工件。

    我感谢你的建议。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Idle_Mind    7 年前

    下面是一些使用面板(Panel1)作为板的启动代码。多次单击某个位置以切换该位置的状态:

    Public Class Form1
    
        Private Const GridSize As Integer = 9 ' small = 9, medium = 13, large = 19
        Private Grid As New List(Of List(Of Boolean?))() ' nullable boolean (black:true, white:false, blank:nothing)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Panel1.BackColor = Color.Tan
    
            ' initialize the grid to all blanks
            For col As Integer = 1 To GridSize
                Dim column As New List(Of Boolean?)
                For row As Integer = 1 To GridSize
                    column.Add(Nothing)
                Next
                Grid.Add(column)
            Next
        End Sub
    
        Private Sub Panel1_SizeChanged(sender As Object, e As EventArgs) Handles Panel1.SizeChanged
            Dim pnl As Panel = DirectCast(sender, Panel)
            pnl.Invalidate() ' redraw the board whenever it gets resized
        End Sub
    
        Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
            Dim pnl As Panel = DirectCast(sender, Panel)
    
            Dim p As Decimal
            Dim x, y As Integer
            Dim margin As Integer
    
            ' draw the vertical lines:
            margin = (1 / (GridSize + 1)) * pnl.Size.Height
            For col As Integer = 1 To GridSize
                p = col / (GridSize + 1)
                x = p * pnl.Size.Width
                e.Graphics.DrawLine(Pens.Black, x, margin, x, pnl.Size.Height - margin)
            Next
    
            ' draw the horizontal lines:
            margin = (1 / (GridSize + 1)) * pnl.Size.Width
            For row As Integer = 1 To GridSize
                p = row / (GridSize + 1)
                y = p * pnl.Size.Height
                e.Graphics.DrawLine(Pens.Black, margin, y, pnl.Size.Width - margin, y)
            Next
    
            ' draw the pieces:
            For x = 0 To GridSize - 1
                Dim column As List(Of Boolean?) = Grid(x)
                For y = 0 To GridSize - 1
                    If Grid(x)(y).HasValue Then
                        Dim clr As Color = If(Grid(x)(y), Color.Black, Color.White)
                        Dim pt As New Point((x + 1) / (GridSize + 1) * pnl.Size.Width, (y + 1) / (GridSize + 1) * pnl.Size.Height)
                        Dim rc As New Rectangle(pt, New Size(1, 1))
                        rc.Inflate((1 / (GridSize + 1)) * pnl.Size.Width / 2, (1 / (GridSize + 1)) * pnl.Size.Height / 2)
                        Using brsh As New SolidBrush(clr)
                            e.Graphics.FillEllipse(brsh, rc)
                        End Using
                    End If
                Next
            Next
        End Sub
    
        Private Sub Panel1_Click(sender As Object, e As EventArgs) Handles Panel1.Click
            Dim pnl As Panel = DirectCast(sender, Panel)
    
            ' figure out where the user clicked: min = 0, max = (gridsize -1)
            Dim pt As Point = pnl.PointToClient(Cursor.Position)
            Dim colWidth As Integer = (1 / (GridSize + 1)) * pnl.Size.Width
            Dim rowHeight As Integer = (1 / (GridSize + 1)) * pnl.Size.Height
            Dim gridPosition As New Point(Math.Min(Math.Max((pt.X / colWidth) - 1, 0), GridSize - 1), Math.Min(Math.Max((pt.Y / rowHeight) - 1, 0), GridSize - 1))
    
            ' now do something with gridPosition: (here we just toggle between black:true, white:false and blank:nothing)
            If Not Grid(gridPosition.X)(gridPosition.Y).HasValue Then
                Grid(gridPosition.X)(gridPosition.Y) = True
            ElseIf Grid(gridPosition.X)(gridPosition.Y) = True Then
                Grid(gridPosition.X)(gridPosition.Y) = False
            Else
                Grid(gridPosition.X)(gridPosition.Y) = Nothing
            End If
            pnl.Invalidate() ' force the board to redraw itself
        End Sub
    
    End Class
    

    示例屏幕截图: Simple Go Board