Any place in your code that has the traffic array currently will be affected by this change, so check all instances where it appears and make the adjustments.
The function Move_Traffic will change considerably, as you now will have to make sure that you don't place the cars too close together and that they will come out in a random fashion. It shouldn't be too hard, though. You are storing the direction of the car, so you move it based upon that. Once your x position moves the car completely off the screen, you change it's status to being available for the next random pick.
I would suggest writing out either on paper or in the code what the different parts of the traffic array stand for (i.e. traffic(x,1) = the object # of the car, traffic(x,2) = direction, etc. This would be a good habit to develop as you may set a game on the shelf for a while and come back to it later and if you have good notes it will make it easier. This is probably a good time to mention using rem statements in your code, describing what a function or subroutine or a particular line of code is doing. Some people tend to carry rem statements a little far, but it is good to remind you of what your intent with a particular section of code is.
I don't have much time tonight yet, but perhaps tomorrow I can post something else that will help you out.
EDIT: The collision stinks, but this is at least a working version of random car placement. You need to set up collision boxes for everything.
Sync On : Sync Rate 60
randomize timer()
hide mouse
Load Image "data\background.jpg", 1, 1
Load Image "data\play.tga", 2, 1
Load Image "data\play_h.tga", 3, 1
Load Image "data\load.tga", 4, 1
Load Image "data\load_h.tga", 5, 1
`Load Music "sounds\frogger.mp3", 1
`Loop Music 1
Global ingame
global Frogx
global Frogy=-500
global Frogz
global Frog
global Score
global Lives
global PondFlag
Global Frog = 1
Global FrogSpeed = 5
Global TrafficSpeed = 15
Global Dim Traffic(9,6)
Global Lives = 3
Global Dim Pond(1)
Global PondFlag
Global Score
PondFlag=0
repeat
Paste Image 1, 0, 0
` check to see if the user has clicked on PLAY
If Mousex() > 400 AND Mousex() < 400 + Image Width(3) AND Mousey() > 200 AND Mousey() < 200 + Image Height(3)
Paste Image 3, 400, 200, 1
If Mouseclick() = 1
`Stop Music 1
ingame = 1
Endif
Else
Paste Image 2, 400, 200, 1
Endif
` check to see if the user clicked on LOAD
If Mousex() > 400 AND Mousex() < 400 + Image Width(5) AND Mousey() > 300 AND Mousey() < 300 + Image Height(5)
Paste Image 5, 400, 300, 1
If Mouseclick() = 1
Load_Game()
` wait until the user lets go of the mouse button
repeat
sync
until mouseclick() = 0
endif
Else
Paste Image 4, 400, 300, 1
Endif
Sync
until ingame = 1
gosub _Load_Game
` **************************** Main Loop ************************************************
disable escapekey
repeat
Hit = 0
Control_Frog()
Move_Traffic()
Hit = Collision_Check()
If Hit = 1
Frog_Death()
Dec Lives
Play Sound 1
Endif
If Hit = 2
Score = Hit_Pond(PondFlag, Score)
Endif
If Lives = 0
repeat
` wait until the user is not pressing any keys
sync
until scancode() = 0
GameSaved = 0
repeat
Center Text 320, 240, "Game Over!"
if GameSaved = 0 then Center Text 320, 260, "Press 'S' to save the game."
Center text 320, 280, "Press ESC to quit."
Center Text 320, 300, "Press ANY other Key to Replay!"
Sync
scan = scancode()
if scan = 31 and GameSaved = 0: ` user pressed S to save
GameSaved = 1
Save_Game()
scan = 0
repeat
` wait until the user is not pressing any keys
sync
until scancode() = 0
endif
until scan > 0
if scan <> 1
Lives = 3
Frog_Death()
score = 0
endif
Endif
Text 10, 10, "Lives: " + Str$(Lives)
Text 10, 20, "Score: " + Str$(Score)
`If Inkey$() = "s" Then Save_Game()
`If Inkey$() = "l" Then Load_Game()
Sync
until keystate(1) = 1 or lives = 0 : ` keep running until ESC is pressed or user quits
` **************************** Main Loop ************************************************
` clean up before exit
enable escapekey
DeleteStuff()
show mouse
end
_Load_Game:
Backdrop On
Load Image "data\level1.jpg", 6
Load Sound "sounds\win1.wav", 2
Load Sound "sounds\frogdeath.wav", 1
Load Sound "sounds\honk1.wav", 3
Load Sound "sounds\honk2.wav", 8
Texture Backdrop 6
Load Object "data\frog.x", Frog
xrotate object Frog, 270.0
fix object pivot frog
Load Image "data\frog1.tga", 2
Texture Object Frog, 2
scale object frog,2000,2000,2000
Set Object Collision On Frog
Position Object Frog, 0, -500, 0
Position Camera 0, 0, -1000
Point Camera 0, 0, 0
Autocam Off
car=freeobj()
Load Object "data\car.x",car
Load Image "data\rio blue.bmp", 10
Load Image "data\rio red.bmp", 11
Load Image "data\rio silver.bmp", 12
Load Image "data\rio yellow.bmp", 13
Rotate Object car, 0, 180, 0
fix object pivot car
scale object car,1500,1500,1500
Set Object Collision On car
For x = 0 to 9
obj = freeobj()
Traffic(x,1) = obj
color = rnd(3) + 10
Traffic(x,6) = color
clone object obj,car
texture object obj,color
Traffic(x,3) = 0 : ` status = inactive
hide object traffic(x,1)
Next x
delete object car
`For x = 0 to 4
` Position Object Traffic(x,1), (250 * x)-500, 100, 0
` Rotate Object Traffic(x,1), 180, 180, 0
`Next x
`For x = 5 to 9
` Position Object Traffic(x,1), (250 * x-4)-500, -100, 0
`Next x
for i=0 to 1
Pond(i)=freeobj()
Make Object Box Pond(i), 500, 250, 5
Color Object Pond(i), RGB(0, 0, 255)
Position Object Pond(i), 0, 500, 0
Set Object Collision On Pond(i)
`hide object Pond(i)
next i
Return
Function Control_Frog()
If Upkey() = 1 Then Position Object Frog, Object Position x(Frog),Object Position y(Frog)+FrogSpeed, Object Position z(Frog)
If Downkey() = 1 Then Position Object Frog, Object Position x(Frog),Object Position y(Frog)-FrogSpeed, Object Position z(Frog)
If Leftkey() = 1 Then Position Object Frog, Object Position x(Frog)- FrogSpeed,Object Position y(Frog), Object Position z(Frog)
If Rightkey() = 1 Then Position Object Frog, Object Position x(Frog)+ FrogSpeed,Object Position y(Frog), Object Position z(Frog)
EndFunction
Function Move_Traffic()
` check to see if we can place another vehicle
CarsMoving = 0 : OkayToPlace = 0
for pv = 0 to 9
if traffic(pv,3) = 1 : ` check the car's status - is it active?
inc CarsMoving,1
x# = object position x(traffic(pv,1))
if x# > 200.0 and x# < 500.0
` this car is okay for allowing a new vehicle to be placed
inc OkayToPlace,1
endif
` move it
dr = -1 : if traffic(pv,2) = 4 then dr = 1
position object traffic(pv,1),Object Position x(Traffic(pv,1)) + (TrafficSpeed * dr), Object Position y(Traffic(pv,1)), 0.0
` check to see if it is off the screen
if object in screen(traffic(pv,1)) = 0
dec CarsMoving,1
hide object traffic(pv,1)
traffic(pv,3) = 0
endif
endif
next pv
if CarsMoving = 0 or (OkayToPlace = CarsMoving)
` we have nothing to run over the frog with; need to get a car rolling
PlaceCar()
endif
remstart
For x = 0 to 4
Position Object Traffic(x,1), Object Position x(Traffic(x,1))+TrafficSpeed, 100, 10
Next x
For x = 5 to 9
Position Object Traffic(x,1), Object Position x(Traffic(x,1))-TrafficSpeed, -100, 0
Next x
If Object Position x(4) > 1500
For x = 0 to 4
Position Object Traffic(x,1), (250 * x)-2000, 100, 0
Play Sound 3
Next x
Endif
If Object Position x(9) < -1500
For x = 5 to 9
Position Object Traffic(x,1), (250 * (x-4))+2000, -100, 0
Play Sound 8
Next x
Endif
remend
EndFunction
Function Collision_Check()
For x = 0 to 9
if traffic(x,3) = 1 : ` is car active?
If Object Collision(Frog, Traffic(x,1)) = 1 Then Hit = 1
endif
Next x
If Object Collision(Frog, Pond(0)) = 1 Then Hit = 2
If Object Collision(Frog, Pond(1)) = 1 Then Hit = 2
EndFunction Hit
Function Frog_Death()
Position Object Frog, 0, -500, 0
EndFunction
Function Hit_Pond(PondFlag, Score)
If Object Collision (Frog, Pond(0)) = 1
If PondFlag = 0
PondFlag = 1
Inc Score
Position Object Frog, 0, -500, 0
Play Sound 2
Endif
Endif
EndFunction Score
Function Save_Game()
If File Exist("data\Savegame.dat") = 1 Then Delete File "data\Savegame.dat"
Open to Write 1, ("data\Savegame.dat")
Write Long 1, Object Position x(Frog)
Write Long 1, Object Position y(Frog)
Write Long 1, Object Position z(Frog)
Write Long 1, Score
Write Long 1, Lives
Write Long 1, PondFlag
Close File 1
Wait 1000
If File Exist("data\Savegame.dat") = 1
Center Text 320, 160, " Save Game Completed! Press ANY Key to Continue."
Sync
Wait Key
Else
Center Text 320, 160, " Save Game Failed! Press ANY Key to Continue."
Sync
Wait Key
Endif
EndFunction
Function Load_Game()
failed=0
If File Exist("data\Savegame.dat") = 1
Open to Read 1, "data\Savegame.dat"
Read Long 1, Frogx
Read Long 1, Frogy
Read Long 1, Frogz
Read Long 1, Score
Read Long 1, Lives
Read Long 1, PondFlag
Close File 1
Else
failed=1
Center Text 320, 160, " File Doesn't Exist!"
Sync
Wait Key
Endif
EndFunction failed
function freeobj()
num = 0
repeat
inc num
until object exist(num) = 0
endfunction num
function DeleteStuff()
if image exist(6) = 1 then delete Image 6
for s = 1 to 3
if sound exist(s) = 1 then delete sound s
next s
if sound exist(8) = 1 then delete sound 8
if object exist(Frog) = 1 then delete object Frog
for p = 0 to 1
if object exist(pond(p)) = 1 then delete object pond(p)
next p
for c = 0 to 9
if object exist(traffic(c,1)) = 1 then delete object traffic(c,1)
next c
endfunction
function PlaceCar()
` first, see if there is an inactive car available
car = -1
for c = 9 to 0 step -1
if traffic(c,3) = 0 then car = c
next c
if car >= 0
side = rnd(1)
if side = 0
`left side
position object traffic(car,1),-800.0,-100.0,0.0
zrotate object traffic(car,1),180.0
traffic(car,2) = 4 : ` direction = moving right
traffic(car,3) = 1 : ` status = active
show object traffic(car,1)
else
`right side
position object traffic(car,1),800.0,100.0,0.0
zrotate object traffic(car,1),0.0
traffic(car,2) = 8 : ` direction = moving left
traffic(car,3) = 1 : ` status = active
show object traffic(car,1)
endif
endif
endfunction
There are many different ways to go about coding. You have to decide which coding style is best and how you want to do things.