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.

Step 9: Swing the Sword <strong>with</strong> StabDragon()<br />

Now, add the last subroutine for the knight in Listing 10-16.<br />

1 Sub StabDragon<br />

2 If (Math.GetRandomNumber(2) = 1) Then<br />

3 Shapes.SetText(msgText, "You killed the dragon! You marry the princess <br />

and 7 dwarves.")<br />

4 GameOver()<br />

5 Else<br />

6 Shapes.SetText(msgText, "Your sword missed! Good one, Lance-a-Little!")<br />

7 <strong>Program</strong>.Delay(2000) ' To read the message<br />

8 EndIf<br />

9 EndSub<br />

Listing 10-16: Stabbing the dragon<br />

You randomly pick the number 1 or 2. If the number is 1 (line 2), the<br />

knight hits the dragon and the game ends (lines 3–4). If the knight misses,<br />

you tell the knight that he missed (line 6), delay the program for your user<br />

<strong>to</strong> read the message (line 7), and return <strong>to</strong> the ProcessChoice() subroutine.<br />

Step 10: Breathe Fire<br />

If the knight didn’t kill Draggy and end the game, the main program<br />

calls DragonFire() <strong>to</strong> give the dragon a fair fight. Add Listing 10-17 <strong>to</strong> your<br />

program.<br />

1 Sub DragonFire<br />

2 Shapes.SetText(msgText, "The dragon ignited his fire. The Pokemon run.")<br />

3 Shapes.HideShape(dragon1Img)<br />

4 Shapes.ShowShape(dragon2Img)<br />

5 <strong>Program</strong>.Delay(1000)<br />

6 Shapes.HideShape(dragon2Img)<br />

7 Shapes.ShowShape(dragon1Img)<br />

8<br />

9 If (Math.GetRandomNumber(2) = 1) Then ' Knight is hit<br />

10 If (shield = 0) Then ' Shield is damaged<br />

11 Shapes.SetText(msgText, "The dragon's fire BURNINATED you!")<br />

12 GameOver()<br />

13 Else<br />

14 shield = shield - 1<br />

15 Shapes.SetText(shieldText, shield)<br />

16 Shapes.SetText(msgText, "You're hit! Your shield became weaker. Use <br />

the force!")<br />

17 EndIf<br />

18 Else<br />

19 Shapes.SetText(msgText, "The fire missed you! Aunt Mildred could've used <br />

your luck.")<br />

20 EndIf<br />

21 EndSub<br />

Listing 10-17: The dragon breathing fire on Good Knight<br />

146 Chapter 10

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

Saved successfully!

Ooh no, something went wrong!