|
-
May 14th, 2002, 02:30 PM
#1
Thread Starter
Member
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:
Dim Cars
Dim blnMoveL As Boolean
Dim blnMoveR As Boolean
Dim blnMoveU As Boolean
Dim blnMoveD As Boolean
Dim blnPlay As Boolean
Dim mintPos As Integer
Dim mintLives As Integer
Dim mintCars As Integer
Dim mintLevel As Integer
Const mintFrog As Integer = 480 'Height and Width of the frog image
Option Explicit
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
'*snip (this code works fine, it's just the code to move the frog image)*
CheckDeath 'Calls the CheckDeath function
If imgFrog.Top = 0 Then AdvanceLevel 'Calls the AdvanceLevel function when frog
'reaches the top
End Sub
Private Sub tmrCar_Timer()
'Moves the "cars" around
On Error GoTo Finish
For Cars = 0 To mintCars
shaCar(Cars).Left = shaCar(Cars).Left + 480
If shaCar(Cars).Left > frmMain.Width Then
mintPos = shaCar(Cars).Left - frmMain.Width
If mintPos >= 0 Then
shaCar(Cars).Left = -(mintFrog)
End If
End If
Next Cars
CheckDeath
Finish:
End Sub
Private Function AdvanceLevel()
'Makes the game progressively harder
Dim intTimer As Integer
intTimer = tmrCar.Interval
intTimer = intTimer - 100
If intTimer = 0 Then
MsgBox "You've beaten the game! You are a true 'Frogger' Master!", vbExclamation, "Congratulations!"
blnPlay = False
tmrCar.Enabled = False
Else
mintLevel = mintLevel + 1
mintCars = mintCars + 2
'Above line is the line that causes the problem I believe - when I comment it out the program works fine
tmrCar.Interval = intTimer
MsgBox "Proceeding to level " & mintLevel, vbOKOnly, "Level passed!"
End If
lblLvl = mintLevel
StartPos 'Calls the StartPos function
End Function
Private Function StartPos()
'Moves the frog image to the starting position
imgFrog.Left = 4800
imgFrog.Top = 4800
End Function
Private Function CheckDeath()
'Checks to see if the frog got ran over
Dim Cars2
On Error GoTo Finish
For Cars2 = 0 To mintCars
If shaCar(Cars2).Left = imgFrog.Left And shaCar(Cars2).Top = imgFrog.Top Then
mintLives = mintLives - 1
If mintLives < 0 Then
MsgBox "Game over!"
blnPlay = False
Else
MsgBox "You died!" & vbCrLf & mintLives & " lives left!", vbCritical, "Splat!"
StartPos 'Calls the StartPos function
lblLives = mintLives
End If
End If
Next Cars2
Finish:
End Function
-
May 14th, 2002, 03:00 PM
#2
Not NoteMe
Change this
VB Code:
If shaCar(Cars2).Left = imgFrog.Left And shaCar(Cars2).Top = imgFrog.Top Then
to this
VB Code:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|