2 loops at once
Author |
Message |
Jaguarxyz
|
Posted: 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)
Turing: |
setscreen ("graphics")
%Background
var x : int
x := 1
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
loop
drawfillbox (x + 200, 190, x + 247, 220, red)
drawfillbox (x + 190, 190, x + 200, 220, darkgrey)
drawfillbox (300, 1, 300, 400, yellow) %yellowline up and down
drawfillbox (640, 183, 0, 183, yellow)
%Boxes
x := x + 1
delay (5)
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
delay (500)
drawfilloval (292, 127, 5, 5, black) %bottom greenblack
drawfilloval (343, 237, 5, 5, black) %top greenblack
delay (500)
drawfilloval (275, 127, 5, 5, yellow) %bottom yellow
drawfilloval (327, 237, 5, 5, yellow) %top yellow
delay (500)
drawfilloval (275, 127, 5, 5, black) %bottom yellowblack
drawfilloval (327, 237, 5, 5, black) %top yellowblack
delay (500)
drawfilloval (260, 127, 5, 5, brightred) %bottom red
drawfilloval (310, 237, 5, 5, brightred) %top red
delay (500)
drawfilloval (235, 260, 5, 5, black) %left redblack
drawfilloval (365, 133, 5, 5, black) %right redblack
%LIGHTS SWTICH: TOP & BOTTOM > LEFT & RIGHT
drawfilloval (235, 231, 5, 5, brightgreen) %left green
drawfilloval (365, 107, 5, 5, brightgreen) %right green
delay (500)
drawfilloval (235, 231, 5, 5, black) %left greenblack
drawfilloval (365, 107, 5, 5, black) %right greenblack
delay (500)
drawfilloval (235, 245, 5, 5, yellow) %left yellow
drawfilloval (365, 122, 5, 5, yellow) %right yellow
delay (500)
drawfilloval (235, 245, 5, 5, black) %left yellowblack
drawfilloval (365, 122, 5, 5, black) %right yellowblack
delay (500)
drawfilloval (235, 260, 5, 5, brightred) %left red
drawfilloval (365, 133, 5, 5, brightred) %right red
delay (500)
drawfilloval (260, 127, 5, 5, black) %bottom redblack
drawfilloval (310, 237, 5, 5, black) %top redblack
end loop
|
Please specify what version of Turing you are using
4.1.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
jbking
|
Posted: 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. |
|
|
|
|
|
DemonWasp
|
Posted: 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:
Turing: |
View.Set ( "offscreenonly" ) % Combined with View.Update later to minimize flicker. You should read about this in the Turing Help.
loop
% Update world objects here.
% If the light has been green for a while, make it yellow; if it's been yellow for a while, make it red; if it's been red for a while, make it green.
% Move the box's coordinates a small amount.
% Re-draw world objects here.
Draw.Cls() % Clear the screen prior to draw...
% Draw the box (probably Draw.FillBox () )
% Draw the light(s)
View.Update() % Push this drawing to the screen to be visible.
Time.DelaySinceLast ( 10 ) % Make sure we loop no more than 100 times per second (100 frames per second, as each "frame" is one drawing).
end loop
|
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. |
|
|
|
|
|
jbking
|
Posted: 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. |
|
|
|
|
|
mirhagk
|
Posted: 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
Turing: |
loop
%increase the time for each light
%check to see if it needs to be changed
%change if neccasary
%move the car
Time.DelaySinceLast(10) %delay max speed 100 fps
end loop
|
|
|
|
|
|
|
Jaguarxyz
|
Posted: 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
|
|
|
|
|
|
DemonWasp
|
Posted: 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. |
|
|
|
|
|
|
|