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.

1 step away). If the arrow hits the dragon, it’ll slay him instantly! With the<br />

sword, the knight has a 50-50 chance of slaying the dragon (but only when<br />

he’s close enough). If Good Knight slays Draggy, he’ll become Knight of the<br />

Year, win his very own dance party, and get his picture on the castle wall.<br />

Once Good Knight makes his move, Draggy breathes his flames at the<br />

knight. If he hits the knight, he’ll weaken the knight’s shield. When the<br />

shield loses its strength, the knight is defenseless. After this point, if the<br />

dragon’s fire hits the knight, it’ll burninate him! The entire city will be<br />

under the attack of the merciless, ferocious dragon. Game over!<br />

The game uses five images that you can find in this chapter’s folder:<br />

the background image (your battlefield), two images for the dragon (one<br />

image shows the dragon’s fire), the knight’s image, and an image of an<br />

arrow. Follow steps 1–10 <strong>to</strong> make a fun dragon game!<br />

Step 1: Open the Startup File<br />

Open the Dragon_Incomplete.sb file from the code folder for this chapter.<br />

This file contains the code in Listing 10-7 and has empty placeholders for<br />

your subroutines. You’ll add the code for these subroutines one step at a<br />

time. The program’s folder has all the images you need as well. It also has<br />

the complete game, Dragon.sb, in case you get stuck.<br />

1 ' Dragon_Incomplete.sb<br />

2 SetUp() ' Does one-time set up<br />

3<br />

4 NewGame() ' Sets the parameters for a new game<br />

5<br />

6 UpdateUserInterface() ' Shows values on background image<br />

7<br />

8 NextMove:<br />

9 GetChoice() ' Displays options and gets the knight's choice<br />

10<br />

11 ProcessChoice() ' Processes the user's choice<br />

12<br />

13 DragonFire() ' Now it's the dragon's turn<br />

14 Go<strong>to</strong> NextMove<br />

Listing 10-7: High-level structure of the dragon game<br />

First, you call the SetUp() subroutine (line 2) <strong>to</strong> draw the background<br />

image, create text shapes (for displaying the distance, number of arrows, and<br />

so on), and load the game’s images (dragon, knight, and arrow). Line 4<br />

calls NewGame() <strong>to</strong> set the parameters for a new game, including the knight’s<br />

arrows, shield strength, and distance from the dragon. In line 6, you call<br />

UpdateUserInterface() <strong>to</strong> update the game’s user interface (UI). Then the code<br />

goes in<strong>to</strong> a loop (lines 8–14) <strong>to</strong> manage the game. Each round, you ask the<br />

knight for his next move (line 9), process his move by calling ProcessChoice()<br />

on line 11, and then give the dragon a turn (line 13). As you’ll see in a<br />

moment, these subroutines will keep track of the game’s status and end the<br />

game when there’s a winner!<br />

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

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

Saved successfully!

Ooh no, something went wrong!