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.

3<br />

j = 1<br />

c<br />

1<br />

1<br />

2<br />

3<br />

j = 4<br />

c<br />

2<br />

2<br />

c<br />

j = 2<br />

2<br />

3<br />

1<br />

1<br />

c<br />

3<br />

j = 3<br />

Figure 13-6: How NestedLoops.sb works<br />

The output should look like this:<br />

Jump 1: Clap 1 Clap 2 Clap 3<br />

Jump 2: Clap 1 Clap 2 Clap 3<br />

Jump 3: Clap 1 Clap 2 Clap 3<br />

Jump 4: Clap 1 Clap 2 Clap 3<br />

Now let’s look at other problems where nested For loops come in handy!<br />

Tessellating for Fun<br />

In this example, an application covers the graphics window by stamping a<br />

small image over it. The complete program is shown in Listing 13-8.<br />

1 ' Stamp.sb<br />

2 GraphicsWindow.Title = "Stamp"<br />

3<br />

4 path = <strong>Program</strong>.Direc<strong>to</strong>ry<br />

5 img = ImageList.LoadImage(path + "\Trophy.ico")<br />

6<br />

7 width = ImageList.GetWidthOfImage(img) ' Width of image<br />

8 height = ImageList.GetHeightOfImage(img) ' Height of image<br />

9<br />

10 GraphicsWindow.Width = 8 * width ' 8 columns<br />

11 GraphicsWindow.Height = 3 * height ' 3 rows<br />

12<br />

13 For row = 0 To 2 ' 3 rows<br />

14 For col = 0 To 7 ' 8 columns<br />

15 GraphicsWindow.DrawImage(img, col * width, row * height)<br />

16 EndFor<br />

17 EndFor<br />

Listing 13-8: Stamping a pattern across the graphics window<br />

190 Chapter 13

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

Saved successfully!

Ooh no, something went wrong!