forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathturtle module
156 lines (136 loc) · 3.96 KB
/
turtle module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<h2> Turtle Module </h2>
**Turtle Graphics**
```
import turtle
scrn = turtle.Screen() #creates a graphics window
sponge = turtle.Turtle() #creates a turtle whose name is sponge
sponge.forward(200) #object.method(parameter)
sponge.left(90)
sponge.forward(100)
sponge.right(90)
sponge.forward(100)
sponge.left(90)
sponge.backward(30)
```
```
#import turtle defines the module turtle which will allow you to create a Turtle object and draw with it.
#turtle.Turtle; here "turtle" tells Python that we are referring to the turtle module, which is where the object "Turtle" is found
```
**Creating a Rectangle**
```
import turtle #here loads a module named turtle
#This module brings two new types: the Turtle type, and the Screen type.
scrn = turtle.Screen() #creates a graphics window
#scrn is an instance of Screen class
ciri = turtle.Turtle() #means the Turtle type that is defined within the turtle module
#ciri is an instance of Turtle class
ciri.forward(180) #object.method(parameter)
ciri.left(90)
ciri.forward(75)
ciri.left(90)
ciri.forward(180)
ciri.left(90)
ciri.forward(75)
```
**Creating a triangle**
```
import turtle
scrn = turtle.Screen()
mini = turtle.Turtle()
mini.forward(180)
mini.left(150)
mini.forward(100) #object.method(parameter)
mini.left(60)
mini.forward(100)
```
**Creating rectangle and triangle together**
```
import turtle
scrn = turtle.Screen()
ciri = turtle.Turtle()
ciri.forward(180) #object.method(parameter)
ciri.left(90)
ciri.forward(75)
ciri.left(90)
ciri.forward(180)
ciri.left(90)
ciri.forward(75)
mini = turtle.Turtle()
mini.forward(180)
mini.left(150)
mini.forward(100) #object.method(parameter)
mini.left(60)
mini.forward(100)
```
**Using properties**
```
import turtle
scrn = turtle.Screen()
scrn.bgcolor("lavender")
#the object scrn has color property(which we write as bgcolor)
arin = turtle.Turtle()
arin.color("blue")
arin.pensize(3)
#the object arin has property/attribute - color,pensize
arin.forward(100)
arin.right(90) #name.right(90) goes downward
arin.forward(90)
arina = turtle.Turtle()
arina.color("hot pink")
arin.pensize(4)
arina.forward(100)
arina.left(90) #name.left(90) goes upward
arina.forward(90)
#name.right(value)/name.left(value) works for defining angles(degrees).
```
**Mutliple objects with properties**
```
import turtle
scrn = turtle.Screen()
scrn.bgcolor("lavender")
#the object scrn has color property(which we write as bgcolor)
arin = turtle.Turtle()
arin.color("blue")
arin.pensize(3)
#the object arin has property/attribute - color,pensize
arin.forward(100)
arin.right(90) #name.right(90) goes downward
arin.forward(90)
arina = turtle.Turtle()
arina.color("hot pink")
arin.pensize(4)
arina.forward(100)
arina.left(90) #name.left(90) goes upward
arina.forward(90)
#name.right(value)/name.left(value) works for defining angles(degrees).
ciri = turtle.Turtle()
ciri.color("yellow")
ciri.forward(180) #object.method(parameter)
ciri.left(90)
ciri.forward(75)
ciri.left(90)
ciri.forward(180)
ciri.left(90)
ciri.forward(75)
mini = turtle.Turtle()
mini.forward(180)
mini.left(150)
mini.forward(100) #object.method(parameter)
mini.left(60)
mini.forward(100)
prity = turtle.Turtle()
prity.color("green")
arin.pensize(2)
prity.right(45)
prity.forward(60)
prity.left(90)
prity.forward(100)
zina = turtle.Turtle()
zina.color("red")
zina.pensize(3)
zina.left(180) #notice this
zina.forward(150)
scrn.exitonclick() # wait for a user click on the canvas
#we invoke its exitonclick method of scrn object, the program pauses execution
#and waits for the user to click the mouse somewhere in the window
```