Our first game - Part 2

Show Index

We talked about earlier how it would be nice if "Our first game" was able to loop over and over if we wanted to play it several times.  Here is one way to do it:

Repeat
  name$ = Input("What is your name? ")
  Print "Hello " + name$
  answer = Input("What is 2 and 2? ")

  If name$ = "Ron"
    Print "Daddy is always right."
  Else
    If answer = 4
      Print "No, 2 and 2 is 22."
    Else
      Print "No, 2 and 2 is 4."
    EndIf
  EndIf

  Print "Press Spacebar to play again or any other key to quit."
  key = WaitKey()
Until key <> 32
End

You can see that a few things have changed.  The biggest change is that the two separate If statements in "Our first game" have been combined into one.   This is called a nested If.  The reason I had to do that was because the first version of the game caused the program to end if name$ didn't equal "Ron".  Since we want our program to loop, we can't allow it to end anymore.

By indenting the code the way I have, you can more easily see how the new combined If statement works.  If name$ equals "Ron" then the computer prints "Daddy is always right." and then comes to the first Else command (which is tied to the first If command).  Since name$ equaled "Ron" at the first If statement, the computer skips all of the code that follows the Else command:

    If answer = 4
      Print "No, 2 and 2 is 22."
    Else
      Print "No, 2 and 2 is 4."
    EndIf

to the last EndIf statement.

However, if at the first If statement, name$ doesn't equal "Ron" then the computer skips over the Print "Daddy is always right." statement and executes the five lines after the first Else command:

    If answer = 4
      Print "No, 2 and 2 is 22."
    Else
      Print "No, 2 and 2 is 4."
    EndIf

Ultimately, no matter what happens during the two nested If statements, the computer will always end up at the end of the nested If statements (the last EndIf) to execute the following Print "Press Spacebar to play again." line.

 

The next big change is the fact that I added these lines:

Repeat
  Print "Press Spacebar to play again or any other key to quit."
  key = WaitKey()
Until key <> 32
End

I'm still using the Waitkey command to wait for a keypress, but now I am storing the ASCII value of the keypress in the variable 'key'.   On the following line, the Repeat-Until commands that I added compare the variable key's value with the number 32.  The number 32 is the ASCII value of a space (which is what Waitkey returns when you press the Spacebar).

If the value of key is not 32 ( <> means 'not equal to'), then the computer executes the following line, and the program ends.

If, however, the value of key does equal 32 (which means that you pressed the spacebar), then the computer goes all the way back to the first line (at the Repeat command) and starts the whole program over by asking the user for their name again.

 

And so now you can play the game over and over as many times as you want...

 

After you've finished playing around with this game, try your hand at making a 'Guess the number' game.
Goto next lesson

 

If you've reached this page and there's no index on the left, Click here to show the Index Page