The procedure ClickPiece() is called whenever the user clicks one of the puzzle pieces with the mouse:
Sub ClickPiece()
If FirstActive Then
Set FirstPiece = ActiveSheet.Shapes(Application.Caller)
FirstPiece.PictureFormat.Brightness = 0.25
FirstActive = False
Else
FirstPiece.PictureFormat.Brightness = 0.5
SwapPieces FirstPiece, ActiveSheet.Shapes(Application.Caller)
Set FirstPiece = Nothing
CheckPositions
FirstActive = True
End If
End Sub
Explanation:
- The procedure first checks whether the clicked puzzle piece is the first or second piece involved in a swap operation.
- At the end of the
StartGame()procedure, just before the first swap, the Boolean variableFirstActiveis set to True. - The
Callerproperty of theApplicationobject returns a reference to the object (shape) that triggered the VBA procedure — in this case, the clicked puzzle piece. - If
FirstActiveis True, the reference to the clicked puzzle piece is stored in the module-level variableFirstPiece. - The piece’s brightness is reduced from the default 0.5 to 0.25 via its
PictureFormat.Brightnessproperty, making it appear slightly darker. - This visual cue helps the user identify which puzzle piece has been selected first for swapping.
FirstActiveis then set to False, signaling that the next clicked piece will be the second in the swap.- If
FirstActiveis False, the brightness of the first selected piece is reset back to normal (0.5). - The procedure
SwapPieces()is called next with two parameters: references to the first selected puzzle piece and the currently clicked (second) puzzle piece. This procedure swaps their positions. - After each swap, the procedure
CheckPositions()is called to verify whether all puzzle pieces are now correctly positioned. - Finally,
FirstActiveis reset to True to prepare for the next swap operation.