Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
Hey,
2
you've reached the final project and as you've seen at the beginning of today's
3
lessons, I showed you what we're going to be making,
4
which is a sort of choose your own adventure game.
5
I don't know if you've ever read those
6
choose your own adventure books where you flip to a different page if you
7
make a particular choice and then the story kind of evolves as you make your
8
choices.
9
Now we're going to make a very simple version of that using what we've learned
10
about conditionals: if, else, elif statements as well as everything we've done
11
previously. So you've already seen what the final outcome should look like.
12
And if you want to review it again,
13
just head over to treasure-island-end.app brewery.repl.run.
14
And here you'll see essentially the game in action.
15
So it start off asking you, you're at a crossroad. Do you want to go left or right?
16
And then you have to type your choice. If you selected left,
17
then it's going to take you to a Lake and it asks you whether if you want to
18
wait for a boat or swim across. So let's go ahead and wait for a boat.
19
And then the final step is it asks you which door would you like to go through?
20
Red, yellow, or blue. Now depending on your choice,
21
you'll either end up with game over or you'll end up with, um,
22
winning the game.
23
So the basic setup for this game looks something like this.
24
And I've included a link to this flow chart in the starting repl.
25
So repl.it/@appbrewery/treasure-island-start
26
and of course you'll find all the links in the course resources as always.
27
But if you scroll down,
28
there's a link right here that takes you to this flow chart.
29
Now the first question you're going to ask the user is to make a choice between
30
left and right. Now you can use my wording. Um, if you want your at a crossroad,
31
where do you want to go? Type left or type right?
32
But I think the fun part of this challenge is really creating your own story,
33
right?
34
But somehow get them to choose between left and right. If they choose right or
35
any other condition for that matter,
36
tell them that they've lost game over for some sort of reason.
37
In my case, if they had chosen, right,
38
the reason is because they fell into a hole. Game over.
39
But I think you're far more imaginative than I am.
40
So go ahead and use all of those skills and make your game really fun.
41
Now if they've chosen left on the other hand,
42
then we're going to take them to another question that asks them whether if they
43
want to swim or whether if they want to wait for a boat. Um,
44
and if they swam, then it's going to be some form of game over.
45
There's crocodiles, there's, um, whatever it may be, but they can't continue.
46
But if they chose wait, then we take them to the third and final question,
47
which asks them to choose between three doors.
48
And if they choose any door other than the yellow door, then they would lose.
49
But if they chose the yellow door, then they somehow win.
50
Now notice how each of these just says, game over, game over, left or right,
51
but you know,
52
the idea is really to use your creativity and make this game really your own.
53
Now one thing to remember though is that when the user types in a answer,
54
let's say left or right, they might type it like this, um,
55
with an upper case R or they might type it like this, all lower case.
56
Have a think about how you might make sure that no matter which version they
57
typed, you're still going to be able to catch their command essentially.
58
So have a think about what you've learned in the previous lessons and take a
59
look at this flow chart,
60
then head over to the starting project and you can get started writing your
61
code. Now if you head over here and what you see is sort of like a mumbo jumbo
62
a bunch of characters,
63
then the reason is probably because there's not enough space for this ASCII art
64
of a treasure chest to display. So you can either do as I did,
65
reduce the size of the console or alternatively and probably easier is to change
66
the layout to stacked, make it go with the bottom, collapse
67
this left hand pane
68
and then you've got as much space as you have to work with.
69
Now ASCII art is really cool because it basically just uses a bunch of characters
70
that you know and love,
71
like the comma and the equal sign and all of these things that, um,
72
together make up a picture. And you can find your own ASCII art if you just go to
73
ascii.co.
74
uk/art and you'll see a whole bunch of different topics.
75
Um, and they've grouped a lot of things together into individual pages.
76
So if you wanted to a rhino ASCII art,
77
then you can find a whole bunch of rhinos that people have drawn.
78
But the easiest way is, of course, using a command F if you're on a Mac or control
79
F if you're on a Windows machine and then just search for the thing that you
80
want. So, for example, I went and searched for treasure,
81
and down here I found this brilliant treasure box, ASCII art,
82
which is what I'm using to print inside this print statement.
83
Now notice how there's three single quotes that are at the start and there's
84
three single quotes at the end.
85
Basically what the single quotes allow you to do is to create multiple lines of,
86
um, a string essentially.
87
So notice how if I go ahead and delete all of these, uh,
88
single quotes and I only had a set of double quotes,
89
then it actually doesn't quite work.
90
It gets quite confused, um,
91
because it thinks that this is where the line ends. But in fact,
92
I want it to print all of this out. So to do that,
93
instead of using a double quote, I use three single quotes.
94
And at the very bottom I tell it, well,
95
this is the end of my multi block string by using three single quotes as well.
96
So I have a go at running that and you should be able to see that in your
97
console
98
you get this little treasure Island ASCII art being printed along with the two
99
sentences that begin the game. So have a look at the flowcharts, um,
100
have a play around with the ASCII arts and also have a look at the final
101
version of the app so that you can go ahead and create your own version to
102
complete this challenge. Pause the video now and give that a go.
103
Yeah.
104
All right,
105
so let's see if we can replicate the functionality or the logic that's shown in
106
this diagram into a treasure Island game.
107
So the first thing it has to do is we have to ask the user whether if they want
108
to turn left or right, and after we ask the user this question,
109
we're going to want to capture their input, right?
110
So let's go ahead and create an input function and ask our question.
111
Yeah.
112
Now notice how when I use my double quotes around my string,
113
the double quotes that are inside the double quotes are interpreted as code
114
because it actually thinks that this is the end of the first string,
115
this is the second string, and this is the final string.
116
This is how the computer is going to see it. Now,
117
previously you saw that we could do the trick where we change this to single
118
quotes instead, but if like me,
119
you have this case where you have the your with the apostrophe,
120
then it's going to think that this is actually the end of the first string,
121
this is the second string and this is the third string.
122
So this doesn't actually really help us.
123
Now what we can do when we want to tell our computer that what I'm writing is
124
actually not code, just see it as text.
125
You can get it to ignore a symbol afterwards by using the backslash.
126
So this way it basically escapes the string and it will see it as text and now
127
we have a complete string like this,
128
and this is just going to be interpreted as text because we told it to with the
129
backslash. Now of course,
130
there's other ways that you could have gone around this.
131
You could have just simply said you are at at a crossroad rather than using the
132
apostrophe. But more often than not,
133
you'll need to use a symbol that you'll need to escape and you can do that with
134
the backslash. So that's just a quick tip. Now at this point,
135
the user is going to type a message left or right.
136
So let's go ahead and save that to a variable.
137
Let's call it choice1 equals their input.
138
Now, remember how I said they could be typing the answer as Right or as right.
139
So how can we make sure that when we're doing our checks using if statement,
140
we ignore the casing?
141
Well one way of doing this is we can use the lower function to change their input
142
no matter how they wrote it, Right, right or RIGHT,
143
it should all be converted into lowercase, to this version.
144
That way we have one thing that we can consistently check for in our if
145
statement, which is coming up next.
146
So we can say if choice1 is equal to, remember the double equals is checking
147
whether a left hand side and righthand side is equal
148
and a single equal sign is assigning the right-hand side to this particular
149
variable name. Now if choice1 equals the string, right,
150
then it's pretty much game over for our player.
151
So let's go ahead and add our colon and print something like Game over.
152
Of course, we can elaborate this a little bit more as to why it was a game over.
153
Well, because you fell into a hole.
154
This is like the ITTI game from the 1980s. Lots of holes.
155
Um, so you fell in to a whole, game over. Um,
156
but what if they had chosen left instead?
157
Well that takes them on to the next question.
158
So that's the option that will allow them to continue along the game.
159
Now, in this case, we could use an else statement and say, well,
160
if they didn't choose right then they probably chose left right?
161
But they could've also chosen like a option that was completely not listed.
162
They could have just written something like this and it would still trigger the
163
else statement.
164
So what we should actually do is to switch it around.
165
So namely we should say if choice1 is equal to left,
166
well in that case, then they continue
167
in the game. But otherwise, namely,
168
if they chose right or anything else for that matter,
169
then it's pretty much game over.
170
So this format makes a lot more sense if you want to continue along the left
171
side of the branch, this way.
172
Now another thing that you might've realized because we learned about combining
173
different conditions is instead of using the dot lower to change the input to
174
lowercase,
175
you could have also said if choice is equal to left or if choice1 is
176
equal to Left, spelt like this, that will both work.
177
Um,
178
but I think in terms of succinctness and less code writing,
179
I think this makes a lot more sense to me.
180
So I'm going to continue with this version. But if you did it the other way,
181
that's perfectly valid as well.
182
Now if their choice1 was left, then they get taken to the next question.
183
Do you want to swim or do you want to wait?
184
So let's go ahead and continue the game and create another input. In this input
185
we're going to ask them a question and say,
186
you've come to a Lake,
187
there is an Island in the middle of the Lake. Type "wait" to wait for a boat. Type
188
"swim" to swim across. Cool.
189
So we've actually got the same problem as we had before,
190
namely that this is being interpreted as code rather than as a continuous single
191
string. So do you remember how we fixed this previously?
192
And if you got stuck on this,
193
I recommend having a go at this yourself before I show you the answer.
194
Alright,
195
so previously we said we were going to switch it to single quotes instead so
196
that these double quotes become interpreted as just normal strings.
197
But of course, we've got this you've, we could, of course, change it
198
to you have instead of you've, but if we want to escape the string,
199
we just add a backslash and now everything is colored in this sort of pinkish
200
color to indicate it's all interpreted as a string,
201
which is exactly what we did previously. Now in terms of this input,
202
they're going to type wait or they're going to type swim.
203
So let's go ahead and lower the casing for it as well.
204
And then we're going to save it inside a choice,
205
we're going to say choice2. Now be really careful and mindful of your
206
indentation because if you wrote it like this,
207
then it's going to be creating some errors for you because this choice2 input
208
line has to run when this choice1 is equal to left.
209
Otherwise we're going to get an indentation error.
210
So let's go ahead and indent it here to show that this part is inside this if
211
statement and will only be executed if this is true.
212
And then we get to check our choice2.
213
So if choice2 is equal to wait,
214
then it means that the game will continue.
215
But else,
216
so if they typed swim or anything else for that matter,
217
then the game is going to end.
218
So we're going to print and tell them that something terrible happened.
219
"You got attacked by an angry trout.
220
Game over." Now we can continue along this path,
221
so we're down to here now.
222
And the final question we're going to ask the user is which door do they want to
223
choose?
224
So here I'm going to use an input to send them the message that "You arrive at
225
the Island unharmed. There is a house with three doors, one red, one yellow,
226
one blue. Which color do you choose?"
227
Now I'm going to save their choice inside a variable called choices, lower
228
case the answer that they provided,
229
and finally I'm going to use if, elif and else to check which one they chose.
230
So if choice3 is equal to red,
231
elif choice3 is equal to yellow,
232
elif choice3 is equal to blue.
233
And then I've got a else.
234
So here are four possible things that could happen. If they chose red,
235
then I'm going to tell them that it's game over,
236
but it's going to be some sort of creative version of game over.
237
So maybe something like, uhm,
238
it's a room full of fire. Game over. If they chose yellow on the other hand,
239
well that's going to be the room where the treasure exists.
240
So I'm going to print. Um, you found the treasure, you win.
241
If they chose blue, they also end up with game over.
242
And I have to give them a different reason. Now,
243
if you had decided that if they chose anything but yellow,
244
the way that they're going to game over is the same. Say if, you know,
245
if you chose red, it's a room full of fire, game over. If you chose blue,
246
it's a room full of fire game over. Anything else
247
is also going to be a fire, game over.
248
Well then you could just use if and else.
249
But if you wanted each choice or each door to have a different outcome for the
250
user, say the red door has fire,
251
the blue door has beasts and if they chose the wrong door then they're going to
252
get something like you chose a door that doesn't exist, game over.
253
So can you see that depending on whether
254
if you want to give the user a different um, print statement,
255
um, based on their choice, then you would use if, elif, else.
256
But if you actually just wanted to give them the same feedback,
257
which is game over, then you could just use if and else.
258
Now let's run our game and just make sure that it works.
259
So I'm going to go left because I know it continues and then I'm going to wait
260
for the boat and that allows me to get to the final condition.
261
And if I choose yellow, I know that I'll win.
262
Now if you want to change the formatting because having the input at the end of
263
the sentence doesn't look as good as letting the user type it on a new line,
264
like what we've got here, then you can simply, if you remember,
265
just add the new line,
266
which is a \n at the end of your inputs.
267
Like so. So how did you get on with this challenge?
268
And if you didn't manage to do it right or if you've done it right,
269
go back and try and modify it.
270
See if you can add some more conditions or see if you can make the game over
271
messages a little bit more interesting than what I've written.
272
See if you can modify and make it really your own. Have fun with that
273
and in the next lesson, I've got more exciting things coming up for you.
274
So hopefully you've scheduled some time for tomorrow to complete the next day's
275
lessons. But for now, it's goodnight from me.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.