19.04.2017 Views

Learn to Program with Small Basic

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Think of the Shapes object as a “shape fac<strong>to</strong>ry”—a fac<strong>to</strong>ry that manufactures<br />

lines, triangles, rectangles, ellipses, and other shapes. When you ask<br />

it <strong>to</strong> create a new shape, it’ll make the shape and return an identifier. Every<br />

time you want <strong>to</strong> do something <strong>with</strong> the shape you created, you’ll pass this<br />

identifier <strong>to</strong> the Shapes object (as an argument <strong>to</strong> the method you call).<br />

We won’t cover all the methods of the Shapes object here. Instead, we’ll<br />

discuss the ones you’ll use in the next program. You’ll learn the other<br />

methods as you progress through this book.<br />

The two methods we’ll use now are AddImage() and Move(). To understand<br />

how these methods work, open the file ImageDemo.sb from this chapter’s<br />

folder. You’ll see the code shown in Listing 9-5, which moves an image.<br />

1 ' ImageDemo.sb<br />

2 path = <strong>Program</strong>.Direc<strong>to</strong>ry + "\Flower.png"<br />

3 imgID = Shapes.AddImage(path)<br />

4 Shapes.Move(imgID, 60, 20)<br />

Listing 9-5: Moving an image using the Shapes object<br />

Click the Run but<strong>to</strong>n. The output of this program is illustrated in<br />

Figure 9-7 (we added the gridlines and the numbers <strong>to</strong> show how the code<br />

works).<br />

20 60 ...<br />

20<br />

40<br />

60<br />

80<br />

...<br />

Figure 9-7: Moving the flower image<br />

Let’s assume that this program is saved <strong>to</strong> C:\Book\Ch09\ImageDemo.<br />

The imageDemo folder also contains the Flower​.png image file. The<br />

<strong>Program</strong>.Direc<strong>to</strong>ry property (line 2) points <strong>to</strong> the direc<strong>to</strong>ry C:\Book\Ch09\<br />

ImageDemo, which contains the executable program (the .exe file). Line 2<br />

uses the + symbol <strong>to</strong> append two things <strong>to</strong> the direc<strong>to</strong>ry: a slash (\) and the<br />

image filename (Flower.png). When the program runs line 2, the path variable’s<br />

given the full file path (C:\Book\Ch09\ImageDemo\Flower.png).<br />

Line 3 calls the AddImage() method and passes the path variable as an<br />

argument. This method loads the image from the file and returns an identifier<br />

of the loaded image; this identifier is saved in a variable named imgID.<br />

An identifier is like a tag that the Shapes object uses <strong>to</strong> keep track of the<br />

shapes it creates (for example, "Image1", "Rectangle3", "Line100", and so on).<br />

The loaded image is displayed in the upper-left corner of the graphics<br />

window.<br />

Using Decisions <strong>to</strong> Make Games 121

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

Saved successfully!

Ooh no, something went wrong!