深瞳

夜如深瞳,瞳深如夜

  :: :: 博问 :: 闪存 :: :: 联系 :: 订阅 订阅 :: 管理 ::
http://davidbau.com/archives/2005/07/29/haaarg_world.html

Haaarg, world!

Continuing the "Anthony's first program" series from last time.

When my six-year-old son Anthony asked me if he could program his own computer game, I was worried that he might be totally disappointed. It is not 1979 anymore. The gap between the primitive software you can put together in an afternoon and highly polished multimedia software you can buy at the store for $9.99 is huge.

In 2005, could programming hold the interest of a six-year-old at all? Does anybody even program their own computers anymore? And what tools would you use as a beginner? Which language? Which operating system?

I am a professional programmer, but the tools I use seem far out of reach of a beginner. I have no idea how a real novice should learn to program in 2005. And so after dinnertime last Friday, when Anthony asked me for the 10th time to help him write a computer game, we would have to come up with something. It was time to get started....

An Operating System for Vampires

For a six-year-old, the idea of writing a computer game was just like arts and crafts, costumes, and pretend. "Sam's sister wants to make a pet show game," Anthony explained to me, "the person who makes their pet do the best tricks wins." At six, you see no limits in the world. You can create anything you can imagine.

I had to explain to him that he would have to pick a simple game with rules. Making, for example, a "pet show" where you could tell your pet to do anything would be hard, because there were too many things you would have to teach the computer about pets.

So we had to pick something like "tic-tac-toe", or "guess a number from 1 to 100" or that kind of thing. Anthony decided "guess a number" would be a great video game, especially if a vampire was the one that was keeping the super-secret number. He would have to have a really scary vampire.

Anthony wanted to get started on his vampire game right away. He didn't have time to watch me fool with installing cygwin or python or the J2SE SDK. We had a couple hours. We would need to get something working before bedtime.

So before even choosing a language, I had to pick which computer to use with Anthony: Windows or Mac. It really only took me a second to decide. On Windows, I would have to install a bunch of development tools - a language, an SDK, etc - and then I would have to get it all configured actually work.

But the Mac is Unix. It is a hacker's system. It comes with all the important languages: C, Java, perl, and python, not to mention bash and sed and awk. And vi. And emacs. On the Mac, you can turn it on and go. We could get started right away.

"Vampire" would be written for the Mac.

Hello - er - "Haaarg," world!

So we opened up two OS X terminal windows and put them side by side. One window (running emacs) was for writing the program, and one window (with a shell) was for running it, I explained. I created a file called "GuessANumber", chmod 755, and then typed in an example program.

I picked the simplest, clearest language I could think of. Then I told Anthony, "this first line tells the computer what language we are using. It is called Python. In python, you can print things by saying print."

This is what I gave him:

 
#!/usr/bin/env python
print "Hello."
 

And I showed him how to run it:

 
> ./GuessANumber
Hello.
>
 

Anthony is a typical 6-year-old, and he does not keep his opinions to himself. "That's no good!" He was disappointed. "The Vampire should say 'Haaarg, I've got the secret!' It needs to be scarier! Can you make it say that?"

But Anthony can type. Hunt-and-peck is something first graders know how to do in 2005. "Anthony, you can type it in if you like."

So here is what he did. He was especially happy with his own all-caps innovation:

 
#!/usr/bin/env python
print "HAAARG! I've got the secret"
print "it's a number. 1-100"
 

And then:

 
> ./GuessANumber
HAAARG! I've got the secret
it's a number. 1-100
>
 

His vampire had printed out HAAARG! A huge smile lit up Anthony's face.

We were off to the races.

Interacting With Python

I will spare you all the details, but I will show you a couple versions of the python program Anthony wrote. I find it interesting to see what a 6-year-old understands first, and what comes next.

Here was the first program that you could actually play:

 
#!/usr/bin/env python
secret_number = 28
print "HAAARG! I've got the secret"
print "it's a number. 1-100"
print "GUESS!"
guess = input()
if guess == secret_number:
print "you win!"
else:
print "I WIN!"
 
 
> ./GuessANumber
HAAARG! I've got the secret
it's a number. 1-100
GUESS!
50
I WIN!
>
 

As soon as the program did this, Anthony said, "I want the game to be even harder! Let's make it one to a THOUSAND!"

"But Anthony," I said, "Even if you do that, I can always win your game by typing 28." He did not seem to be concerned about that at all. But then he changed his mind and decided that it was more important that the vampire needed to be better at taunting you.

Timing Is Everything

"I want it to wait two seconds before it says GUESS! Can we do that?"

The beauty of python is that it is a professional programming language, not just a plaything. The answer to 'can we do that?' is almost always 'yes', and 'it is well-doumented and discussed on the internet.' Googling [python sleep] gave Anthony a beautifully small piece of code to copy, with just two more lines.

 
#!/usr/bin/env python
import time
secret_number = 28
print "HAAARG! I've got the secret"
print "it's a number. 1-100"
time.sleep(2)
print "GUESS!"
guess = input()
if guess == secret_number:
print "you win!"
else:
print "I WIN!"
 

As we watched and waited two seconds for "GUESS!" to show up, Anthony's face lit up again. "GUESS!" he exclaimed, in his best Scary Vampire voice. And then he laughed. And then back to programming. "It should wait a little longer! Maybe seven seconds," as he started typing into emacs again.

Timing is everything.

To be continued

The next step, of course, was to teach Anthony how to make a "while" loop so that you could guess a few times. And then after that it took a little cajoling to convince him that the gameplay would be improved if you gave hints like "try a bigger number" or "try a smaller number". Somehow we got through it all.

Before the end of the evening, we had a classic "Guess My Number" game running. The two hours between dinnertime and bedtime had just barely been enough to get it written and working. We showed it off to Mom, who was suitably impressed, and then got into PJs and tucked into bed.

In the next article, I will show you the code, and then tell you what happened the next morning.

Posted by David at July 29, 2005 05:53 AM | TrackBack
Comments

We're hiring.

Let me know when he learns Java.

Peace. ;-)

Posted by: Cameron at July 29, 2005 09:35 AM

I wish my dad would have been like that. Great story!

Posted by: Michael at July 29, 2005 02:37 PM

I was about seven when I used the internet to teach myself QBasic on an old Compaq desktop. My dad gave me the computer and the freedom to learn how to use it. It wasn't long before I wanted to do more than just play other people's games. I totally understand your son's ambition, and I really appreciate what you're doing for him.

Posted by: Clay at July 29, 2005 02:50 PM

I hope i have kids like that one day!!!
Although i cant code....Damn.
So when ure son wants to teach me how to write python send him round!

NOGG3R5

Posted by: NOGG3R5 at July 29, 2005 03:09 PM

this anthony has to be just about the smartest kid in the whole wide world.

love, Grandpa Paul

Posted by: paul bau at July 29, 2005 04:02 PM

Ah... you should start his own blog... "As my childs game development continues..." it'd be fun to watch him progress and make more games.

Posted by: Marble at July 29, 2005 07:01 PM

Awwww... That's sooooo cute! Makes me want to have kids!! :)

Posted by: dJsLiM at July 29, 2005 10:21 PM

All my dad did was teach me fortran. :(

Posted by: Stupidideas at July 30, 2005 08:08 AM
 
posted on 2005-07-31 09:14  深瞳  阅读(420)  评论(0编辑  收藏  举报