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

Saturday, November 12, 2011

Literals, Constants, and Variables

Literals can be used in output either alone or in combination with constants, variables, or other literals.

String Literals
The following line of BASIC code uses the PRINT command to print the string literal "Hello World!" to the screen:
PRINT "Hello World!"

Numeric Literals
The following line of BASIC code uses the PRINT command to print the sum of 2 and 3 to the screen:
PRINT 2 + 3

Numeric and string literals may be used together. The following code uses one PRINT command to print a string literal and a numeric literal on the same line:
PRINT "The sum of 2 and 3 is" 2 + 3

GEEK NOTE: Literals are technically "literal constants". The term "literal constant" doesn't make any sense though, since they are NOT stored in memory, they CANNOT be called anywhere else in a program, and are therefore NOT constants! They are literal and temporary. Not constant or permanent. For this reason I call them "literals".

In QuickBASIC there are two types of constants. The first type of constant is called "literal constant" and is what I just described as a "literal" (scroll up and see the note in yellow). The second type of constant is what I think of as a true constant. The technical term is "symbolic constant". For the sake of simplicity I will refer to these as "constants".

Constants are values you can call from other parts of your program. Constants remain constant. They do not vary. You cannot change them somewhere else in your program. You can, however, refer to them in other parts of your program.

Real-world constants include your birth date, the number of hours in a day (24), and how many quarters make up a dollar (4).

Constants are declared with the CONST statement. The syntax for the CONST statement is:
CONST name = expression [,name = expression]...

If you were writing a program which would use pi, you could either write out 3.14159 every time you need to use pi, or you could declare pi as a constant:
CONST pi = 3.14159

Using constants in this way not only cuts down on errors (what happens if you type the number wrong just once?), it also increases the readability of your programs.

It should be noted that in the example just given, QuickBASIC made pi a single precision number. This is a default in QuickBASIC. You may want to get in the habit of using a data type suffix for single and double precision numbers. You cannot use any other numeric type declaration characters on constants.

As mentioned before, you cannot change the value of a constant. If you declare pi as in the sample above and then later in the program write the following statement...
pi = pi +1

... the compiler will generate an error.

Variables have a name, data type, and a value. You can call and change variables throughout your program.

Real-world variables include your age, the number of hours you sleep in a day (0 to 24), and how many quarters are in your pocket.

Variable Names
Variable names begin with a letter and may contain up to 40 letters or numbers. You may not use QB reserved words as variable names. It is recommended that you use a data type suffix for all variables.

Variable Types
QB has five standard data types. Here's a handy table that shows these data types and the type declaration character (or "suffix") for each type.
Data TypeSuffix
"Type Declaration Character"
Highest ValueLowest Value
Long Integer
(absolute value)
(absolute value)
(absolute value)
(absolute value)
STRING$32,767 Characters0 Characters

 You can assign one of these data types to a variable by using type declaration characters like so:

This gives our program two string variables and one integer variable.

You could also assign a data type to a variable as part of a DIM statement:
DIM FirstName as STRING
DIM LastName as STRING

Do not use type declaration characters in DIM statements unless you are using arrays.

Wednesday, June 23, 2010

Comments and REMarks

REM is the QB keyword for REMark. REM statements are ignored entirely by QB. They are only there for programmers and anyone else who reads your BASIC source code.

Your computer would be just fine if you programmed it in assembler or machine code, but would you? BASIC is an easy to understand language with simple English keywords, but even BASIC programs can look cryptic and confusing without proper comments.

You can also use apostrophes to begin a comment. Unlike the keyword REM, you can place this second form of REMark after other BASIC statements.

Take a look at REMarks.bas to get an idea of how comments work. Run the code to see how QB ignores all REMarks.
'      O------------------------O
' | REMarks.bas |
' |REMarks.bas demonstrates|
' |the use of comments and |
' | REMarks in QB |
' | |
' | REM |
' | ' |
' O------------------------O

PRINT "This line gets printed."
REM "And this line does not."

PRINT "These words are printed, " ' and these are not.


The output for REMarks.bas should be:
This line gets printed.
These words are printed,

Note that you can place an apostrophe comment after the PRINT statement (or any other executable statement) but you can't place an executable statement after an apostrophe.

Different programmers use REMarks in different ways. You will see all of my programs start with a text box of sorts with each line beginning with an apostrophe. These text boxes contain information about the program. Similar, less ornate comments appear throughout my source code explaining what is happening in SUBs, FUNCTIONs, and wherever I feel the need.

You can use REMarks to explain what a SUB or FUNCTION is for, like this:
REM This function calculates collision damage.

When I first started looking at professional source code I noticed many inside jokes and general sillyness inside source comments, function names, and variable names. At first I thought it was annoying, and then I realized it helped programmers "play" with the variables, code, and functions. This kind of play increases a programmer's interest in the code and this in turn increases creativity and understanding of the code. So don't be surprised when you see references in other people's code to Monty Python, the Hitch Hiker's Guide, or game characters named superSpeedyBadass.

Wednesday, June 2, 2010

PRINT Statement

PRINT Statement
Syntax: PRINT [expression list] [{,|;}]...

Description: Prints text to the screen at the cursor location.


PRINT "hello world!"

Standard Use

If your program displays text on the screen for users, you probably want to use the PRINT statement. The first form of PRINT we will look at has the following syntax:
PRINT expression

This simple form of the PRINT statement can be seen in hello.bas, which is our "Hello World" program (the standard first program in any programming language).
'      O------------------------------------------O
'      |                hello.bas                 |
'      |   This is the standard first program     |
'      |in any programming language. This program |
'      |   prints the words "Hello World!" to     |
'      |               the screen.                |
'      |                                          |
'      |                KEYWORDS:                 |
'      |                  PRINT                   |
'      O------------------------------------------O

PRINT "Hello world!"



This program prints the words "Hello World!" to the screen.

String Variables

The text in quotes in hello.bas is called a "string literal". It is not assigned a place in memory and there is no way for you to call this string anywhere else in the program or to change this string as the program is running. PRINT displays string literals just as they appear in code (minus the quotes). PRINT can also display string string variables.

In our next program, hello2.bas, we assign a string to a string variable and then use the PRINT command to print the contents of the variable to the screen.

'      O-----------------------------O
'      |         hello2.bas          |
'      |  hello2.bas uses a string   |
'      | variable to print the words |
'      |"Hello World!" to the screen.|
'      |                             |
'      |          KEYWORDS:          |
'      |             DIM             |
'      |            PRINT            |
'      O-----------------------------O

DIM hello$
hello$ = "Hello world!"

PRINT hello$


In hello2.bas, we assign a string ("Hello World!") as a value to a string variable (hello$). We can change the value of hello$ anywhere in our program.

In hello3.bas, we have one string variable that holds a series of strings. Each PRINT statement is the same, but as the value in the string variable changes, so does the text printed to the screen.
'      O-------------------------O
'      |       hello3.bas        |
'      |hello3.bas uses a string |
'      |variable to print strings|
'      |     to the screen.      |
'      |                         |
'      |        KEYWORDS:        |
'      |           DIM           |
'      |          PRINT          |
'      O-------------------------O

DIM hello$

hello$ = "Hello Canada!"
PRINT hello$

hello$ = "Hello world!"
PRINT hello$

hello$ = "Hello universe!"
PRINT hello$

hello$ = "What's the frequency, Kenneth?"
PRINT hello$


Full Syntax

The use of QuickBASIC's PRINT statement in hello.bas demonstrates how PRINT can display information on the screen, but does not show the real potential of PRINT. In hello.bas, PRINT displayed the string literal "Hello world!", but PRINT can also display the results of simple or complex expressions involving variables, constants, math, and string concatenation. The full syntax of the QuickBASIC PRINT statement is:
PRINT [expression list] [{,|;}]...

Numeric Literals

Sunday, May 30, 2010

Run Your .BAS Files in Windows Explorer

Want to double click your .BAS files in Windows Explorer and have them run by QB? Type the following text into your favorite text editor and type (or copy/paste) the following text:

REM qbrunner.bat
REM To install, place this bat in your QB folder,
REM right click on a .bas file and choose the
REM program to open with. Navigate to and select
REM this batch file. Then make sure you check
REM the "Alway use the selected program..." box.
REM After that you can just run .bas files in
REM Windows Explorer.

TITLE "%~n1" is now running...

REM Start in your file's folder
CD "%~dp1"
REM Change the path to QB on your computer
C:\QBasic\QB.exe /L /RUN %~nx1


Save this file as qbrunner.bat in your QB install folder.

Next, right click on a .bas file. Select "Open With > Choose Program..."
In the window that pops up, click the "Browse..." button.

Navigate to qbrunner.bat and press "Open".

Back in the "Open With" dialog, check the "Always use..." box and click "OK".

Now, when you double click a .bas file in Windows Explorer your code will be interpreted by QB.

If you do not want the QB environment to open when the .BAS file is done, use keyword SYSTEM instead of END.

This file can be found HERE.