Elementary Computer Graphics: Drawing with Pixels
Pages: 1, 2, 3, 4
Just like the Tic-Tac-Toe grid, the Wish window is described by x and y coordinates. The upper lefthand corner of the Wish Shell window is the origin just as the upper lefthand corner of the Tic-Tac-Toe grid had its origin. The origin is where the x and y coordinates are both 0. Figure 15 looks a lot like Figure 4, only the Wish window is 640 x 480 instead of 3 x 3 like the Tic-Tac-Toe grid.
|
|
You move around using the coordinates in the game window we created in much the same way we moved around the Tic-Tac-Toe grid. At the top of the window y=0, and y gets bigger as you move down. To the left of the window, x=0, and x gets bigger as you move right through the window. Now for a pop quiz. What are the x and y values of the middle of the window?
Let's see, the biggest value of x is 639, on the righthand side of the window and half of 639 is roughly 320. The biggest value of y, at the bottom of the window is 479. Half of 479 is roughly 240. The middle x value is 320 and the middle y value is 240. The pixel coordinates of the middle of the window are x=320 and y=240.
Want to have a little fun and prove this is the middle of the window? We can run a simple experiment to tell us the x and y coordinates of the Wish app window. Tk uses x and y values for tracking the computer's mouse. The x mouse value is %x and the y mouse value is %y in Tk. These values are used to tell us where the mouse pointer is inside our Wish Shell window. Type in the following command and see this for yourself:
bind .video_game_window <Motion> { puts "mouse x: %x mouse y: %y" }
The Tk command bind instructs Tk we want our Tcl command puts
bound to the motion of the mouse. Every time you move the mouse, Tk will
hand off the mouse coordinates to our Tcl command puts. You will need
to move your mouse inside the Wish Shell game window to see the mouse
coordinates. The mouse is only bound there. Move outside the window
and Tk will not display any coordinates. Move back in and the coordinates
will scroll down the console window again.
Let's give it a try and see if you can prove the middle of the screen is x=320 and y=240. My test values are shown in Figure 16.
|
|
The Script:
set video_game_width 640
set video_game_height 480
canvas .video_game_window \
-width $video_game_width -height $video_game_height
pack .video_game_window
bind .video_game_window <Motion> { puts "mouse x: %x mouse y: %y" }
Lesson 6: Drawing a Pixel
The moment you have been waiting for, all young game-playing careers. Here is the dark secret to drawing on the computer. Are you worthy of having such knowledge and power? Behold, plotting the pixel.
The Tk graphics environment doesn't actually provide a utility to plot a pixel, so we are going to have to make do with drawing extremely small rectangles. The rectangle will be one pixel wide by one pixel tall. Basically, we're going to draw a square and pass it off as a pixel for this example. Type in this small piece of code into the console,
.video_game_window create rectangle 320 240 320 240This should draw a teeny-tiny black pixel in the center of your game window. Now you're cooking, and just a few short minutes ago you typed in your first Tcl program.
The Script:
set video_game_width 640
set video_game_height 480
canvas .video_game_window -width $video_game_width -height $video_game_height
pack .video_game_window
.video_game_window create rectangle 320 240 320 240
If you're not as enthusiastic with those results, how about we draw a larger rectangle we can see. And let's give it some color!
.video_game_window create rectangle 320 240 350 270 -fill red
The -fill option for the Tk create rectangle command allows you to change
the colors. Try playing around with black, blue, red, green, and yellow.
The Script:
set video_game_width 640
set video_game_height 480
canvas .video_game_window -width $video_game_width -height $video_game_height
pack .video_game_window
.video_game_window create rectangle 320 240 350 270 -fill red
Lesson 7: The Grand Finale
In just a few short lessons of experimenting with scripts you can't become a game-coding master. That takes time and experience. But what can we do with what we already know? Well, Tcl/Tk is powerful enough to allow us to code a simple paint program. Most kids have played with Broderbund's Kid Pix in school. This drawing program can be crudely replicated with a few lines of scripting code in Tcl/Tk. We are going to create a very basic paint program using our video game window. We first create our window and display it in the usual manner.
set video_game_width 640
set video_game_height 480
canvas .video_game_window -width $video_game_width -height $video_game_height
pack .video_game_window
To draw with the mouse, you hold the left button down and draw. The left
mouse button is B1 in Tcl/Tk language. We will be dragging the mouse
to draw pixels. Moving the mouse in Tcl/Tk we use the Motion option,
as we did while experimenting with the window mouse coordinates. Just
as we did before, our script must bind to the B1-Motion action
on the computer. When a computer is looking for an action to take place
in a window it is said to be looking for an event. The event our computer
is looking for in order to draw is the B1-Motion, where you
are pressing the left mouse button down and moving the mouse. This is
also called dragging the mouse.
We will need to write our own special Tcl command, which will be called when
this event is seen by the computer. When we write a special command
of our own in Tcl it is called a procedure. Consider it a mini-program
script. To let Tcl know we're implementing our own script we must use
the Tcl command proc. The following procedure will tell the Tcl interpreter
how to handle the mouse drawing on our videogame window. Our mini-script
procedure name is mouse_draw. Here is what it looks like:
proc mouse_draw { x y } {
set xul [expr $x - 3]
set yul [expr $y - 3]
set xlr [expr $x + 3]
set ylr [expr $y + 3]
.video_game_window create rectangle $xul $yul $xlr $ylr -fill red
}
What are the variable names xul, yul, xlr, and ylr? Remember, we are drawing a rectangle, well, actually a square, 6 pixels by 6 pixels. The xul means x, upper left, and yul means y, upper left. The xlr stands for
x lower right, and so forth. Doesn't this look a lot like how we described
the Tic-Tac-Toe grid?
What is the { x y } in the Tcl command proc mouse_draw { x y }? We need
to collect the mouse coordinates. Remember how we did this in the simple
mouse experiment from before? The Tk graphics tool allows us to examine
the mouse coordinates using the special notation, %x and %y. We will
call our mouse_draw procedure in this manner:
mouse_draw %x %y
This command as it is won't work for us. Remember, the computer is listening
for an event. In this case we want to draw only when the left mouse
button (Bl) is held down and the mouse is moving (Motion).
We need to bind the event (B1-Motion).
bind .video_game_window <B1-Motion> { mouse_draw %x %y }
That's it. You have a simple paint program. My work of art is displayed in Figure 17.
|
|
There is a paint online version of Kid Pix, try it out and see how your paint program compares.
The Script:
set video_game_width 640
set video_game_height 480
canvas .video_game_window -width $video_game_width -height $video_game_height
pack .video_game_window
proc mouse_draw { x y } {
set xul [expr $x - 3]
set yul [expr $y - 3]
set xlr [expr $x + 3]
set ylr [expr $y + 3]
.video_game_window create rectangle $xul $yul $xlr $ylr -fill red
}
bind .video_game_window <B1-Motion> { mouse_draw %x %y }
Congratulations!
You mastered basic graphics scripting. We didn't write a first person shooter game but you did master writing a simple draw program. You will need to play around with scripts presented here and go back and play with them. You have the web address for the Kid Pix online page -- why don't you see if you can copy some of the operations of Kid Pix.
A lot of cool computer graphics projects can be tackled using Tcl. Try to add keyboard bindings to your programs. Keep at it. I find computer graphics programming to be as fun as building with Legos. Don't take my word for it. Find out for yourself. Remember to use your new-found knowledge for the good of mankind.
Michael J. Norton is a software engineer at Cisco Systems.
Return to the Mac DevCenter
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 15 of 15.
-
Wonderful article!
2004-01-13 07:27:17 anonymous2 [Reply | View]
Tcl and Tk make such a wonderful starting language for someone learning programming.
I'm familiar with one company whose staff, many of whom were technically fearful of the computers, let alone programming, who implemented their desktop environment using C and C++ apps, but with Tcl interpreters for customization purposes.
The less fearful of the staff began experimenting, and found they could do so much more than just customize - and began extending the applications in sophisticated ways to make doing their jobs significantly easier.
What a great place for a new person to start.
Keep up the great articles.
And readers looking for more info will find http://wiki.tcl.tk/ , http://www.tcl.tk/ and news:comp.lang.tcl as places where the tcl community of developers provide friendly help.
Tcl's a great example of free open source at work.
And MacOS X is a great example of a platform
to use.
Thanks for the article!
-
Language != relevent
2003-12-30 17:15:55 anonymous2 [Reply | View]
I learned Cocoa on a #@! damned commodore and now I write in C, C++, Objective-C. The language you start with is irrelevant.
-
bug/typo on page 3
2003-12-18 12:41:17 anonymous2 [Reply | View]
On http://www.macdevcenter.com/pub/a/mac/2003/12/16/begin_programming.html?page=3
about halfway down are the lines:
canvas .video_game_window -width $video_game_width -height $ video_game_height
The output from your console should look similar to the console output in Figure 13.
That first line should be:
canvas .video_game_window -width $video_game_width -height $video_game_height
The extra space before video_game_height will cause a problem if the student cuts and pastes the code...
-
Try C!
2003-12-17 14:28:40 anonymous2 [Reply | View]
Tcl? Python?
Teach 'em C! I was only a year older (5th grade) when I begain teaching myself C...
Of course, I got confused on how pointers etc. work for a year or so (picked it back up then) but you can just give a lecture on it. :)
-
good start
2003-12-17 09:43:22 anonymous2 [Reply | View]
I have to agree that graphics programming is a great way to get kids interested in programming. OK, so Tcl may not be the easiest language to start with, but I think that with a little direction, kids could get a lot out of it. And the instant feedback aspect is great, making it a better choice than something like python and pygame (which takes a lot to set up).
-
TCL is fine
2003-12-17 08:36:02 Jack Herrington |
[Reply | View]
I started with Basic and APL and it was a fine foundation. If you are interested you will learn and re-learn, re-think and change what you think about how to program. It's really in understanding the fundamental decomposition of problems.
Bravo to the author for trying to get people interested in teaching their kids about programming.
-
Programming for Kids
2003-12-17 06:01:27 anonymous2 [Reply | View]
How to put your kid off programming for life. What a truly awful place to start. Why not try something like RealBasic or even Python. Tcl ? No Way ! -
Programming for Kids
2003-12-17 12:40:27 anonymous2 [Reply | View]
hi,
Basically, RealBasic was my first choice. But it's pricey. Eventually, we will migrate to that once I finance the endeavor.
Meanwhile, Tcl provides enough tools to get kids started in the basic concepts of computer graphics.
Besides, I can blit with Tcl <okay and Python for you Python purists out there>.
Michael Norton
-
Programming for Kids
2003-12-17 11:28:35 anonymous2 [Reply | View]
In case you don't remember your first programming experience, the syntax is irrelevant. (Unless it makes things overly complicated, which Tcl doesn't for this tutorial.) Tcl may be ugly to you, but it's pretty straight forward. This article is getting kids making a graphics app in 4 pages of easy-to-read text. Kudos to the author.
If your 9-year-old knows that Tcl is less elegant than Python, give them GCC and start training them to kernel hack. -
Programming for Kids
2003-12-17 08:25:52 anonymous2 [Reply | View]
Whatever! Who cares what language is used. I learned Apple Basic when I was in 5th Grade it was a blast. I've been facinated with anything computer related since and I beleive it planted the seed that lead to my career in computer technology. I will always have fond memories of my Apple Basic class. It's creating that interest that is important. Loved the article! -
Programming for Kids
2003-12-17 07:42:52 anonymous2 [Reply | View]
Well then, the door is open for you to provide a tutorial in either Python or RealBasic. I cannot wait to see what I will learn from what you will offer. -
Programming for Kids
2003-12-17 07:28:14 anonymous2 [Reply | View]
O.K. Tcl might put them off but...
As mentioned in the article, RealBasic was out of the budget.
As for Python... it's a little dry. The ability to make something like a draw program will excite the kids more (nice direct feedback.) Then over time one can introduce more complex concepts and languages.
(Of course I'm the father who had his one year old daughter sitting on my knee while setting up an OpenBSD firewall, so she's doomed already...) -
Programming for Kids
2003-12-18 10:56:33 bazzargh [Reply | View]
I thought about suggesting Logo since it was created for this purpose, but reading the docs for StarLogo I wonder how they ever thought this was suitable for kids?
Something that is pretty good is Squeak. Hey, it was written at Disney, it must be for children. But seriously, its direct-manipulation graphics toolkit makes it fairly easy to build interesting apps, and the 3d authoring tool is brilliant, here's a shot of the demo:
http://www.squeak.org/images/3D_large.gif
...the best bit is when the rabbit turns to look at you. Freaky.
A lot of squeak use is squarely aimed at schoolchildren, so there's plenty of material:
http://www.squeakland.org/
-
Programming for Kids
2003-12-19 18:45:58 anonymous2 [Reply | View]
The language doesn't matter (you can pick up a language in a weekend). Its logic that matters, and drawing graphics on the screen is a great way to get instant feedback on logic errors.
Why not Logo? In particular, ACSLogo? Its one of the MacDevCenter's Innovators Contest Winners (2nd place). <http://www.alancsmith.co.uk/>
Its got a nice simple interface and its free.









A great job! Thanks!
Dave