Monday, May 14, 2012

Video Game Programming for Kids

Recently I searched for a book on QB64. I found Video Game Programming for Kids by Jonathan S. Harbour. I checked out the preview of the book and downloaded the source code from the author's website (the URL was in the book preview).

The book is very basic. I mean, it's written for kids and uses QB64. That's BASIC. It is also a very good introduction to game programming.

As far as I can tell, this is the only book on Amazon that teaches QB64. Since it was only like $12, I bought it.

I had tried QB64 a couple of years ago and couldn't get some of my old code to work in it so I went back to my QB45. Now, working along with this kids book I can see just how great QB64 really is.

Also, much of the code in the book will compile in QB45. Here's a screen of me making the combination lock program in chapter 2 much more complicated than it needs to be:

I think that's OK since the author included the extraneous statement:
state = 1

But that is the only mention of state. He did just write about the concept of finite states right before giving the source code for this program, but the program does not actually give an example for the idea. Hmm, maybe that's the next thing I should do to the program.

Don't get me wrong, it's still the type of book I would have loved when I was 10.

Excuse me, I have to use the concept in the program now . . .

I just took a few moment out and used the idea of finite states in the program. The lock now has two states (locked and unlocked). Here's the code:

|       ComLock2.bas          |
|  Was CombinationLock.bas    |
| But was shortened for QB45  |
|                             |
|   And then I used arrays    |
|         and loops           |
| Just because I was bored!   |
|                             |
|The lock now has two states: |
|          1 Locked           |
|         2 Unlocked          |

PRINT "Combination Lock Game"
PRINT "Guess the combination lock if you can!"
PRINT "Each number will be from 1 to 6."
guesses = 0
state = 1     'State 1 is locked. State 2 is unlocked!
DIM number%(1 to 3)
DIM num%(1 to 3)

REM make the combination lock
FOR i = 1 to 3
 number%(i) = INT(RND * 6) + 1

REM Start the main LOOP
    correct = 0
    INPUT "Guess #1: ", num%(1)
    INPUT "Guess #2: ", num%(2)
    INPUT "Guess #3: ", num%(3)

    REM check all three guesses
FOR count% = 1 to 3
    IF num%(count%) = number%(count%) THEN
        correct = correct + 1
        PRINT "#"count% " is correct!"
        IF number%(count%) > num%(count%) THEN PRINT "#"count% " is higher"
        IF number%(count%) < num%(count%) THEN PRINT "#"count% " is lower"
    END IF
NEXT count%

    guesses = guesses + 1
IF correct = 3 THEN state = 2

LOOP UNTIL state = 2

REM the player wins
PRINT "You opened the lock and got the treasure!"
PRINT "It took you "; guesses; " guesses."

Anyway, Video Game Programming for Kids is a great little book for any young aspiring game programmer. You also might want to check out the author's forums HERE