Mastering PyCharm Transcripts
Lecture: Moving code
0:01 Now let's go through our code again here,
0:04 we've got our little main, it's going to print the headers and the game loop
0:07 and now the real meat of this thing is the game loop.
0:11 So let's suppose that for some reason we would like to take this method
0:15 and put it into a separate module called like game core or something like that.
0:21 So we can highlight this over here,
0:24 and we'll say refactor move,
0:29 this is great, we're going to move it from program.py and the Wizard
0:33 to another module— what do you want to call it, let's call it game core, like this.
0:39 And if we hit go, it's going to say we will create some dunder inits for you,
0:46 it's treating this as if this was creating like a subpackage,
0:52 we're not going to do that, so we can get rid of these,
0:55 because they're not part of this, so packages make sense.
0:59 But notice over here in game core, we now have game loop
1:02 and we're creating our wizard, like so,
1:05 it's trying to do this, but I don't think that's going to work
1:10 because over here we're importing game loop
1:13 and this is going to create an infinite loop,
1:16 so it should have just really taken this with it.
1:19 Okay, I think everything is good,
1:21 It's not quite pep 8— there we go,
1:25 so if we look at our program down here,
1:27 we should have this and game loop— where does game loop come from now,
1:32 it comes from game core, right,
1:34 so now we've got just this one dedicated module
1:36 for working with the game loop, and I think I kind of like that,
1:39 let's run it again make sure everything hangs together.
1:42 Awesome, a Tiger of level 12 has appeared, let's attack it.
1:47 We've handily triumphed over it, how about the Frog,
1:51 there's a Bat, let's fight that—
1:54 ah yes, we've triumphed over it,
1:57 that Dragon, it's a little more tricky,
1:59 oh we have been defeated, the Wizard must rest.
2:04 Okay, so it looks like our refactoring, all of this refractoring we've done,
2:08 is truly a refactoring, in that we've changed our code,
2:12 it has quite a bit of different structure than when I started this chapter,
2:15 and yet, from the outside, it's indistinguishable
2:18 in how it behaves and it runs and so on.
2:21 That's awesome, that's refactoring.