Our first game

Show Index

So far we have learned how to print data to the screen, how to assign data to variables, how to get input from the user, and how to use conditional statements.

That is enough to create our first game.

And here it is:

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

If name$ = "Ron"
  Print "Daddy is always right."
  WaitKey()
  End
EndIf

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

WaitKey()
End
A small note: Some of these program listings are becoming long.  You can save yourself time by just selecting the above text and then pasting it into Blitz.   But, make sure that you use the Paste command on the Edit menu.  Don't Ctrl-V the text into Blitz or you will get weird results...

I love playing this game with my two daughters.  If you follow what the program does then you can see why...

First it asks for the players name and stores that in the name$ variable.
Then it tells them Hello.
Next, it asks them what 2 and 2 is and stores their response in the answer variable.
Next, it checks if the players name is "Ron".  If it is then it immediately prints "Daddy is always right." and then the program ends (by way of the End statement) and the remainder of the program is not executed.  Note that this occurs regardless of what answer 'Ron' types in to the second question (What is 2 and 2?).

If the players name is not "Ron" then it doesn't execute the code between the first If and EndIf statements and instead continues on to the next If statement which checks if the answer that was given was 4.

If the answer given was 4 then the computer prints that they are incorrect with the response "No, 2 and 2 is 22."
But if the answer given was not 4 then the computer prints that they are incorrect with the response "No, 2 and 2 is 4."

So no matter what answer is typed in, if the players name is not "Ron" they will be incorrect.  If the players name IS "Ron" then he will always be correct. :)

 

Type the program in and see how it works.

Try typing in "ron" instead of "Ron" and see what happens.   Did you notice that it has to match exactly in order to work?
The BlitzBASIC version of the BASIC language has an easy fix for that.  You could change the If name$ = "Ron" line to use the Lower command so that it would look like this: If Lower(name$) = "ron"
This forces the data (the players name) that is stored in name$ to be lowercase.  This way, no matter if the player types in "Ron" or "ron" or "rOn", it will always work.

Try changing a few things, like making it check for a different name other than "Ron".

 

Moving on to Loops...
Goto next lesson

 

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