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.

Then you set the graphics window’s size (lines 5–7), font size (line 8),<br />

and position (line 9). Next, you position the text window <strong>to</strong> appear <strong>to</strong> the<br />

right of the graphics window (lines 11–12). After drawing the background<br />

image (lines 14–15), you create and position the text shapes that you’ll use<br />

<strong>to</strong> show all the numbers on the game’s UI (lines 19–26). Then you load<br />

and position the images for the knight, dragon, and arrow (lines 29–35).<br />

Finally, you hide the images for the firing dragon and the arrow because<br />

they aren’t needed at this time (lines 37–38): you’ll show these images when<br />

Draggy breathes fire and Good Knight shoots the arrow.<br />

When we built this program, we figured out where <strong>to</strong> place the text and<br />

images (<strong>with</strong> the numbers we’re using) on the background’s image by using<br />

a trial-and-error method (we guessed and tweaked it until we got it right).<br />

You’ll likely need <strong>to</strong> do that when designing your own UIs for your awesome<br />

future games.<br />

Step 3: Add a Bit of Chance<br />

Next, you need <strong>to</strong> add some luck <strong>to</strong> the game. Each time we run the game,<br />

we want Good Knight <strong>to</strong> get a different number of arrows, be a random<br />

distance away from the dragon, and have a different shield strength. To do<br />

this, add the NewGame() subroutine in Listing 10-9 <strong>to</strong> your program.<br />

1 Sub NewGame<br />

2 dist = 9 + Math.GetRandomNumber(10) ' 10 <strong>to</strong> 19<br />

3 arrows = Math.Floor(0.4 * dist) ' 4 <strong>to</strong> 8<br />

4 shield = Math.Floor(0.4 * dist) ' 4 <strong>to</strong> 8<br />

5 moveStep = 280 / dist ' Knight's move in pixels<br />

6 EndSub<br />

Listing 10-9: Setting up a new game<br />

In line 2, you add 9 <strong>to</strong> a random number between 1 and 10, which sets<br />

the distance, dist, between 10 and 19. This is the number of steps Good<br />

Knight has <strong>to</strong> take <strong>to</strong> get <strong>to</strong> Draggy. Next, you set the number of arrows<br />

as 40 percent of the distance (line 3). The farther the knight is from the<br />

dragon, the more arrows he’ll have. In line 4, you set the strength of the<br />

knight’s shield—again, as a fraction of his distance.<br />

Let’s think about the moveStep line a little. The width of the background<br />

image is 480 pixels. The width of the dragon is 100 pixels, and the width of<br />

the knight is 100 pixels. When we place the dragon and the knight on the<br />

background, the distance from the dragon’s right edge <strong>to</strong> the knight’s left<br />

edge is 280 pixels. So every time Good Knight moves forward, we’ll move<br />

his image <strong>to</strong> the left by 280 / dist pixels.<br />

TIP<br />

You can change the fraction in lines 3 and 4 from 0.4 <strong>to</strong> a different value <strong>to</strong> make<br />

the game easier or harder. After you complete the game, try changing the fraction and<br />

play the game a couple of times!<br />

Solving Problems <strong>with</strong> Subroutines 141

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

Saved successfully!

Ooh no, something went wrong!