Previous |
| Next |
One thing a game (or any other computer program) has to be able to do is to tell a human what's going on. If you don't know what the computer is doing (or when it's done), it might as well be turned off.
There are lots of ways to get information out of the computer. You can make the computer draw a picture, make it speak words, print something on a printer, or write data to a file on the disk or CD-ROM.
If you have a bunch of words to tell the user, one of the easiest
Tcl/Tk commands is the tk_messageBox
command.
This command will create a new window on the monitor that looks like this:
The command that would create that window is:
tk_messageBox -type ok -message "You Win!"
There you have it. A simple game that you can't ever lose. It's not much, but it's a start, and we can build a better game from this.
We'll look at the details of the tk_messageBox
command
later in this lesson. For now, lets just deal with the details of
making it work.
The first part of learning any computer language is just making sure that you can do something simple. This proves that you've got all the programs you need installed in the right places and you know the names to use. It's basic, but every computer programmer starts with something simple when they learn a new language.
To create this first program you need to
wish
interpreter to run your new program
On Windows, you start Komodo Edit by clicking the Komodo icon that was put on your desktop when you installed Komodo Edit. Or you can go to the Start menu and run it from there.
On a Linux system, you can click an icon, or open a command (or
xterm) window and type komodo
.
The first time you start Komodo Edit, it will look like this:
After a few minutes, it will finish loading the initialization stuff (it only does this the first time you start), and you'll see this screen:
Select the File menu and then select New File, like this:
Komodo Edit helps you write lots of different kinds of programs. Since we're using Tcl/Tk, select the Tk Template. After you click on the Tk line, click the Open button.
After you click Open, you'll see a window like the one just below. Komodo Edit puts some code at the beginning of the window for you. We can ignore that for now. These lines tell the computer some stuff we always want to do in every piece of Tcl/Tk code.
When you type in commands, Komodo Edit will help you by trying to guess which command you want. If you keep typing, it will suggest a smaller and smaller list of commands you might mean. You can click on the command you want from the list, or keep typing until there is just one possible command. If there's just one command left, you can type the Tab key to have Komodo Edit fill in the rest of the command for you.
Komodo Edit will also add the second quote for you, so you don't need to remember to close all your quotation pairs.
Now, type in the tk_messageBox command. That will look like this:
Next, we need to save the program onto the disk. We do this so that
another program (like the wish
interpreter) can read the
program off the disk, convert the Tcl/Tk commands to on/off values and
then tell the CPU to start running the program.
Select the File menu, and then click the Save As line near the middle. That will open a window something like this:
You can put the file in any folder you want to - but be sure to
remember what folder that is. You can name it anything you want with
one rule - the last 3 letters (after a period) have to be
tcl
. Those letters tell us that this is a Tcl/Tk program,
and let Windows know what to use to run the program when you double click
on it.
If you are running Windows, it might be simplest to put the files in a folder on the C: drive. You'll need to use the My Computer screen to set up the new folder. It's easier to run programs from within Komodo if they are in a folder directly under C: instead of in My Documents or such.
Finally, let's run this program. On a MS Windows computer, you can run this program by browsing to the folder where you stored it and double clicking the file.
Or, we can run the program inside the Komodo Editor. This takes a couple more steps, but it's a better way to run programs while you are working on them.
To run a Tcl/Tk program from inside Komodo Edit, start by clicking the Tools button at the top of the Komodo Edit window, then select Run Command.
If you are using Windows XP, it will look like this.
Type in the path to the Wish interpreter. That path is probably like
the line below, but it might be wish84
instead of
wish85
. If this fails, use the find/search to figure out
where your wish interpreter is. The interpreter will be named something
that starts with the word wish
.
C:\tcl\bin\wish85
Enter the path to your first Tcl program next. Put it in quotes for Windows. If you stored the program in My Documents you'll have a string similar to what's shown, but you'll have your name instead of Test.
If you are using Linux, it will look like this:
On Linux, you don't need the full path to the wish interpreter, but you should enter the full path where you saved the Tcl script.
When you click the Run button, the computer will pause for a second or so, and then show the You Win! message.
Actually, there will be two boxes on your computer screen. One with
the You Win! message, and one blank one. You need to X
or destroy the blank window to exit the wish
interpreter.
The tk_messageBox
command needs to know some things
in order to run. It needs to know how many buttons to show
and what message to display for starters. We call this information
arguments and sometimes options. The entire
list of things you can do with a tk_messageBox
is
called the definition for the command.
The definition for using the tk_messageBox
looks like this:
tk_messageBox option value...
tk_messageBox is a dialog box with
an icon, text, and a selection of buttons. The dialog box
takes focus and returns the text for the button clicked.
Options for the tk_messageBox include:
-title : A string to display in the window decoration.
|
Play with this command a little bit, you can create a new very, simple, but frustrating game by typing this command:
tk_messageBox -type ok -message "You Lose!"
Try putting in a long message and see what happens. In the next lesson, we'll make this game a bit more interesting.
Previous | Next |