Presentation is loading. Please wait.

Presentation is loading. Please wait.

Hello, little turtles. Hello, little turtles! There are many modules in Python that provide very powerful feature that we can use in our own program.

Similar presentations


Presentation on theme: "Hello, little turtles. Hello, little turtles! There are many modules in Python that provide very powerful feature that we can use in our own program."— Presentation transcript:

1 Hello, little turtles

2 Hello, little turtles! There are many modules in Python that provide very powerful feature that we can use in our own program. The one we’ll look at allow us to create turtles and get them to draw shapes and patterns.

3 Our first turtle program Create a new turtle And start drawing a rectangle. The variable that refers to our first turtle is called alex

4 Load module named turtle Select type

5 Select Screen type Then create and open a window Which assign to variable n. Every window contains a canvas, which is the area inside the window on which we can draw.

6 Select Turtle type The dot notation turtle.Turtle means “The Turtle type that is defined within the turtle module”.

7 Instruct the object alex to move forward by 50 units.

8 Instruct the object alex to turn by 90 degrees

9 Line 4-6 instruct the object alex to move and to turn by activating alex’s method.

10 The variable n refer to this window. When we invoke its mainloop method, it enters a state where it wait for events (like keypresses, or mouse movement and clicks) Wait for user to close window

11 Method Methods are things object can do. An object can have various methods. An object can also have attributes (sometimes called properties) For example, each turtle has a color attribute. alex.color(“red”) will make alex red and drawing will be red too.

12 The state of the object The color of the turtle, the width of its pen, the position of the turtle within the window are all part of its current state. Similarly, the window object has a background color, and some text in the title bar, and a size and position on the screen. These are all part of the state of the window object.

13

14

15

16

17

18 Extend this program … 1.Modify this program so that before it create the window. It prompts the user to enter the desired background color. It should store the user’s responses in a variable, and modify the color of the window according to the user’s wishes. 2.Do similar changes to allow the user, at runtime, to set tess’ color

19 Instances We can have many turtles. Each of them is called an instance. Each instance has its own attributes and method – So alex might draw with a thin black pen and be at some position – While tess might be going in her own direction with a fat pink pen

20

21

22 The for loop

23

24

25

26 This move alex, but no line is drawn

27

28

29

30

31

32

33 x y 200,0 3.04,34.73 190.98,-33.67 17.77,66.330.00,-0.00


Download ppt "Hello, little turtles. Hello, little turtles! There are many modules in Python that provide very powerful feature that we can use in our own program."

Similar presentations


Ads by Google