Computer Science Canada

Clickable buttons

Author:  sammi [ Sun Oct 16, 2011 1:59 pm ]
Post subject:  Clickable buttons

What is it you are trying to achieve?
<Im trying to make my yes/no buttons clickable>


What is the problem you are having?
<they wont click>


Describe what you have tried to solve this problem
< I used what it said in the Turing Walkthrough for the GUI and nothings happening. >


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


<import GUI

%%% WHAT I WANT THE BUTTON TO DO WHEN I CLICK IT%%%
procedure NextQuestionMenu
locate (1,1)
put "Ask the magic 8 ball another question"
end NextQuestionMenu

procedure ExitGame
locate (1,1)
put " Awww you can't tell me your done already....." 
end ExitGame


%% DECLARING MY VARIABLES %%
var Question : string %the typing cursor
var Answer : int  % part of the array
var valid : boolean %number check
var words : array 1 .. 11 of string %the answers
%var Mouse.Where(350, 50, YesButton)

%% PARTS OF THE ARRAY %%
words (1) := "Yes"
words (2) := "No"
words (3) := "Maybe"
words (4) := "Thats for me to know and you to find out :)"
words (5) := "Of course"
words (6) := "I doubt it"
words (7) := "In your dreams"
words (8) := "Keep dreaming"
words (9) := "How the heck am i suppossed to know?"
words (10) := "Obviously"
words (11) := "Absolutely"

%% PROGRAM%%%%%
loop
valid := true % sets questions to valid until it hits check

% Asking the question
locatexy (200, 390)
put "Ask the Magic 8-Ball a question"

% drawing the ball
Draw.FillOval (330, 250, 100, 100, brightred)

% Getting a response
locatexy (250, 250)
get Question : *

% Make sure the question contains no numbers
for i : 1 .. length (Question) % Represents the Character of the string
    for b : 1 .. 9 % checks to see if the character, i, has a number.
        if Question (i) = intstr(b) then % If it does, it sets valid to false.
            valid := false % if it finds a number then it recognizes an improper question
            cls()
            exit
        end if
    end for
end for
 
locatexy (200, 100) % position of answer and "invalid" comment

if valid then
   Answer := Rand.Int (1, 11)
   put words (Answer)
   delay (2000)% lets person read answer
   cls()%gets rid of the answer
   locatexy (200, 80)
   put "Would you like to ask another question?"
   var YesButton : int:= GUI.CreateButton (250, 50, 0, "Yes", NextQuestionMenu)
   var NoButton : int:= GUI.CreateButton (350, 50, 0, "No", ExitGame)
   
   
else
    locatexy (200, 220)
   put "Thats not a question silly!!!"
   delay (1500)
   cls()
end if



end loop

loop
exit when GUI.ProcessEvent
end loop
>



Please specify what version of Turing you are using
<4.1.1>

Author:  Aange10 [ Sun Oct 16, 2011 2:15 pm ]
Post subject:  Re: Clickable buttons

Hmm, well

sammi @ 16/10/2011, 12:59 pm wrote:

Turing:

end loop

loop
exit when GUI.ProcessEvent
end loop




you're exit when GUI.ProcessEvent statement isn't being read, because it's not in the Main loop. You program is never getting to it.

Author:  sammi [ Sun Oct 16, 2011 2:25 pm ]
Post subject:  RE:Clickable buttons

ohh ok thanks

Author:  sammi [ Sun Oct 16, 2011 2:26 pm ]
Post subject:  RE:Clickable buttons

but they still aren't clickable.......

Author:  Aange10 [ Sun Oct 16, 2011 2:30 pm ]
Post subject:  RE:Clickable buttons

You should check your other post, http://compsci.ca/v3/viewtopic.php?p=244915#244915 ... You pretty much asked the same question (Why aren't your buttons working)
..
try
Turing:

loop
       var YesButton : int:= GUI.CreateButton (250, 50, 0, "Yes", NextQuestionMenu)
   var NoButton : int:= GUI.CreateButton (350, 50, 0, "No", ExitGame)
    exit when GUI.ProcessEvent
end loop

Author:  sammi [ Sun Oct 16, 2011 2:35 pm ]
Post subject:  RE:Clickable buttons

if i put the var YesButton things anywhere but where they are now i get an error message saying "procedure's may only be declared at the program, module or moniter level"

i have no idea what that means

Author:  Aange10 [ Sun Oct 16, 2011 2:44 pm ]
Post subject:  Re: Clickable buttons

If you would've followed the help on the other post, your code would look like this.

Turing:

import GUI

%%% WHAT I WANT THE BUTTON TO DO WHEN I CLICK IT%%%
procedure NextQuestionMenu
    put "Ask the magic 8 ball another question"
end NextQuestionMenu

procedure ExitGame
    put " Awww you can't tell me your done already....."
end ExitGame

%% DECLARING MY VARIABLES %%
var Question : string %the typing cursor
var valid : boolean := true %number check
var words : array 1 .. 11 of string %the answers
%var Mouse.Where(350, 50, YesButton)

%% PARTS OF THE ARRAY %%
words (1) := "Yes"
words (2) := "No"
words (3) := "Maybe"
words (4) := "Thats for me to know and you to find out Smile"
words (5) := "Of course"
words (6) := "I doubt it"
words (7) := "In your dreams"
words (8) := "Keep dreaming"
words (9) := "How the heck am i suppossed to know?"
words (10) := "Obviously"
words (11) := "Absolutely"

%% PROGRAM%%%%%
loop
    valid := true % Reset the question value
    % Asking the question
    locatexy (200, 390)
    put "Ask the Magic 8-Ball a question"
    % drawing the ball
    Draw.FillOval (330, 250, 100, 100, brightred)
    % Getting a response
    locatexy (250, 250)
    get Question : *
    % Make sure the question contains no numbers
    for i : 1 .. length (Question) % Represents the Character of the string
        for b : 1 .. 9 % checks to see if the character, i, has a number.
            if Question (i) = intstr (b) then % If it does, it sets valid to false.
                valid := false % if it finds a number then it recognizes an improper question
                cls ()
                exit
            end if
        end for
    end for
    locatexy (200, 100) % position of answer and "invalid" comment
    if valid then
        put words (Rand.Int(1,11)) % CHANGEd
    else
        locatexy (200, 220)
        put "Thats not a question silly!!!"
        delay (1500)
        cls ()
    end if
    locatexy (200, 20)
    put "Would you like to ask another question?"
    loop
        var YesButton : int := GUI.CreateButton (250, 50, 0, "Yes", NextQuestionMenu)
        var NoButton : int := GUI.CreateButton (350, 50, 0, "No", ExitGame)
        exit when GUI.ProcessEvent
    end loop
end loop


Now, you're program does exactly what you told it to.

Author:  sammi [ Sun Oct 16, 2011 2:48 pm ]
Post subject:  RE:Clickable buttons

well that one lets me click it but it goes haywire when i do

Author:  Aange10 [ Sun Oct 16, 2011 3:05 pm ]
Post subject:  Re: RE:Clickable buttons

sammi @ 16/10/2011, 1:48 pm wrote:
well that one lets me click it but it goes haywire when i do


Think about what your telling it to do. All your telling it to do is put a statement. You're not telling it to leave the loop, or to re do the program. Try something like this:

Turing:

import GUI

%% DECLARING MY VARIABLES %%
var Question : string %the typing cursor
var valid : boolean := true %number check
var picked_button_1, picked_button_2 : boolean := false
var words : array 1 .. 11 of string %the answers
%var Mouse.Where(350, 50, YesButton)

%% PARTS OF THE ARRAY %%
words (1) := "Yes"
words (2) := "No"
words (3) := "Maybe"
words (4) := "Thats for me to know and you to find out Smile"
words (5) := "Of course"
words (6) := "I doubt it"
words (7) := "In your dreams"
words (8) := "Keep dreaming"
words (9) := "How the heck am i suppossed to know?"
words (10) := "Obviously"
words (11) := "Absolutely"
%%% WHAT I WANT THE BUTTON TO DO WHEN I CLICK IT%%%
procedure NextQuestionMenu
    picked_button_1 := true
end NextQuestionMenu

procedure ExitGame
    picked_button_2 := true
end ExitGame


%% PROGRAM%%%%%
loop
    cls
    if picked_button_1 = true then
       put "Ask the magic 8 ball another question"
    elsif picked_button_2 = true then
       put " Awww you can't tell me your done already....."
    end if
    picked_button_2 := false % Reset the buttons
    picked_button_1 := false % Reset the buttons
    valid := true % Reset the question value
    % Asking the question
    locatexy (200, 390)
    put "Ask the Magic 8-Ball a question"
    % drawing the ball
    Draw.FillOval (330, 250, 100, 100, brightred)
    % Getting a response
    locatexy (250, 250)
    get Question : *
    % Make sure the question contains no numbers
    for i : 1 .. length (Question) % Represents the Character of the string
        for b : 1 .. 9 % checks to see if the character, i, has a number.
            if Question (i) = intstr (b) then % If it does, it sets valid to false.
                valid := false % if it finds a number then it recognizes an improper question
                cls ()
                exit
            end if
        end for
    end for
    locatexy (200, 100) % position of answer and "invalid" comment
    if valid then
        put words (Rand.Int(1,11)) % CHANGEd
    else
        locatexy (200, 220)
        put "Thats not a question silly!!!"
        delay (1500)
        cls ()
    end if
    locatexy (200, 20)
    put "Would you like to ask another question?"
    loop
        var YesButton : int := GUI.CreateButton (250, 50, 0, "Yes", NextQuestionMenu)
        var NoButton : int := GUI.CreateButton (350, 50, 0, "No", ExitGame)
        exit when GUI.ProcessEvent
        if picked_button_1 = true or picked_button_2 = true then
            exit
        end if
    end loop
end loop


Now, with that we added a variable to tell us if we hit the button. If we hit the button then we leave the loop. When we leave the loop the program finishes the Main Loop and repeats it. So at the top, when it repeats, we have it do what we want the buttons to do, then after it does what we wanted it to do we reset the buttons.

Now, think of what you're telling the buttons to do, and what you actually want them to do. See if you can figure out how to make it all work properly.

EDIT: Also, you need to define your procedures AFTER you've made your global variables, not before.

Author:  sammi [ Sun Oct 16, 2011 3:25 pm ]
Post subject:  RE:Clickable buttons

oh ok i'll give that a try and see if i can figure it out

Author:  sammi [ Sun Oct 16, 2011 3:31 pm ]
Post subject:  RE:Clickable buttons

i figured out another way to do it but it only works for one button....when i try to do it with the other button it gives me an error message...

import GUI

%%% WHAT I WANT THE BUTTON TO DO WHEN I CLICK IT%%%
%procedure TryAgainButton
%put "Ask the magic 8 ball another question"
%end TryAgainButton



%% DECLARING MY VARIABLES %%
var Question : string %the typing cursor
var Answer : int % part of the array
var valid : boolean %number check
var words : array 1 .. 11 of string %the answers
%var Mouse.Where(350, 50, YesButton)

%% PARTS OF THE ARRAY %%
words (1) := "Yes"
words (2) := "No"
words (3) := "Maybe"
words (4) := "Thats for me to know and you to find out Smile"
words (5) := "Of course"
words (6) := "I doubt it"
words (7) := "In your dreams"
words (8) := "Keep dreaming"
words (9) := "How the heck am i suppossed to know?"
words (10) := "Obviously"
words (11) := "Absolutely"


%%% Magic 8 Ball PROGRAM %%%
procedure MagicBall
cls() %refresh screen

procedure ExitButton
cls()

loop

valid := true % sets questions to valid until it hits check

% Asking the question
locatexy (200, 390)
put "Ask the Magic 8-Ball a question"

% drawing the ball
Draw.FillOval (330, 250, 100, 100, brightred)

% Getting a response
locatexy (250, 250)
get Question : *

% Make sure the question contains no numbers
for i : 1 .. length (Question) % Represents the Character of the string
for b : 1 .. 9 % checks to see if the character, i, has a number.
if Question (i) = intstr(b) then % If it does, it sets valid to false.
valid := false % if it finds a number then it recognizes an improper question
cls()
exit
end if
end for
end for

locatexy (200, 100) % position of answer and "invalid" comment

if valid then
Answer := Rand.Int (1, 11)
put words (Answer)
delay (2000)% lets person read answer
cls()%gets rid of the answer
locatexy (200, 80)
put "Would you like to ask another question?"

else
locatexy (200, 220)
put "Thats not a question silly!!!"
delay (1500)
cls ()
end if
loop
var YesButton : int := GUI.CreateButton (250, 50, 0, "Yes", MagicBall)
var NoButton : int := GUI.CreateButton (350, 50, 0, "No", ExitButton)
exit when GUI.ProcessEvent
end loop
end loop
end MagicBall
end ExitButton
MagicBall
ExitButton

Author:  Aange10 [ Sun Oct 16, 2011 3:57 pm ]
Post subject:  RE:Clickable buttons

Okay, your getting an error message because you can't define a procedure within a procedure.
...
If you wanted you could make the second button's procedure say

code:

proc ExitButton
quit
end ExitButton


Then the program would stop when you hit the button.
........
Though I'd like to point out that if you used the code above this one, that you could simply change

Turing:

    if picked_button_1 = true then
       put "Ask the magic 8 ball another question"
    elsif picked_button_2 = true then
       put " Awww you can't tell me your done already....."
    end if


to make it do whatever you want... for example..

Turing:

    if picked_button_1 = true then
      locatexy(maxx div 2, maxy div 2)
       put "Ask the magic 8 ball another question"
       delay(2000)
       cls
    elsif picked_button_2 = true then
       put " Awww you can't tell me your done already....."
      delay (2000)
      put "Alright, goodbye!"
      delay (1000)
      quit
    end if

Author:  sammi [ Sun Oct 16, 2011 4:01 pm ]
Post subject:  RE:Clickable buttons

sorry i was going to use your way but my dad said this way would be easier (he just told me hes good at computer programming as soon as i read your answer) so i'll try your way instead.

Author:  sammi [ Sun Oct 16, 2011 5:08 pm ]
Post subject:  RE:Clickable buttons

ok i ran into another problem.......when i click the no button, i want the message to pop up and then the buttons to disappear but i can't seem to make them disappear

import GUI

procedure ExitButton
cls()
put " Aww you can't tell me your done already....."
delay(2000)
put "ok fine......bye bye"
end ExitButton
cls()

%% DECLARING MY VARIABLES %%
var Question : string %the typing cursor
var Answer : int % part of the array
var valid : boolean %number check
var words : array 1 .. 11 of string %the answers
%var Mouse.Where(350, 50, YesButton)

%% PARTS OF THE ARRAY %%
words (1) := "Yes"
words (2) := "No"
words (3) := "Maybe"
words (4) := "Thats for me to know and you to find out Smile"
words (5) := "Of course"
words (6) := "I doubt it"
words (7) := "In your dreams"
words (8) := "Keep dreaming"
words (9) := "How the heck am i suppossed to know?"
words (10) := "Obviously"
words (11) := "Absolutely"

%%% MAGIC BALL PROGRAM %%%
procedure MagicBall
cls()%refresh screen
loop

valid := true % sets questions to valid until it hits check

% Asking the question
locatexy (200, 390)
put "Ask the Magic 8-Ball a question"

% drawing the ball
Draw.FillOval (330, 250, 100, 100, brightred)

% Getting a response
locatexy (250, 250)
get Question : *

% Make sure the question contains no numbers
for i : 1 .. length (Question) % Represents the Character of the string
for b : 1 .. 9 % checks to see if the character, i, has a number.
if Question (i) = intstr (b) then % If it does, it sets valid to false.
valid := false % if it finds a number then it recognizes an improper question
cls ()
exit
end if
end for
end for

locatexy (200, 100) % position of answer and "invalid" comment

if valid then
Answer := Rand.Int (1, 11)
put words (Answer)
delay (2000) % lets person read answer
cls () %gets rid of the answer
locatexy (200, 80)
put "Would you like to ask another question?"

else
locatexy (200, 220)
put "Thats not a question silly!!!"
delay (1500)
cls ()
end if

loop
var YesButton : int := GUI.CreateButton (250, 50, 0, "Yes", MagicBall)
var NoButton : int := GUI.CreateButton (350, 50, 0, "No", ExitButton)
exit when GUI.ProcessEvent
end loop

end loop
end MagicBall
MagicBall

Author:  Aange10 [ Sun Oct 16, 2011 5:17 pm ]
Post subject:  Re: Clickable buttons

If you watch carefully the buttons are going away properly.

Turing:

procedure ExitButton
cls()
put " Aww you can't tell me your done already....."
delay(2000)
put "ok fine......bye bye"
end ExitButton


This is what you're no button is doing. Nothing more, nothing less. When you hit the no button it clears your screen, it puts the message, it delays for 2 seconds, puts another message and thats it. It doesn't exit the loop (which is what is redrawing the buttons) it doesn't quit the game. The only thing it does is what is in the procedure's parameters.


......... Also, please put [ syntax="turing" ] before your code and [ /syntax ] after it. (With no spaces). It makes people want to help you more if they can read it

Author:  sammi [ Sun Oct 16, 2011 5:21 pm ]
Post subject:  RE:Clickable buttons

oh im sorry! i didnt even know i could do that!!!!!

Author:  sammi [ Sun Oct 16, 2011 7:38 pm ]
Post subject:  RE:Clickable buttons

me and my dad have tried everything and we still cant get the darn buttons to go away after i click 'no'. we think it has something to do with putting cls in somewhere but thats not working and we couldn't anything in the walkthrough can someone please help.


Turing:

import GUI
View.Set ("graphics, offscreen only")

procedure ExitButton
    cls ()
    put "Aww you can't tell me your done already....."
    delay (2000)
    put "ok fine......bye bye"
end ExitButton

%% DECLARING MY VARIABLES %%
var Question : string %the typing cursor
var Answer : int  % part of the array
var valid : boolean %number check
var words : array 1 .. 11 of string %the answers
%var Mouse.Where(350, 50, YesButton)

%% PARTS OF THE ARRAY %%
words (1) := "Yes"
words (2) := "No"
words (3) := "Maybe"
words (4) := "Thats for me to know and you to find out :)"
words (5) := "Of course"
words (6) := "I doubt it"
words (7) := "In your dreams"
words (8) := "Keep dreaming"
words (9) := "How the heck am I suppossed to know?"
words (10) := "Obviously"
words (11) := "Absolutely"

%%% MAGIC BALL PROGRAM %%%
procedure MagicBall
    cls () %refresh screen
    loop

        valid := true % sets questions to valid until it hits check

        % Asking the question
        locatexy (200, 390)
        put "Ask the Magic 8-Ball a question"

        % drawing the ball
        Draw.FillOval (330, 250, 100, 100, brightred)

        % Getting a response
        locatexy (250, 250)
        get Question : *

        % Make sure the question contains no numbers
        for i : 1 .. length (Question) % Represents the Character of the string
            for b : 1 .. 9 % checks to see if the character, i, has a number.
                if Question (i) = intstr (b) then % If it does, it sets valid to false.
                    valid := false % if it finds a number then it recognizes an improper question
                    cls ()
                    exit
                end if
            end for
        end for

        locatexy (200, 100) % position of answer and "invalid" comment

        if valid then
            Answer := Rand.Int (1, 11) % pick any one of my answers
            put words (Answer)
            delay (2000) % lets person read answer
            cls () %gets rid of the answer
            locatexy (200, 100)
            put "Would you like to ask another question?"
           
            loop
                var YesButton : int := GUI.CreateButton (250, 50, 0, "Yes", MagicBall)
                var NoButton : int := GUI.CreateButton (350, 50, 0, "No", ExitButton)
                exit when GUI.ProcessEvent
            end loop

        else
            locatexy (200, 220)
            put "Thats not a question silly!!!"
            delay (1500)
            cls ()
        end if

    end loop
end MagicBall
MagicBall

Author:  Aange10 [ Sun Oct 16, 2011 7:51 pm ]
Post subject:  Re: RE:Clickable buttons

sammi @ 16/10/2011, 6:38 pm wrote:
me and my dad have tried everything and we still cant get the darn buttons to go away after i click 'no'. we think it has something to do with putting cls in somewhere but thats not working and we couldn't anything in the walkthrough can someone please help.



Try this

Turing:

%Declare this variable at the top
var exit_counter : int := 0
procedure ExitButton
loop
    cls ()
    put "Aww you can't tell me your done already....."
    if exit_counter = 0 then
        delay (2000)
    end if
    put "ok fine......bye bye"
    delay (1000) % Prevents flickering
    exit_counter := 1
end loop
end ExitButton


That should fix your problem. It indefinitely loops the drawling, so that it doesn't continue on to redrawing the buttons. It only delays the first time it execute, aswell. (That away you can't tell it's looping)

also the quit command will stop the program, if you want to use that.
... Also, your global variables need to be defined BEFORE your procedures. (So move the extibutton procedure down, right above your magiball procedure)

EDIT: Also, using

Turing:

        Draw.FillOval (330, 250, 100, 100, black)
        Draw.FillOval (330, 250, 60, 60, white)
        Draw.FillOval (330, 275, 30, 30, brightred)
        Draw.FillOval (330, 220, 30, 30, brightred)
        Draw.FillOval (330, 275, 15, 15, white)
        Draw.FillOval (330, 220, 15, 15, white)


To draw your 8 ball would look much better Very Happy


: