Characters, Strings, and Text Output

We are now heading into the fascinating subject of strings, which store characters, words, sentences, and any other types of information that you need to track. You can use strings to hold all other data types, which is useful when you need to make sure a user typed in the correct type of information (for example, a dollar amount). In this chapter, you will learn how to create, manipulate, and display strings.

This chapter shows you how to do all kinds of things with strings, from converting them to numbers to printing them to the screen using graphical fonts to reading substrings within a string. By then end of this chapter, you will be able to create a really cool game (in fact, it's a game I used to play when I was young). Now on to strings….

Introduction to Strings and Text Output

Strings, strings, strings. What does that make you think about? I think of one of Bach's concertos being performed in an orchestral hall. The cello is to the left of the piano, in tune with trombones and flutes. To the right, you have the violin section, adding to the harmony of the piece. "String" is one of those unfortunate overworked and overused words in the English language, and it has been used to mean many different things. (It is the bane of the foreigner learning English as a second language!)

What is so special about strings? They are the lines of text that bind together programs. Stringed instruments allow the flow of the melody and the harmony of a song; strings in DarkBASIC do essentially the same thing. They allow you to convey thoughts that you want to portray in your game. Computer scientists decided to use the word "string" to represent a series of characters in the computer's memory that display textual information. In the old days, everything was printed out by a printer, but today most output is sent to the high-resolution monitor sitting next to your computer. Regardless of the output device, a string is used to store and display text.

Strings are the most popular data type that programmers use. Why? String variables are versatile and capable of storing numbers, letters, and punctuation in any combination. One string can store your name, while another string can store how many oranges you have.


Programming Strings

Strings are easy to program or define in DarkBASIC. To give you a little perspective, I'll explain how strings work in most languages. They are more or less collections of characters. A character, of course, is a number, letter, or punctuation mark. In C you can define a string by creating a character array, whereas in C++ you just define a string class. In DarkBASIC it is much, much easier.

Declaring Strings

So how is a string declared? Simply by enclosing what you want to say in quotes (""). For example, "This sentence is declared as a string." Notice how the string was enclosed in quotes? The quotes tell DarkBASIC that what comes between them is a string.

Assigning Strings to Variables

To assign a string to a variable, you just add an = sign followed by the string. If you wanted to create a string called sentence$, containing "This sentence is declared as a string," you would type in the following code.

sentence$ = "This sentence is declared as a string"

Copying and Adding Strings

Sometimes declaring a string is just not enough. You might need to copy a string, which is quite simple in DarkBASIC. The following code will copy the string from the preceding example into a new variable called newsentence$.

sentence$ = "This sentence is declared as a string."
newsentence$ = sentence$
PRINT newsentence$

Sometimes you will need to add two strings, which will allow you to make more sense of the data you have within your strings. There are two strings in the following code—firstname$ and lastname$. Suppose you want to get the wholename$. You just add the strings together, like this:

firstname$ = "Joshua"
lastname$ = "Smith"
wholename$ = firstname$ + lastname$
PRINT wholename$

When you are running this program, you'll notice that wholename$ prints out, but there is no space between my first and last name. That does not help you when you are putting my whole name together. You need to add a space between the first and last name. The following code will show you how to do that.

firstname$ = "Joshua"
lastname$ = "Smith"
wholename$ = firstname$ + " " + lastname$
PRINT wholename$

String Conversion Commands

Now it's time to do some legwork with strings. You know how to define them, but what's the good of defining them if you don't have something to use them? DarkBASIC contains several commands that will convert strings into useful data.

The ASC Command

The ASC command converts a character (one letter) into its corresponding numeric ASCII value. The command returns the ASCII value of the first character in the string you input. The syntax to return a string as an integer is ASC(String).

What is an ASCII value? Good question! ASCII stands for American Standard Code for Information Interchange. Computers don't know what characters and words are, but they do understand numbers. Therefore, a standards committee assigned a specific numeric value to every single character on the keyboard, along with many special control characters, a long, long time ago. Appendix C, "ASCII Chart," contains a complete ASCII table, and I recommend that you flip back there now to see what it looks like. There are 256 ASCII values, which means there are 256 characters in the ASCII chart. For example, the uppercase A is equal to ASCII value 65. If you need to know an ASCII value for any reason, the ASC command will give it to you. The following code demonstrates a short program that will return an ASCII value.

REM This will return the value of 65
AsciiValue = ASC("A")
REM This will print the number 65
PRINT AsciiValue

The VAL Command

The VAL command, mentioned in Chapter 3, converts a string to an integer. It only converts strings that contain actual numbers, such as "123". Thus VAL is useful if you have a number stored in a string and you need to convert it to an actual integer for calculations. The syntax to return a string as an integer is VAL(String$).

The following code demonstrates the use of the VAL command.

REM This will return a value of 35
Number = VAL("35")
REM This will print the number 35
PRINT Number

What happens if you enter a word instead of a number? Good question, I'm glad you asked. I just so happened to check that out and guess what? VAL returned a 0 for the answer.

REM This will return a value of 0
Number = VAL("My String Here")
REM This will print the number 0
PRINT Number

The CHR$ Command

The CHR$ command converts a decimal number that represents an ASCII character into a single character string. Remember the ASC command? The CHR$ command performs the opposite operation—that is, it converts numbers to characters, rather than characters to numbers. The syntax for this command is CHR$(number), where number is the decimal integer of the ASCII value you want to convert to a string. This command returns the desired ASCII character as a string.

REM this will return the value of "A"
MyString$ = CHR$(65)
REM this will print "A"
PRINT MyString$

String Manipulation Commands

Now you have a long list of commands to use with strings, but what can you actually do with them? Strings are pretty complicated, yet simple at the same time. However, to understand the next series of commands, you will need a better understanding of strings.

As I said before, a string is a collection of characters. You can envision a string as an array of characters. See Figure 4.1 for an example of a string broken into an array of characters.

click to expand
Figure 4.1: This illustration shows how a string is represented in computer memory.

Now that you understand what a string is made of, the next series of commands will make strings a lot more interesting. They are string manipulation commands, which means you can use them to get information from strings.

The LEN Command

The LEN command returns the number of characters in a string, which is quite useful when used with other string manipulation commands. The LEN command syntax is LEN(String), where String is the string for which you want to compute the length.

Many string manipulation commands require you to know how large or small a string is before you manipulate it. If you do not know the length of a string, you could end up going outside the scope of the string and generating an error. For instance, suppose you want to display a message on the right side of the screen, but you don't know how long the string will be. Suppose you are displaying the mouse position, like this:

MOUSE X/Y = (539,290)

Because the mouse could be up at (0,0) on the screen, the length of the message will change depending on the mouse position. The LEN command is useful for computing the length of a message right before it is displayed on the screen.

REM this will return the value of 17
Number = LEN("This is a string!")
REM this will print a 17
PRINT Number

The MID$ Command

The MID$ command lets you retrieve any character from the middle of a string (and also from the start or end of the string). All you need to know for this command is the position of the letter in your string. The MID$ command syntax is MID$(String, Number), which returns a string of the character found at the position in the string indicated by number.

REM this will set MyString$ to "This is my string!"
MyString$ = "This is my string!"
REM this will print a "T"
PRINT MID$(MyString$, 1)
REM this will print a "s"
PRINT MID$(MyString$, 12)

The RIGHT$ Command

The RIGHT$ command returns the rightmost set of characters in a string, which means that you can find out the last five characters of the "Hello World" string. (This would be "World," of course.) The RIGHT$ command syntax is RIGHT$(String, Number), which returns a string containing the rightmost letters up to the number specified.

MyString$ = "This is my string!"
REM this will print "string!"
PRINT RIGHT$(MyString$,7)
REM this will print a "ing!"
PRINT RIGHT$(MyString$,4)

The LEFT$ Command

The LEFT$ command is the exact opposite of the RIGHT$ command. That is, it returns the leftmost set of characters in a string. In the "Hello World" string, the first five characters are "Hello." The LEFT$ command syntax is LEFT$(String, Number), which returns a string containing the leftmost letters up to the number specified.

MyString$ = "This is my string!"
REM this will print a "This"
PRINT LEFT$(MyString$,4)
REM this will print a "This is my"
PRINT LEFT$(MyString$,10)

The STR$ Command

The STR$ command converts any integer into a string. This is a good command for converting numbers read from a file into strings. The STR$ command syntax is STR$(integer value), where integer value is returned as a string instead of an integer.

REM this will print a "4"
PRINT STR$(4)
REM this will print a "45"
PRINT STR$(45)


Displaying Text on the Screen

Now you've learned several commands for manipulating strings. The only thing that's lacking is some way to display the strings in a more precise manner than using PRINT. DarkBASIC provides a few commands to display text on the screen. However, before I jump into the text-printing commands, I'd like to talk for a minute about the computer screen.

First you need to understand what makes up the objects on a computer screen. The display is made up of thousands of small picture elements (also called pixels). A pixel is simply the smallest point on a display screen, comprised of red, green, and blue elements. Each of these elements can be lit independently and to a fine degree of luminosity, providing a mighty range of colors. In later chapters I'll show you how to set the video mode used by DarkBASIC programs. DarkBASIC runs at 640480 in 16-bit color mode by default, but it is capable of displaying any resolution that your video card supports. This means you can write high-resolution 3D games!

The pixels on the screen are arranged in rows and columns. If your screen is set to 1024768, there are 1024 pixels from left to right across the screen, and 768 rows of those 1024 pixels. As you can imagine, that adds up to a ton of pixels! To better understand this concept, take a look at Figure 4.2. Like a sheet of graph paper, the screen is represented by columns and rows described as X and Y, respectively.

click to expand
Figure 4.2: The display screen is made up of many rows and columns of picture elements (pixels).

The next few commands will help you position strings on the screen. Some commands will simply print text in a pseudo-graphics mode, while others will let you draw text anywhere on the screen.

The PRINT Command

We have already gone over the PRINT command, but it is good to review it. The PRINT command just prints whatever string or series of strings you want on the screen. It starts at the top and works its way down to the bottom. The PRINT command syntax is PRINT "string to print", where string to print is displayed on the screen. Remember that if you want to print anything other than a variable after a PRINT command, you must enclose it in quotes. Figure 4.3 shows the output from a program called PrintTest that demonstrates how to use the PRINT command. You will find this program on the CD in the SourcesChapter04PrintTest folder.

click to expand
Figure 4.3: The PrintTest program demonstrates some uses for the PRINT command.

REM ---------------------------------
REM Beginner's Guide To DarkBASIC Game Programming
REM Copyright (C)2002 Jonathan S. Harbour and Joshua R. Smith
REM Chapter 4 - PrintTest Program
REM ---------------------------------

PRINT "This is the PrintTest program"

REM this will print a "The time is " followed by the current time.
TheTime$ = "The Time is " + GET TIME$()
PRINT TheTime$
WAIT KEY

The TEXT Command

The TEXT command is just like the PRINT command but with a twist. The TEXT command takes two additional arguments at the beginning of the command—the X and Y position at which you want to print the text. This allows you to place text anywhere you want on the screen. The syntax for the TEXT command is TEXT X, Y, String.

Figure 4.4 shows the output from the TextTest program, which follows. You will find this program on the CD in the SourcesChapter04TextTest folder.

click to expand
Figure 4.4: The TextTest program demonstrates how to use the TEXT command.

REM this will print a "My Text is here" a little bit off set from the
REM upper left of the screen
TEXT 100,100, "DarkBASIC is fun!"

REM this will print a "The time is " followed by the current time
REM in the top center of the screen.
TheTime$ = "The Time is " + GET TIME$()
TEXT 300, 50, TheTime$
WAIT KEY

The CENTER TEXT Command

The CENTER TEXT command is just like the TEXT command but again with a twist. This command centers whatever you want to print around the X and Y positions that you enter. If you want to print someone's name on the top of the screen but you do not want to figure out the spacing, this command is for you. The syntax for the CENTER TEXT command is CENTER TEXT X, Y, String, where the X and Y values must be integers.

REM This will print "DarkBASIC is awesome!" centered on the screen.
Message$ = "DarkBASIC is awesome!"
CENTER TEXT 320, 240, Message$

The SET CURSOR Command

The SET CURSOR command is one of my favorites. There is a similar command in GWBASIC called locate. This command places the cursor anywhere you want on the screen. The TEXT command is simply this command plus a PRINT command. The syntax of the SET CURSOR command is SET CURSOR X, Y, where the X and Y values must be integers. Figure 4.5 shows the output from the CursorTest program, which follows. You will find this program on the CD in the SourcesChapter04CursorTest folder.

click to expand
Figure 4.5: The CursorTest program demonstrates how to use the SET CURSOR command.

REM --------------------------------
REM Beginner's Guide To DarkBASIC Game Programming
REM Copyright (C)2002 Jonathan S. Harbour and Joshua R. Smith
REM Chapter 4 - CursorTest Program
REM --------------------------------

REM This will print a message at the top left
SET CURSOR 10, 100
PRINT "Who ya gonna call?"

REM This will print a message at the top right
SET CURSOR 500, 100
PRINT "DarkBASIC!"
WAIT KEY

Font Commands

Now I have covered the basics of displaying text on the screen, but that text sure is ugly. In a word processing application, you have a choice of many different fonts, sizes, and styles. Well, DarkBASIC offers that to you as well. It provides a variety of commands to change the appearance, size, and other aspects of any text printed. I will not cover all of the commands in this chapter, but rest assured I will cover them in a future chapter.

Something to note about the text font commands: They only work with the TEXT or CENTER TEXT command. They will not work with the PRINT command, so if you want to change the appearance of your font, make sure you use the TEXT command.

Changing the Text Output Font and Size

The default font used to output text in DarkBASIC is the "system" font, which is monospaced and good for general-purpose messages. However, DarkBASIC gives you access to all the fonts installed on your PC, including TrueType fonts such as Times New Roman and Arial. You use the SET TEXT FONT command to change the font. The syntax of the command is SET TEXT FONT "Font Name".

SET TEXT SIZE is a complementary command to change the font size that is almost always used with SET TEXT FONT. The command sets the point size of the font, with common values of 10, 12, 14, 16, and 18, to name a few. Just like in a word processor, this command defines how large the text will appear on the screen. The average font size is usually 12 points. The syntax for the SET TEXT SIZE command is SET TEXT SIZE Value.

Figure 4.6 shows a comparison of different point sizes as output by the FontTest program that follows. You will find this program on the CD in the SourcesChapter04FontTest folder.

click to expand
Figure 4.6: The FontTest program demonstrates how to change the font type and size.

REM --------------------------
REM Beginner's Guide To DarkBASIC Game Programming
REM Copyright (C)2002 Jonathan S. Harbour and Joshua R. Smith
REM Chapter 4 - FontTest Program
REM --------------------------

REM Set font to Arial 18
SET TEXT FONT "Arial"
SET TEXT SIZE 18
TEXT 100,100, "This font is Arial 18"

REM Set font to Times New Roman 24
SET TEXT FONT "Times New Roman"
SET TEXT SIZE 24
TEXT 100,200, "This font is Times New Roman 24"
WAIT KEY

Returning the Current Font Name

There might come a situation in which you need to know the name of the current font used for text output. To return the font name as a string, you use the TEXT FONT$ command, which has the format Font Name = TEXT FONT$(). Here is a sample snippet of code that shows how you might use the TEXT FONT$ command.

FontName$ = TEXT FONT$()
PRINT "The current font in use is "; FontName$

Font Size Commands

In DarkBASIC, size does matter. If the print is too small, no one will be able to read it; if it is too large, it won't fit on the screen. Therefore, you need to define the size of your fonts.

The TEXT SIZE Command

The TEXT SIZE command is like the TEXT FONT$ command. However, instead of returning the name of the font in use, it returns the size of the font as an integer. The syntax of the TEXT SIZE command is Font Size = TEXT SIZE(). The following sample snippet of code shows you how you might use the TEXT SIZE command.

FontSize = TEXT SIZE()
PRINT "The current font size is "; FontSize

The TEXT WIDTH Command

The TEXT WIDTH command can be very helpful at times. It gives you the width of a string in pixels, as it would appear on the screen. Remember, a string is made up of characters. The LEN command tells you how many characters are in a string, but font types and sizes vary widely when printed on the screen. That's where the TEXT WIDTH command is useful. For example, suppose you want to right-justify a string at the right edge of the screen. Without knowing the exact width of the string, you wouldn't be able to position it precisely without trial and error. The syntax for the TEXT WIDTH command is Width Value = TEXT WIDTH(String).

Here is a short program that will center a text message on the screen.

MyText$ = "DarkBASIC is a great programming language."
CurrentWidth = TEXT WIDTH(MyText$)
TEXT 320 - CurrentWidth / 2, 235, MyText$

The TEXT HEIGHT Command

The TEXT HEIGHT command returns the height of the string you want to print in pixels. This gives you the Y size of the text, whereas the TEXT WIDTH command gave you the X size of the text. The syntax for the TEXT HEIGHT command is Height Value = TEXT HEIGHT(String).

The BounceText Program

To demonstrate the font width and height commands, I've written a short program called BounceText, which is shown in Figure 4.7. The BounceText program is somewhat advanced at this time, using many commands that I have not yet discussed. However, I want to give you a glimpse of things to come and help to challenge your creativity by delving into the unknown from time to time. You might find this program intriguing enough that you will want to experiment with it on your own by changing values, such as the speed at which the text moves across the screen. You will find this program on the CD in the SourcesChapter04BounceText folder.

click to expand
Figure 4.7: The BounceText program demonstrates how to use the font width and height commands.

REM ---------------------------------
REM Beginner's Guide To DarkBASIC Game Programming
REM Copyright (C)2002 Jonathan S. Harbour and Joshua R. Smith
REM Chapter 4 - Bounce Text Program
REM ---------------------------------

REM Initialize the program
MyText$ = "DarkBASIC Rules!"
SET TEXT FONT "Times New Roman"
SET TEXT SIZE 72
INK RGB(255,255,255), RGB(10,10,10)
Width = TEXT WIDTH(MyText$)
Height = TEXT HEIGHT(MyText$)
CurrentX = 320 - Width / 2
CurrentY = 240 - Height / 2
SpeedX = 2
SpeedY = -2
REM Manually control the screen
SYNC ON
DO

 REM Clear the screen
 CLS

 REM Update X position
 CurrentX = CurrentX + SpeedX
 IF CurrentX + Width > 635 THEN SpeedX = -4
 IF CurrentX < 5 THEN SpeedX = 4

 REM Update Y position
 CurrentY = CurrentY + SpeedY
 IF CurrentY + Height > 475 THEN SpeedY = -4
 IF CurrentY < 5 THEN SpeedY = 4

 REM Display the text message
 TEXT CurrentX, CurrentY, MyText$

 REM Update the screen
 SYNC
LOOP
END

Text Style Commands

Now we come to the fashionable part of the text commands—the text style commands. These commands allow you to alter the appearance of the text once again. You can change the text to bold, italic, or both.

Setting the Font to Normal

The SET TEXT TO NORMAL command sets the font style to normal, which removes any bold or italic font styles for the next text output command. Here is a snippet of code that demonstrates how to use this command:

SET TEXT FONT "Arial"
SET TEXT TO NORMAL
SET TEXT SIZE 16
TEXT 100,100, "This text is normal"

Setting the Font to Italic

The SET TEXT TO ITALIC command sets all the text that follows it to italics. That means your text will not be bold, but it will be in italics. Here is a snippet of code that demonstrates how to use this command:

SET TEXT FONT "Arial"
SET TEXT TO ITALIC
SET TEXT SIZE 16
TEXT 100,100, "This text is italic"

The SET TEXT TO BOLD Command

The SET TEXT TO BOLD command sets all the text that follows it to bold. That means your text will not be in italics, but it will be in bold. Here is a snippet of code that demonstrates how to use this command:

SET TEXT FONT "Arial"
SET TEXT TO BOLD
SET TEXT SIZE 16
TEXT 100,100, "This text is bold"

The SET TEXT TO BOLDITALIC Command

The SET TEXT TO BOLDITALIC command sets all the text that follows it to bold italics. That means your text will be in both bold and italics. Here is a snippet of code that demonstrates how to use this command:

SET TEXT FONT "Arial"
SET TEXT TO BOLDITALIC
SET TEXT SIZE 16
TEXT 100,100, "This text is bold and italic"

Determining the Current Text Style

The TEXT STYLE command returns an integer indicating the current font style in use (normal, italic, bold, or bold italic). The command returns a number from 0 to 3, corresponding to the current text style. Table 4.1 describes the four values returned by this command.

Table 4.1: Text Style Values

Return Value

Style

0

Normal

1

Bold

2

Italic

3

Bold and italic

I have written a short program that demonstrates how to use the TEXT STYLE command. The program is called TextStyles, and it is shown in Figure 4.8. You will find this program on the CD in the SourcesChapter04TextStyles folder.

click to expand
Figure 4.8: The TextStyles program demonstrates how to use the TEXT STYLE command.

REM ----------------------------
REM Beginner's Guide To Game Programming With DarkBASIC
REM Copyright (C)2002 Jonathan S. Harbour and Joshua R. Smith
REM Chapter 4 - TextStyles Program
REM ----------------------------

REM Create an array
DIM Styles$(4)
Styles$(0) = "NORMAL"
Styles$(1) = "BOLD"
Styles$(2) = "ITALIC"
Styles$(3) = "BOLD ITALIC"

REM Set the font name
SET TEXT FONT "Times New Roman"
SET TEXT SIZE 36

REM Print out the text style strings
SET TEXT TO NORMAL
TEXT 0, 0, "This text is " + Styles$( TEXT STYLE() )
SET TEXT TO BOLD
TEXT 0, 40, "This text is " + Styles$( TEXT STYLE() )
SET TEXT TO ITALIC
TEXT 0, 80, "This text is " + Styles$( TEXT STYLE() )
SET TEXT TO BOLDITALIC
TEXT 0, 120, "This text is " + Styles$( TEXT STYLE() )
WAIT KEY
END

Text Transparency

One last thing I need to cover for text appearance is text transparency. That is, what does the background of the text look like? Not the letters per se, but behind the letters. Each letter printed in DarkBASIC is really a square. Anything that is not part of a letter is black or whatever color you specify it to be. However, if you want to put text over a picture, you definitely do not want its background to be black. DarkBASIC has some commands to control transparency.

Transparency defines how much you can see through something. A piece of glass is transparent. Opacity refers to something that is completely non- transparent and opaque.

click to expand
Figure 4.9: Transparency versus opacity

The SET TEXT OPAQUE Command

The SET TEXT OPAQUE command sets the text background to the current color specified in the INK command (which is covered in Chapter 9, "Basic Graphics Commands"). If text is printed over any pictures, it will look like a solid rectangle with words in the middle.

REM Set the background color to red
CLS RGB(255,0,0)
REM Set the font color to white on black
INK RGB(255,255,255), RGB(0,0,0)
REM Display the opaque text
SET TEXT OPAQUE
TEXT 100,100, "This Text is Opaque"

The SET TEXT TRANSPARENT Command

The SET TEXT TRANSPARENT command is the exact opposite of the SET TEXT OPAQUE command. That is, it sets the text background to clear so whatever is behind it can be seen.

REM Set the background color to red
CLS RGB(255,0,0)
REM Set the font color to white on black
INK RGB(255,255,255), RGB(0,0,0)
REM Display the transparent text
SET TEXT TRANSPARENT
TEXT 100,100, "This Text is Transparent"

The TEXT BACKGROUND TYPE Command

The TEXT BACKGROUND TYPE command returns the background type of the current font—either opaque or transparent. Like the TEXT STYLE command, TEXT BACKGROUND TYPE returns an integer. If the background is opaque, a 0 is returned; for transparent, a 1 is returned. The syntax for the command is Background Value = TEXT BACKGROUND TYPE().


The Gab Lib Game

I have talked a lot about strings and text in this chapter, so what better example than a Mad Lib game that uses a lot of strings? In case you have never heard of a Mad Lib, the concept is rather funny. You ask the user to type in various nouns, verbs, adjectives, and so on, and then you fill in the words of a story using the user's input.

Running the Gab Lib Game

Take a look at Figure 4.10, which shows the Gab Lib program asking the user for input.

click to expand
Figure 4.10: The first part of the Gab Lib game asks the user to type in parts of speech that are then used to create a story.

Figure 4.11 shows the final output of the Gab Lib game, after it has assembled the user input into a story.

click to expand
Figure 4.11: The second part of the Gab Lib game uses the user input to print the story.

The Source Code for Gab Lib

It takes a while to type in the source code that make up the Gab Lib game, but the end result is worth it! However, if you're in a hurry you can just load the program from the CD-ROM under SourcesChapter04GabLib. The compiled program is also in that folder; if you want to run it directly from Windows Explorer, you can double-click GabLib.exe.

The best way to learn is by practicing, so I recommend typing in the source code for the Gab Lib game. You will learn much faster because the mental connections are strengthened through hand-eye coordination, which is what it takes to see a command before typing it into DarkBASIC. Regardless of the method of input, you run the game by pressing F5 or selecting Execute from the Run menu.

REM ---------------------------------
REM Beginner's Guide To DarkBASIC Game Programming
REM Copyright (C)2002 Jonathan S. Harbour and Joshua R. Smith
REM Chapter 4 - MadLib Program
REM ---------------------------------

SET TEXT FONT "Arial"
CLS
PRINT
PRINT "Welcome to your first Gab Lib."
PRINT
PRINT "Please guess the answer to the questions"
PRINT "to create your own unique, humorous story."
PRINT

REM Ask questions
INPUT "1. Enter a female name: ", NAME$
INPUT "2. Enter a skin problem: ", PROBLEM$
INPUT "3. Enter a body part: ", BODYPART1$
INPUT "4. Enter a negative emotion: ", EMOTION$
INPUT "5. Enter a plural noun: ", NOUN1$
INPUT "6. Enter a present tense verb: ", VERB1$
INPUT "7. Enter a time of day: ", TIME$
INPUT "8. Enter an object you can read: ", NOUN2$
INPUT "9. Enter a dollar amount: ", NUMBER$
INPUT "10. Enter a type of medicine: ", NOUN3$
INPUT "11. Enter a present tense verb: ", VERB2$
INPUT "12. Enter a positive emotion: ", EMOTION2$
INPUT "13. Enter a color: ", COLOR$
INPUT "14. Enter a noun: ", NOUN4$
INPUT "15. Enter a present tense verb: ", VERB3$
INPUT "16. Enter a past tense verb: ", VERBP$
INPUT "17. Enter a noun: ", NOUN5$
INPUT "18. Enter a present tense verb: ", VERB4$

REM Prepare to display story
SET TEXT SIZE 16
CLS

REM Calculate line height
offset = 480 / 30

REM First paragraph
a$ = "Once upon a time there was a princess named " + NAME$
CENTER TEXT 320, offset, a$
a$ = "who had a " + PROBLEM$ + " on her " + BODYPART1$ + "."
CENTER TEXT 320, offset * 2, a$
a$ = "This " + PROBLEM$ + " was so unsightly and caused"
CENTER TEXT 320, offset * 3, a$
a$ = "Princess " + NAME$ + " much " + EMOTION$ + "."
CENTER TEXT 320, offset * 4, a$
a$ = "For you see, none of the " + NOUN1$ + " in the"
CENTER TEXT 320, offset * 5, a$
a$ = "kingdom could stand to " + VERB1$ + " at her."
CENTER TEXT 320, offset * 6, a$

REM Second paragraph
a$ = "One night, a little after " + TIME$ + ", Princess " + NAME$
CENTER TEXT 320, offset * 8, a$
a$ = "was sitting in her room reading a " + NOUN2$ + "."
CENTER TEXT 320, offset * 9, a$
a$ = "The "+NOUN2$+" was about the cure for " + PROBLEM$ + "s."
CENTER TEXT 320, offset * 10, a$
a$ = "It had an address to mail " + NUMBER$ + " dollars for"
CENTER TEXT 320, offset * 11, a$
a$ = "a " + NOUN3$ + " to " + VERB2$ + " the " + PROBLEM$ + "."
CENTER TEXT 320, offset * 12, a$
a$ = "She sent off for the " + NOUN3$ + "."
CENTER TEXT 320, offset * 13, a$

REM Third paragraph
a$ = "The day the " + NOUN3$ + " arrived, Princess " + NAME$
CENTER TEXT 320, offset * 15, a$
a$ = "was " + EMOTION2$ + "."
CENTER TEXT 320, offset * 16, a$
a$ = "She ripped open the " + NOUN3$ + " and looked inside."
CENTER TEXT 320, offset * 17, a$
a$ = "There was some " + COLOR$ + " " + NOUN4$ + " in a"
CENTER TEXT 320, offset * 18, a$
a$ = "tube in the " + NOUN3$ + "."
CENTER TEXT 320, offset * 19, a$
a$ = "The instructions said to " + VERB3$ + " the " + NOUN4$
CENTER TEXT 320, offset * 20, a$
a$ = " on the " + PROBLEM$ + " and it would disappear."
CENTER TEXT 320, offset * 21, a$

REM Fourth paragraph
a$ = "Princess " + NAME$ + " " + VERBP$ + " the " + NOUN4$ + " all over"
CENTER TEXT 320, offset * 22, a$
a$ = "her " + BODYPART1$ + " and poof! her " + BODYPART1$ + " " CENTER TEXT 320, offset * 23, a$
a$ = "disappeared."
CENTER TEXT 320, offset * 24, a$
a$ = "The moral of this " + NOUN5$ + " is to follow directions"
CENTER TEXT 320, offset * 25, a$
a$ = "or you will not be able to " + VERB4$ + " the consequences."
CENTER TEXT 320, offset * 26, a$

REM Wait for user to press a key
WAIT KEY
END


Summary

This chapter was dedicated to the study of strings, the most useful and versatile data type available in DarkBASIC. There are numerous text formatting, manipulation, and display commands built into DarkBASIC for manipulating strings. After you learned about string handling, you discovered how to display strings on the screen with user-definable fonts, which are necessary because DarkBASIC programs run in graphical full-screen mode. Finally, you put your newfound knowledge of strings to use in the Gab Lib game.


Quiz

The chapter quiz will help reinforce the material you have learned in this chapter and will provide feedback on your progress. For the answers to the quiz, refer to Appendix A, "Answers to the Chapter Quizzes."

1.

Which symbol do you use to add two strings?

  1. =
  2. +
  3. /

b

2.

Which command will convert a string to an integer?

  1. STR$
  2. VAL
  3. STRINGTONUM
  4. NUMBER$

b

3.

Which command centers text in a given location?

  1. CENTER TEXT
  2. TEXT CENTER
  3. CENTER PRINT
  4. TEXT

a

4.

What does the SET TEXT TRANSPARENT command do?

  1. Sets the background of the text to transparent
  2. Makes the text invisible
  3. Displays the word "Transparent" on the screen
  4. None of the above

a

5.

What is the result of the following command: LEN("This is a string")?

  1. 42
  2. 19
  3. 16
  4. 02

c

6.

What would the following source code print on the screen?

MyString$ = "This is my string!"
PRINT RIGHT$(MyString$,7)
  1. This is
  2. is my
  3. my string
  4. string!

d

7.

What would the following source code print on the screen?

MyString$ = "This is my string!"
PRINT LEFT$(MyString$,6)
  1. This is
  2. is my
  3. my string
  4. string!

a

8.

Which symbol attached to a variable denotes it as a string?

  1. $
  2. *
  3. %
  4. #

a

9.

Which value would TEXT STYLE] return if you were in bold mode?

  1. 0
  2. 1
  3. 2
  4. 3

b

10.

Which command returns the font of the text being displayed?

  1. FONT OF TEXT
  2. FONT$
  3. TEXT FONT$
  4. SET FONT$

c

Answers

1.

B

2.

B

3.

A

4.

A

5.

C

6.

D

7.

A

8.

A

9.

B

10.

C




Beginner's Guide to DarkBASIC Game Programming
Beginners Guide to DarkBASIC Game Programming (Premier Press Game Development)
ISBN: 1592000096
EAN: 2147483647
Year: 2002
Pages: 203
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net