VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Touch Module 2.0 More advanced game collision detection.

by GAMEGUY (4 Submissions)
Category: Games
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Wed 24th August 2005
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Touch Module 2.0 More advanced game collision detection.

API Declarations


Create a wall (Picture2) and copy it and say yes for array. (you have picture2(0) and picture2(1)
Copy first section of code into a module (TouchModule)
Copy second section of code into Picture1 Keydown


Rate Touch Module 2.0 More advanced game collision detection.



Function touch(Picture1 As PictureBox, Picture2 As PictureBox) As String
Step = 50 'MAKE SURE IF YOU CHANGE THE AMOUNT CHAR MOVES TO CHANGE THIS TO THE SAME #'
touch = "False"

If ((Picture1.Top + Picture1.Height >= Picture2.Top) _
  And (Picture1.Top <= Picture2.Top + Picture2.Height) _
  And (Picture1.Left + Picture1.Width >= Picture2.Left) _
  And (Picture1.Left + Picture1.Width - Step < Picture2.Left)) _
  Then
  touch = "Right"
  Picture1.Left = Picture1.Left - Step
End If


If ((Picture1.Top + Picture1.Height >= Picture2.Top) _
  And (Picture1.Top <= Picture2.Top + Picture2.Height) _
  And (Picture1.Left <= Picture2.Left + Picture2.Width) _
  And (Picture1.Left + Step > Picture2.Left + Picture2.Width)) _
  Then
  touch = "Left"
  Picture1.Left = Picture1.Left + Step
End If


If ((Picture1.Left + Picture1.Width >= Picture2.Left) _
  And (Picture1.Left <= Picture2.Left + Picture2.Width) _
  And (Picture1.Top <= Picture2.Top + Picture2.Height) _
  And (Picture1.Top + Step > Picture2.Top + Picture2.Height)) _
  Then
  touch = "Top"
  Picture1.Top = Picture1.Top + Step
End If


If ((Picture1.Left + Picture1.Width >= Picture2.Left) _
  And (Picture1.Left <= Picture2.Left + Picture2.Width) _
  And (Picture1.Top + Picture1.Height >= Picture2.Top) _
  And (Picture1.Top + Picture1.Height - Step < Picture2.Top)) _
  Then
  touch = "Bottom"
Picture1.Top = Picture1.Top - Step
End If
End Function

'SECTION 2

Select Case KeyCode
            Case vbKeyUp
                Picture1.Top = Picture1.Top - 50
                Picture1.picture = LoadPicture("Your bitmap of char facing up.bmp") 'Example : Picture1.picture = LoadPicture("C:/Program Files/My Game/Character/Up.bmp")
            Case vbKeyDown
                Picture1.Top = Picture1.Top + 50
                Picture1.picture = LoadPicture("Your bitmap of char facing down.bmp")
            Case vbKeyRight
                Picture1.Left = Picture1.Left + 50
                Picture1.picture = LoadPicture("Your bitmap of char facing right.bmp")
            Case vbKeyLeft
                Picture1.Left = Picture1.Left - 50
                Picture1.picture = LoadPicture("Your bitmap of char facing left.bmp")

                'Code to not allow picture1 pass through other pictures (you have array for picture2)
                For x = 0 To 3 '3 means you have 4 walls (0,1,2,3) change 3 if you have more walls
                    Print(touch(picture1, picture2(x)))
                Next x

                'if you want to run an event when picture1 hits another,

                If touch(picture1, Picture2) = "Left" Then 'Substitute "Left" for whatever side
                    'your code
                    'ex: form2.show
                End If

Download this snippet    Add to My Saved Code

Touch Module 2.0 More advanced game collision detection. Comments

No comments have been posted about Touch Module 2.0 More advanced game collision detection.. Why not be the first to post a comment about Touch Module 2.0 More advanced game collision detection..

Post your comment

Subject:
Message:
0/1000 characters