Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Making a traffic simulator
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
dudeofgods




PostPosted: Sat May 21, 2011 3:49 pm   Post subject: Making a traffic simulator

Hey guys I am making a simple traffic simulator for my computer class, however i am stumped on how to implement turns and multiple cars into my program.
Can anyone help??

Thanks!!



trafic sim.t
 Description:
feel free to modify anything you like to help me succeed in this traffic simulator!!

Download
 Filename:  trafic sim.t
 Filesize:  9.88 KB
 Downloaded:  383 Time(s)


trafic sim.t
 Description:

Download
 Filename:  trafic sim.t
 Filesize:  9.88 KB
 Downloaded:  230 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
HRI




PostPosted: Sat May 21, 2011 9:27 pm   Post subject: RE:Making a traffic simulator

Well the very first thing I'd do is get rid of that horrible flickering Sad
You can do so via a combination of setscreen ("offscreenonly") and View.Update.

Next, I notice you use 360 degree arcs for circles. drawfilloval might be easier.

Instead of drawing 4 specific cars, why not create an array of cars, have the procedure to draw a car take in the position etc as arguments, and have a variable for each saying whether they're on the screen or not (whether to draw them). To be honest, I would personally create a record with this info and make an array of that record, as that basically lets you store each car's position, direction, visibility, whatever you want.

Turing:

type Car :
    record
        %variables go here
    end record

var randomCar : Car
randomCar.colour := brightred
% you can access the variables via the dot operator


As for turns, the only thing I can say is check whether the car is in the right spot to make a turn, and figure out a trigonometric relationship you can use to draw the car at a certain angle as it turns. Or you can try using pictures and Pic.Rotate, although the one time I used that it had some weird clipping issues :p

EDIT: Almost forgot, NEVER USE PROCESSES Razz
There's a good thread in the tutorials section explaining why. Also, I think it might be making that View.Update still cause flickering as both are running at once.

To resolve the issue (again, this would be easier with an array or preferably record), store the position of everything, draw it once in your main loop, and then use View.Update once after everything is drawn. I also think you forgot the "offscreenonly" part, because View.Update is a bit useless without it :p
Tony




PostPosted: Sat May 21, 2011 9:57 pm   Post subject: RE:Making a traffic simulator

HRI gives good advice here. If you implement everything for a single car in an array of size 1, it is trivial to increase the size of array and have more cars without writing (much?) additional code.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
dudeofgods




PostPosted: Sun May 22, 2011 10:14 am   Post subject: Re: Making a traffic simulator

Thanks, HRI

you have very good idea's. However, i am still a noob at Turing so, i think i will not use array's and stick with the function pic.rotate that you just told me about.

Thanks alot,

Dudeofgods
Raknarg




PostPosted: Sun May 22, 2011 2:28 pm   Post subject: RE:Making a traffic simulator

Although your free to do that, it's probably much better if you at least try arrays first. Not just because it's good for your program, it's a very helpful thing to learn for programming. I'd recommend at least looking into it, it'll put you a step ahead of your class.
dudeofgods




PostPosted: Mon May 23, 2011 6:11 pm   Post subject: Re: RE:Making a traffic simulator

Raknarg @ Sun May 22, 2011 2:28 pm wrote:
Although your free to do that, it's probably much better if you at least try arrays first. Not just because it's good for your program, it's a very helpful thing to learn for programming. I'd recommend at least looking into it, it'll put you a step ahead of your class.


Ya!! I tried it with arrays today, and it made the program so much simpler and less complex than it was before.

thanks,
dudeofgods
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: