65.9K
CodeProject 正在变化。 阅读更多。
Home

Magic Blocks(游戏教程)

starIconstarIconstarIconstarIconstarIcon

5.00/5 (9投票s)

2017年8月11日

CPOL

6分钟阅读

viewsIcon

18759

downloadIcon

730

Magic blocks - 简单的逻辑游戏教程

 

引言

大家好,

我来自波兰,这是我在codeproject上的第一个教程,所以请勿苛责。我经常使用codeproject,因此我非常感谢这个codeproject社区。

对任何拼写或风格上的错误,我深表歉意。

 

使用代码

我最近在电视上看到一个名为“Magic Block Game”的棋盘游戏,它看起来非常有趣。因此,我决定写一篇教程,介绍如何用vb.net编写这样的游戏。当然,这将是其中的一课,我将向您展示如何做到这一点以及它是如何工作的。无论您是按时完成游戏,还是在单人电脑上竞争,甚至是在网络上竞争,我都已经为您铺平了道路。规则很简单:移动方块,使主棋盘的中心形成生成的图案,但只能移动紧邻空白区域的元素。游戏看起来是这样的

额外的好处是,我们可以在不丢失数据的情况下编辑大小。我们将从准备窗体开始。这里的工作量不大,因为所有元素都将自动生成。

 

元素类型元素名称设置
表单Form1

名称:Form1

文本:Magic blocks

大小:675;669

Splitter1Splitter1 大小:659;188
Panelpattern

 大小:282;152

背景颜色:黑色

位置:(Splitter上的任意位置)

PanelMainBoard

 大小:282;152

背景颜色:黑色

位置:(任意位置)

此外,我们还需要六个图块。我们的主棋盘将有25个格子,即6 * 4 = 24 + 一个空白格子。图块可供下载: tiles.zip

您需要将它们解压缩到一个文件夹中,然后按照gif的方式进行安装。

好的,一切就绪。我们将首先添加变量,然后添加我们将讨论的方法。

Imports System.Drawing.Drawing2D

Public Class Form1
    'Main Structure of the sliding elements
    Private Structure polaGryS
        Dim Col As Integer 'Color as an index in the listaKolorow
        Dim rect As Rectangle
        Dim x As Integer
        Dim y As Integer
        Dim img As Image
    End Structure

    Private Structure fPatternStr
        Dim col As Integer
        Dim x As Integer
        Dim y As Integer
        Dim img As Image
    End Structure


    'Pattern board
    Private fPattern(3, 3) As Rectangle
    'Main board game board
    Private mFields(4, 4) As Rectangle
    Private mFieldsBool(4, 4) As Boolean

    Dim mBitmap As Bitmap 'Game board bitmap (as a panel background)
    Dim patternBitmap As Bitmap 'Pattern bitmap (as a panel background)

    Dim PatternFieldSize As Integer 'Size of the pattern field
    Dim FieldSize As Integer 'Size of the main board field

    Dim GameStart As Boolean = False 'Variable separating pattern generation from variation of mold size
    Dim GameOverList As New List(Of Integer) 'Checklist for the end of the game

    Dim ColorsList As New List(Of Image) ' List of images (in this case colors)

    Dim PatternCollection As New Collection 'The collection stores the elements of the pattern fields

    'Keeps list of game fields (list, not collection, because list is more flexible)
    Dim FieldCollection As New List(Of polaGryS)

    Dim ran As New Random

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class

我们需要导入System.Drawing.Drawing2D库,其中的元素用于将图像的大小调整为格子的大小。我们将开始准备我们的图块,将它们添加到“Color List”列表中,我们将创建一个方法来完成此操作,然后再生成图案和格子。添加到项目中。

    Private Sub ColorSet()
        'Takes pictures of moving squares
        ColorsList.Add(My.Resources.czerwony2)
        ColorsList.Add(My.Resources.bialy2)
        ColorsList.Add(My.Resources.niebieski)
        ColorsList.Add(My.Resources.pomarancz)
        ColorsList.Add(My.Resources.zielony)
        ColorsList.Add(My.Resources.zolty)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Set the game
        ColorSet()
        SizeChange()
        GameStart = True
    End Sub

“ColorSet()”方法将填充我们的图块列表。您需要调整列表中项目的数量,使其能被可能的选择数量整除。24。现在您可以选择一个图块作为4个,因为4 * 6 = 24。如果我们想增加或减少图块的数量,我们可以这样做:6 * 4 = 24,或者8 * 3 = 24,3 * 8 = 24,2 * 12 = 24,1 * 24 = 24 等。当我们加载图块列表后,我们转到图案和主游戏棋盘的设置。您需要调整它们的大小,图案棋盘的大小将取决于Splitter1的大小,游戏棋盘的大小取决于模具的大小。将该方法添加到项目中。

    Private Sub SizeChange()
        'Sets the size of the game fields and pattern
        pattern.Size = New Size(Splitter1.Height - 10, Splitter1.Height - 10) 'Size of the board
        pattern.Location = New Point((Splitter1.Width - pattern.Width) / 2, 5) 'The pattern board is always in the middle
        'The size of the game board
        If Me.Width > (Me.Height - Splitter1.Height) Then
            MainBoard.Size = New Size((Me.Height - Splitter1.Height) - 60, (Me.Height - Splitter1.Height) - 60)
        Else
            MainBoard.Size = New Size((Me.Width) - 20, (Me.Width) - 20)
        End If
        'Location in the middle
        MainBoard.Location = New Point((Me.Width - MainBoard.Width) / 2, Splitter1.Height + 10)
        'generuj pola (wzoru i planszy główenj)
        FieldsGenerator()
    End Sub

看起来是这样的

以下方法将负责实时更改。

    Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize
        'Enabled only after game preparation
        If GameStart = True Then
            SizeChange()
        End If
    End Sub

当主窗体大小改变时,棋盘的大小将更新。现在,让我们看看图案和游戏格子的设计,为了使游戏有意义,格子必须随机生成。至于图案,它的格子不会改变位置,所以这个元素将更静态。主游戏棋盘将更复杂,这个空间将由几个元素组成。

(PlanszaGlowna=MainBoard,polaGlowne=mFields,polaGlowneBool=mFieldsBool, kolekcjapol=FieldCollection)

mFieldsBool - 指定空白格子的位置。

mFields - 它们的大小在棋盘大小改变时生成。我们将它们放入我们的图像颜色。

此方法有两个状态。第一个是游戏开始时运行,它准备两个游戏棋盘。第二个是在加载棋盘并更改其大小时运行。

     Private Sub FieldsGenerator()

        ' Prepares a clear map Of the fields, their size And location 
        'then clone the board of the game (pattern And main game board) nad writes in variables: mBitmap And patternBitmap
        ' which we paint appropriate squares

        Dim MainBitmapRize As New Bitmap(MainBoard.Width, MainBoard.Height)
        Dim PatternBitmapRise As New Bitmap(pattern.Width, pattern.Height)

        Dim g As Graphics = Graphics.FromImage(MainBitmapRize)
        Dim g2 As Graphics = Graphics.FromImage(PatternBitmapRise)

        PatternFieldSize = (pattern.Width / 3) - 3
        FieldSize = (MainBoard.Width / 5) - 5

        If GameStart = False Then
            'Generated in the beginning, it sets the squares, their size and location
            Dim Fcolor As New SolidBrush(Color.FromArgb(60, 240, 240, 240))
            For i As Integer = 0 To 4
                For j As Integer = 0 To 4
                    mFields(i, j) = New Rectangle(10 + (FieldSize * i), 10 + (FieldSize * j), FieldSize - 1, FieldSize - 1)
                    g.FillRectangle(Fcolor, mFields(i, j))

                    If Not j >= 3 Then
                        If Not i >= 3 Then
                            fPattern(i, j) = New Rectangle(5 + (PatternFieldSize * i), 5 + (PatternFieldSize * j), PatternFieldSize - 1, PatternFieldSize - 1)
                            g2.FillRectangle(Fcolor, fPattern(i, j))
                        End If
                    End If
                    'The mFieldsBool array defines which square is empty, in this case, the lower right corner
                    If Not (i = 4 And j = 4) Then
                        mFieldsBool(i, j) = False
                    Else
                        mFieldsBool(i, j) = True
                    End If
                Next
            Next
        Else
            'If this is not the beginning of the game, change only the size and position of the fields, we change the size of the fields in the list
            Dim Fcolor As New SolidBrush(Color.FromArgb(60, 240, 240, 240))
            For i As Integer = 0 To 4
                For j As Integer = 0 To 4
                    'We change the size of the public board
                    mFields(i, j) = New Rectangle(10 + (FieldSize * i), 10 + (FieldSize * j), FieldSize - 1, FieldSize - 1)
                    g.FillRectangle(Fcolor, mFields(i, j))
                    For a As Integer = 0 To FieldCollection.Count - 1
                        If i = FieldCollection(a).x And j = FieldCollection(a).y Then
                            'We assign the changed field to the structure list
                            Dim p As polaGryS = FieldCollection(a)
                            p.rect = mFields(i, j)
                            FieldCollection(a) = p
                        End If
                    Next
                Next
            Next
        End If
        'We set the background of the panels
        mBitmap = MainBitmapRize.Clone
        patternBitmap = PatternBitmapRise.Clone
        g.Dispose()
        g2.Dispose()
        'If this is the beginning of the game, you should prepare the pattern and randomly lay out colors
        If GameStart = False Then
            GenerateBoards()
        Else
            'If this is the beginning of the game, you should prepare the pattern and randomly lay out colors
            CompleteFields()
        End If

    End Sub

暂时什么也没发生,因为我们只准备了主格子并确定了它们的大小。如果我们添加两行代码,我们将看到已经完成的工作。

        pattern.BackgroundImage = patternBitmap
        MainBoard.BackgroundImage = mainBitmap

现在我们将使用“GenerateBoards()”方法,该方法将图块加载到棋盘上并准备好空白格子。

     Private Sub GenerateBoards()
        ' collection and the list of fields 

        'initiate the structure
        Dim FieldsW As fPatternStr
        Dim polaG As polaGryS
        'We create instant bitmaps to avoid damaging the main bitmap
        Dim iPatternBitmap As Bitmap = patternBitmap.Clone
        Dim iMainFields As Bitmap = mBitmap.Clone

        Dim gr_dest As Graphics = Graphics.FromImage(iPatternBitmap)
        Dim gr_dest2 As Graphics = Graphics.FromImage(iMainFields)

        Dim drawList(5) As Integer ' The board will ensure that the colors are no more than 4
        'We start by creating a 3x3 pattern
        For i As Integer = 0 To 2
            For j As Integer = 0 To 2
                'We draw the color
                Dim rand As Integer
                Do
                    rand = ran.Next(0, 6)
                    'If we have three draws of such colors, then we draw again
                    If drawList(rand) + 1 <= 4 Then
                        Exit Do
                    End If
                Loop
                'We fill in the data structure
                FieldsW.x = i
                FieldsW.y = j
                FieldsW.col = rand
                FieldsW.img = cheSize(ColorsList(rand), New Size(PatternFieldSize, PatternFieldSize))

                GameOverList.Add(rand)
                drawList(rand) += 1 'We add a color to the list watching the color amount
                'drw elements
                gr_dest.DrawImage(FieldsW.img, fPattern(i, j).Location.X, fPattern(i, j).Location.Y, FieldsW.img.Width - 1, FieldsW.img.Height - 1)
                PatternCollection.Add(FieldsW) ' We complete the collection
            Next
        Next
        'we generete 5x5 board
        'clean board
        ReDim drawList(5) ' The board will ensure that the colors are no more than 4
        For i As Integer = 0 To 4
            For j As Integer = 0 To 4
                'Complements only false fields, leaving one clean field
                If mFieldsBool(i, j) = False Then
                    'We draw the color
                    Dim rand As Integer
                    Do
                        rand = ran.Next(0, 6)
                        If drawList(rand) + 1 <= 4 Then
                            Exit Do
                        End If
                    Loop
                    'We fill in the data structure
                    polaG.rect = mFields(i, j)
                    polaG.x = i
                    polaG.y = j
                    polaG.Col = rand
                    polaG.img = cheSize(ColorsList(rand), New Size(FieldSize, FieldSize))

                    drawList(rand) += 1 'We add a color to the list watching the color amount
                    'drw elements
                    gr_dest2.DrawImage(polaG.img, mFields(i, j).Location.X, mFields(i, j).Location.Y, polaG.img.Width - 1, polaG.img.Height - 1)
                    FieldCollection.Add(polaG) ' We complete the collection
                End If
            Next
        Next
        'We set new panel backgrounds
        pattern.Image = iPatternBitmap
        MainBoard.Image = iMainFields
    End Sub

变量“rand”负责绘制图块,“drawList”确保一个图块不超过四个。如果您想增加或减少图块的数量,您必须根据您拥有的图块数量更改这些选项。根据格子的大小,我们必须准备我们图块的图像,使其大小等于预定义的格子大小。因此,“cheSize()”函数负责缩小或放大其大小,它使用System.Drawing.Drawing2D库。

     Public Shared Function cheSize(ByVal img As Image, ByVal largeness As Size, Optional ByVal keepImageRatio As Boolean = True) As Image

        Dim NewWidth As Integer
        Dim NewHeight As Integer
        'We determine the size of the new image, depending on whether the aspect ratio is to be retained
        If keepImageRatio Then
            Dim OriginalWidth As Integer = img.Width
            Dim OriginalHeight As Integer = img.Height
            Dim PreWidth As Single = CSng(largeness.Width) / CSng(OriginalWidth)
            Dim PreHeight As Single = CSng(largeness.Height) / CSng(OriginalHeight)
            Dim Percent As Single = If(PreHeight < PreWidth,
                PreHeight, PreWidth)
            NewWidth = CInt(OriginalWidth * Percent)
            NewHeight = CInt(OriginalHeight * Percent)
        Else
            NewWidth = largeness.Width
            NewHeight = largeness.Height
        End If

        Dim newImg As Image = New Bitmap(NewWidth, NewHeight)
        'We generate a new image
        Using graphicsHandle As Graphics = Graphics.FromImage(newImg)
            graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
            graphicsHandle.DrawImage(img, 0, 0, NewWidth, NewHeight)
        End Using
        'Function returns newImg
        Return newImg
    End Function

它是如何工作的

如我们所见,格子已经添加,但我们的格子不符合窗体的大小。由于“Form1_Resize”方法已经将“GameStart”变量设置为true,这意味着如果我们更改窗体大小,则会调用CompleteFields()。如果在调整大小时调用“GenerateBoards()”方法,那么在每次更改时,程序都会生成一个新的随机图案和新的随机格子。所以,让我们添加“CompleteFields()”方法。

     Private Sub CompleteFields()
        'Method of changing game size pattern and board already generated
        Dim iPatternBitmap As Bitmap = patternBitmap.Clone
        Dim iMainBitmap As Bitmap = mBitmap.Clone

        Dim gr_dest As Graphics = Graphics.FromImage(iPatternBitmap)
        Dim gr_dest2 As Graphics = Graphics.FromImage(iMainBitmap)
        'First, we draw the colors Of the pattern On the basis Of the altered size Of the fields,
        'although Nothing Is tampered with here (by the molds of the mold) adds it may be useful to someone who does Not spoil,
        'you can remove it if you want
        For i As Integer = 0 To 2
            For j As Integer = 0 To 2
                For a As Integer = 1 To PatternCollection.Count
                    If i = PatternCollection(a).x And j = PatternCollection(a).y Then
                        Dim myimage As Image = cheSize(PatternCollection(a).img, New Size(PatternFieldSize, PatternFieldSize))
                        gr_dest.DrawImage(myimage, fPattern(i, j).Location.X, fPattern(i, j).Location.Y, myimage.Width - 1, myimage.Height - 1)
                    End If
                Next
            Next
        Next
        pattern.Image = iPatternBitmap 'We set the pattern panel background
        'Then we draw the colors of the main fields of the board based on these field sizes
        For i As Integer = 0 To 4
            For j As Integer = 0 To 4
                If mFieldsBool(i, j) = False Then
                    For a As Integer = 0 To FieldCollection.Count - 1
                        If i = FieldCollection(a).x And j = FieldCollection(a).y Then
                            Dim myimage As Image = cheSize(FieldCollection(a).img, New Size(FieldSize, FieldSize))
                            gr_dest2.DrawImage(myimage, FieldCollection(a).rect.Location.X, FieldCollection(a).rect.Location.Y, myimage.Width - 1, myimage.Height - 1)
                        End If
                    Next
                End If
            Next
        Next
        MainBoard.Image = iMainBitmap
        GameOver()
    End Sub

该方法的第一步实际上什么也不做,因为图案棋盘是固定的。第二步根据“collectipipol”设置图块,其中包含格子的坐标、大小和图片。最后一项是控制游戏是否结束。

     Private Sub GameOver()
        'The method checks the end of the game
        Dim YouWin As Boolean = True 'We assume it's over
        Dim CompList As New List(Of Integer) ' List storing the color numbers of the center of the board
        'Get the color numbers in the middle of the board
        For i As Integer = 1 To 3
            For j As Integer = 1 To 3
                For k As Integer = 0 To FieldCollection.Count - 1
                    If FieldCollection(k).x = i And FieldCollection(k).y = j Then
                        CompList.Add(FieldCollection(k).Col)
                    End If
                Next
            Next
        Next
        ' If there is no empty field in the middle, the method goes to comparing the list
        If CompList.Count = GameOverList.Count Then
            For i As Integer = 0 To GameOverList.Count - 1
                If Not GameOverList(i) = CompList(i) Then
                    'The lists are not identical, the end of the comparison
                    YouWin = False
                    Exit For
                End If
            Next
            If YouWin = True Then
                'Lists are the same
                MainBoard.Enabled = False
                MsgBox("Game over!")
            End If
        End If
    End Sub

它创建一个比较列表,其中存储了“ColorsList”列表中的图像索引。然后进行比较,如果它们相同(图案 - 游戏棋盘中心),则游戏结束。

当然,这并不是项目的结束,因为我们必须以某种方式移动图块。我们将从功能键开始。我将使用箭头键,但您可以选择其他键。您必须考虑对您来说最方便的方式。如果您按下向上箭头,哪个图块将移动?您可以将空白格子下方的图块向上移动,或者将空白格子上方的图块向下移动。

     Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        'Move with the arrow keys
        Dim Ilocation As Integer
        Dim Jlocation As Integer
        'We are getting the location of the empty field
        For i As Integer = 0 To 4
            For j As Integer = 0 To 4
                If mFieldsBool(i, j) = True Then
                    Ilocation = i
                    Jlocation = j
                End If
            Next
        Next
        'If this is not the end of the game, then we will check which key was used
        If MainBoard.Enabled = True Then
            If e.KeyCode = Keys.Down Then
                'down key
                If Not Jlocation = 0 Then 'If it was less than 0 then it would be outside the array
                    For a As Integer = 0 To FieldCollection.Count - 1
                        'Gets the location of the field above the blank field
                        If Ilocation = FieldCollection(a).x And (Jlocation - 1) = FieldCollection(a).y Then
                            'And change them to new ones
                            Dim p As polaGryS = FieldCollection(a)
                            p.y = Jlocation
                            p.rect = mFields(Ilocation, Jlocation)
                            FieldCollection(a) = p 'Overwrites an item in the list
                        End If
                    Next
                    'Changes the field's boolean value
                    mFieldsBool(Ilocation, Jlocation) = False
                    mFieldsBool(Ilocation, Jlocation - 1) = True
                End If
            ElseIf e.KeyCode = Keys.Up Then
                If Not Jlocation = 4 Then 'If it was more than 4 then it would be outside the array
                    For a As Integer = 0 To FieldCollection.Count - 1
                        If Ilocation = FieldCollection(a).x And (Jlocation + 1) = FieldCollection(a).y Then
                            Dim p As polaGryS = FieldCollection(a)
                            p.y = Jlocation
                            p.rect = mFields(Ilocation, Jlocation)
                            FieldCollection(a) = p
                        End If
                    Next
                    mFieldsBool(Ilocation, Jlocation) = False
                    mFieldsBool(Ilocation, Jlocation + 1) = True
                End If
            ElseIf e.KeyCode = Keys.Right Then
                If Not Ilocation = 0 Then 'If it was less than 0 then it would be outside the array
                    For a As Integer = 0 To FieldCollection.Count - 1
                        If (Ilocation - 1) = FieldCollection(a).x And Jlocation = FieldCollection(a).y Then
                            Dim p As polaGryS = FieldCollection(a)
                            p.x = Ilocation
                            p.rect = mFields(Ilocation, Jlocation)
                            FieldCollection(a) = p

                        End If
                    Next
                    mFieldsBool(Ilocation, Jlocation) = False
                    mFieldsBool(Ilocation - 1, Jlocation) = True
                End If
            ElseIf e.KeyCode = Keys.Left Then
                If Not Ilocation = 4 Then 'If it was more than 4 then it would be outside the array
                    For a As Integer = 0 To FieldCollection.Count - 1
                        If (Ilocation + 1) = FieldCollection(a).x And Jlocation = FieldCollection(a).y Then
                            Dim p As polaGryS = FieldCollection(a)
                            p.x = Ilocation
                            p.rect = mFields(Ilocation, Jlocation)
                            FieldCollection(a) = p
                        End If
                    Next
                    mFieldsBool(Ilocation, Jlocation) = False
                    mFieldsBool(Ilocation + 1, Jlocation) = True
                End If
            End If
        End If
        'Redraw new colors
        CompleteFields()
    End Sub

相对于空白格子的我的设置是:

 向下 箭头

 向上箭头

 向左箭头

 向右箭头

代码从“mFieldsBool”表中检索空白格子的位置,并根据按下的键定位格子。然后它克隆结构并更改其x或y格子,并占据(rect)格子。

鼠标光标会稍微复杂一些。我们向“MainBoard”面板添加“MouseMove”事件。当光标在面板上时,将下载e.x和e.y的光标坐标。然后,通过指定空白格子的位置,我们在“FieldCollection”集合中搜索,并检查光标是否不在左、右、上或下方的(rect)格子上。如果不是,光标将变成一个手柄,否则是箭头。

     'mouse click
    Private Sub PlanszaGlowna_MouseMove(sender As Object, e As MouseEventArgs) Handles MainBoard.MouseMove
        'To be able to select a field using the left mouse button, you first need to specify where the mouse cursor is
        Dim Ilocation As Integer
        Dim Jlocation As Integer

        For i As Integer = 0 To 4
            For j As Integer = 0 To 4
                If mFieldsBool(i, j) = True Then
                    Ilocation = i
                    Jlocation = j
                End If
            Next
        Next

        For i As Integer = 0 To FieldCollection.Count - 1
            If (FieldCollection(i).rect.Location.X - 2 <= e.X And (FieldCollection(i).rect.Location.X + FieldCollection(i).rect.Width + 2 >= e.X)) _
                And (FieldCollection(i).rect.Location.Y - 2 <= e.Y And
                (FieldCollection(i).rect.Location.Y + FieldCollection(i).rect.Height + 2 >= e.Y)) Then
                'If the cursor is to the left or to the right of the empty field, the cursor will change to the handle
                If (FieldCollection(i).x - 1 = Ilocation Or FieldCollection(i).x + 1 = Ilocation) And FieldCollection(i).y = Jlocation Then
                    Me.Cursor = Cursors.Hand
                    Exit For
                End If
                'If the cursor is above or below the empty field, the cursor will change the type to the handle
                If (FieldCollection(i).y - 1 = Jlocation Or FieldCollection(i).y + 1 = Jlocation) And FieldCollection(i).x = Ilocation Then
                    Me.Cursor = Cursors.Hand
                    Exit For
                End If
            Else
                'The cursor will remain an arrow or change the arrow type if the above conditions are not met
                Me.Cursor = Cursors.Arrow
            End If
        Next
    End Sub

效果如下面的gif所示。

正如您所见,光标已更改为手柄状态。至于单击方法,它与使用按键时相同。该方法检查光标是否位于紧邻空白格子的格子上(代码从MouseMove复制),如果指定了哪个格子,则克隆其结构,用空白格子替换数据,然后写入集合。

      Private Sub PlanszaGlowna_MouseClick(sender As Object, e As MouseEventArgs) Handles MainBoard.MouseClick
        Dim Ilocation As Integer
        Dim Jlocation As Integer
        'Gets the location of the empty field
        For i As Integer = 0 To 4
            For j As Integer = 0 To 4
                If mFieldsBool(i, j) = True Then
                    Ilocation = i
                    Jlocation = j
                End If
            Next
        Next
        'Looks at which field is the cursor
        For i As Integer = 0 To FieldCollection.Count - 1
            If (FieldCollection(i).rect.Location.X - 2 <= e.X And (FieldCollection(i).rect.Location.X +
                FieldCollection(i).rect.Width + 2 >= e.X)) And (FieldCollection(i).rect.Location.Y - 2 <= e.Y _
                And (FieldCollection(i).rect.Location.Y + FieldCollection(i).rect.Height + 2 >= e.Y)) Then
                'If you click the box to the right of the empty field
                If FieldCollection(i).x + 1 = Ilocation And FieldCollection(i).y = Jlocation Then
                    'Will retrieve the field structure from the list
                    Dim p As polaGryS = FieldCollection(i)
                    'change x location
                    p.x = Ilocation
                    'Will assign a new square
                    p.rect = mFields(Ilocation, Jlocation)
                    'Will overwrite the new structure in the list
                    FieldCollection(i) = p
                    'set new true/false position
                    mFieldsBool(Ilocation, Jlocation) = False
                    mFieldsBool(Ilocation - 1, Jlocation) = True
                End If
                'If you clicked the box to the left of the empty field
                If FieldCollection(i).x - 1 = Ilocation And FieldCollection(i).y = Jlocation Then
                    Dim p As polaGryS = FieldCollection(i)
                    p.x = Ilocation
                    p.rect = mFields(Ilocation, Jlocation)
                    FieldCollection(i) = p
                    mFieldsBool(Ilocation, Jlocation) = False
                    mFieldsBool(Ilocation + 1, Jlocation) = True
                End If
                'If you clicked the box is above the empty field
                If FieldCollection(i).y - 1 = Jlocation And FieldCollection(i).x = Ilocation Then
                    Dim p As polaGryS = FieldCollection(i)
                    p.y = Jlocation
                    p.rect = mFields(Ilocation, Jlocation)
                    FieldCollection(i) = p

                    mFieldsBool(Ilocation, Jlocation) = False
                    mFieldsBool(Ilocation, Jlocation + 1) = True
                End If
                'If you clicked the box is below the empty field
                If FieldCollection(i).y + 1 = Jlocation And FieldCollection(i).x = Ilocation Then
                    Dim p As polaGryS = FieldCollection(i)
                    p.y = Jlocation
                    p.rect = mFields(Ilocation, Jlocation)
                    FieldCollection(i) = p
                    mFieldsBool(Ilocation, Jlocation) = False
                    mFieldsBool(Ilocation, Jlocation - 1) = True
                End If
            End If
        Next
        CompleteFields()
    End Sub

好的,游戏准备就绪。

现在轮到您了,根据您的喜好创建和添加。创建您想要的游戏。

© . All rights reserved.