Results 1 to 2 of 2

Thread: A simple (?) error in a Frogger-type game

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    52

    A simple (?) error in a Frogger-type game

    Okay so I've got a slight problem here. I'm coding a simple Frogger type game, and it all works fine on the first level, but on the second level, the cars move faster and some of the cars hidden off the form become activated to create more of them, to increase the difficulty.

    My problem is, is after you pass the first level, the frog doesn't "die" when a car runs it over. It *will* however, die if the frog runs into a car. The first level doesn't have this problem. I think I've isolated the line of code that causes the error but I'm not sure, I've got it marked in the code.

    Pasted below is all the code I believe would be relevant (it's not all of it, but only the parts that would be needed to find the problem). Thanks in advance for any help anyone can give me.


    VB Code:
    1. Dim Cars
    2. Dim blnMoveL    As Boolean
    3. Dim blnMoveR    As Boolean
    4. Dim blnMoveU    As Boolean
    5. Dim blnMoveD    As Boolean
    6. Dim blnPlay     As Boolean
    7. Dim mintPos     As Integer
    8. Dim mintLives   As Integer
    9. Dim mintCars    As Integer
    10. Dim mintLevel   As Integer
    11. Const mintFrog  As Integer = 480    'Height and Width of the frog image
    12. Option Explicit
    13.  
    14. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    15. '*snip (this code works fine, it's just the code to move the frog image)*
    16.     CheckDeath  'Calls the CheckDeath function
    17.     If imgFrog.Top = 0 Then AdvanceLevel    'Calls the AdvanceLevel function when frog
    18.                                             'reaches the top
    19. End Sub
    20.  
    21. Private Sub tmrCar_Timer()
    22.     'Moves the "cars" around
    23.    
    24.     On Error GoTo Finish
    25.     For Cars = 0 To mintCars
    26.         shaCar(Cars).Left = shaCar(Cars).Left + 480
    27.         If shaCar(Cars).Left > frmMain.Width Then
    28.             mintPos = shaCar(Cars).Left - frmMain.Width
    29.             If mintPos >= 0 Then
    30.                 shaCar(Cars).Left = -(mintFrog)
    31.             End If
    32.         End If
    33.     Next Cars
    34.     CheckDeath
    35. Finish:
    36. End Sub
    37.  
    38. Private Function AdvanceLevel()
    39.     'Makes the game progressively harder
    40.    
    41.     Dim intTimer    As Integer
    42.     intTimer = tmrCar.Interval
    43.     intTimer = intTimer - 100
    44.     If intTimer = 0 Then
    45.         MsgBox "You've beaten the game! You are a true 'Frogger' Master!", vbExclamation, "Congratulations!"
    46.         blnPlay = False
    47.         tmrCar.Enabled = False
    48.     Else
    49.         mintLevel = mintLevel + 1
    50.         mintCars = mintCars + 2
    51. 'Above line is the line that causes the problem I believe - when I comment it out the program works fine
    52.         tmrCar.Interval = intTimer
    53.         MsgBox "Proceeding to level " & mintLevel, vbOKOnly, "Level passed!"
    54.     End If
    55.     lblLvl = mintLevel
    56.     StartPos    'Calls the StartPos function
    57. End Function
    58.  
    59. Private Function StartPos()
    60.     'Moves the frog image to the starting position
    61.    
    62.     imgFrog.Left = 4800
    63.     imgFrog.Top = 4800
    64. End Function
    65.  
    66. Private Function CheckDeath()
    67.     'Checks to see if the frog got ran over
    68.    
    69.     Dim Cars2
    70.    
    71.     On Error GoTo Finish
    72.     For Cars2 = 0 To mintCars
    73.         If shaCar(Cars2).Left = imgFrog.Left And shaCar(Cars2).Top = imgFrog.Top Then
    74.             mintLives = mintLives - 1
    75.             If mintLives < 0 Then
    76.                 MsgBox "Game over!"
    77.                 blnPlay = False
    78.             Else
    79.                 MsgBox "You died!" & vbCrLf & mintLives & " lives left!", vbCritical, "Splat!"
    80.                 StartPos    'Calls the StartPos function
    81.                 lblLives = mintLives
    82.             End If
    83.         End If
    84.     Next Cars2
    85. Finish:
    86. End Function

  2. #2
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Change this

    VB Code:
    1. If shaCar(Cars2).Left = imgFrog.Left And shaCar(Cars2).Top = imgFrog.Top Then

    to this

    VB Code:
    1. If shaCar(Cars2).Left < imgFrog.Left + ImgFrog.Width And ShaCar(Cars2).Left + ShaCar(Cars2).Width > imgFrog.Left And shaCar(Cars2).Top < imgFrog.Top + ImgFrog.Height And ShaCar(Cars2).Top + ShaCar(Cars2).Height > imgFrog.Top Then

    Basically the problem is that, as the car moves a long it may 'jump' the exact left value of the frog, so the frog would suvive. While at low speeds this would happen less frequently (or never in your case), it would happen very often unless the speed and start location of the car were multiples of the frogs start location, and move speed (or something like that).
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width