Collect User Input
00:00 The player should have the options to attack, heal, and run away. Three different options, and we need some sort of way for the user to input their decision of which of those options they want to choose, right? So, you could use numbers to do that.
00:13
You could say attack is 1
, heal is 2
, run away is 3
. But for a text-based game, I have the feeling it’s maybe easier to understand if it’s something that they type in, some sort of text they type in. Let me draft this up.
00:25
I’m going to collect the action
that the player chooses. I’m going to say input()
,
00:33
and then you have the option to attack, heal, or run away. I will want to do one of those three. I’m going to say, "Do you want to"
and then give them an option to input something.
00:47 And now I don’t really want them to have to type out the whole word. Something you may have seen around is I’m going to indicate that they should just put in the first letter of these different options.
00:59
If they type an A
, I’m going to take that to mean that they want to attack. If they type an H
, it’s going to mean that they want to heal. And if they type R
, it’s going to mean they want to run away.
01:08
So that’ll be my action, and I’m collecting it here in this variable, action
. Let’s print it out, give the game a little spin. This is just going to print back in whatever they say, but that’s okay for now.
01:23
I always like to run things in between. Welcome to the role-playing game!
Do you want to (A)ttack, (H)eal, or (R)un away?
I think that looks pretty nice. So, and then I’m going to say I want to attack, and then it returns the a
for attack.
01:35 One thing that I’m spotting here is that I want to make sure that they can do both lowercase or uppercase, that it doesn’t really matter which of the two they type in.
01:43
So at the end of this input, I’m just going to put a string method and .uppercase()
whatever the input so that both lowercase a
, h
, and r
are going to work as well as uppercase ones.
01:56 Okay, let’s try it again. Run it one more time.
02:01
Attack, heal, or run away. A
, and then I have the action A
. Great. We’re collecting the user input and uppercasing it just to make it a little bit more generalizable.
Become a Member to join the conversation.