Skip to content

Commit 3fb1e21

Browse files
committed
transcript for app 7
1 parent 6be9a13 commit 3fb1e21

File tree

11 files changed

+102
-113
lines changed

11 files changed

+102
-113
lines changed

transcripts/txt/07_app/1.txt

+11-12
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
0:12 Well, it's going to be a text based game 
66
0:14 and here you can see a standard ground of game play. 
77
0:16 Of course, like all of our apps we start with a little header that says what the app is, 
8-
0:19 and then right off we have our hero in the game, the wizard Gandolf. 
9-
0:23 And he sees an Evil Wizard,
10-
0:25 it turn out that one is super strong 
11-
0:27 and so he is like I am not going to battle this, he could attack it, 
12-
0:30 he could look around, but he is just going to run away as quick as he can, 
13-
0:33 so he does, and then next he finds a Bat, and decides 
8+
0:19 and then right off bat we have our hero in the game, the wizard Gandolf. 
9+
0:23 And he sees an Evil Wizard.
10+
0:25 It turn out that one is super strong 
11+
0:27 and so he is like... I am not going to battle this, he could attack it, 
12+
0:30 he could look around, but he is just going to run away as quick as he can. 
13+
0:33 So he does, and then next he finds a Bat, and decides 
1414
0:37 hey, I can probably attack and kill this Bat, so he does, 
1515
0:40 but just barely he roles a 22, the bat roles at 22 
1616
0:44 and I guess because the element of surprise 
@@ -37,16 +37,16 @@
3737
1:39 we have to chain those initializers through the inheritance tree, 
3838
1:41 so we are going to talk about initializer chaining. 
3939
1:44 Speaking of inheritance, that lets us model our concepts
40-
1:47 in our program with different levels of specialization, 
41-
1:51 so we can model all of the actors, the dragon, the toad, the wizard 
40+
1:47 in our program with different levels of specialization. 
41+
1:51 So we can model all of the actors, the dragon, the toad, the wizard 
4242
1:55 as something maybe we'll call the creature in the game 
4343
1:58 and then we have a specialized version of the creature 
4444
2:00 that has other data and other features called the wizard 
4545
2:03 and it knows how to battle creatures 
4646
2:05 and then we can also have different types of creatures 
4747
2:07 that may themselves have special features 
48-
2:09 like a dragon that has a special attack or something like that,
49-
2:12 so this is called inheritance and it's a very powerful feature, when use judiciously. 
48+
2:09 like a dragon that has a special attack or something like that.
49+
2:12 So this is called inheritance and it's a very powerful feature, when use judiciously. 
5050
2:16 When we talk about inheritance, 
5151
2:18 we are also going to talk about polymorphism and duck typing. 
5252
2:21 Some languages have very strict rules 
@@ -57,5 +57,4 @@
5757
2:34 Python doesn't have compiling or this concept of strong typing
5858
2:37 instead we are going to use something called duck typing
5959
2:40 we'll talk about that near the end of our app.
60-
2:43 Let's get onto build a super fun dungeons and dragon style wizard game. 
61-
60+
2:43 Let's get onto build a super fun dungeons and dragon style wizard game.

transcripts/txt/07_app/10.txt

+23-24
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
0:02 we kind of would like to use the wizard specialization, 
33
0:06 the wizard class as any other fighter in our game. 
44
0:10 And the commonality that we need for any kind of fighter
5-
0:13 is of course that it is a creature.
5+
0:13 is of course that it is a creature, right?.
66
0:18 And we just saw that we can use inheritance up here like so 
77
0:22 and say this wizard is a creature. 
88
0:25 Now, this is little annoying, 
@@ -21,7 +21,7 @@
2121
1:03 one way we could just say we are going to have this init method here 
2222
1:08 and we could say super and this will give us access to 
2323
1:11 the direct methods on creature, 
24-
1:14 we could say dunder init and we can say name and level, 
24+
1:14 we could say __init__() and we can say name and level, 
2525
1:18 and now the warning will go away, right, 
2626
1:20 and we are actually passing this off, 
2727
1:22 so when you call this method, by creating a wizard 
@@ -69,15 +69,15 @@
6969
3:54 We could use that for something, but we don't really in this game. 
7070
3:57 I guess the thing we can do to make this small animals special is 
7171
4:00 maybe it's a little extra weak, so down here we can change this get defensive roll, 
72-
4:06 in the creature class, it has a get defensive roll and it just does this, 
72+
4:06 in the creature class, it has a get_defensive_roll() and it just does this, 
7373
4:10 let me copy the whole thing, and we can actually change how this works ok,
7474
4:16 so we can come over here and say the small animal has 
7575
4:19 all the behaviors and everything from the creature 
76-
4:21 except it's going to have its own version of get defensive roll
76+
4:21 except it's going to have its own version of get _defensive_roll()
7777
4:24 Now, some programming languages when you are doing inheritance 
7878
4:28 you have to have special words like override or virtual or these sorts of things, 
7979
4:31 Python doesn't have that, we just have one method 
80-
4:35 it's called get defensive roll
80+
4:35 it's called get_defensive_roll()
8181
4:36 if we just say it in the other classes something replaces it. 
8282
4:39 If we leave it this way, things should just still work again
8383
4:42 other than the wizard is not what we are looking for, but the bat is, perfect.
@@ -91,9 +91,9 @@
9191
5:14 so I got 6 so then we cut that down to 3.0. 
9292
5:17 So now we specialize our small creature to be 
9393
5:20 a little easier to defeat than the others, well quite a bit really. 
94-
5:23 However, this part of code, is literally this base get defensive roll
94+
5:23 However, this part of code, is literally this base get_defensive_roll()
9595
5:32 Ok, so we can actually change this, we can say you know what, 
96-
5:36 it could be the super, go to the creature class and we can call it it get defensive roll
96+
5:36 it could be the super, go to the creature class and we can call it it ge_ defensive_roll()
9797
5:40 and that way as this evolves over time 
9898
5:43 maybe we decide to switch to a 15 sided dice or something like that 
9999
5:48 we don't have to maintain the small animal, 
@@ -115,13 +115,13 @@
115115
6:46 basically the setup in the data that the wizard and the small animal use
116116
6:52 is the same as the creature, but the dragon, I am going to change this,
117117
6:54 I want the dragon to have a scaliness and an indicator whether it breaths fire or not. 
118-
7:00 So what we are going to do is we are going to define a dunder init
118+
7:00 So what we are going to do is we are going to define a __init__()
119119
7:03 and it's going to have to take all the data 
120120
7:06 or at least somehow supply the data that the creature is going to need, 
121-
7:10 so name and level, and then I am going to have a scaliness and a breaths fire
121+
7:10 so name and level, and then I am going to have a scaliness and a breaths_fire
122122
7:16 this will be a boolean to say it does or does not breath fire. 
123123
7:19 and then the first thing that we should do is 
124-
7:21 we should set up the creature feature so we'll say super.dunder init
124+
7:21 we should set up the creature feature so we'll say super.__init__()
125125
7:27 name and level, and then we need to assign to the dragon 
126126
7:32 whether or not his scaliness and whether or not it breaths fire. 
127127
7:35 So we can just say add this parameter here 
@@ -139,27 +139,27 @@
139139
8:15 maybe we'll have a factor of 5 or something, 
140140
8:18 I mean that's a pretty serious defensive measure that it literally can breath fire. 
141141
8:22 I could write this, let me say none and I'll say 
142-
8:26 if self.breaths fire, the fire modifier = 5 maybe this could just be 1 
142+
8:26 if self.breaths_fire, the fire_modifier = 5 maybe this could just be 1 
143143
8:36 but let me put it this way just an else statement, 
144-
8:38 so it's a little more clear, so else fire modifier = 1, 
144+
8:38 so it's a little more clear, so else fire_modifier = 1, 
145145
8:43 now this bit right here, we are using all these lines of code to do this,
146146
8:46 we could actually write this in a much nicer way, 
147-
8:49 we'll say fire modifier = and I can put this 
147+
8:49 we'll say fire_modifier = and I can put this 
148148
8:53 all in one line in this condensed if statement the Python supports, 
149149
8:56 let me just sketch out the placeholder 
150150
8:59 so value if true say if some test else value if false, right, 
151151
9:08 so if we use this the value if- let's do the test first, 
152-
9:11 so the test is going to be if breaths fire, self breaths fire
152+
9:11 so the test is going to be if breaths_fire, self breaths_fire
153153
9:18 so if it does breath fire, I would like the modifier to be 5, 
154-
9:21 but if it does not breath fire, I would like the modifier to be 1,
154+
9:21 but if it does not breath_fire, I would like the modifier to be 1,
155155
9:25 ok, so let me put this like so,
156156
9:27 so now this will actually let us in one nice little condensed line, 
157157
9:31 specify the fire modifier, the other thing is the scaliness factor, 
158158
9:36 let's say this i s a number between 1 and 100, 
159159
9:40 and it has like a 10% effect on here, so we'll have something like this, 
160-
9:44 scale, modifier = scaliness self.scaliness, divided by 10, right, 
160+
9:44 scale_modifier = scaliness self.scaliness, divided by 10, right, 
161161
9:55 so if it's a 100 it multiply it by 10, if it 1 it actually takes it down a little bit, 
162-
9:59 so then we are going to return base roll times fire modifier times scale modifier
162+
9:59 so then we are going to return base_roll times fire_modifier times scale_modifier
163163
10:07 Ok, now let's go back to our program here 
164164
10:10 and let me just put this back for a minute, 
165165
10:13 that's how it was before we started to talk about these dragons, 
@@ -168,11 +168,11 @@
168168
10:21 that's because I again forgot to import it, 
169169
10:24 it's kind of why I like the name spaces you don't have to keep doing this, 
170170
10:26 but it's all good. 
171-
10:29 So now we have our dragon, but notice PyCharm is already telling us not so good, 
171+
10:29 So now we have our dragon, but notice PyCharm is already telling us hummm? not so good, 
172172
10:32 but it won't run it will say you are missing
173-
10:34 this parameter of scaliness and breaths fire right there, 
173+
10:34 this parameter of scaliness and breaths_fire right there, 
174174
10:37 so let's say that this dragon has a scaliness of 50, no let's say 75, 
175-
10:45 that's a pretty serious scaliness and it does breath fire
175+
10:45 that's a pretty serious scaliness and it does breath_fire
176176
10:49 so this dragon is now a much harder thing to fight, all right,
177177
10:52 so let's look around, so we see- let's run away
178178
10:58 until we see something that is a dragon. 
@@ -194,8 +194,8 @@
194194
11:59 it takes all that into account during this battle 
195195
12:02 but the wizard doesn't have to know it's a dragon, it can still treat it like a creature. 
196196
12:07 And yet it behaves differently, finally the wizard can also behave 
197-
12:11 like just another creature in the game because it derives from creature, 
198-
12:15 we do this derivation by just defining the creature class 
197+
12:11 like just another creature in the game because it derives from creature. 
198+
12:15 We do this derivation by just defining the creature class 
199199
12:18 and then we say the wizard is a creature, here we have an additional method 
200200
12:23 that's not in the creature class called attack, 
201201
12:26 PyCharm we can collapse those, and then the small animal 
@@ -210,5 +210,4 @@
210210
12:55 like the scale modifier which apparently I misspelled. 
211211
13:00 So this just gives you a little taste of object oriented programming 
212212
13:03 but the types of applications that you can build are seriously powerful 
213-
13:06 and it's a lot of fun to think about solving problems this way. 
214-
213+
13:06 and it's a lot of fun to think about solving problems this way.

transcripts/txt/07_app/11.txt

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

0:00 Let's look at the final core concept 
22
0:02 in this object oriented wizard game called polymorphism. 
33
0:06 Now you can see that we have a list of diverse creatures,
4-
0:09  we have small animals, creatures, dragons, wizards and so on, 
4+
0:09 we have small animals, creatures, dragons, wizards and so on, 
55
0:13 and as you saw on a previous examples 
66
0:15 and you can see in the note on the left here, 
77
0:17 all of these objects all of these classes, small animal, dragon and wizard, 
@@ -21,23 +21,23 @@
2121
1:03 and that's fantastic, that lets us as we evolve our application 
2222
1:07 create more specialization other types of objects, 
2323
1:10 later in the game we want to add some kind of like water monster
24-
1:13  to attack the wizard when he is out in the ocean or something 
24+
1:13 to attack the wizard when he is out in the ocean or something 
2525
1:17 as long as that thing derives from creature, 
2626
1:20 we potentially don't have to rewrite the code 
2727
1:22 that handles wizard's battling water monsters
2828
1:25 it's just using this base class. 
29-
1:27 So that's a really powerful feature, 
30-
1:29 one other concept closely related to this is something called duck typing, 
31-
1:32 in statically typed compiled languages like CSharp, Java, C++ and so on, 
29+
1:27 So that's a really powerful feature. 
30+
1:29 One other concept closely related to this is something called duck typing, 
31+
1:32 in statically typed compiled languages like C#, Java, C++ and so on, 
3232
1:38 you absolutely must have these objects derived from creature 
3333
1:43 or trying to pass them to the attack method 
34-
1:46 which assumes it takes a creature literally not compile. 
34+
1:46 which assumes it takes a creature or literally not compile, your code won't run ever
3535
1:49 In Python it's not like that, Python uses something called duck typing 
3636
1:55 and as long as the shape of the pieces of the creature class
37-
1:59  that the attack method assumes are there, get defensive roll, name,
38-
2:04  as long as the types you pass match that, 
37+
1:59 that the attack method assumes are there, get defensive roll, name,
38+
2:04 as long as the types you pass match that, 
3939
2:07 you technically can use it here, and that is called duck typing, 
4040
2:09 it's like if it acts like a creature, if it looks like a creature, it is a creature, 
4141
2:13 but I still encourage you to create these object hierarchies 
4242
2:16 as it helps reduce code duplication, it helps with maintenance 
43-
2:18 and it's generally just the right thing to do. 
43+
2:18 and it's generally just the right thing to do.

transcripts/txt/07_app/2.txt

+11-14
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
0:07 or probably refine it and add some enhancements, 
44
0:09 but let's just get started in kind of the same way we have with other apps. 
55
0:14 So, again, we are going to add our program.py file 
6-
0:18 and we are going to have our main method, we'll have our print header, as always
7-
0:26 and in this one we are going to have what we are going to call a game loop
8-
0:30 And then in our main let's just go and say print header 
9-
0:32 and then we'll just run the game loop
10-
0:35 And finally, let's use our PyCharm live template to call the main
6+
0:18 and we are going to have our main() method, we'll have our print_header(), as always
7+
0:26 and in this one we are going to have what we are going to call a game_loop()
8+
0:30 And then in our main let's just go and say print_header() 
9+
0:32 and then we'll just run the game_loop()
10+
0:35 And finally, let's use our PyCharm live template to call the main()
1111
0:41 only if it's actually being executed rather than imported. 
1212
0:45 So header is pretty standard as always so let's just do this, 
13-
0:48 
14-
0:55 ok, standard header now let's focus in on this game loop we have here. 
15-
1:00 So the concept of our game loop is we just want to go around and around 
13+
0:55 ok, standard header now let's focus in on this game_loop() we have here. 
14+
1:00 So the concept of our game_loop() is we just want to go around and around 
1615
1:03 getting input from the user until the game ends
1716
1:07 so let's just start really simple with the concept of going around and around, 
1817
1:11 and so we'll just say while true and we are going to do some work here. 
@@ -25,15 +24,14 @@
2524
1:38 one could be they actually said they want to attack 
2625
1:41 and let's just print out some things we can kind of test a little loop here, 
2726
1:45 then we'll rewrite the behaviors. 
28-
1:47 So if they say a we'll say attack, elif if it's run away we'll just print run away,
29-
1:54 
30-
1:59 if they are going to look around we are going to print look around, and finally, 
27+
1:47 So if they say a we'll say attack, elif if it's run away we'll just print('run away'),
28+
1:59 if they are going to look around we are going to print('look around'), and finally, 
3129
2:04 if they hit something else and we don't know what it is we'll just say
3230
2:07 all right, you must want to exit the game, 
3331
2:09 like if we just hit enter with no command that means you are gone, 
3432
2:12 so we'll just say something like this, ok, exiting game. Bye. 
3533
2:17 So, let's go ahead and run our app just to make sure everything is hanging together. 
36-
2:22 
34+
2:22 Now, no run configuration, so we kow how to get that going.
3735
2:26 All right, so down here we can attack, we can look around, 
3836
2:30 we can run away, or we can say enter and we should exit. 
3937
2:33 Now, oops, we didn't really exit we just printed exit, right, 
@@ -43,5 +41,4 @@
4341
2:46 run the next line which is empty and we're done. 
4442
2:47 So look around, enter, exit, perfect. 
4543
2:51 So our little loop is running, it's time to build up the data structures
46-
2:55 with classes and objects it's going to be great. 
47-
44+
2:55 with classes and objects it's going to be great.

0 commit comments

Comments
 (0)