我目前正在开展一个项目,我正在创建一个名为go的游戏。这是一个领域的棋盘游戏,尽管规则很简单,但这是一个难以掌握的游戏。如果您想了解更多信息,请点击此处: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阵列。但是,石头放置在网格的交叉点上,而不是放在线条之间的空格中。
我找不到任何人尝试与此类似的事情的例子。我只能想到创建/使用电路板的图片,将其导入窗体,并在点击时用不可见的2D阵列覆盖,点击时会出现黑/白石。这是我的另一个问题;如何在点击的表单中显示一块。
我感谢任何建议。
答案 0 :(得分:1)
这是使用Panel(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