19.04.2017 Views

Learn to Program with Small Basic

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Lines 2–8 set up the GraphicsWindow object. Line 10 defines the line feed<br />

character (for appending new lines <strong>to</strong> the strings). Lines 12–22 define the<br />

rhyme array, which contains the strings for this rhyme. Note how the elements<br />

of this array relate <strong>to</strong> the boxes in Figure 18-8. Line 24 creates the Next but<strong>to</strong>n,<br />

and line 25 registers the handler for the But<strong>to</strong>nClicked event. Then the<br />

nextLine variable is set <strong>to</strong> 11 <strong>to</strong> point <strong>to</strong> the 11th element of the rhyme array,<br />

which is the first page of the s<strong>to</strong>ry (line 26), and OnBut<strong>to</strong>nClicked() is called<br />

<strong>to</strong> show the first page of the rhyme (line 27).<br />

Now we’ll add the OnBut<strong>to</strong>nClicked() subroutine in Listing 18-12. This<br />

subroutine is called when the user clicks the Next but<strong>to</strong>n.<br />

1 Sub OnBut<strong>to</strong>nClicked<br />

2 img = <strong>Program</strong>.Direc<strong>to</strong>ry + "\Page" + (12 - nextLine) + ".png"<br />

3 GraphicsWindow.DrawImage(img, 0, 0)<br />

4<br />

5 strOut = "This is "<br />

6 For N = nextLine To 11<br />

7 strOut = Text.Append(strOut, rhyme[N])<br />

8 EndFor<br />

9 GraphicsWindow.DrawText(10, 10, strOut)<br />

10<br />

11 nextLine = nextLine - 1<br />

12 If (nextLine = 0) Then<br />

13 nextLine = 11<br />

14 EndIf<br />

15 EndSub<br />

Listing 18-12: The OnBut<strong>to</strong>nClicked() subroutine<br />

Line 2 fills img <strong>with</strong> the name of the image for the current page of the<br />

rhyme. When nextLine is 11, we’ll show Page1.png (which is 12 minus 11).<br />

When nextLine is 10, we’ll show Page2.png (12 minus 10), and when nextLine<br />

is 9, we’ll show Page3.png (12 minus 9), and so on. Line 3 draws the image<br />

on the graphics window. We then build up the output string (lines 5–8).<br />

We set strOut <strong>to</strong> "This is " (line 5) and then start a loop that goes from<br />

nextLine <strong>to</strong> 11 (lines 6–8). When nextLine is 11, the loop runs one time and<br />

appends rhyme[11] <strong>to</strong> strOut. When nextLine is 10, the loop runs from 10 <strong>to</strong> 11<br />

and appends rhyme[10] and then rhyme[11] <strong>to</strong> strOut. Similarly, when nextLine<br />

is 9, the loop runs from 9 <strong>to</strong> 11 and appends rhyme[9], rhyme[10], and then<br />

rhyme[11] <strong>to</strong> strOut.<br />

When the loop ends, strOut contains the entire string for the rhyme at<br />

this stage of the s<strong>to</strong>ry. We display this string using DrawText() in line 9.<br />

Then we decrease nextLine by 1 <strong>to</strong> point <strong>to</strong> the previous element in the<br />

rhyme array (line 11). If nextLine becomes 0 (line 12), the s<strong>to</strong>ry is done, so we<br />

set it back <strong>to</strong> 11 <strong>to</strong> start over (line 13). As a result, when the user clicks the<br />

Next but<strong>to</strong>n at the last page of the s<strong>to</strong>ry, the program goes back <strong>to</strong> displaying<br />

the first page. We’ve finished the tale before it got stale!<br />

Advanced Text Magic 283

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!