Block.vb
上传用户:c_chunwei
上传日期:2022-05-07
资源大小:80k
文件大小:23k
源码类别:

其他智力游戏

开发平台:

Visual Basic

  1. Imports System
  2. Imports System.Drawing
  3. Imports System.Drawing.Drawing2D
  4. Namespace Nettrix
  5.     Public Class Block
  6.         Public Enum BlockTypes
  7.             Undefined = 0
  8.             Square = 1
  9.             Line = 2
  10.             J = 3
  11.             L = 4
  12.             T = 5
  13.             Z = 6
  14.             S = 7
  15.         End Enum 'BlockTypes
  16.         Private ActualBlockType As BlockTypes
  17.         Public Property BlockType() As BlockTypes
  18.             Get
  19.                 Return ActualBlockType
  20.             End Get
  21.             Set(ByVal Value As BlockTypes)
  22.                 ActualBlockType = Value
  23.             End Set
  24.         End Property
  25.         Public Enum RotationDirections
  26.             North = 1
  27.             East = 2
  28.             South = 3
  29.             West = 4
  30.         End Enum 'RotationDirections
  31.         Private ActualStatusRotation As RotationDirections = RotationDirections.North
  32.         Public Property StatusRotation() As RotationDirections
  33.             Get
  34.                 Return ActualStatusRotation
  35.             End Get
  36.             Set(ByVal Value As RotationDirections)
  37.                 ActualStatusRotation = Value
  38.             End Set
  39.         End Property
  40.         ' The Colors Of Each Block Type 
  41.         Private BackColors As Color() = {Color.Empty, Color.Red, Color.Blue, Color.Red, Color.Yellow, Color.Green, Color.White, Color.Black}
  42.         Private ForeColors As Color() = {Color.Empty, Color.Purple, Color.LightBlue, Color.Yellow, Color.Red, Color.LightGreen, Color.Black, Color.White}
  43.         ' The 4 Squares That Compose The Block
  44.         Private Actualsquare1 As Square
  45.         Private Actualsquare2 As Square
  46.         Private Actualsquare3 As Square
  47.         Private Actualsquare4 As Square
  48.         Public Property Square1() As Square
  49.             Get
  50.                 Return Actualsquare1
  51.             End Get
  52.             Set(ByVal Value As Square)
  53.                 Actualsquare1 = Value
  54.             End Set
  55.         End Property
  56.         Public Property Square2() As Square
  57.             Get
  58.                 Return Actualsquare2
  59.             End Get
  60.             Set(ByVal Value As Square)
  61.                 Actualsquare2 = Value
  62.             End Set
  63.         End Property
  64.         Public Property Square3() As Square
  65.             Get
  66.                 Return Actualsquare3
  67.             End Get
  68.             Set(ByVal Value As Square)
  69.                 Actualsquare3 = Value
  70.             End Set
  71.         End Property
  72.         Public Property Square4() As Square
  73.             Get
  74.                 Return Actualsquare4
  75.             End Get
  76.             Set(ByVal Value As Square)
  77.                 Actualsquare4 = Value
  78.             End Set
  79.         End Property
  80.         Private SquareSize As Integer = GameField.SquareSize
  81.         Private Shared Random As New Random
  82.         Public Function Top() As Integer
  83.             Return Math.Min(Square1.Location.Y, Math.Min(Square2.Location.Y, Math.Min(Square3.Location.Y, Square4.Location.Y)))
  84.         End Function 'Top
  85.         Public Sub New(ByVal Location As Point, ByVal NewBlockType As BlockTypes)
  86.             ' Create The New Block
  87.             If NewBlockType = BlockTypes.Undefined Then
  88.                 BlockType = CType(Random.Next(7) + 1, BlockTypes)
  89.                 'BlockType = BlockTypes.Line 'Force Line Block For Testing
  90.             Else
  91.                 BlockType = NewBlockType
  92.             End If
  93.             ' Create Each Of The Squares Of The Block
  94.             ' Set The Square Colors, Based On The Block Type
  95.             Square1 = New Square(New Size(SquareSize, SquareSize), BackColors(CInt(BlockType)), ForeColors(CInt(BlockType)))
  96.             Square2 = New Square(New Size(SquareSize, SquareSize), BackColors(CInt(BlockType)), ForeColors(CInt(BlockType)))
  97.             Square3 = New Square(New Size(SquareSize, SquareSize), BackColors(CInt(BlockType)), ForeColors(CInt(BlockType)))
  98.             Square4 = New Square(New Size(SquareSize, SquareSize), BackColors(CInt(BlockType)), ForeColors(CInt(BlockType)))
  99.             ' Set The Squares Positions Based On The Block Type
  100.             Select Case BlockType
  101.                 Case BlockTypes.Square
  102.                     Square1.Location = New Point(Location.X, Location.Y)
  103.                     Square2.Location = New Point(Location.X + SquareSize, Location.Y)
  104.                     Square3.Location = New Point(Location.X, Location.Y + SquareSize)
  105.                     Square4.Location = New Point(Location.X + SquareSize, Location.Y + SquareSize)
  106.                 Case BlockTypes.Line
  107.                     Square1.Location = New Point(Location.X, Location.Y)
  108.                     Square2.Location = New Point(Location.X, Location.Y + SquareSize)
  109.                     Square3.Location = New Point(Location.X, Location.Y + 2 * SquareSize)
  110.                     Square4.Location = New Point(Location.X, Location.Y + 3 * SquareSize)
  111.                 Case BlockTypes.J
  112.                     Square1.Location = New Point(Location.X + SquareSize, Location.Y)
  113.                     Square2.Location = New Point(Location.X + SquareSize, Location.Y + SquareSize)
  114.                     Square3.Location = New Point(Location.X + SquareSize, Location.Y + 2 * SquareSize)
  115.                     Square4.Location = New Point(Location.X, Location.Y + 2 * SquareSize)
  116.                 Case BlockTypes.L
  117.                     Square1.Location = New Point(Location.X, Location.Y)
  118.                     Square2.Location = New Point(Location.X, Location.Y + SquareSize)
  119.                     Square3.Location = New Point(Location.X, Location.Y + 2 * SquareSize)
  120.                     Square4.Location = New Point(Location.X + SquareSize, Location.Y + 2 * SquareSize)
  121.                 Case BlockTypes.T
  122.                     Square1.Location = New Point(Location.X, Location.Y)
  123.                     Square2.Location = New Point(Location.X + SquareSize, Location.Y)
  124.                     Square3.Location = New Point(Location.X + 2 * SquareSize, Location.Y)
  125.                     Square4.Location = New Point(Location.X + SquareSize, Location.Y + SquareSize)
  126.                 Case BlockTypes.Z
  127.                     Square1.Location = New Point(Location.X, Location.Y)
  128.                     Square2.Location = New Point(Location.X + SquareSize, Location.Y)
  129.                     Square3.Location = New Point(Location.X + SquareSize, Location.Y + SquareSize)
  130.                     Square4.Location = New Point(Location.X + 2 * SquareSize, Location.Y + SquareSize)
  131.                 Case BlockTypes.S
  132.                     Square1.Location = New Point(Location.X, Location.Y + SquareSize)
  133.                     Square2.Location = New Point(Location.X + SquareSize, Location.Y + SquareSize)
  134.                     Square3.Location = New Point(Location.X + SquareSize, Location.Y)
  135.                     Square4.Location = New Point(Location.X + 2 * SquareSize, Location.Y)
  136.             End Select
  137.         End Sub 'New
  138.         Public Sub Rotate()
  139.             ' Store The Current Block Position
  140.             Dim OldPosition1 As Point = Square1.Location
  141.             Dim OldPosition2 As Point = Square2.Location
  142.             Dim OldPosition3 As Point = Square3.Location
  143.             Dim OldPosition4 As Point = Square4.Location
  144.             Dim OldStatusRotation As RotationDirections = StatusRotation
  145.             Hide(GameField.WinHandle)
  146.             ' Rotate The Blocks
  147.             Select Case BlockType
  148.                 Case BlockTypes.Square
  149.                     ' Do Nothing. Squares Don'T Rotate.
  150.                 Case BlockTypes.Line
  151.                     ' Rotate All Squares Around Square 2
  152.                     Select Case StatusRotation
  153.                         Case RotationDirections.North
  154.                             StatusRotation = RotationDirections.East
  155.                             Square1.Location = New Point(Square2.Location.X - SquareSize, Square2.Location.Y)
  156.                             Square3.Location = New Point(Square2.Location.X + SquareSize, Square2.Location.Y)
  157.                             Square4.Location = New Point(Square2.Location.X + 2 * SquareSize, Square2.Location.Y)
  158.                         Case RotationDirections.East
  159.                             StatusRotation = RotationDirections.North
  160.                             Square1.Location = New Point(Square2.Location.X, Square2.Location.Y - SquareSize)
  161.                             Square3.Location = New Point(Square2.Location.X, Square2.Location.Y + SquareSize)
  162.                             Square4.Location = New Point(Square2.Location.X, Square2.Location.Y + 2 * SquareSize)
  163.                     End Select
  164.                 Case BlockTypes.J
  165.                     ' Rotate All Squares Around Square 3  
  166.                     Select Case StatusRotation
  167.                         Case RotationDirections.North
  168.                             StatusRotation = RotationDirections.East
  169.                             Square1.Location = New Point(Square3.Location.X, Square3.Location.Y - SquareSize)
  170.                             Square2.Location = New Point(Square3.Location.X + SquareSize, Square3.Location.Y)
  171.                             Square4.Location = New Point(Square3.Location.X + 2 * SquareSize, Square3.Location.Y)
  172.                         Case RotationDirections.East
  173.                             StatusRotation = RotationDirections.South
  174.                             Square1.Location = New Point(Square3.Location.X + SquareSize, Square3.Location.Y)
  175.                             Square2.Location = New Point(Square3.Location.X, Square3.Location.Y + SquareSize)
  176.                             Square4.Location = New Point(Square3.Location.X, Square3.Location.Y + 2 * SquareSize)
  177.                         Case RotationDirections.South
  178.                             StatusRotation = RotationDirections.West
  179.                             Square1.Location = New Point(Square3.Location.X, Square3.Location.Y + SquareSize)
  180.                             Square2.Location = New Point(Square3.Location.X - SquareSize, Square3.Location.Y)
  181.                             Square4.Location = New Point(Square3.Location.X - 2 * SquareSize, Square3.Location.Y)
  182.                         Case RotationDirections.West
  183.                             StatusRotation = RotationDirections.North
  184.                             Square1.Location = New Point(Square3.Location.X - SquareSize, Square3.Location.Y)
  185.                             Square2.Location = New Point(Square3.Location.X, Square3.Location.Y - SquareSize)
  186.                             Square4.Location = New Point(Square3.Location.X, Square3.Location.Y - 2 * SquareSize)
  187.                     End Select
  188.                 Case BlockTypes.L
  189.                     ' Rotate All Squares Around Square 3                
  190.                     Select Case StatusRotation
  191.                         Case RotationDirections.North
  192.                             StatusRotation = RotationDirections.East
  193.                             Square1.Location = New Point(Square3.Location.X + SquareSize, Square3.Location.Y)
  194.                             Square2.Location = New Point(Square3.Location.X + 2 * SquareSize, Square3.Location.Y)
  195.                             Square4.Location = New Point(Square3.Location.X, Square3.Location.Y + SquareSize)
  196.                         Case RotationDirections.East
  197.                             StatusRotation = RotationDirections.South
  198.                             Square1.Location = New Point(Square3.Location.X - SquareSize, Square3.Location.Y)
  199.                             Square2.Location = New Point(Square3.Location.X, Square3.Location.Y + SquareSize)
  200.                             Square4.Location = New Point(Square3.Location.X, Square3.Location.Y + 2 * SquareSize)
  201.                         Case RotationDirections.South
  202.                             StatusRotation = RotationDirections.West
  203.                             Square1.Location = New Point(Square3.Location.X - 2 * SquareSize, Square3.Location.Y)
  204.                             Square2.Location = New Point(Square3.Location.X - SquareSize, Square3.Location.Y)
  205.                             Square4.Location = New Point(Square3.Location.X, Square3.Location.Y - SquareSize)
  206.                         Case RotationDirections.West
  207.                             StatusRotation = RotationDirections.North
  208.                             Square1.Location = New Point(Square3.Location.X, Square3.Location.Y - 2 * SquareSize)
  209.                             Square2.Location = New Point(Square3.Location.X, Square3.Location.Y - SquareSize)
  210.                             Square4.Location = New Point(Square3.Location.X + SquareSize, Square3.Location.Y)
  211.                     End Select
  212.                 Case BlockTypes.T
  213.                     Select Case StatusRotation
  214.                         Case RotationDirections.North
  215.                             StatusRotation = RotationDirections.East
  216.                             Square1.Location = New Point(Square2.Location.X, Square2.Location.Y - SquareSize)
  217.                             Square3.Location = New Point(Square2.Location.X, Square2.Location.Y + SquareSize)
  218.                             Square4.Location = New Point(Square2.Location.X - SquareSize, Square2.Location.Y)
  219.                         Case RotationDirections.East
  220.                             StatusRotation = RotationDirections.South
  221.                             Square1.Location = New Point(Square2.Location.X + SquareSize, Square2.Location.Y)
  222.                             Square3.Location = New Point(Square2.Location.X - SquareSize, Square2.Location.Y)
  223.                             Square4.Location = New Point(Square2.Location.X, Square2.Location.Y - SquareSize)
  224.                         Case RotationDirections.South
  225.                             StatusRotation = RotationDirections.West
  226.                             Square1.Location = New Point(Square2.Location.X, Square2.Location.Y + SquareSize)
  227.                             Square3.Location = New Point(Square2.Location.X, Square2.Location.Y - SquareSize)
  228.                             Square4.Location = New Point(Square2.Location.X + SquareSize, Square2.Location.Y)
  229.                         Case RotationDirections.West
  230.                             StatusRotation = RotationDirections.North
  231.                             Square1.Location = New Point(Square2.Location.X - SquareSize, Square2.Location.Y)
  232.                             Square3.Location = New Point(Square2.Location.X + SquareSize, Square2.Location.Y)
  233.                             Square4.Location = New Point(Square2.Location.X, Square2.Location.Y + SquareSize)
  234.                     End Select
  235.                 Case BlockTypes.Z
  236.                     ' Rotate All Squares Around Square 2                
  237.                     Select Case StatusRotation
  238.                         Case RotationDirections.North
  239.                             StatusRotation = RotationDirections.East
  240.                             Square1.Location = New Point(Square2.Location.X, Square2.Location.Y - SquareSize)
  241.                             Square3.Location = New Point(Square2.Location.X - SquareSize, Square2.Location.Y)
  242.                             Square4.Location = New Point(Square2.Location.X - SquareSize, Square2.Location.Y + SquareSize)
  243.                         Case RotationDirections.East
  244.                             StatusRotation = RotationDirections.North
  245.                             Square1.Location = New Point(Square2.Location.X - SquareSize, Square2.Location.Y)
  246.                             Square3.Location = New Point(Square2.Location.X, Square2.Location.Y + SquareSize)
  247.                             Square4.Location = New Point(Square2.Location.X + SquareSize, Square2.Location.Y + SquareSize)
  248.                     End Select
  249.                 Case BlockTypes.S
  250.                     ' Rotate All Squares Around Square 2                
  251.                     Select Case StatusRotation
  252.                         Case RotationDirections.North
  253.                             StatusRotation = RotationDirections.East
  254.                             Square1.Location = New Point(Square2.Location.X, Square2.Location.Y - SquareSize)
  255.                             Square3.Location = New Point(Square2.Location.X + SquareSize, Square2.Location.Y)
  256.                             Square4.Location = New Point(Square2.Location.X + SquareSize, Square2.Location.Y + SquareSize)
  257.                         Case RotationDirections.East
  258.                             StatusRotation = RotationDirections.North
  259.                             Square1.Location = New Point(Square2.Location.X - SquareSize, Square2.Location.Y)
  260.                             Square3.Location = New Point(Square2.Location.X, Square2.Location.Y - SquareSize)
  261.                             Square4.Location = New Point(Square2.Location.X + SquareSize, Square2.Location.Y - SquareSize)
  262.                     End Select
  263.             End Select
  264.             ' After Rotating The Squares, Test If They Overlap Other Squares.  
  265.             '   If So, Return To Original Position
  266.             If Not (GameField.IsEmpty(Square1.Location.X / SquareSize, Square1.Location.Y / SquareSize) AndAlso GameField.IsEmpty(Square2.Location.X / SquareSize, Square2.Location.Y / SquareSize) AndAlso GameField.IsEmpty(Square3.Location.X / SquareSize, Square3.Location.Y / SquareSize) AndAlso GameField.IsEmpty(Square4.Location.X / SquareSize, Square4.Location.Y / SquareSize)) Then
  267.                 StatusRotation = OldStatusRotation
  268.                 Square1.Location = OldPosition1
  269.                 Square2.Location = OldPosition2
  270.                 Square3.Location = OldPosition3
  271.                 Square4.Location = OldPosition4
  272.             End If
  273.             Show(GameField.WinHandle)
  274.         End Sub 'Rotate
  275.         Public Function Down() As Boolean
  276.             ' If There's No Block Below The Current One, Go Down         
  277.             If GameField.IsEmpty(Square1.Location.X / SquareSize, Square1.Location.Y / SquareSize + 1) AndAlso GameField.IsEmpty(Square2.Location.X / SquareSize, Square2.Location.Y / SquareSize + 1) AndAlso GameField.IsEmpty(Square3.Location.X / SquareSize, Square3.Location.Y / SquareSize + 1) AndAlso GameField.IsEmpty(Square4.Location.X / SquareSize, Square4.Location.Y / SquareSize + 1) Then
  278.                 ' Hide The Block (In The Previous Position)
  279.                 Hide(GameField.WinHandle)
  280.                 ' Update The Block Position
  281.                 Square1.Location = New Point(Square1.Location.X, Square1.Location.Y + SquareSize)
  282.                 Square2.Location = New Point(Square2.Location.X, Square2.Location.Y + SquareSize)
  283.                 Square3.Location = New Point(Square3.Location.X, Square3.Location.Y + SquareSize)
  284.                 Square4.Location = New Point(Square4.Location.X, Square4.Location.Y + SquareSize)
  285.                 ' Draw The Block In The New Position
  286.                 Show(GameField.WinHandle)
  287.                 Return True
  288.             Else
  289.                 ' If There's A Block Below The Current One, Doesn't Go Down 
  290.                 ' -> Put It On The Array That Controls The Game And Return FALSE
  291.                 GameField.StopSquare(Square1, Square1.Location.X / SquareSize, Square1.Location.Y / SquareSize)
  292.                 GameField.StopSquare(Square2, Square2.Location.X / SquareSize, Square2.Location.Y / SquareSize)
  293.                 GameField.StopSquare(Square3, Square3.Location.X / SquareSize, Square3.Location.Y / SquareSize)
  294.                 GameField.StopSquare(Square4, Square4.Location.X / SquareSize, Square4.Location.Y / SquareSize)
  295.                 Return False
  296.             End If
  297.         End Function 'Down
  298.         Public Function Right() As Boolean
  299.             ' If There's No Block To The Right Of The Current One, Go Right         
  300.             If GameField.IsEmpty(Square1.Location.X / SquareSize + 1, Square1.Location.Y / SquareSize) AndAlso GameField.IsEmpty(Square2.Location.X / SquareSize + 1, Square2.Location.Y / SquareSize) AndAlso GameField.IsEmpty(Square3.Location.X / SquareSize + 1, Square3.Location.Y / SquareSize) AndAlso GameField.IsEmpty(Square4.Location.X / SquareSize + 1, Square4.Location.Y / SquareSize) Then
  301.                 ' Hide The Block (In The Previous Position)
  302.                 Hide(GameField.WinHandle)
  303.                 ' Update The Block Position
  304.                 Square1.Location = New Point(Square1.Location.X + SquareSize, Square1.Location.Y)
  305.                 Square2.Location = New Point(Square2.Location.X + SquareSize, Square2.Location.Y)
  306.                 Square3.Location = New Point(Square3.Location.X + SquareSize, Square3.Location.Y)
  307.                 Square4.Location = New Point(Square4.Location.X + SquareSize, Square4.Location.Y)
  308.                 ' Draw The Block In The New Position
  309.                 Show(GameField.WinHandle)
  310.                 Return True
  311.             Else
  312.                 ' If There's A Block To The Right Of The Current One, 
  313.                 ' Doesn't Go Right And Return FALSE
  314.                 Return False
  315.             End If
  316.         End Function 'Right
  317.         Public Function Left() As Boolean
  318.             ' If There's No Block To The Left Of The Current One, Go Left    
  319.             If GameField.IsEmpty(Square1.Location.X / SquareSize - 1, Square1.Location.Y / SquareSize) AndAlso GameField.IsEmpty(Square2.Location.X / SquareSize - 1, Square2.Location.Y / SquareSize) AndAlso GameField.IsEmpty(Square3.Location.X / SquareSize - 1, Square3.Location.Y / SquareSize) AndAlso GameField.IsEmpty(Square4.Location.X / SquareSize - 1, Square4.Location.Y / SquareSize) Then
  320.                 ' Hide The Block (In The Previous Position)
  321.                 Hide(GameField.WinHandle)
  322.                 ' Update The Block Position
  323.                 Square1.Location = New Point(Square1.Location.X - SquareSize, Square1.Location.Y)
  324.                 Square2.Location = New Point(Square2.Location.X - SquareSize, Square2.Location.Y)
  325.                 Square3.Location = New Point(Square3.Location.X - SquareSize, Square3.Location.Y)
  326.                 Square4.Location = New Point(Square4.Location.X - SquareSize, Square4.Location.Y)
  327.                 ' Draw The Block In The New Position
  328.                 Show(GameField.WinHandle)
  329.                 Return True
  330.             Else
  331.                 ' If There's A Block To The Left Of The Current One, 
  332.                 ' Doesn't Go Left And Return FALSE
  333.                 Return False
  334.             End If
  335.         End Function 'Left
  336.         ' Draws Each Square Of The Block On The Game Field
  337.         Public Sub Show(ByVal WinHandle As System.IntPtr)
  338.             Square1.Show(WinHandle)
  339.             Square2.Show(WinHandle)
  340.             Square3.Show(WinHandle)
  341.             Square4.Show(WinHandle)
  342.         End Sub 'Show
  343.         ' Hides Each Square Of The Block On The Game Field
  344.         Public Sub Hide(ByVal WinHandle As System.IntPtr)
  345.             Square1.Hide(WinHandle)
  346.             Square2.Hide(WinHandle)
  347.             Square3.Hide(WinHandle)
  348.             Square4.Hide(WinHandle)
  349.         End Sub 'Hide
  350.     End Class 'Block
  351. End Namespace 'Nettrix