Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Form1
- Public pressingLeft As Boolean
- Public pressingRight As Boolean
- Public pressingUp As Boolean
- Public pressingDown As Boolean
- 'var to keep track of current direction
- Public dy As Integer
- Public dx As Integer
- Public generator As New Random
- Public score As Integer
- Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
- If e.KeyCode = Keys.Right Then
- pressingRight = True
- End If
- If e.KeyCode = Keys.Left Then
- pressingLeft = True
- End If
- If e.KeyCode = Keys.Up Then
- pressingUp = True
- End If
- If e.KeyCode = Keys.Down Then
- pressingDown = True
- End If
- End Sub
- Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
- If e.KeyCode = Keys.Right Then
- pressingRight = False
- End If
- If e.KeyCode = Keys.Left Then
- pressingLeft = False
- End If
- If e.KeyCode = Keys.Up Then
- pressingUp = False
- End If
- If e.KeyCode = Keys.Down Then
- pressingDown = False
- End If
- End Sub
- Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles tmrMoveGuy.Tick
- 'move mario
- If pressingRight = True Then
- picMario.Left = picMario.Left + 10
- picMario.Image = My.Resources.marioRight
- End If
- If pressingLeft = True Then
- picMario.Left = picMario.Left - 10
- picMario.Image = My.Resources.marioLeft
- End If
- If pressingUp = True Then
- picMario.Top = picMario.Top - 10
- picMario.Image = My.Resources.marioUp
- End If
- If pressingDown = True Then
- picMario.Top = picMario.Top + 10
- picMario.Image = My.Resources.marioDown
- End If
- 'enforce edge boundaries
- If picMario.Left < 0 Then
- picMario.Left = 0
- ElseIf picMario.Left + picMario.Width > Me.Width Then
- picMario.Left = Me.Width - picMario.Width
- ElseIf picMario.Top < 0 Then
- picMario.Top = 0
- ElseIf picMario.Top + picMario.Height > Me.Height - 35 Then
- picMario.Top = Me.Height - picMario.Height - 35
- End If
- End Sub
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- dy = generator.Next(5, 10)
- dx = generator.Next(5, 10)
- score = 0
- End Sub
- Private Sub tmrMoveMush_Tick(sender As Object, e As EventArgs) Handles tmrMoveMush.Tick
- 'move mush
- picMush.Left = picMush.Left + dx
- picMush.Top = picMush.Top + dy
- 'check bounce off edge maybe
- If picMush.Left < 0 Or
- picMush.Left + picMush.Width > Me.Width Then
- dx = -dx
- End If
- If picMush.Top < 0 Or
- picMush.Top + picMush.Height > Me.Height Then
- dy = -dy
- End If
- End Sub
- Public Function detect_hits(p1 As PictureBox, p2 As PictureBox) As Boolean
- If ((p1.Left < p2.Left + p2.Width) _
- And (p1.Left + p1.Width > p2.Left) _
- And (p1.Top + p1.Height > p2.Top) _
- And (p1.Top < p2.Top + p2.Height)) Then
- Return True
- Else
- Return False
- End If
- End Function
- Private Sub tmrDetectHits_Tick(sender As Object, e As EventArgs) Handles tmrDetectHits.Tick
- If detect_hits(picMario, picMush) = True Then
- score = score + 1
- lblScore.Text = score
- picMush.Top = generator.Next(0, Me.Height - picMush.Height)
- picMush.Left = generator.Next(0, Me.Width - picMush.Width)
- End If
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement