• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

moving cars and logs in array.

I am currently using a game to creat a frogger program and so far I produced some moving cars and logs across the screen. I can get the cars and logs to go across the screen but after awhile the longer logs will cramp up with the shorter ones and the trucks will overlap with the small cars.  And I feel this isn't the most efficicient method in doing this.  Can anyone just read through the code and tell me how I can improve on this?

Thanks!

Option Explicit

Dim i As Integer
Dim j As Integer
Dim CarSpeed(8) As Integer
Dim LogSpeed(10) As Integer

Sub LoadGame()

For i = 0 To 3: CarSpeed(i) = 70: Next i
For i = 4 To 8: CarSpeed(i) = -80: Next i
For i = 0 To 2: LogSpeed(i) = 30: Next i
For i = 3 To 5: LogSpeed(i) = -40: Next i
For i = 6 To 8: LogSpeed(i) = 35: Next i

TmrRoad.Enabled = True
TmrRiver.Enabled = True

End Sub

Private Sub Form_Load()

LoadGame

End Sub
Private Sub TmrRiver_Timer()
    For j = 0 To 8
        ImgLog(j).Left = ImgLog(j).Left + LogSpeed(j)
       
            If ImgLog(j).Left > frmFrogger.ScaleWidth Then
           
                ImgLog(j).Left = 0 - ImgLog(j).Width
               
            End If
           
            If ImgLog(j).Left < (0 - ImgLog(j).Width) Then
               
                ImgLog(j).Left = frmFrogger.ScaleWidth
               
            End If
    Next j
End Sub

Private Sub TmrRoad_Timer()

    For j = 0 To 8
       
        ImgCar(j).Left = ImgCar(j).Left + CarSpeed(j)
       
            If ImgCar(j).Left > frmFrogger.ScaleWidth Then
               
                ImgCar(j).Left = 0 - ImgCar(j).Width
           
            End If
           
            If ImgCar(j).Left < (0 - ImgCar(j).Width) Then
               
                ImgCar(j).Left = frmFrogger.ScaleWidth
           
            End If
    Next j
   
End Sub
0
n-generation
Asked:
n-generation
  • 2
1 Solution
 
PaulHewsCommented:
I guess I would introduce a random element into the speed of the various objects:

For i = 0 To 3: CarSpeed(i) = 70 + CInt(Rnd * 10): Next i
For i = 4 To 8: CarSpeed(i) = -(80 + CInt(Rnd * 10)): Next i
For i = 0 To 2: LogSpeed(i) = 30 + CInt(Rnd * 10): Next i
For i = 3 To 5: LogSpeed(i) = -(40 + CInt(Rnd * 10)): Next i
For i = 6 To 8: LogSpeed(i) = 35 + CInt(Rnd * 10): Next i
0
 
PaulHewsCommented:
Also unsure why you are processing with two separate timers.  
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now