Python for Entrepreneurs Transcripts
Chapter: Python language refresher
Lecture: Demo: Memory game - Implementing the game
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
Now when we have the basic shape of our game built, let's go and actually implement it.
0:07
So to get started, there is two basic things we need to keep track of. What are the things we could show a user,
0:13
like what colors and what have we shown the user. So we'll come over here and we want to define a variable on this object
0:19
so we'll say "self." and we'll just create a variable name out of thin air, and that is how you define the variables,
0:24
make sure this happens in the __init__ method, not somewhere else, so we want to say "history", and we want this just to be an empty list
0:31
so we can do that with just empty brackets like so. The other thing we want to keep track of is we want to have the choices,
0:37
or "plays" let's call them. This is going to be another list and this is going to contain all the distinct plays that we could have,
0:44
so we might have red, we might have blue, we might have green, let's say we are going to have four colors, OK.
0:50
Now, in order to make this a little nicer game, let's add some color and some more verbiage, but we want to have a short thing to test against,
0:57
so we don't make the user type red they can hit "r" and "b" and so on. So instead of putting individual items in here,
1:03
we are going to put something called tuples, and this will let us put multiple values for each particular play,
1:08
so we are going to put in here, we are going to say "Red", that's going to be what we show the user but what we are going to test for
1:14
is just "r" and then yellow and so on. Ok, this is a good start, we are going to improve this in just a minute,
1:21
but let's finish some of the other implementation here and then we'll move on. Next, let's turn our attention to show_level();
1:27
I would like to be able to clear the screen, whatever is on there remove it,
1:30
show them just the colors that we've picked or that we have at our current level maybe four colors at level four, and then take that away again,
1:37
so we are going to need a "clear" method, so we can say self.clear, like so, and we can add that method here, which goes at the bottom,
1:45
we are going to use a pretty cheap way to clear the screen here, we are going to go and import one of the system modules,
1:50
so to use something out of the standard library, we have to import it, so we are going to "import os".
1:55
Now we could do a little bit of work to test what platform we are running on,
1:58
but I am just going to do it from Mac, you do it for whatever you want to run it in for,
2:02
and you could use the sys module to figure out whether you are on Windows or on a Linux Unix based machine.
2:09
But we are going to say "os.system" and call a system command, and a system command we are going to do is clear.
2:14
Right, that's what you type in the console or the terminal to clear the screen and that's where we are going to have our game here.
2:21
Now PyCharm is giving us a little warning that this doesn't have to be a instance method, it could be a static one, but just to keep things consistent,
2:28
I am going to say "don't bother us with that". So you won't see any squiggles there. Right, so first thing we are going to do when we show the level
2:34
is we want to say "we want to clear the screen", and then we want to go through each item in the history remember up here we have this list, "history",
2:42
so we'll say "for h in history" and when you... in self.history, in Python the primary way to process a collection is to use this "for...in" loop,
2:50
so "for" some variable we make up in the collection, so if we have ten items in our history, we are going to pull them out
2:56
and work with them one at a time here, so than we could say "print", now, we need to think about what are we going to put into this history here,
3:04
and what we are going to put in are basically selections of these, so we want to print out the first element here,
3:09
so there is a couple of things that we can do but let's just keep things simple we'll print [0] there.
3:14
The next thing we want to do is also we don't want to have this pile vertically,
3:17
let's have this just go horizontally, it seems like that's a little better so we can say the "end" is just going to be two spaces,
3:23
or a comma or something like that. If it prints this out as fast as they can and it goes away, that's not going to be so great
3:29
so what we want to do is we want to make it go slowly so we can put this thread to sleep using the time method here,
3:35
and we could go to the top up here and type "import time" or in PyCharm hit Alt+Enter and say "we would like to just import this name",
3:43
so see how I wrote "import time" at the top, and now, it has a handy little sleep function and let's say it's going to sleep for two seconds.
3:50
Now, something that happens in this that you won't see until you run it but while I am here I will go and take care of it,
3:56
I'll put a note that we need to flush the output, just because the way it works in the terminal this doesn't always
4:02
flush that stream as you sleep, and so it doesn't show in a nice smooth way, we'll see that we need to do that here.
4:08
And maybe two is a little long, let's put that at one, and last thing, after we are done showing them that, let's "clear",
4:15
this is going to take that screen away, so we are going to show them the history,
4:18
clear the screen, show them all the moves they are suppose to remember and then take it away. And then we are going to test them,
4:23
but before we test them, let's add a move, right? We've got to start out with the empty set of history,
4:28
let's add some will say self.history to add something to a list we just append to it.
4:32
Now, there is a really nice way to randomly select from a sequence in Python, so if we have the random module imported, we can come down here and say
4:42
"I would like to pick one of those random items", so I say random.choice and pick some sequence and it will just give us one random,
4:53
so that's really nice to write there. let's hold off on the test here and let's just say "return True",
5:00
let's just say they are passing for now so we can see the other stuff working.
5:03
All right, so like I said, there is going to be a little bit of a problem when we run this, but let's go ahead and save and run this.
5:09
Now, how do we run this? Right-click over here and we can say "run" and pick whatever thing it is we want to run, and run,
5:17
now, notice, it doesn't really like this stuff being empty here when we call clear, and really we have to just run this outside in the terminal.
5:25
So a simple way to do that is just copy what PyCharm is doing. Here you can see something is happening but we are not really seeing this item,
5:32
so like I said, we need to flush this because that's a little bit funky,
5:36
so we are going to say "sys" import that module, go to standard and just say "flush". Now let's try this again, so yellow, blue, yellow, blue, green,
5:51
yellow, blue, green, blue- you can see it's just slowly building up as it goes. All right, we are of course not testing the user,
6:00
but it looks like this works, except there is one problem, something I don't really like so much here, that green is yellow, that blue is yellow,
6:07
everything like that, so we can come over here and use an external library, external package that I have installed,
6:14
and I have installed it with "pip3 install colorama", so we are going to use this thing called colorama, and that lets us add coloring to the console.
6:24
I have already installed that like I said, and so the way you use it is you just add in a string colorama.for.red let's say, like so.
6:34
We are just going to do this for each one. Let's just run that again, make sure I hit save, and run it,
6:41
and now you see, blue is blue, blue is blue, red, red is red, and as it builds up we'll see more of these, yellow is yellow and so on.
6:49
Great, so that feels much more like a game to me. Now it's really down just to testing the player,
6:55
so what we are going to do is we are going to go through for each thing in our history
6:59
we are going to ask the user what is the first item, what is the second item, and so on and we'll just see if they can get that right,
7:05
so let's start out with the little message to them, just to say "look, you are going to need to remember four items", or five items,
7:10
or whatever it is and let's do this white, so we'll say colorama.fore.white
7:14
and then what we want to tell them is we want to say "there are three moves, or two moves, or something, we can use this string format style, like so,
7:23
and we can say .format and say I want to give it the length, the number of items in this object's history.
7:31
And then for each one of those things in the history we want to loop over it and test so we'll say "for h in self.history", we want to test something.
7:41
Now, I'll show you another good trick here, another good technique, remember, each thing in the history is one of these
7:47
and what we really care about is the second value, not the first. Down here, we were just using indexes into it,
7:54
[0], [1] and so on, but we can do what's called tuple unpacking here, so I could say we can give the text and the entry or the "v" for value,
8:03
and because this is a length two tuple, the first value is assigned to you and the second is assigned there.
8:09
So we can just test against that "v", so let's say something like this, we want to get input from the user, that's super easy, we'll say input,
8:16
use the input method and we just give the prompt. And we'll give them a little hint on the things that they can type and then we'll say
8:23
"if guess != v: return False". So, the very first time that they get it wrong let's say nope, that was wrong,
8:30
and in the end if they make it through all of them, they must have gotten them all right, so we'll say "return True".
8:36
All right, I think our game is just about ready to play, let's save it and then run it over here and see what we get; green,
8:43
OK, so it says one move we have to remember, and that was "g", OK, worked, green, green, green, green, now red, so "g", "g", "r",
8:56
it really seems to like red and green. Let's just put something wrong so we can see that it said "all right, look,
9:05
you entered something wrong", it's supposed to be green, you said red, there it goes. But, we made it to level 7, that was pretty awesome,
9:11
but what is even more awesome is our game is totally implemented here, so we come in, we create the game, we start out with the empty history,
9:18
and the various plays that we can make, to show the level, we just loop over the items and we print out the text,
9:24
and because we are not using an inline, it doesn't flush, so we are going to force the flush here, do rest,
9:31
to add a move it's super easy, we get a random one from our plays,
9:34
and to test the player we just go through each one of them, these entries in the history
9:39
and we unpack the values and we check that their guess is the right value. There is so many things to learn in Python
9:47
and it's a very rich and powerful language, but at the same time, you've seen a significant portion of it, in action.
9:53
If any of this was too fast, or unclear, please go and review the reference section where we go over every one of these ideas topic by topic,
10:02
there is something on loops, there is something on conditionals, there is something on methods and so on.
10:07
If you need to find out more details about something, before you start the rest of the class, make sure you check out the reference section.
10:13
Otherwise, let's move on to getting started with web development.