Computer Science Canada

Button doesn't work

Author:  asdfasdf123 [ Thu Jan 08, 2015 1:43 pm ]
Post subject:  Button doesn't work

What is it you are trying to achieve?
I'm trying to make an exit button for a game I'm making.


What is the problem you are having?
When I click the button, nothing happens.


Describe what you have tried to solve this problem
I've tried using a picture of an exit button and the buttonwait command instead of a GUI button, but when I ran the program with that, it showed a blank screen with just the button, and when I clicked the button, the rest of the program started working, and the button didn't work anymore.


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

Turing:

import GUI

% Screen setting
setscreen ("graphics:1280;905;nobuttonbar")

% Variables/constants
% Variable types
var mainMenuFont, game1Font, game2Font, exitFont, x, y, notused1, notused2, font5, pic1, pic2, pic3, pic4, scoreFont, smallFont, scoreFont2, playernum : int
var mainMenu, game1, game2, Exit, scores, back, instructions, name, slotScoreDisplay1, slotScoreDisplay2, raceScoreDisplay1, raceScoreDisplay2 : string
var slotScore1, slotScore2, raceScore1, raceScore2 : real := 0
% Variable values
% Fonts
mainMenuFont := Font.New ("Arial:90:Bold")
game1Font := Font.New ("Calibri:80:Italic")
game2Font := Font.New ("Calibri:80:Bold")
exitFont := Font.New ("Arial:40:Bold")
smallFont := Font.New ("Calibri:12:Italic")
scoreFont2 := Font.New ("Arial:20:Bold")
scoreFont := Font.New ("Arial:50:Bold")
font5 := Font.New ("Arial:90:Bold")
% Words/sentences
mainMenu := "MAIN MENU"
game1 := "RACING"
game2 := "SLOTS"
Exit := "EXIT"
scores := "SCORES"
instructions := "How to play"
back := "Press any key to go back to the main menu."
% Pictures
pic2 := Pic.FileNew ("slotmachine.bmp")
pic3 := Pic.FileNew ("bag.bmp")
pic4 := Pic.FileNew ("cherry2.bmp")

% Processes/procedures
process playfile % Sound of a slot machine spinning
    Music.PlayFile ("SlotMachineWithPayout.wav")
end playfile
procedure quitGame % Exits the game
    GUI.Quit
end quitGame

% Button
var b : int := GUI.CreateButton (maxx - 100, 0, 0, "Quit Game", quitGame)



Please specify what version of Turing you are using
4.1 1.0.1

Author:  Insectoid [ Thu Jan 08, 2015 6:58 pm ]
Post subject:  RE:Button doesn\'t work

The problem here is that your program quits immediately after creating the button, not because of any error in your code, but because there is no code to run after you create the button. It's the last thing your program does. The button doesn't work because your program isn't even running anymore. Read up on a GUI tutorial or the Turing documentation to learn more about the built-in GUI.

As for your homemade (non-GUI) button, it's probably working fine, but you've put it in the wrong loop.

Author:  asdfasdf123 [ Fri Jan 09, 2015 12:13 pm ]
Post subject:  RE:Button doesn\'t work

I fixed the problem, but the button doesn't work very well with any other user input. For example, when I have a get statement that comes after the exit button in the code, I need to click the exit button first, then type in the input for the get statement, in order for the button to work. If the get statement comes before the button, I need to finish typing the input first, before the button even appears, then it's the same situation as I mentioned above.
Turing:

import GUI
setscreen ("graphics:max;max")
procedure leave
    GUI.Quit
end leave
process exitbutton
    var b : int := GUI.CreateButton (maxx - 100, 0, 0, "Quit", leave)
end exitbutton
var asdf : string
loop
    fork exitbutton
    exit when GUI.ProcessEvent
    get asdf
    delay (100)
end loop

Author:  Insectoid [ Fri Jan 09, 2015 12:24 pm ]
Post subject:  RE:Button doesn\'t work

There's a lot of reasons that could happen. Without seeing your code I have no idea you did wrong.

Author:  asdfasdf123 [ Fri Jan 09, 2015 12:28 pm ]
Post subject:  RE:Button doesn\'t work

Edited with code ^

Author:  Insectoid [ Fri Jan 09, 2015 1:15 pm ]
Post subject:  RE:Button doesn\'t work

'get' is a blocking instruction. That means that while the program is waiting for input, nothing else happens. If you click the button, it does 'work', but because you're waiting for the get to finish, it won't run the 'exit when' line until you hit enter. You need to either design a non-blocking input method using getch or Input.KeyDown or use GUI text boxes. It's' generally a bad idea to mix the built-in GUI with the text-based get.

There's a few other things wrong with this. GUI.Quit is a procedure, which means you don't need the procedure 'leave'. You can put GUI.Quit directly into the button declaration.

You've got your button declaration inside a process. That's a bad idea because as soon as the process finishes, everything inside it gets tossed out. The button still exists however because the GUI is magic (as far as you need to know for now). This causes all kinds of problems, especially because you've got it in a loop. You're actually creating lots and lots of buttons (if you take out the get and the delay, the program will eventually crash due to making too many buttons).

Oh, and the delay. Get rid of it. There's no reason for it to be there. Absolutely zero.

Author:  asdfasdf123 [ Tue Jan 13, 2015 11:37 am ]
Post subject:  Re: Button doesn't work

I made a bunch of changes, and I still got the exact same result: I press the button, I enter input, and it finishes afterwards. Here's the code:
Turing:

import GUI
setscreen ("graphics:max;max")
var c : string (1)
var b : int := GUI.CreateButton (maxx - 100, 0, 0, "Quit", GUI.Quit)
loop
    exit when GUI.ProcessEvent
    getch (c)
    put (c)
end loop
[/syntax]


: