February 19, 2005

What do you do after "Hello, world"?

What small program do you often write to teach yourself a programming language? Also, what capabilities do you really need, beyond the basics such as if-then-else or while loops?

I find myself needing to do a lot of basic graphics for output. It's a lot harder to do graphics now than it was back in the days when every language had a setPixel(x, y, color) and clearScreen() command. To draw anything you first have to open a window and put something in it that you can draw on. If you want to animate anything you have to figure out how to do time delays. You often have to figure out where the mouse pointer is, so that the user can interact with the program.

One simple learning program draws an object and has it follow the mouse pointer. Here's a version in Perl:
use Tk;

$mw = MainWindow->new;
$c = $mw->Canvas()->pack(-fill=>'both',
$id = $c->createText(0, 0,
-text=>'Hello, world!');
$mx = $my = 100;
$c->repeat(50, \&move);

sub drag {
$mx = $Tk::event->x;
$my = $Tk::event->y;

sub move {
($x, $y) = $c->coords($id);
$vx = ($mx - $x) / 10;
$vy = ($my - $y) / 10;
$c->move($id, $vx, $vy);
If I were really doing this, I'd use strict and warnings too.

Oddly enough, this program doesn't require if-then-else, loops, or even print.


At February 20, 2005, Blogger JeremyHussell said...

I should put up a sign: "Warning: posts will be edited until another post is made." Since I'm trying to learn to write better, I've been editing my posts for readability. The above post in particular saw a complete re-write from the original, which used a different example program.


Post a Comment

<< Home