1 00:00:07,940 --> 00:00:11,230 Welcome back, we can have 2 00:00:11,230 --> 00:00:15,700 more than one instance of a class and each one will have its own attributes, 3 00:00:15,700 --> 00:00:17,680 its own internal state. 4 00:00:17,680 --> 00:00:22,505 In this code, we'll create two turtles Alex and Tess. 5 00:00:22,505 --> 00:00:25,900 Tess is going to draw a triangle with a thick black pen, 6 00:00:25,900 --> 00:00:28,615 Alex will draw a square with a pink pen. 7 00:00:28,615 --> 00:00:33,650 So let's go through the code, first we will run it. 8 00:00:38,750 --> 00:00:44,170 That is Tess drawing there and now Alex is drawing. 9 00:00:46,580 --> 00:00:51,140 So let's go through the code as before, 10 00:00:51,140 --> 00:00:55,015 we need to import the turtle module 11 00:00:55,015 --> 00:01:01,390 and create the screen that creates the place for us to do the drawings. 12 00:01:01,390 --> 00:01:06,370 In this case, we get that instance bound to the variable WN and we're 13 00:01:06,370 --> 00:01:12,340 calling the BG color method on it to make it be light green as you've seen here. 14 00:01:12,740 --> 00:01:20,970 We create a turtle test and we set its pen size to be five, 15 00:01:20,970 --> 00:01:22,960 that's what makes Tess have 16 00:01:22,960 --> 00:01:27,470 this wider pen instead of just the narrow pen that's the default. 17 00:01:27,470 --> 00:01:34,470 We create a second turtle called Alex and we said Alex's colored to be hot pink. 18 00:01:35,150 --> 00:01:38,180 After that we have a whole segment where we 19 00:01:38,180 --> 00:01:40,910 have the movement commands for Tess that's where 20 00:01:40,910 --> 00:01:47,440 she does her drawing and then below that, 21 00:01:47,440 --> 00:01:52,650 we have some movement commands for Alex. 22 00:01:55,250 --> 00:02:01,970 Finally there's this little wn.exitonclick that just 23 00:02:01,970 --> 00:02:09,270 says that when somebody clicks on the window, it disappears. 24 00:02:11,810 --> 00:02:15,860 Let me make a couple of comments about the coding style 25 00:02:15,860 --> 00:02:20,300 here and one thing to notice is that we 26 00:02:20,300 --> 00:02:29,400 have chunked the code using some wide space and that just makes it easier to read. 27 00:02:31,450 --> 00:02:37,040 That segment that I just marked lines 12 to 17, 28 00:02:37,040 --> 00:02:41,060 is what causes Tess to make a triangle and 29 00:02:41,060 --> 00:02:47,220 then there's a break with a blank line on line 18 and then lines 19 30 00:02:47,220 --> 00:02:53,240 and 20 have her make an extra line that goes off to the left at the end of 31 00:02:53,240 --> 00:02:57,050 the drawing and there's another blank space and we 32 00:02:57,050 --> 00:03:02,685 get from lines 20 to 29, Alex's movements. 33 00:03:02,685 --> 00:03:07,775 We don't have to do this chunking in order to make the picture come out right. 34 00:03:07,775 --> 00:03:10,370 We do this chunking because it makes it easier for people 35 00:03:10,370 --> 00:03:12,830 to read the programs you can sort of look at it and say oh, 36 00:03:12,830 --> 00:03:17,330 there's a chunk of code from 22 to 29 and that's drawing the square. 37 00:03:17,330 --> 00:03:22,020 There's a chunk of code from 12 to 17 and that's drawing a triangle. 38 00:03:22,220 --> 00:03:25,625 One final thing to notice and I'm going to 39 00:03:25,625 --> 00:03:29,430 rerun this so that you will get to see it again. 40 00:03:36,700 --> 00:03:42,180 You might notice that on line 17, 41 00:03:42,180 --> 00:03:45,040 Tess turns to the left by 120 degrees then on 42 00:03:45,040 --> 00:03:49,015 line 19 she turns to the right by a 180 degrees. 43 00:03:49,015 --> 00:03:54,490 We're following a little convention that lines 12 to 17 are drawing 44 00:03:54,490 --> 00:03:59,450 a triangle and putting Tess back into the same orientation that she started in. 45 00:03:59,450 --> 00:04:05,590 It just makes it easier for a programmer to think about it if the net impact of all 46 00:04:05,590 --> 00:04:12,770 of the movements is to leave Tess back in her original position in the same orientation. 47 00:04:12,890 --> 00:04:17,070 Similarly on line 29, 48 00:04:17,070 --> 00:04:20,360 you'll see that after we've completed the square, 49 00:04:20,360 --> 00:04:25,520 we make Alex turn to the left for 90 degrees even though we're not 50 00:04:25,520 --> 00:04:28,040 going to make it move again and that's again just trying to get him 51 00:04:28,040 --> 00:04:31,420 back into the same orientation he was in before. 52 00:04:31,420 --> 00:04:35,330 This would be especially useful if we were going to take the whole block of 53 00:04:35,330 --> 00:04:38,675 code and reuse it 54 00:04:38,675 --> 00:04:42,635 and that's something that we'll see later in the course where we defined functions. 55 00:04:42,635 --> 00:04:46,910 We could make a function to draw a square and wherever he starts, 56 00:04:46,910 --> 00:04:50,435 the turtle would create that square and 57 00:04:50,435 --> 00:04:54,930 leave the turtle in the same spot that it was in before. 58 00:04:56,020 --> 00:05:03,250 So, let's go on and do one of the Parsons problems. 59 00:05:03,250 --> 00:05:06,635 At the bottom of this page, 60 00:05:06,635 --> 00:05:11,395 you'll find more than one Parsons problem I'm just gonna go through one of them. 61 00:05:11,395 --> 00:05:19,229 Here we're going to create a program that has one turtle Jamal, 62 00:05:19,400 --> 00:05:28,210 draw that capital L in blue and then Tina is going to draw on that orange line. 63 00:05:28,210 --> 00:05:32,025 So this is a mixed up code problem. 64 00:05:32,025 --> 00:05:38,000 We have all of the code that we need to have Jamal draw his blue L and Tina draw 65 00:05:38,000 --> 00:05:44,985 her orange but your job is to drag the parts over and drop them off in the right order. 66 00:05:44,985 --> 00:05:48,155 The first thing to remember about this is that, 67 00:05:48,155 --> 00:05:51,830 the very first thing in any turtle program is you have to import 68 00:05:51,830 --> 00:05:55,490 the turtle module so that we can have all of those turtle commands available, 69 00:05:55,490 --> 00:05:58,160 and then you have to create 70 00:05:58,160 --> 00:06:02,890 an instance of the screen so that there's a place for the turtles to draw. 71 00:06:02,890 --> 00:06:09,680 In the instructions, it said that all the stuff of Jamal should happen first. 72 00:06:09,680 --> 00:06:14,170 So those things are going to come before the Tina's stuff. 73 00:06:14,170 --> 00:06:18,140 But we do have a little decision to make should we make the left and 74 00:06:18,140 --> 00:06:23,025 the forward commands first or should we have this larger block first. 75 00:06:23,025 --> 00:06:29,915 The clue here is that you can't have Jamal turn left before you create Jamal. 76 00:06:29,915 --> 00:06:34,370 So we need to have this block first because it has a line of code 77 00:06:34,370 --> 00:06:38,980 jamal= turtle.Turtle that creates the instance Jamal. 78 00:06:38,980 --> 00:06:43,125 The Tina's stuff happens after Jamal. 79 00:06:43,125 --> 00:06:48,780 Got to get that a little lower and finally we have 80 00:06:48,780 --> 00:06:54,695 our exitonclick so that when the user clicks on the drawing it disappears. 81 00:06:54,695 --> 00:06:57,590 Let's check it and make sure I got it right. 82 00:06:57,590 --> 00:07:03,300 Perfect. All right, see you next time.