Справочник Жаркова по проектированию и программированию искусственного интеллекта. Том 6: Программирование на Visual Basic искусственного интеллекта. Продолжение 2. Валерий Алексеевич Жарков
Читать онлайн книгу.кнопку Add. В проект (и в панель Solution Explorer) добавляется этот файл, открывается пустое окно редактирования кода, в которое записываем код со следующего листинга.
Листинг 20.16. Новый файл.
Imports System.Drawing.Drawing2D
''' <summary>
''' This class represents one of the balls in the game grid.
''' </summary>
''' <remarks></remarks>
Public Class Block
Public Const BlockSize As Integer = 25
Private colorValue As Color
Private deletionValue As Boolean = False
Private Shared rand As New Random
Public Property Color() As Color
Get
Return colorValue
End Get
Set(ByVal Value As Color)
colorValue = Value
End Set
End Property
Public Property MarkedForDeletion() As Boolean
Get
Return deletionValue
End Get
Set(ByVal Value As Boolean)
deletionValue = Value
End Set
End Property
Public Sub New(ByVal newColor As Color)
colorValue = newColor
End Sub
Public Sub New(ByVal colors() As Color)
Dim ncolors As Integer = colors.Length
Dim pickedColor As Integer
pickedColor = rand.Next(0, ncolors)
colorValue = colors(pickedColor)
End Sub
Public Sub Draw(ByVal graphics As Graphics, ByVal point As Point)
Dim brush As System.Drawing.Drawing2D.LinearGradientBrush = _
CreateTheBrush(point)
DrawTheCircle(graphics, brush, point)
End Sub
Private Sub DrawTheCircle(ByVal graphics As Graphics, _
ByVal brush As LinearGradientBrush, ByVal location As Point)
Dim topleft As Point = location
Dim bottomright As Point = New Point(location.X + _
BlockSize, location.Y + BlockSize)
Dim transTopLeft As Point = PointTranslator.TranslateToBL( _
topleft)
Dim transBottomRight As Point = _
PointTranslator.TranslateToBL(bottomright)
Dim transwidth As Integer = transBottomRight.X – transTopLeft.X
Dim transheight As Integer = _
transBottomRight.Y – transTopLeft.Y
graphics.FillEllipse(brush, New Rectangle(transTopLeft, _
New Size(transwidth, transheight)))
End Sub
Private Function CreateTheBrush(ByVal location As Point) As _
LinearGradientBrush
Dim transLocation As Point = _
PointTranslator.TranslateToBL(location)
Dim brushpt1 As Point = transLocation
Dim brushpt2 As New Point(transLocation.X + Block.BlockSize _
+ 4, transLocation.Y – BlockSize – 4)
Dim brush As New LinearGradientBrush(brushpt1, _
brushpt2, Me.Color, System.Drawing.Color.White)
Return brush
End Function
End Class
По второму варианту, в панели Solution Explorer выполняем правый щелчок по имени проекта и в контекстном меню выбираем Add, New Item, в панели Add New Item выделяем шаблон Code File, в окне Name записываем имя Grid.vb и щёлкаем кнопку Add. В проект (и в панель Solution Explorer) добавляется этот файл, открывается пустое окно редактирования кода, в которое записываем код со следующего листинга.
Листинг 20.17. Новый файл.
''' <summary>
''' This class represents the grid of blocks. It handles most of
''' the game play.
''' </summary>
''' <remarks></remarks>
Public Class Grid
' The grids is 12 columns and 15 rows of Block objects.
Dim matrix(11, 14) As Block
''' <summary>
''' Creates a few rows of blocks to start the game.
''' Game starts with Red, Blue, and Green blocks.
''' </summary>
''' <param name="nrows">Number of rows of blocks to create
''' to start the game.</param>
''' <remarks></remarks>
Public Sub New(ByVal nrows As Integer)
If nrows > matrix.GetLength(0) Then
Throw New Exception("Must start with " & _
matrix.GetLength(0) & " or fewer rows.")
End If
Dim row As Integer
Dim column As Integer
For row = 0 To nrows – 1
For column = 0 To matrix.GetLength(1) – 1
matrix(row, column) = New Block( _
New Color() {Color.Red, Color.Blue, Color.Green})
Next
Next
For row = nrows To matrix.GetLength(0) – 1
For column = 0 To matrix.GetLength(1) – 1
matrix(row, column) = Nothing
Next
Next
End Sub
''' <summary>
''' A new row may be added at any time. New rows have Gray
''' blocks in addition
''' to Red, Blue, and Green. This makes the game more difficult.
''' </summary>
''' <remarks></remarks>
Public Sub AddRow()
Dim column As Integer
'