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.

To move the square, we call the Move() method of the Shapes object<br />

(line 8). This method takes three arguments: the identifier of the shape we<br />

want <strong>to</strong> move and the x- and y-coordinates of the position we’re moving it<br />

<strong>to</strong>. The squares in each row have their x positions (left edge) at 1 × 20 = 20,<br />

2 × 20 = 40, 3 × 20 = 60, and so on. The squares in each column have their<br />

y positions (<strong>to</strong>p edge) at 1 × 20 = 20, 2 × 20 = 40, 3 × 20 = 60, and so on.<br />

This is why we use c * 20 and r * 20 in the call <strong>to</strong> Move().<br />

At the end of this For loop, the box matrix contains 32 unique identifiers<br />

for the 32 squares created by the Shapes object.<br />

The program then animates the squares (lines 13–18), using a nested<br />

For loop <strong>to</strong> access the rows and the columns of box. During each iteration,<br />

we ask the Shapes object <strong>to</strong> animate one square (line 15) and then pause for a<br />

short time (line 16). The Animate() method takes four arguments: the identifier<br />

of the shape we want <strong>to</strong> animate, the x- and y-coordinates of the destination,<br />

and the animation duration in milliseconds. We ask the Shapes object <strong>to</strong><br />

move each square <strong>to</strong> point (0, 0) in 1 second (1000 milliseconds).<br />

TRY IT OUT 17-2<br />

Change the program in Listing 17-2 <strong>to</strong> animate the squares by columns instead of<br />

by rows. If you’re feeling artistic, try moving the squares <strong>to</strong> create a pattern in the<br />

graphics window.<br />

Using String Indices<br />

The previous examples used integer indices <strong>to</strong> access the elements of a<br />

matrix. Our next example teaches you how use strings for indices. You’ll<br />

examine an application that keeps track of students’ scores in different<br />

subjects.<br />

Welcome <strong>to</strong> Professor Xavier’s School for Gifted Youngsters! The class<br />

has only three students right now: Scott, Jean, and Logan (the others are<br />

on an important mission). The school teaches only three subjects: math,<br />

science, and combat. Let's write a program that prompts the user <strong>to</strong> enter<br />

a student’s name and then displays the student’s average score. The complete<br />

program is shown in Listing 17-3.<br />

1 ' StudentAvg.sb<br />

2 score["Scott"]["Math"] = 92<br />

3 score["Scott"]["Science"] = 90<br />

4 score["Scott"]["Combat"] = 87<br />

5 score["Jean"]["Math"] = 85<br />

6 score["Jean"]["Science"] = 82<br />

7 score["Jean"]["Combat"] = 92<br />

8 score["Logan"]["Math"] = 85<br />

9 score["Logan"]["Science"] = 95<br />

10 score["Logan"]["Combat"] = 99<br />

11<br />

246 Chapter 17

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

Saved successfully!

Ooh no, something went wrong!