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.

<strong>to</strong> become a value other than 0 (this is called polling; it’s similar <strong>to</strong> asking<br />

“Are we there yet?” during a long trip). When choice1 becomes a value other<br />

than 0 (line 13), the body of the If block is executed (lines 14–19). We draw<br />

image W3 <strong>to</strong> show a blank result (lines 14–15). Next, we set the computer’s<br />

choice, choice2, <strong>to</strong> a random value between 1 and 3 (line 16). Then we call<br />

SwitchImages() <strong>to</strong> show the images that correspond <strong>to</strong> choice1 and choice2<br />

(line 17). Then we call ShowWinner() <strong>to</strong> show the result of this round of the<br />

game (line 18). Finally, we set choice1 back <strong>to</strong> 0 <strong>to</strong> tell the OnMouseDown()<br />

subroutine that the main loop is ready for another round of the game<br />

(line 19).<br />

Next, you’ll add each subroutine one at a time.<br />

Step 2: Add the MouseDown Handler<br />

Now let’s handle the MouseDown event <strong>to</strong> figure out the player’s choice. Add<br />

the OnMouseDown() subroutine in Listing 14-5 <strong>to</strong> the bot<strong>to</strong>m of the program.<br />

1 Sub OnMouseDown<br />

2 If (choice1 = 0) Then ' Ready for another round<br />

3 y = GraphicsWindow.MouseY ' Vertical click position<br />

4 If ((y > 80) And (y < 120)) Then ' Within range<br />

5 x = GraphicsWindow.MouseX ' Horizontal click<br />

6 If ((x > 40) And (x < 80)) Then ' Rock<br />

7 choice1 = 1<br />

8 ElseIf ((x > 110) And (x < 150)) Then ' Paper<br />

9 choice1 = 2<br />

10 ElseIf ((x > 175) And (x < 215)) Then ' Scissors<br />

11 choice1 = 3<br />

12 EndIf<br />

13 EndIf<br />

14 EndIf<br />

15 EndSub<br />

Listing 14-5: Checking the choice the user clicked<br />

<strong>Small</strong> <strong>Basic</strong> calls this subroutine when the player clicks anywhere in the<br />

graphics window. First, the subroutine checks the value of choice1 (line 2).<br />

If choice1 is 0, the subroutine checks where the player clicked <strong>to</strong> see whether<br />

they clicked one of the three but<strong>to</strong>ns. If choice1 is not 0, that means the<br />

main loop is still processing the player’s last choice, so the subroutine just<br />

ignores the mouse click. This way your game won’t get confused if the<br />

player clicks all over the place.<br />

To see whether the player clicks one of the three image but<strong>to</strong>ns, the<br />

subroutine checks the vertical position of the click (line 4). If it’s <strong>with</strong>in the<br />

range of the images, the subroutine checks the horizontal position (line 6).<br />

The If/ElseIf ladder then compares the horizontal position <strong>with</strong> the left and<br />

right edges of each image and sets choice1 accordingly (lines 6–12).<br />

202 Chapter 14

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

Saved successfully!

Ooh no, something went wrong!