Computer Science Canada 2 loops at once |
Author: | Jaguarxyz [ Wed Jan 06, 2010 10:28 am ] | ||
Post subject: | 2 loops at once | ||
What is it you are trying to achieve? Have a box move across the screen while traffic lights are operating. What is the problem you are having? Making both happen at once, instead of one after the other. Describe what you have tried to solve this problem Trying to put the box moving first, or all in 1 loop, but the box moves a little, lights do 1 rotation, box moves, repeat. Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Please specify what version of Turing you are using 4.1.1 |
Author: | jbking [ Wed Jan 06, 2010 11:19 am ] |
Post subject: | Re: 2 loops at once |
What you do know about threads and concurrent programming? What you want is really parallel programming, but I'd think concurrency may be close enough in terms of being a topic to research a bit to discover that this isn't that simple as you are shifting from sequential programming where things are done in a specific order to where the order can be jumbled because of multiple paths of execution being done at once. |
Author: | DemonWasp [ Wed Jan 06, 2010 11:37 am ] | ||
Post subject: | RE:2 loops at once | ||
jbking: No multithreading is required. What you need to do is similar to what's listed under your first attempt, but a little finer-grained. Did you know that movies, instead of displaying actual fluid motion, just show you a new image about 24 times a second? They do. What if we apply the same principle here? Well, if we keep re-drawing the scene fast enough, it'll look like it's really animated, even when we're changing multiple things at the same time. So the code will look something like this:
If you want to get more advanced, you may want to use the time required for each loop to more accurately represent the speeds of your objects. That's likely beyond the scope of the assignment though. |
Author: | jbking [ Wed Jan 06, 2010 11:50 am ] |
Post subject: | Re: 2 loops at once |
Perhaps I took at the "at once" part a little too literally. ![]() |
Author: | mirhagk [ Wed Jan 06, 2010 12:25 pm ] | ||
Post subject: | RE:2 loops at once | ||
for your problem you will likely want to store the "state" of the lights, as well as how long they've been like that. That way you can simply
|
Author: | Jaguarxyz [ Fri Jan 08, 2010 9:09 am ] |
Post subject: | Re: 2 loops at once |
Thanks for the help guys! I got the program to work but.. there's a lot of flickering ![]() I've tried View.Update, but it doesn't do anything, my screen just remains blank, I think I'm putting it in the wrong spot? Here's my current code ![]() Quote: View.Set("graphics,offscreenonly")
View.Update %Background drawfillbox (0, 0, 640, 400, darkgrey) drawfillbox (0, 225, 250, 470, green) %top right grass drawfillbox (0, 0, 250, 140, green) % bottom right grass drawfillbox (350, 225, 640, 400, green) %top left grass drawfillbox (350, 0, 640, 140, green) %bottom left grass drawfillbox (250, 267, 220, 225, black) %box on the left drawfillbox (350, 140, 380, 100, black) %box on the right drawfillbox (301, 250, 350, 225, black) %box on the Top drawfillbox (299, 113, 250, 140, black) %box on the bottom drawfillbox (300, 1, 300, 400, yellow) %yellowline up and down drawfillbox (640, 183, 0, 183, yellow) drawfillbox (200, 150,247, 180, red) %car drawfillbox (350, 190,397, 220, blue) %car drawfilloval (235, 260, 5, 5, brightred) %left red drawfilloval (365, 133, 5, 5, brightred) %right red drawfilloval (292, 127, 5, 5, brightgreen) %bottom green drawfilloval (343, 237, 5, 5, brightgreen) %top green drawfilloval (275, 127, 5, 5, black) %bottom yellowblack drawfilloval (327, 237, 5, 5, black) %top yellowblack %LIGHTS SWTICH: TOP & BOTTOM > LEFT & RIGHT drawfilloval (235, 245, 5, 5, black) %left yellowblack drawfilloval (365, 122, 5, 5, black) %right yellowblack drawfilloval (235, 260, 5, 5, brightred) %left red drawfilloval (365, 133, 5, 5, brightred) %right red drawfilloval (260, 127, 5, 5, black) %bottom redblack drawfilloval (310, 237, 5, 5, black) %top redblack delay (1500) Draw.Cls drawfillbox (0, 0, 640, 400, darkgrey) drawfillbox (0, 225, 250, 470, green) %top right grass drawfillbox (0, 0, 250, 140, green) % bottom right grass drawfillbox (350, 225, 640, 400, green) %top left grass drawfillbox (350, 0, 640, 140, green) %bottom left grass drawfillbox (250, 267, 220, 225, black) %box on the left drawfillbox (350, 140, 380, 100, black) %box on the right drawfillbox (301, 250, 350, 225, black) %box on the Top drawfillbox (299, 113, 250, 140, black) %box on the bottom drawfillbox (300, 1, 300, 400, yellow) %yellowline up and down drawfillbox (640, 183, 0, 183, yellow) drawfillbox (200, 150,247, 180, red) %CAR drawfillbox (350, 190,397, 220, blue) %car drawfilloval (235, 260, 5, 5, brightred) %left red drawfilloval (365, 133, 5, 5, brightred) %right red drawfilloval (275, 127, 5, 5, yellow) %bottom yellow drawfilloval (327, 237, 5, 5, yellow) %top yellow drawfilloval (292, 127, 5, 5, black) %bottom greenblack drawfilloval (343, 237, 5, 5, black) %top greenblack delay (1000) Draw.Cls %CAR STARTS MOVING var x:int x:=2 loop View.Update drawfillbox (0, 0, 640, 400, darkgrey) drawfillbox (0, 225, 250, 470, green) %top right grass drawfillbox (0, 0, 250, 140, green) % bottom right grass drawfillbox (350, 225, 640, 400, green) %top left grass drawfillbox (350, 0, 640, 140, green) %bottom left grass drawfillbox (250, 267, 220, 225, black) %box on the left drawfillbox (350, 140, 380, 100, black) %box on the right drawfillbox (301, 250, 350, 225, black) %box on the Top drawfillbox (299, 113, 250, 140, black) %box on the bottom drawfillbox (300, 1, 300, 400, yellow) %yellowline up and down drawfillbox (640, 183, 0, 183, yellow) drawfillbox (x + 200, 150, x + 247, 180, red) %CAR drawfillbox (x - 350, 190, x - 397, 220, blue) %car drawfilloval (235, 231, 5, 5, brightgreen) %left green drawfilloval (365, 107, 5, 5, brightgreen) %right green drawfilloval (260, 127, 5, 5, brightred) %bottom red drawfilloval (310, 237, 5, 5, brightred) %top red delay (15) Draw.Cls x:=x+2 end loop |
Author: | DemonWasp [ Fri Jan 08, 2010 10:38 am ] |
Post subject: | RE:2 loops at once |
You want your View.Update to come right after all of your drawing code, sometime before the delay and Draw.Cls, otherwise it will clear the screen and display a cleared screen to the user, then start drawing again. |