【问题标题】:How to create grid/game board using Visual Basic(windows form)如何使用 Visual Basic(windows 窗体)创建网格/游戏板
【发布时间】:2018-05-08 19:57:30
【问题描述】:

我目前正在进行一个项目,我正在创建一个名为围棋的游戏。这是一款棋盘游戏,虽然规则简单,但很难掌握。如果你想了解更多,请点击这里: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 窗体作为面向对象的游戏。

我在网上搜索了如何对这个板子进行编程,但找不到任何对我来说简单易懂的东西(我的编程技能不是太先进)。主要问题是我需要通过单击空白区域来记录用户在棋盘上放置棋子或棋子的位置,因此使用二维数组。但是,石头放置在网格的交叉点上,而不是线之间的空间中。

我找不到任何人尝试进行类似操作的示例。我只能想到创建/使用电路板的图片,将其导入 Windows 窗体,并在单击时覆盖不可见的 2D 数组,单击时会出现黑色/白色石头。这是我的另一个问题;如何在点击的表单中显示一个片段。

感谢任何建议。

【问题讨论】:

  • 不幸的是这个问题太宽泛了。 Stack Overflow 是为了帮助您解决问题,而不是为您指明正确的方向,帮助您找到资源或为您编写代码。自己编写一些代码或尝试在其他地方寻求帮助,并在遇到更具体的问题时返回此处。
  • 最灵活的解决方案是让您绘制Paint() 中的线条和片段,例如面板。简单的整数数学会告诉您哪些网格线最接近用户单击的位置。

标签: vb.net visual-studio visual-studio-2012 grid game-physics


【解决方案1】:

这里有一些使用面板(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

示例截图:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    相关资源
    最近更新 更多