TGC Codebase Backup



simple traffic simulation by Anonymous Coder

30th Sep 2008 14:27
Summary

Simple Traffic simulation Six cars and traffic lights,



Description

To run the code copy and paste the folder called 1 in C:
It s not fully completed but runs ok, there r few bugs, nothing special about this, it s just my first dbasic program so enjoy it



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    sync on
sync rate 0
load image "c:\\1\\bluecar.png",1,1
load image "c:\\1\\background.png",2,1
load image "c:\\1\\greenlight.png",3,1
load image "c:\\1\\redlight.png",4,1
load image "c:\\1\\redcar.png",5,1
load image "c:\\1\\lamppost.png",6,1
load image "c:\\1\\caryellow.png",7,1
rem car variables-start
rem -------bluecar1-------
bluecar1x=-10
bluecar1y=69
rem -------bluecar1-------
rem -------bluecar2-------
bluecar2x=-100
bluecar2y=69
rem -------bluecar2-------
rem --------redcar1-------
redcar1x=640
redcar1y=110
rem --------redcar1-------
rem -------redcar2--------
redcar2x=750
redcar2y=110
rem -------redcar2--------
rem -------yellowcar1-----
yellowcar1x=300
yellowcar1y=600
rem -------yellowcar1-----
yellowcar2x=300
yellowcar2y=800
rem car speed-start
bluecar1_speed=1
bluecar2_speed=2
redcar1_speed=0.5
redcar2_speed=0.5
yellowcar1_speed=1
yellowcar2_speed=1
rem car speed-end
rem car variables-end
bluecar1angle=1
bluecar2angle=1
redcar1angle=1
redcar2angle=1
yellowcar1angle=1
yellowcar2angle=1
rem traffic light reference-start
lightrefx=-10
lightrefy=1
rem traffic light reference-end
rem random numbers for cars-start

do rem MAIN LOOP
paste image 2,1,1
rem Traffic lights-start
rem -black background of lights start
sprite 10,250,3,6
sprite 11,223,185,6
sprite 12,420,207,6
rotate sprite 12,180
rotate sprite 11,270
rem -black background of lights end
inc lightrefx
sprite 3,lightrefx,lightrefy,1
hide sprite 3
if lightrefx=1800
dec lightrefx,1900
endif
if lightrefx=1
sprite 4,250,3,3
sprite 5,250,3,4
sprite 13,223,185,4
sprite 14,223,185,3
sprite 15,420,207,3
sprite 16,420,207,4
rotate sprite 13,270
rotate sprite 14,270
rotate sprite 15,180
rotate sprite 16,180
hide sprite 16
show sprite 15
hide sprite 14
show sprite 13
hide sprite 5
show sprite 4
endif

if lightrefx=300
show sprite 4
show sprite 13
show sprite 15
rem green sprite
sprite 4,250,3,3
sprite 14,223,185,3
sprite 15,420,207,3
endif

if lightrefx=1000
hide sprite 13
show sprite 14
hide sprite 15
show sprite 16
hide sprite 4
show sprite 5
sprite 14,223,185,3
rem red sprite
sprite 5,250,3,4
sprite 16,420,207,4
endif
rem Traffic Lights-end

rem bluecar1 ROAD AUTO SWITCHER -START
if bluecar1x=>10 and bluecar1x<=20
randomize timer()
for c=1 to 1 step 1
rndnobluecar1=rnd(c)+(rnd(-1)+1)
next c
endif
rem bluecar1 ROAD AUTO SWITCHER -END

rem bluecar2 ROAD AUTO SWITCHER -START
if bluecar2x=>10 and bluecar2x<=20
randomize timer()
for v=1 to 1 step 1
rndnobluecar2=rnd(v)+(rnd(-1)+1)
next v
endif
rem bluecar2 ROAD AUTO SWITCHER -END

rem redcar1 ROAD AUTO SWITCHER -START
if redcar1x=>550 and redcar1x<=570
randomize timer()
for d=1 to 1 step 1
rndnoredcar1=rnd(d)+(rnd(-1)+1)
next d
endif
rem redcar1 ROAD AUTO SWITCHER -END


rem redcar2 ROAD AUTO SWITCHER -START
if redcar2x=>550 and redcar2x<=570
randomize timer()
for d=1 to 1 step 1
rndnoredcar2=rnd(d)+(rnd(-1)+1)
next d
endif
rem redcar2 ROAD AUTO SWITCHER -END

rem yellowcar1 ROAD AUTO SWITCHER -START
if yellowcar1y=>400 and yellowcar1y<=420
randomize timer()
for f=1 to 1 step 1
rndnoyellowcar1=rnd(f)+(rnd(-1)+1)
next f
endif
rem yellowcar1 ROAD AUTO SWITCHER -END

rem yellowcar2 ROAD AUTO SWITCHER -START
if yellowcar2y=>400 and yellowcar2y<=420
randomize timer()
for f=1 to 1 step 1
rndnoyellowcar2=rnd(f)+(rnd(-1)+1)
next f
endif
rem yellowcar2 ROAD AUTO SWITCHER -END


rem car movements-start-


rem bluecar1 move-start

inc bluecar1x,bluecar1_speed
sprite 1,bluecar1x,bluecar1y,1
rem bluecar1 move-end

rem bluecar2 move-start
inc bluecar2x,bluecar2_speed
sprite 2,bluecar2x,bluecar2y,1
rem bluecar2 move-end

rem redcar1 move-start
dec redcar1x,redcar1_speed
sprite 17,redcar1x,redcar1y,5
rem redcar1 move-end

rem redcar2 move-start
dec redcar2x,redcar2_speed
sprite 18,redcar2x,redcar2y,5
rem redcar2 move-end

rem yellowcar1 move-start
dec yellowcar1y,yellowcar1_speed
sprite 19,yellowcar1x,yellowcar1y,7
rem yellowcar1 move-end


rem yellowcar2 move-start
dec yellowcar2y,yellowcar2_speed
sprite 20,yellowcar2x,yellowcar2y,7



rem carblue1 straight loop
if bluecar1x=>750
dec bluecar1x,850
endif
rem -----------------------

rem carblue2 straight loop
if bluecar2x=>650
dec bluecar2x,1000
endif
rem ----------------------
rem redcar1 straight loop
if redcar1x<=-50
inc redcar1x,800
endif
rem ---------------------
rem redcar2 straight loop
if redcar2x<=-50
inc redcar2x,1000
endif
rem ---------------------

rem bluecar1 turn right loop---start
if bluecar1x=>290 and rndnobluecar1=2 and bluecar1y=>69

inc bluecar1y,1
inc bluecar1angle,1
rotate sprite 1,bluecar1angle
endif
if bluecar1y=>150
bluecar1x=375
bluecar1angle=90
inc bluecar1y,bluecar1_speed
sprite 1,bluecar1x,bluecar1y,1
endif
if bluecar1y=>550 and bluecar1x=>375
inc bluecar1angle,270
rotate sprite 1,bluecar1angle
bluecar1x=-400
bluecar1y=69
endif
rem bluecar1 turn right loop---end

rem bluecar2 turn right loop---start
if bluecar2x=>290 and rndnobluecar2=2 and bluecar2y=>69

inc bluecar2y,1
inc bluecar2angle,1
rotate sprite 2,bluecar2angle
endif

if bluecar2y=>150
bluecar2x=375
bluecar2angle=90
inc bluecar2y,bluecar2_speed
sprite 2,bluecar2x,bluecar2y,1
endif
if bluecar2y=>550 and bluecar2x=>375
inc bluecar2angle,270
rotate sprite 2,bluecar2angle
bluecar2x=-600
bluecar2y=69
endif
rem bluecar2 turn right loop---end

rem redcar1 turn left loop -start
if redcar1x<=376 and rndnoredcar1=1
inc redcar1y,1
dec redcar1angle,2
rotate sprite 17,redcar1angle
endif
if redcar1angle=>315
inc redcar1y,2
endif
if redcar1y=>150
inc redcar1x,1
redcar1x=352
redcar1angle=270
inc redcar1y,redcar1_speed
sprite 17,redcar1x,redcar1y,5
endif

rem ----------redcar1 turn-important part------
if redcar1y=>580 and redcar1x=>352
dec redcar1angle,270
rotate sprite 17,redcar1angle
redcar1y=110
redcar1x=800
endif
rem --------------------------------------------
rem redcar1 turn right loop---end

rem redcar2 turn left loop -start
if redcar2x<=376 and rndnoredcar2=1
inc redcar2y,1
dec redcar2angle,2
rotate sprite 18,redcar2angle
endif
if redcar2angle=>315
inc redcar2y,2
endif
if redcar2y=>150
inc redcar2x,1
redcar2x=352
redcar2angle=270
inc redcar2y,redcar2_speed
sprite 18,redcar2x,redcar2y,5
endif


rem ----------redcar2 turn-important part------
if redcar2y=>600 and redcar2x=>352
dec redcar2angle,270
rotate sprite 18,redcar2angle
redcar2y=110
redcar2x=1200
endif
rem redcar2 turn right loop---end


rem yellowcar1 turn left loop -start
if yellowcar1y<=140 and rndnoyellowcar1=1
dec yellowcar1x,yellowcar1_speed
sprite 19,yellowcar1x,yellowcar1y,7
endif
if yellowcar1y<=150 and rndnoyellowcar1=1
dec yellowcar1angle,2
rotate sprite 19,yellowcar1angle
endif
if yellowcar1angle<=-40 and rndnoyellowcar1=1
yellowcar1angle=-90
yellowcar1y=140
dec yellowcar1x,yellowcar1_speed
sprite 19,yellowcar1x,yellowcar1y,7
endif
if yellowcar1x<=-20 and rndnoyellowcar1=1
inc yellowcar1angle,90
rotate sprite 19,yellowcar1angle
yellowcar1x=300
yellowcar1y=900
yellowcar1angle=0
dec yellowcar1y,yellowcar1_speed
sprite 19,yellowcar1x,yellowcar1y,7
endif
rem yellowcar1 turn left loop---end




rem yellowcar2 turn left loop -start
if yellowcar2y<=140 and rndnoyellowcar2=1
dec yellowcar2x,yellowcar2_speed
sprite 20,yellowcar2x,yellowcar2y,7
endif
if yellowcar2y<=150 and rndnoyellowcar2=1
dec yellowcar2angle,2
rotate sprite 20,yellowcar2angle
endif
if yellowcar2angle<=-40 and rndnoyellowcar2=1
yellowcar2angle=-90
yellowcar2y=140
dec yellowcar2x,yellowcar2_speed
sprite 20,yellowcar2x,yellowcar2y,7
endif
if yellowcar2x<=-20 and rndnoyellowcar2=1
inc yellowcar2angle,90
rotate sprite 20,yellowcar2angle
yellowcar2x=300
yellowcar2y=1000
yellowcar2angle=0
dec yellowcar2y,yellowcar2_speed
sprite 20,yellowcar2x,yellowcar2y,7
endif
rem yellowcar2 turn left loop---end



rem yellowcar1 turn right loop-start
if yellowcar1y<=160 and rndnoyellowcar1=2
inc yellowcar1angle,1
rotate sprite 19,yellowcar1angle
inc yellowcar1x,yellowcar1_speed
endif


if yellowcar1angle=>83  and yellowcar1x=>383 and rndnoyellowcar1=2
yellowcar1y=70
yellowcar1angle=90
inc yellowcar1x,1
sprite 19,yellowcar1x,yellowcar1y,7
endif
if yellowcar1x=>700
inc yellowcar1angle,270
rotate sprite 19,yellowcar1angle
yellowcar1x=300
yellowcar1y=1200
yellowcar1angle=0
endif
rem yellowcar1 turn right loop-end


rem yellowcar2 turn right loop-start
if yellowcar2y<=160 and rndnoyellowcar2=2
inc yellowcar2angle,1
rotate sprite 20,yellowcar2angle
inc yellowcar2x,yellowcar2_speed
endif


if yellowcar2angle=>83  and yellowcar2x=>383 and rndnoyellowcar2=2
yellowcar2y=70
yellowcar2angle=90
inc yellowcar2x,1
sprite 20,yellowcar2x,yellowcar2y,7
endif
if yellowcar2x=>700
inc yellowcar2angle,270
rotate sprite 20,yellowcar2angle
yellowcar2x=300
yellowcar2y=1300
yellowcar2angle=0
endif
rem yellowcar1 turn right loop-end
rem ------------------------------LIGHTS------------------------------------
rem bluecar1 light settings-start
if lightrefx=>1000 and lightrefx<=1800 and bluecar1x=>240 and bluecar1x<=245
bluecar1_speed=0
endif
if lightrefx<=1 and lightrefx=>-100 and bluecar1x=>240 and bluecar1x<=245
bluecar1_speed=0
endif
if lightrefx=>1 and lightrefx<=1000 and bluecar1_speed=0
bluecar1_speed=1
endif
rem bluecar1 light settings-end

rem bluecar2 light settings-start
if lightrefx=>1000 and lightrefx<=1800 and bluecar2x=>240 and bluecar2x<=245
bluecar2_speed=0
endif
if lightrefx<=1 and lightrefx=>-100 and bluecar2x=>240 and bluecar2x<=245
bluecar2_speed=0
endif
if lightrefx=>1 and lightrefx<=1000 and bluecar2_speed=0
bluecar2_speed=1
endif
rem bluecar2 light settings-end

rem first red light bluecar2 stops behind-start
if lightrefx=>1000 and lightrefx<=1800 and bluecar1x=>240 and bluecar1x<=245 and bluecar1_speed=0 and bluecar2x=>180 and bluecar2x<=200
bluecar2_speed=0
endif
if lightrefx<=1 and lightrefx=>-100 and bluecar1x=>240 and bluecar1x<=245 and bluecar1_speed=0 and bluecar2x=>180 and bluecar2x<=200
bluecar2_speed=0
endif
rem first red light bluecar2 stops behind-end
rem second red light bluecar1 stops behind-start
if lightrefx=>1000 and lightrefx<=1800 and bluecar2x=>240 and bluecar2x<=245 and bluecar2_speed=0 and bluecar1x=>180 and bluecar1x<=200
bluecar1_speed=0
endif
if lightrefx<=1 and lightrefx=>-100 and bluecar2x=>240 and bluecar2x<=245 and bluecar2_speed=0 and bluecar1x=>180 and bluecar1x<=200
bluecar1_speed=0
endif
rem second red light bluecar1 stops behind-end

rem redcar1 light settings-start
if lightrefx=>1000 and lightrefx<=1800 and redcar1x>377 and redcar1x<385
redcar1_speed=0
endif
if lightrefx<=1 and lightrefx=>-100 and redcar1x>377 and redcar1x<385
redcar1_speed=0
endif
if lightrefx=>1 and lightrefx<=1000 and redcar1_speed=0
redcar1_speed=1
endif
rem redcar1 light settings-end
rem redcar2 light settings-start
if lightrefx=>1000 and lightrefx<=1800 and redcar2x>377 and redcar2x<385
redcar2_speed=0
endif
if lightrefx<=1 and lightrefx=>-100 and redcar2x>377 and redcar2x<385
redcar2_speed=0
endif
if lightrefx=>1 and lightrefx<=1000 and redcar2_speed=0
redcar2_speed=1
endif
rem redcar2 light settings-end

rem first red light redcar2 stops behind-start
if lightrefx=>1000 and lightrefx<=1800 and redcar1x=>377 and redcar1x<=385 and redcar1_speed=0 and redcar2x=>400 and redcar2x<=460
redcar2_speed=0
endif
if lightrefx<=1 and lightrefx=>-100 and redcar1x=>377 and redcar1x<=385 and redcar1_speed=0 and redcar2x=>400 and redcar2x<=460
redcar2_speed=0
endif
rem first red light redcar2 stops behind-end
rem first red light redcar1 stops behind-start
if lightrefx=>1000 and lightrefx<=1800 and redcar2x=>377 and redcar2x<=385 and redcar2_speed=0 and redcar1x=>400 and redcar1x<=460
redcar1_speed=0
endif
if lightrefx<=1 and lightrefx=>-100 and redcar2x=>377 and redcar2x<=385 and redcar2_speed=0 and redcar1x=>400 and redcar1x<=460
redcar1_speed=0
endif
rem first red light redcar2 stops behind-end

rem yellowcar1 light settings-start
if lightrefx=>1000 and lightrefx<=1800 and yellowcar1y=>162 and yellowcar1y<=164
yellowcar1_speed=1
endif
if lightrefx<=1 and lightrefx=>-100 and yellowcar1y=>162 and yellowcar1y<=164
yellowcar1_speed=1
endif
if lightrefx=>1 and lightrefx<=1000 and yellowcar1_speed=1 and yellowcar1y=>162 and yellowcar1y<=164
yellowcar1_speed=0
endif
if lightrefx=>-100 and lightrefx<=1 and yellowcar1_speed=1 and yellowcar1y=>162 and yellowcar1y<=164
yellowcar1_speed=0
endif
rem yellowcar1 light settings-end
rem yellowcar2 light settings-start
if lightrefx=>1000 and lightrefx<=1800 and yellowcar2y=>162 and yellowcar2y<=164
yellowcar2_speed=1
endif
if lightrefx<=1 and lightrefx=>-100 and yellowcar2y=>162 and yellowcar2y<=164
yellowcar2_speed=1
endif
if lightrefx=>1 and lightrefx<=1000 and yellowcar2_speed=1 and yellowcar2y=>162 and yellowcar2y<=164
yellowcar2_speed=0
endif
if lightrefx=>-100 and lightrefx<=1 and yellowcar2_speed=1 and yellowcar2y=>162 and yellowcar2y<=164
yellowcar2_speed=0
endif
rem yellowcar2 light settings-end

rem first red light yellowcar2 stops behind-start
if lightrefx=>1 and lightrefx<=1000 and yellowcar1y=>162 and yellowcar1y<=164 and yellowcar1_speed=0 and yellowcar2y=>165 and yellowcar2y<=250 and yellowcar1x=300 and yellowcar2x=300
yellowcar2_speed=0
endif
if lightrefx=>-100 and lightrefx<=1 and yellowcar1y=>162 and yellowcar1y<=164 and yellowcar1_speed=0 and yellowcar2y=>165 and yellowcar2y<=250 and yellowcar1x=300 and yellowcar2x=300
yellowcar2_speed=0
endif
rem first red light yellowcar2 stops behind-end

rem first red light yellowcar1 stops behind-start
if lightrefx=>1 and lightrefx<=1000 and yellowcar2y=>162 and yellowcar2y<=164 and yellowcar2_speed=0 and yellowcar1y=>165 and yellowcar1y<=250 and yellowcar1x=300 and yellowcar2x=300
yellowcar1_speed=0
endif
if lightrefx=>-100 and lightrefx<=1 and yellowcar2y=>162 and yellowcar2y<=164 and yellowcar2_speed=0 and yellowcar1y=>165 and yellowcar1y<=250 and yellowcar1x=300 and yellowcar2x=300
yellowcar1_speed=0
endif
rem first red light yellowcar1 stops behind-end
rem yellowcar2 restart-start
if lightrefx=>1000 and lightrefx<=1800 and yellowcar1_speed=1
yellowcar2_speed=1
endif
rem yellowcar2 restart-end
rem yellowcar1 restart-start
if lightrefx=>1000 and lightrefx<=1800 and yellowcar2_speed=1
yellowcar1_speed=1
endif
rem yellowcar1 restart-end


REM -------------SAME---CARS-----------------CAR DISTANCES------------------------------------------------
rem bluecar2 distance start
if bluecar1y=69 and bluecar2y=69 and bluecar2x<=bluecar1x-1 and bluecar1x=>-500 and bluecar2x=>-500 and bluecar1x<=245 and bluecar2x<=245
bluecar2_speed=0
endif
if bluecar2x<=bluecar1x-60 and bluecar1y=69 and bluecar2y=69 and bluecar1x<=245 and bluecar2x<=245 and bluecar1x=>-500 and bluecar2x=>-500
bluecar2_speed=1
endif
rem bluecar2 distance end
rem bluecar1 distance start
if bluecar1y=69 and bluecar2y=69 and bluecar1x<=245 and bluecar2x<=245 and bluecar1x<=bluecar2x-1 and bluecar1x<=245 and bluecar2x<=245
bluecar1_speed=0
endif
if bluecar1x<=bluecar2x-60 and bluecar1y=69 and bluecar2y=69 and bluecar1x<=700 and bluecar2x<=700 and bluecar1x=>-500 and bluecar2x=>-500
bluecar1_speed=1
endif
rem bluecar1 distance end
rem yellow car2 distance start
if yellowcar1x=300 and yellowcar2x=300 and yellowcar2y=>yellowcar1y+1 and yellowcar1y<=1100 and yellowcar2y<=1100 and yellowcar1y=>165 and yellowcar2y=>165
yellowcar2_speed=0
endif
if yellowcar2y=>yellowcar1y+70 and yellowcar1x=300 and yellowcar2x=300 and yellowcar1y<=1100 and yellowcar2y<=1100 and yellowcar1y=>165 and yellowcar2y=>165
yellowcar2_speed=1
endif
rem yellowcar2 distance end
rem yellowcar1 distance start
if yellowcar1x=300 and yellowcar2x=300 and yellowcar1y=>yellowcar2y+1 and yellowcar1y<=1100 and yellowcar2y<=1100 and yellowcar1y=>165 and yellowcar2y=>165
yellowcar1_speed=0
endif
if yellowcar1y=>yellowcar2y+70 and yellowcar2x=300 and yellowcar1x=300 and yellowcar1y<=1100 and yellowcar2y<=1100 and yellowcar1y=>165 and yellowcar2y=>165
yellowcar1_speed=1
endif
rem yellowcar1 distance end
REM ----------SAME-CARS------------DISTANCES 1ST PART--------------------------------------
REM ----------ALL CARS-------------DISTANCES-----------------------------------------------
rem Bluecar1 with Redcars-start
if rndnobluecar1=2 and bluecar1x=>285 and bluecar1x<=288 and redcar1x<=360 and redcar1x=>220
bluecar1_speed=0
endif
if rndnobluecar1=2 and bluecar1x=>285 and bluecar1x<=288  and redcar1x<=360 and redcar1x=>220
bluecar1_speed=0
endif
if rndnobluecar1=2 and bluecar1x=>285 and bluecar1x<=288 and redcar1y<=250 and redcar2y=>165
bluecar1_speed=0
endif
if rndnobluecar1=2 and bluecar1x=>285 and bluecar1x<=288  and redcar2y=>250 and redcar1y<=200
bluecar1_speed=0
endif
rem bluecar2 with redcars
if rndnobluecar2=2 and bluecar2x=>250 and bluecar2x<=255 and redcar1x<=360 and redcar1x=>220
bluecar2_speed=0
endif
if rndnobluecar2=2 and bluecar2x=>250 and bluecar2x<=255  and redcar1x<=360 and redcar1x=>220
bluecar2_speed=0
endif
if rndnobluecar2=2 and bluecar2x=>250 and bluecar2x<=255 and redcar1y<=250 and redcar2y=>165
bluecar2_speed=0
endif
if rndnobluecar2=2 and bluecar2x=>250 and bluecar2x<=255  and redcar2y=>165 and redcar1y<=250
bluecar2_speed=0
endif




wait 7


sync



loop