Dim i As Integer = ImgList(value).LastIndexOf("\")
Me.Tag = ImgList(value).Substring(i + 1, _
ImgList(value).Length – i – 5)
ZoomIn()
End Sub
Private Sub ZoomIn()
Me.Top = picTop + (picHeight – 4) \ 2
Me.Left = picLeft + (picWidth – 4) \ 2
Me.Width = 4
Me.Height = 4
Me.Image = Image.FromFile(ImgList(picIndex))
AddHandler myTimer.Tick, AddressOf TimerEventZoomIn
myTimer.Interval = 10
myTimer.Start()
End Sub
Private Sub TimerEventZoomIn(ByVal myObject As Object, _
ByVal myEventArgs As EventArgs)
If Me.Top > picTop And Me.Width < picWidth Then
Me.Top -= 2
Me.Left -= 2
Me.Width += 4
Me.Height += 4
Else
myTimer.Enabled = False
picState = BallState.NORMAL_BALL
RemoveHandler myTimer.Tick, AddressOf TimerEventZoomIn
End If
End Sub
Public Sub Jump()
If picState = BallState.NORMAL_BALL Then
sign = 1
picState = BallState.JUMPING_BALL
AddHandler myTimer.Tick, AddressOf TimerEventJump
myTimer.Interval = 20
myTimer.Start()
ElseIf picState = BallState.JUMPING_BALL Then
StopJump()
End If
End Sub
Public Sub StopJump()
If picState = BallState.JUMPING_BALL Then
picState = 0
myTimer.Enabled = False
RemoveHandler myTimer.Tick, AddressOf TimerEventJump
Me.Top = picTop
Me.Left = picLeft
Me.Height = picHeight
Me.Width = picWidth
End If
End Sub
Private Sub TimerEventJump(ByVal myObject As Object, _
ByVal myEventArgs As EventArgs)
Me.Height -= sign * 1
Me.Top = picTop + (Me.Height – picHeight) \ 4
If Me.Height = picHeight Or Me.Height <= 3 * picHeight / 4 Then
sign *= -1
End If
End Sub
Public Sub Destroy()
If picState = BallState.JUMPING_BALL Then
StopJump()
End If
picState = BallState.DESTROYING_BALL
AddHandler myTimer.Tick, AddressOf TimerEventDestroy
Me.Top = picTop + 1
Me.Left = picLeft + 1
Me.Width = picWidth – 2
Me.Height = picHeight – 2
myTimer.Interval = 10
myTimer.Start()
End Sub
Private Sub TimerEventDestroy(ByVal myObject As Object, _
ByVal myEventArgs As EventArgs)
If Me.Top > picTop And Me.Width > 0 Then
Me.Top += 2
Me.Left += 2
Me.Width -= 4
Me.Height -= 4
Else
Me.Image = Nothing
Me.Top = picTop
Me.Left = picLeft
Me.Width = picWidth
Me.Height = picHeight
myTimer.Enabled = False
Me.picState = BallState.NO_BALL
Me.picIndex = -1
Me.Tag = ""
RemoveHandler myTimer.Tick, AddressOf TimerEventDestroy
End If
End Sub
Public Sub Reset()
While Me.picState = BallState.DESTROYING_BALL
Application.DoEvents()
End While
If Me.picState = BallState.JUMPING_BALL Then
StopJump()
End If
While Me.picState = BallState.ZOOMING_BALL
Application.DoEvents()
End While
Me.Image = Nothing
Me.picIndex = -1
Me.picState = BallState.NO_BALL
Me.Tag = ""
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As _
System.Windows.Forms.MouseEventArgs)
If picState = BallState.NORMAL_BALL Or _
picState = BallState.JUMPING_BALL Then
Me.Image = Image.FromFile(ImgList(picIndex + 1))
End If
End Sub
Protected Overrides Sub OnMouseLeave( _
ByVal e As System.EventArgs)
If picState = BallState.NORMAL_BALL Or picState = _
BallState.JUMPING_BALL Then
Me.Image = Image.FromFile(ImgList(picIndex))
End If
End Sub
End Class
После этих добавлений (DPaint.vb, mModule.vb, MotionPic.vb, open.ico, save.ico) в панели Solution Explorer должны быть файлы, показанные выше. Дважды щёлкая по имени файла, любой файл можно открыть, изучить и редактировать.
Теперь в наш проект добавляем переменные и методы, связанные с формой Form2 для вывода результатов игры.
Открываем файл Form2.vb (например, так: File, Open, File) и вверху записываем директивы для подключения требуемых пространств имен:
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.IO
Теперь в классе Form2 нашего проекта записываем следующие переменные и методы.
Листинг 21.12. Переменные и методы.
Dim gr As Graphics
Dim lbrTitle As LinearGradientBrush
Dim lbrBoard As LinearGradientBrush
Dim midPoint As Point
Dim startPoint As PointF
Dim intGradiantStep As Integer = 5
Dim intCurrentGradientShift As Integer = 0
Const colW1 As Integer = 250
Const colW2 As Integer = 150
Const rowH As Integer = 30
Dim AddedPlayer As New Player("", "-1")
Dim ArrPlayer As New ArrayList
Dim intCurrentGradientRow As Integer = 110
Public WriteOnly Property AddPlayer() As Player
Set(ByVal Value As Player)
If Value.PlayerName.Length > 14 Then
Value.PlayerName = _
Value.PlayerName.Substring(0, 14)
End If
AddedPlayer = Value
End Set
End Property
Public Sub drawTable()
Me.BackgroundImage = Nothing
Me.BackColor = Color.Moccasin
Application.DoEvents()
Dim g As Graphics = CreateGraphics()
Dim tpen1 As New Pen(Color.Red, 1)
Dim tpen2 As New Pen(Color.Black, 1)
Dim P1 As New Point(2, 80)
Dim P2 As New Point(400, 80)
For i As Integer = 0 To 11
g.DrawLine(tpen1, P1, P2)
P1.Y += 1
P2.Y += 1
g.DrawLine(tpen2, P1, P2)
P1.Y += rowH – 1
P2.Y += rowH – 1
Next
P1.Y = 80
P2.X = P1.X
P2.Y -= rowH
g.DrawLine(tpen2, P1, P2)
P1.X += 1
P2.X += 1
g.DrawLine(tpen1, P1, P2)
P1.X += colW1
P2.X += colW1
g.DrawLine(tpen2, P1, P2)
P1.X += 1
P2.X += 1
g.DrawLine(tpen1, P1, P2)
P1.X += colW2 – 3
P2.X += colW2 – 3
g.DrawLine(tpen1, P1, P2)
P1.X += 1
P2.X += 1
g.DrawLine(tpen2, P1, P2)
SaveScore()
LoadScore()
Timer1.Enabled = True
Timer2.Enabled = True
End Sub
Public Sub LoadScore()
If Not File.Exists("Score.dat") Then
GoTo newScore
End If
Dim FSR As New StreamReader("Score.dat")
Dim s As String
s = FSR.ReadLine
If Trim(s) <> "#Assignment Line#" Then
FSR.Close()
GoTo newScore
End If
For i As Integer = 0 To 9
s = FSR.ReadLine
Dim PlayerName As String = s.Split(CChar(";"))(0)
Dim PlayerScore As String = s.Split(CChar(";"))(1)
ArrPlayer.Add(New Player(PlayerName, PlayerScore))
Next
FSR.Close()
Exit Sub
newScore:
ArrPlayer.Add(New Player("AAA", "5000"))
ArrPlayer.Add(New Player("AAA", "4500"))
ArrPlayer.Add(New Player("AAA", "4000"))
ArrPlayer.Add(New Player("AAA", "3500"))
ArrPlayer.Add(New Player("AAA", "3000"))
ArrPlayer.Add(New Player("AAA", "2500"))
ArrPlayer.Add(New Player("AAA", "2000"))
ArrPlayer.Add(New Player("AAA", "1500"))
ArrPlayer.Add(New Player("AAA", "1000"))
ArrPlayer.Add(New Player("AAA", "500"))
End Sub
Public Sub SaveScore()
LoadScore()
For i As Integer = 0 To 9
If CDbl(AddedPlayer.PlayerScore) >= _
CDbl(CType(ArrPlayer(i), Player).PlayerScore) Then
ArrPlayer.Insert(i, AddedPlayer)
Exit For
End If
Next
Dim FSW As New StreamWriter("Score.dat")
Dim s As String = "#Assignment Line#"
s += Chr(13) + Chr(10)
For i As Integer = 0 To 9
s += CType(ArrPlayer(i), Player).PlayerName + ";" + _
CType(ArrPlayer(i), Player).PlayerScore
s += Chr(13) + Chr(10)
Next
FSW.Write(s)
FSW.Close()
End Sub
Public Sub PlashScreen()
Me.BackgroundImage = Image.FromFile("GameOver.gif")
Application.DoEvents()
Threading.Thread.Sleep(2000)
End Sub
Ниже формы Form2 дважды щёлкаем по значку первого таймера Timer. Появляется шаблон метода, который после записи нашего кода принимает следующий вид.
Листинг 21.13. Метод-обработчик события Tick.
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
gr = CreateGraphics()
midPoint = New Point(Me.Width \ 2, 10)
Dim strText As String = "Score Board"
Dim fnt As New Font("Microsoft Sans Serif", 30, _
FontStyle.Bold, GraphicsUnit.Point)
Dim strSize As New SizeF(gr.MeasureString(strText, fnt))
Dim ptfGradientStart As New _
PointF(intCurrentGradientShift, 0)
Dim ptfGradientEnd As New PointF(0, intCurrentGradientRow)
lbrTitle = New LinearGradientBrush(ptfGradientStart, _
ptfGradientEnd, Color.SteelBlue, Color.Brown)
startPoint = New PointF(midPoint.X – _
CInt(strSize.Width / 2), midPoint.Y)
gr.DrawString(strText, fnt, lbrTitle, startPoint)
ptfGradientStart = New PointF(0, intCurrentGradientShift)
ptfGradientEnd = New PointF(intCurrentGradientRow, 0)
lbrTitle = New LinearGradientBrush(ptfGradientEnd, _
ptfGradientStart, Color.MediumSlateBlue, _
Color.GhostWhite)
gr.DrawString(strText, fnt, lbrTitle, startPoint.X – 2, _
startPoint.Y + 2)
intCurrentGradientShift += intGradiantStep
If intCurrentGradientShift = 400 Then
intGradiantStep = -5
ElseIf intCurrentGradientShift = -400 Then
intGradiantStep = 5
End If
End Sub
Ниже формы Form2 дважды щёлкаем по значку второго таймера Timer. Появляется шаблон метода, который после записи нашего кода принимает следующий вид.
Листинг 21.14. Метод-обработчик события Tick.
Private Sub Timer2_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer2.Tick
'showScore()
Dim g As Graphics = CreateGraphics()
Dim fnt As New Font("Courier New", 20, FontStyle.Bold, _
GraphicsUnit.Point)
Dim startPoint As PointF = New PointF(20, 80)
Dim nextPoint As PointF = _
New PointF(startPoint.X + colW1, 80)
Dim ptfGradientStart As New PointF(intCurrentGradientRow, _
startPoint.X)
Dim ptfGradientEnd As New PointF(nextPoint.Y, _
intCurrentGradientRow)
lbrBoard = New LinearGradientBrush(ptfGradientStart, _
ptfGradientEnd, Color.GreenYellow, Color.SlateGray)
Dim PlayerNames As String = "Name" + Chr(13) + Chr(10)
Dim PlayerScores As String = "Score" + Chr(13) + Chr(10)
For i As Integer = 0 To 9
PlayerNames += CType(ArrPlayer(i), Player).PlayerName _
+ Chr(13) + Chr(10)
PlayerScores += CType(ArrPlayer(i), Player).PlayerScore _
+ Chr(13) + Chr(10)
Next
g.DrawString(PlayerNames, fnt, lbrBoard, startPoint)
g.DrawString(PlayerScores, fnt, lbrBoard, nextPoint)
intCurrentGradientRow += intGradiantStep
End Sub
В случае необходимости, методика добавления в проект звукового сигнала Beep (по-русски: Бип) описана ранее.
О проекте
О подписке