📄 appleandbug.vb
字号:
Public Class AppleAndBug
Public timer1 As Timer
Dim nNoBugs As Integer
Dim nNoApples As Integer
Dim b As Integer
Dim a As Integer
Dim nEnergy As Integer
Dim AllApples As ArrayList
Dim AllBugs As ArrayList
Dim oBug As Bug
Protected dir1 As Integer
Protected speed1 As Integer
Protected margin1 As Integer
Protected maxEnergy As Integer
Protected moveCount1 As Integer
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
If txtBugs.Text.Trim <> "" Then
If Not IsNumeric(txtBugs.Text.Trim) Then
MsgBox("Please mention no of bugs")
txtBugs.Focus()
Exit Sub
Else
If Val(txtBugs.Text) <= 0 Then
MsgBox("Please mention no of bugs")
txtBugs.Focus()
Exit Sub
End If
End If
Else
MsgBox("Please mention no of bugs")
txtBugs.Focus()
Exit Sub
End If
If txtApples.Text.Trim <> "" Then
If Not IsNumeric(txtApples.Text.Trim) Then
MsgBox("Please mention no of apples")
txtApples.Focus()
Exit Sub
Else
If Val(txtApples.Text) <= 0 Then
MsgBox("Please mention no of apples")
txtApples.Focus()
Exit Sub
End If
End If
Else
MsgBox("Please mention no of apples")
txtApples.Focus()
Exit Sub
End If
txtApples.Enabled = False
txtBugs.Enabled = False
btnStart.Enabled = False
btnStop.Enabled = True
lblResult.Visible = False
speed1 = 10
margin1 = 50
maxEnergy = 200
dir1 = 4
nNoBugs = txtBugs.Text
nNoApples = txtApples.Text
a = nNoApples
AllApples = New ArrayList()
For i As Integer = 1 To nNoApples
Dim apple1 As New Apple
apple1.Create(Utilities.RandomNumber(Me.Width - 60, 0), Utilities.RandomNumber(Me.Height - 70, margin1))
Me.Controls.Add(apple1.getObject())
AllApples.Add(apple1)
Next
b = nNoBugs
AllBugs = New ArrayList()
Dim nTop As Int32 = 100
For i As Integer = 1 To nNoBugs
Dim bug1 As New Bug
bug1.Create(0, nTop)
nTop += 20
Me.Controls.Add(bug1.getObject())
AllBugs.Add(bug1)
Next
DoPlay()
End Sub
Public Sub DoPlay()
If b > 0 And AllApples.Count > 0 Then
oBug = AllBugs(b - 1)
b = b - 1
nEnergy = maxEnergy
'lblEnergyLeft.Text = nEnergy
timer1 = New Timer
timer1.Interval = 300
timer1.Enabled = True
AddHandler timer1.Tick, AddressOf OnTimerEvent
Else
lblStatus.Text = "GAME IS OVER"
lblResult.Text = b & " of " & nNoBugs & " bugs left, " & AllApples.Count & " of " & nNoApples & " apples left"
lblResult.Visible = True
txtApples.Enabled = True
txtBugs.Enabled = True
btnStart.Enabled = True
btnStop.Enabled = False
End If
End Sub
Public Sub OnTimerEvent(ByVal sender As Object, ByVal e As EventArgs)
If nEnergy > 0 And AllApples.Count > 0 Then
EatApple()
nEnergy -= 1
Select Case dir1
Case 1 'Left
oBug.MoveTo(oBug.getObject.Left - speed1, oBug.getObject.Top)
Case 2 'Right
oBug.MoveTo(oBug.getObject.Left + speed1, oBug.getObject.Top)
Case 3 'Up
oBug.MoveTo(oBug.getObject.Left, oBug.getObject.Top - speed1)
Case 4 'Down
oBug.MoveTo(oBug.getObject.Left, oBug.getObject.Top + speed1)
End Select
moveCount1 = moveCount1 + 1
'Random direction change
Dim rndMove As Integer = Utilities.RandomNumber(30, 20)
If moveCount1 > rndMove Then
moveCount1 = 0
dir1 = Utilities.RandomNumber(5, 1)
speed1 = Utilities.RandomNumber(10, 50)
End If
If oBug.getObject.Left <= margin1 Or oBug.getObject.Left >= Me.Width - margin1 Then
If oBug.getObject.Left < margin1 Then
oBug.getObject.Left = margin1 + 1
End If
If oBug.getObject.Left > Me.Width - margin1 Then
oBug.getObject.Left = Me.Width - margin1 - 1
End If
dir1 = Utilities.RandomNumber(5, 1)
speed1 = Utilities.RandomNumber(10, 50)
ElseIf oBug.getObject.Top <= margin1 Or oBug.getObject.Top >= Me.Height - margin1 Then
If oBug.getObject.Top < margin1 Then
oBug.getObject.Top = margin1 + 1
End If
If oBug.getObject.Top > Me.Height - margin1 Then
oBug.getObject.Top = Me.Height - margin1 - 1
End If
dir1 = Utilities.RandomNumber(5, 1)
speed1 = Utilities.RandomNumber(10, 50)
End If
lblStatus.Text = "Left Energy:" & nEnergy '& " D:" & dir1 & "(" & oBug.getObject.Left & "," & oBug.getObject.Top & ")" & speed1 & " "
Else
lblStatus.Text = "Bug is dead"
timer1.Enabled = False
oBug.Destroy()
txtBugs.Text = b
DoPlay()
End If
End Sub
Private Sub EatApple()
Dim xApple As Apple = Nothing
For Each oApple As Apple In AllApples
If Utilities.CheckOverlap(oBug, oApple) Then
xApple = oApple
AllApples.Remove(oApple)
nEnergy = maxEnergy
Exit For
End If
Next
If xApple IsNot Nothing Then
xApple.Destroy()
a = a - 1
txtApples.Text = AllApples.Count
End If
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
txtApples.Enabled = True
txtBugs.Enabled = True
btnStart.Enabled = True
btnStop.Enabled = False
timer1.Enabled = False
lblStatus.Text = "Game Stopped"
If oBug IsNot Nothing Then
oBug.Destroy()
End If
For i As Integer = AllApples.Count - 1 To 0 Step -1
CType(AllApples(i), Apple).Destroy()
AllApples.RemoveAt(i)
Next
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -