How to end a procedure with a button?
Author |
Message |
Injust
|
Posted: Mon Jun 01, 2015 7:16 pm Post subject: How to end a procedure with a button? |
|
|
What is it you are trying to achieve?
I'm trying to make a shutdown button remove everything on the screen and display a message.
What is the problem you are having?
If I'm running a loop in a procedure, it displays stuff on the screen.
Describe what you have tried to solve this problem
I've tried to use a boolean and an integer to detect when the shutdown button is pressed and to stop the procedure, but I can't get it to work.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
import GUI %Imports GUI files
var win := Window.Open ("nobuttonbar") %Opens run window without button bar
var counter : int %Declares counter
drawfillbox (0, 100, 100, 400, black) %Draws black background for East/West (EW) traffic light
drawfillbox (150, 100, 250, 400, black) %Draws black background for North/South (NS) traffic light
drawfillbox (300, 100, 400, 400, black) %Draws black background for left turn (LT) signal light
proc delay1000 %Waits for 1 second
counter := 1
loop
counter := counter + 1
exit when GUI.ProcessEvent
delay (50)
exit when (counter > 20)
end loop
end delay1000
proc delay4000 %Waits for 4 seconds
counter := 1
loop
counter := counter + 1
exit when GUI.ProcessEvent
delay (50)
exit when (counter > 80)
end loop
end delay4000
proc delay12000 %Waits for 12 seconds
counter := 1
loop
counter := counter + 1
exit when GUI.ProcessEvent
delay (50)
exit when (counter > 240)
end loop
end delay12000
proc EWred %Turns EW red
drawfilloval (50, 350, 40, 40, 12)
drawfilloval (50, 250, 40, 40, 116)
drawfilloval (50, 150, 40, 40, 121)
end EWred
proc EWyellow %Turns EW yellow
drawfilloval (50, 350, 40, 40, 112)
drawfilloval (50, 250, 40, 40, 14)
drawfilloval (50, 150, 40, 40, 121)
end EWyellow
proc EWgreen %Turns EW green
drawfilloval (50, 350, 40, 40, 112)
drawfilloval (50, 250, 40, 40, 116)
drawfilloval (50, 150, 40, 40, 10)
end EWgreen
proc NSred %Turns NS red
drawfilloval (200, 350, 40, 40, 12)
drawfilloval (200, 250, 40, 40, 116)
drawfilloval (200, 150, 40, 40, 121)
end NSred
proc NSyellow %Turns NS yellow
drawfilloval (200, 350, 40, 40, 112)
drawfilloval (200, 250, 40, 40, 14)
drawfilloval (200, 150, 40, 40, 121)
end NSyellow
proc NSgreen %Turns NS green
drawfilloval (200, 350, 40, 40, 112)
drawfilloval (200, 250, 40, 40, 116)
drawfilloval (200, 150, 40, 40, 10)
end NSgreen
proc LTred %Turns LT red
drawfilloval (350, 350, 40, 40, 12)
drawfilloval (350, 250, 40, 40, 116)
drawfilloval (350, 150, 40, 40, 121)
end LTred
proc LTyellow %Turns LT yellow
drawfilloval (350, 350, 40, 40, 112)
drawfilloval (350, 250, 40, 40, 14)
drawfilloval (350, 150, 40, 40, 121)
end LTyellow
proc LTgreen %Turns LT green
drawfilloval (350, 350, 40, 40, 112)
drawfilloval (350, 250, 40, 40, 116)
drawfilloval (350, 150, 40, 40, 121)
drawfillbox (380, 145, 330, 155, 10)
var x : array 1 .. 3 of int := init (350, 350, 320)
var y : array 1 .. 3 of int := init (170, 130, 150)
drawfillpolygon (x, y, 3, 10)
end LTgreen
proc EWon %Turns EW all on
drawfilloval (50, 350, 40, 40, 12)
drawfilloval (50, 250, 40, 40, 14)
drawfilloval (50, 150, 40, 40, 10)
end EWon
proc NSon %Turns NS all on
drawfilloval (200, 350, 40, 40, 12)
drawfilloval (200, 250, 40, 40, 14)
drawfilloval (200, 150, 40, 40, 10)
end NSon
proc LTon %Turns LT all on
drawfilloval (350, 350, 40, 40, 12)
drawfilloval (350, 250, 40, 40, 14)
drawfilloval (350, 150, 40, 40, 121)
drawfillbox (380, 145, 330, 155, 10)
var x : array 1 .. 3 of int := init (350, 350, 320)
var y : array 1 .. 3 of int := init (170, 130, 150)
drawfillpolygon (x, y, 3, 10)
end LTon
proc EWoff %Turns EW all off
drawfilloval (50, 350, 40, 40, 112)
drawfilloval (50, 250, 40, 40, 116)
drawfilloval (50, 150, 40, 40, 121)
end EWoff
proc NSoff %Turns NS all off
drawfilloval (200, 350, 40, 40, 112)
drawfilloval (200, 250, 40, 40, 116)
drawfilloval (200, 150, 40, 40, 121)
end NSoff
proc LToff %Turns LT all off
drawfilloval (350, 350, 40, 40, 112)
drawfilloval (350, 250, 40, 40, 116)
drawfilloval (350, 150, 40, 40, 121)
end LToff
proc AllRed %Turns EW, NS, and LT all red
EWred
NSred
LTred
end AllRed
proc AllOn %Turns EW, NS, and LT all on
EWon
NSon
LTon
end AllOn
proc AllOff %Turns EW, NS, and LT all off
EWoff
NSoff
LToff
end AllOff
proc FlashRed %Flashes EW and NS red
loop
NSred
EWred
delay1000
EWoff
NSoff
delay1000
end loop
end FlashRed
proc FlashYellow %Flashes EW and NS yellow
loop
NSyellow
EWyellow
delay1000
EWoff
NSoff
delay1000
end loop
end FlashYellow
proc FlashGreen %Flashes EW and NS green
loop
NSgreen
EWgreen
delay1000
EWoff
NSoff
delay1000
end loop
end FlashGreen
proc FullAuto %Automatic mode
loop
NSred
EWred
delay4000
EWgreen
delay12000
EWyellow
delay4000
EWred
delay4000
NSgreen
delay12000
NSyellow
delay4000
NSred
EWred
end loop
end FullAuto
proc Shutdown %Shuts down the program
drawfillbox (0, 0, 700, 400, white)
var goodbyemsg : int := GUI.CreateLabelFull (320, 200, "Goodbye!", 0, 0, GUI.CENTER + GUI.MIDDLE, 0)
GUI.Quit
end Shutdown
AllRed %Draws EW, NS, and LT all off
var EW := GUI.CreateLabelFull (25, 85, " East/West", 0, 0, GUI.LEFT, 0) %EW label
var NS := GUI.CreateLabelFull (170, 85, "North/South", 0, 0, GUI.LEFT, 0) %NS label
var LT := GUI.CreateLabelFull (325, 85, "Left Turn", 0, 0, GUI.LEFT, 0) %LT label
var FlashingMode := GUI.CreateLabelFull (540, 390, "Flashing Mode", 0, 0, GUI.CENTER + GUI.MIDDLE, 0) %Flashing Mode label
var AutomaticMode := GUI.CreateLabelFull (540, 260, "Automatic Mode", 0, 0, GUI.CENTER + GUI.MIDDLE, 0) %Automatic Mode label
var EWredButton : int := GUI.CreateButtonFull (20, 60, 0, " Red ", EWred, 0, '^D', true) %EW red button
var EWyellowButton : int := GUI.CreateButtonFull (20, 31, 0, "Yellow", EWyellow, 0, '^D', true) %EW yellow button
var EWgreenButton : int := GUI.CreateButtonFull (20, 2, 5, " Green", EWgreen, 0, '^D', true) %EW green button
var NSredButton : int := GUI.CreateButtonFull (170, 60, 0, " Red ", NSred, 0, '^D', true) %NS red button
var NSyellowButton : int := GUI.CreateButtonFull (170, 31, 0, "Yellow", NSyellow, 0, '^D', true) %NS yellow button
var NSgreenButton : int := GUI.CreateButtonFull (170, 2, 5, " Green", NSgreen, 0, '^D', true) %NS green button
var LTredButton : int := GUI.CreateButtonFull (320, 60, 0, " Red ", LTred, 0, '^D', true) %LT red button
var LTyellowButton : int := GUI.CreateButtonFull (320, 31, 0, "Yellow", LTyellow, 0, '^D', true) %LT yellow button
var LTgreenButton : int := GUI.CreateButtonFull (320, 2, 5, " Green", LTgreen, 0, '^D', true) %LT green button
var FlashRedButton : int := GUI.CreateButtonFull (485, 350, 5, " Flash Red ", FlashRed, 0, '^D', true) %Flash Red button
var FlashYellowButton : int := GUI.CreateButtonFull (485, 320, 5, "Flash Yellow ", FlashYellow, 0, '^D', true) %Flash Yellow button
var FlashGreenButton : int := GUI.CreateButtonFull (485, 290, 5, " Flash Green ", FlashGreen, 0, '^D', true) %Flash Green button
var AutomaticModeStartButton : int := GUI.CreateButtonFull (465, 220, 5, "Start Automatic Mode", FullAuto, 0, '^D', true) %Start Automatic Mode button
var AutomaticModeStopButton : int := GUI.CreateButtonFull (465, 190, 5, " Stop Automatic Mode", AllRed, 0, '^D', true) %Stop Automatic Mode button
var AllOnButton : int := GUI.CreateButton (550, 95, 0, " All On ", AllOn ) %All On button
var AllOffButton : int := GUI.CreateButton (550, 65, 0, " All Off ", AllOff ) %All Off button
var ResetButton : int := GUI.CreateButton (550, 35, 0, " Reset ", AllRed ) %Reset button
var ShutdownButton : int := GUI.CreateButton (550, 5, 0, "Shutdown", Shutdown ) %Shutdown button
loop %Allows user input
exit when GUI.ProcessEvent
end loop
|
Please specify what version of Turing you are using
Version 4.1.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Insectoid
|
Posted: Mon Jun 01, 2015 8:42 pm Post subject: RE:How to end a procedure with a button? |
|
|
The easiest way to do this, and most valuable method to learn, is to take all the loops out of your procedures, and instead have just one big loop. Design your procedures to draw just one frame when you call them. The next time you call them, they draw the next frame. Now instead of trying to terminate procedures prematurely to stop drawing something, you can just stop calling the procedure.
Also, you should look into procedure parameters. You have several nearly identical procedures, where the only difference is a single number. A parameter lets you give the procedure a number when you call it (just like delay(x) takes x as input and delays for x seconds).
A procedure with parameters look like the following:
code: | procedure putNumber (num : int)
put num
end putNumber |
'num' does not need to be declared anywhere else in your program. It also does not exist anywhere else in your program. It's a little mini variable that only exists in your procedure. Applying this to your code will reduce all your nearly-identical procedures to a single procedure that you can use everywhere. |
|
|
|
|
|
|
|