Simple tutorial to help with the basics

YakraXIII
Posted: 7/23/2012 2:29:33 AM
(post 3 of 4)

Next thing we need to do is check the player's response. Because we're looking for three different buttons (A, B, and START), this will be a little more complicated than last time, but the basic principle is the same: keep checking the buttons until the player has pressed one of the ones we're looking for.

First, we should get the button codes for the three buttons. From the manual we find the codes are 16 for A, 32 for B, and 1024 for START. Add these lines of code to setup our loop:

@INPUTLOOP
IF BTRIG() == 16 THEN GOTO @ABUTTON
IF BTRIG() == 32 THEN GOTO @BBUTTON
IF BTRIG() == 1024 THEN GOTO @ENDGAME
GOTO @INPUTLOOP

Let's take this line by line. @INPUTLOOP simply labels the beginning of the loop, as before. Next we have another IF conditional. Recall that BTRIG() checks the button presses and returns a number code for the button(s) that were pressed. The double equals sign (==) means "equal to", so here we're checking if BTRIG() is equal to 16, which means that the A button was pressed. If that's the case, we go to the @ABUTTON label, and continue from there. (We haven't made the @ABUTTON label yet, but don't worry, that's the next step.) If BTRIG() is not equal to 16, we move on to the next line, which is another IF. Here we're checking if BTRIG() is equal to 32, which means the B button was pressed. If so, we move on to the (currently nonexistent) @BBUTTON label. If not, we move on to the next line, which is checking BTRIG() for 1024, or the START button. If START was pressed, the game is over, so we go to the @ENDGAME label, where we'll display the results. If START wasn't pressed, we move on to the next line, GOTO @INPUTLOOP, which will take us back to the beginning of the input loop to check again, and again, and again, and again...

Now that we're detecting button presses, we need to do something about them. We just wired the A button to go to a label called @ABUTTON, so we better set that label up. Enter the following code to handle the A button behavior:

@ABUTTON
LOWEST = FLOOR((LOWEST + HIGHEST)/2) + 1
GOTO @GAMELOOP

As we know, if the user presses the A button in response to a guess, that means that the user's number is higher than the computer's guess. Think about it - if we guess 50, and the user says "Higher!", what numbers are ruled out? 1, 2, 3, 4, 5, ... all the way up to 50. So now the lowest possible number is 51, one greater than the guess. So when the user says "Higher!" by pressing A, we set the new LOWEST number to our guessing formula + 1. After we've readjusted the bounds, the line GOTO @GAMELOOP kicks us all the way back to the @GAMELOOP label, where the computer will guess again given its new bounds. Because we've raised the lower bound, the computer's guess will be higher than last time - just the effect we want.

Now we need to set up something similar for the B button. It's just as easy. Add this code:

@BBUTTON
HIGHEST = FLOOR((LOWEST + HIGHEST)/2) - 1
GOTO @GAMELOOP

The logic is the same here. If the user says "Lower!" by pressing B, that rules out the guess and all numbers greater than it. So the the HIGHEST possible number must be adjusted downward to the guess minus 1. This will result in a lower guess once GOTO @GAMELOOP kicks us back to the beginning of the loop.

Now all we need to do is handle what happens when the user ends the game by pressing START. All we need to do is display the number of guesses the computer used, but for fun, we're going to display it in an interesting way. For each guess, we'll print a heart character (<3) to the screen, one by one, with a beep sound accompanying each heart. Here's the code:

@ENDGAME
CLS
LOCATE 0, 11
PRINT "I got it!"
PRINT "Guesses: ";

FOR I = 1 TO GUESSES
PRINT "<3";
BEEP
WAIT 20
NEXT

WAIT 120
END

No Action Available

No actions are currently available to you with this message.