Advertisement
ssoni

MoveGuyMarioDetectHit

Apr 23rd, 2021
1,638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Public Class Form1
  2.     Public pressingLeft As Boolean
  3.     Public pressingRight As Boolean
  4.     Public pressingUp As Boolean
  5.     Public pressingDown As Boolean
  6.     'var to keep track of current direction
  7.    Public dy As Integer
  8.     Public dx As Integer
  9.     Public generator As New Random
  10.     Public score As Integer
  11.  
  12.     Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
  13.         If e.KeyCode = Keys.Right Then
  14.             pressingRight = True
  15.         End If
  16.  
  17.         If e.KeyCode = Keys.Left Then
  18.             pressingLeft = True
  19.         End If
  20.  
  21.         If e.KeyCode = Keys.Up Then
  22.             pressingUp = True
  23.         End If
  24.  
  25.         If e.KeyCode = Keys.Down Then
  26.             pressingDown = True
  27.         End If
  28.     End Sub
  29.  
  30.     Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
  31.         If e.KeyCode = Keys.Right Then
  32.             pressingRight = False
  33.         End If
  34.  
  35.         If e.KeyCode = Keys.Left Then
  36.             pressingLeft = False
  37.         End If
  38.  
  39.         If e.KeyCode = Keys.Up Then
  40.             pressingUp = False
  41.         End If
  42.  
  43.         If e.KeyCode = Keys.Down Then
  44.             pressingDown = False
  45.         End If
  46.     End Sub
  47.  
  48.     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles tmrMoveGuy.Tick
  49.  
  50.         'move mario
  51.        If pressingRight = True Then
  52.             picMario.Left = picMario.Left + 10
  53.             picMario.Image = My.Resources.marioRight
  54.         End If
  55.  
  56.         If pressingLeft = True Then
  57.             picMario.Left = picMario.Left - 10
  58.             picMario.Image = My.Resources.marioLeft
  59.         End If
  60.  
  61.         If pressingUp = True Then
  62.             picMario.Top = picMario.Top - 10
  63.             picMario.Image = My.Resources.marioUp
  64.         End If
  65.  
  66.         If pressingDown = True Then
  67.             picMario.Top = picMario.Top + 10
  68.             picMario.Image = My.Resources.marioDown
  69.         End If
  70.  
  71.         'enforce edge boundaries
  72.        If picMario.Left < 0 Then
  73.             picMario.Left = 0
  74.         ElseIf picMario.Left + picMario.Width > Me.Width Then
  75.             picMario.Left = Me.Width - picMario.Width
  76.         ElseIf picMario.Top < 0 Then
  77.             picMario.Top = 0
  78.         ElseIf picMario.Top + picMario.Height > Me.Height - 35 Then
  79.             picMario.Top = Me.Height - picMario.Height - 35
  80.         End If
  81.  
  82.     End Sub
  83.  
  84.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  85.         dy = generator.Next(5, 10)
  86.         dx = generator.Next(5, 10)
  87.         score = 0
  88.     End Sub
  89.  
  90.     Private Sub tmrMoveMush_Tick(sender As Object, e As EventArgs) Handles tmrMoveMush.Tick
  91.  
  92.         'move mush
  93.        picMush.Left = picMush.Left + dx
  94.         picMush.Top = picMush.Top + dy
  95.  
  96.         'check bounce off edge maybe
  97.        If picMush.Left < 0 Or
  98.             picMush.Left + picMush.Width > Me.Width Then
  99.             dx = -dx
  100.         End If
  101.  
  102.         If picMush.Top < 0 Or
  103.                 picMush.Top + picMush.Height > Me.Height Then
  104.             dy = -dy
  105.         End If
  106.     End Sub
  107.  
  108.     Public Function detect_hits(p1 As PictureBox, p2 As PictureBox) As Boolean
  109.         If ((p1.Left < p2.Left + p2.Width) _
  110.                 And (p1.Left + p1.Width > p2.Left) _
  111.                 And (p1.Top + p1.Height > p2.Top) _
  112.                 And (p1.Top < p2.Top + p2.Height)) Then
  113.             Return True
  114.         Else
  115.             Return False
  116.         End If
  117.     End Function
  118.  
  119.     Private Sub tmrDetectHits_Tick(sender As Object, e As EventArgs) Handles tmrDetectHits.Tick
  120.         If detect_hits(picMario, picMush) = True Then
  121.             score = score + 1
  122.             lblScore.Text = score
  123.             picMush.Top = generator.Next(0, Me.Height - picMush.Height)
  124.             picMush.Left = generator.Next(0, Me.Width - picMush.Width)
  125.         End If
  126.     End Sub
  127. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement