Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Form1
- Dim Upc As Boolean
- Dim Downc As Boolean
- Dim Leftc As Boolean
- Dim rightc As Boolean
- Dim score As Integer = 0
- Dim lives As Integer = 3
- Public Property Movement As Object
- Public Property PitureBox2 As Object
- Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
- If e.KeyCode = Keys.Up Then
- Upc = True
- End If
- If e.KeyCode.Down Then
- Downc = True
- End If
- If e.KeyCode.Left Then
- Leftc = True
- End If
- If e.KeyCode.Right Then
- rightc = True
- End If
- If e.KeyCode = Keys.Escape Then
- PictureBox3.Top = 300
- PictureBox3.Top = 300
- PictureBox1.Left = 0
- PictureBox1.Top = 0
- lives = 3
- Label1.Text = "Lives: " & lives
- score = 0
- Label2.Text = "Score: " & score
- rightc = False
- Upc = False
- Downc = False
- Leftc = False
- Movement.Start()
- End If
- End Sub
- Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles MyBase.KeyUp
- If e.KeyCode = Keys.Up Then
- Upc = False
- End If
- If e.KeyCode.Down Then
- Downc = False
- End If
- If e.KeyCode.Left Then
- Leftc = False
- End If
- If e.KeyCode.Right Then
- rightc = False
- Label1.Text = "Left:" & PictureBox1.Left
- Label2.Text = "Left:" & PictureBox1.Top
- End If
- End Sub
- Private Sub MovementT_Tick(sender As Object, e As EventArgs) Handles MovementT.Tick
- Chase(PictureBox3, PictureBox2)
- If Upc = True Then
- PictureBox1.Top -= 5
- If PictureBox1.Top <= 0 Then
- PictureBox1.Top = 0
- End If
- End If
- If Downc = True Then
- PictureBox1.Top += 5
- If PictureBox1.Top + PictureBox1.Height >= Me.Height Then
- PictureBox1.Top = Me.Height - PictureBox1.Height
- End If
- End If
- If Leftc = True Then
- PictureBox1.Left -= 5
- End If
- If PictureBox1.Left <= 0 Then
- PictureBox1.Top = 0
- End If
- If rightc = True Then
- PictureBox1.Left += 5
- End If
- If PictureBox1.Left + PictureBox1.Width >= Me.Width Then
- PictureBox1.Left = Me.Width - PictureBox1.Width
- End If
- If Collision(PictureBox1, PictureBox2) = True Then
- PictureBox2.Top = Int(Rnd() * (480 - 2 * PictureBox2.Height) + PictureBox2.Height)
- PictureBox2.Left = Int(Rnd() * (640 - 2 * PictureBox2.Width) + PictureBox2.Width)
- PictureBox2.BringToFront()
- Debug.WriteLine("gem top" & PitureBox2.Top)
- Debug.WriteLine("Gem left" & PictureBox2.Left)
- score = score + 1
- Label1.Text = "Score " & score
- End If
- If Collision(PictureBox1, PictureBox3) Then
- If lives > 1 Then
- lives = lives - 1
- Label1.Text = "Lives : " & lives
- 'reset position of player'
- PictureBox1.Left = 0
- PictureBox1.Top = 0
- 'reset position of attack'
- PictureBox3.Top = 300
- PictureBox3.Left = 300
- Else
- lives = lives - 1
- Label1.Text = "Lives: " & lives
- MovementT.Stop()
- MsgBox("Game Over")
- End If
- End If
- End Sub
- Private Function Collision(ByVal Object1 As Object, ByVal Object2 As Object) As Boolean
- Dim Collided As Boolean = False
- If Object1.Top + Object1.Height >= Object2.Top And
- Object2.Top + Object2.Height >= Object1.Top And
- Object1.Left + Object1.Width >= Object2.Left And
- Object2.Left + Object2.Width >= Object2.Left Then
- Collided = True
- End If
- Return Collided
- End Function
- Private Function Chase(ByRef Object1 As Object, ByRef Object2 As Object)
- 'If the enemy object is to right of player, then enemy object will move to left'
- If Object1.Left + Object1.Width > Object2.Left Then
- Object1.Left -= 1
- End If
- 'If the enemy object is to left of player, then object will move right'
- If Object1.Left + Object1.Width < Object2.Left Then
- Object1.Left += 1
- End If
- 'If the enemy object is lower of the player, then enemy object will move up'
- If Object1.Top + Object1.height > Object2.Top Then
- Object1.Top -= 1
- End If
- 'if the enenmy object is higher of player, then enemy object will move down'
- If Object1.Top + Object1.height < Object2.Top Then
- Object1.Top += 1
- End If
- End Function
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- Me.KeyPreview = True
- Me.Size = New Size(800, 600)
- Me.CenterToScreen()
- Label1.Text = "Left:" & PictureBox1.Left
- Label2.Text = "Top:" & PictureBox1.Top
- Label3.Text = "Screen:" & Me.Width & "*" & Me.Height
- End Sub
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- 'Save score to a csv database file on desktop'
- Dim FileName, SaveInfoStr As String
- FileName = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Score.csv"
- Dim out As IO.StreamWriter = System.IO.File.AppendText(FileName)
- SaveInfoStr = Label1.Text & "," & Label2.Text
- out.WriteLine(SafeInfoStr)
- out.Close()
- MsgBox("Your Score has been saved to file on Desktop")
- End Sub
- Private Function SafeInfoStr() As Char
- Throw New NotImplementedException()
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement