Skip to content

Commit 3fc40bd

Browse files
authored
Update turtle.md
1 parent 7991b7b commit 3fc40bd

File tree

1 file changed

+202
-71
lines changed

1 file changed

+202
-71
lines changed
Lines changed: 202 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,202 @@
1-
# lab01_turtle.py
2-
3-
import turtle
4-
5-
# right arm
6-
turtle.setposition(0, 0)
7-
turtle.left(45)
8-
turtle.forward(35)
9-
10-
# left arm
11-
turtle.penup()
12-
turtle.setposition(0, 0)
13-
turtle.pendown()
14-
turtle.left(90)
15-
turtle.forward(35)
16-
17-
# body
18-
turtle.penup()
19-
turtle.setposition(0, 35)
20-
turtle.pendown()
21-
turtle.left(135)
22-
turtle.forward(70)
23-
24-
# right leg
25-
turtle.left(45)
26-
turtle.forward(45)
27-
turtle.penup()
28-
29-
# left leg
30-
turtle.setposition(0, -35)
31-
turtle.pendown()
32-
turtle.right(90)
33-
turtle.forward(45)
34-
35-
# left eye
36-
turtle.fillcolor('green')
37-
turtle.begin_fill()
38-
turtle.penup()
39-
turtle.setposition(-14, 70)
40-
turtle.pendown()
41-
turtle.circle(5)
42-
turtle.end_fill()
43-
44-
# right eye
45-
turtle.fillcolor('green')
46-
turtle.begin_fill()
47-
turtle.penup()
48-
turtle.setposition(8, 70)
49-
turtle.pendown()
50-
turtle.circle(5)
51-
turtle.end_fill()
52-
53-
# mouth
54-
turtle.penup()
55-
turtle.setposition(-6, 45)
56-
turtle.left(135)
57-
turtle.pendown()
58-
turtle.forward(20)
59-
60-
# head
61-
turtle.penup()
62-
turtle.setposition(0, 35)
63-
turtle.pendown()
64-
turtle.circle(25)
65-
66-
# nose
67-
turtle.penup()
68-
turtle.setposition(-5, 52)
69-
turtle.right(145)
70-
71-
turtle.done()
1+
# Lab 1: Turtle
2+
3+
## Explanation
4+
5+
Turtle is a python `module` that allows us to move a virtual turtle around the screen using programming statements. This turtle has a position and a heading. Below are a list of commands, you can more in the [turtle docs](https://docs.python.org/3.6/library/turtle.html).
6+
7+
- `forward(distance)` moves the turtle forward the given number of pixels
8+
- `left(angle)` and `right(angle)` turns the turtle left or right by the given angle (in degrees)
9+
- `color(color_name)` sets the pen's color, which can be `penup()` `penup()` `penup()`
10+
- `penup()` raises the pen, a line won't be drawn when the turtle moves, `pendown()` lowers the pen again
11+
12+
- `setposition(x, y)` moves the turtle to the given position
13+
14+
- `fillcolor(color_name)` sets the fill color, `begin_fill()` indicates you'd like to begin filling in whatever you draw, `end_fill()` actually fills the shape in.
15+
16+
Use these functions to draw a stick figure with a head, body, two arms, and two legs. Once you're done, go through the examples below and create your own drawing.
17+
18+
## Examples
19+
20+
21+
### Drawing a Square
22+
23+
```python
24+
from turtle import *
25+
26+
forward(100)
27+
left(90)
28+
forward(100)
29+
left(90)
30+
forward(100)
31+
left(90)
32+
forward(100)
33+
left(90)
34+
35+
done()
36+
37+
```
38+
39+
### Filling in a Square
40+
41+
```python
42+
from turtle import *
43+
44+
fillcolor('red')
45+
begin_fill()
46+
47+
forward(100)
48+
left(90)
49+
forward(100)
50+
left(90)
51+
forward(100)
52+
left(90)
53+
forward(100)
54+
left(90)
55+
56+
end_fill()
57+
58+
done()
59+
60+
```
61+
62+
### Drawing a Star
63+
64+
```python
65+
from turtle import *
66+
67+
forward(100)
68+
right(144)
69+
forward(100)
70+
right(144)
71+
forward(100)
72+
right(144)
73+
forward(100)
74+
right(144)
75+
forward(100)
76+
77+
done()
78+
79+
```
80+
81+
82+
### Drawing a Square with a Loop
83+
84+
```python
85+
from turtle import *
86+
87+
i = 0
88+
while i < 4:
89+
forward(100)
90+
left(90)
91+
i = i + 1
92+
93+
done()
94+
```
95+
96+
### Drawing a Circle with a Loop
97+
```python
98+
from turtle import *
99+
100+
i = 0
101+
while i < 100:
102+
forward(2)
103+
left(360/100)
104+
i = i + 1
105+
106+
done()
107+
```
108+
109+
### Drawing a Staircase
110+
111+
```python
112+
from turtle import *
113+
114+
i = 0
115+
while i < 4:
116+
forward(100)
117+
left(90)
118+
forward(100)
119+
right(90)
120+
i = i + 1
121+
done()
122+
```
123+
124+
125+
### Filling in a Square
126+
127+
```python
128+
from turtle import *
129+
130+
fillcolor('red')
131+
begin_fill()
132+
133+
forward(100)
134+
left(90)
135+
forward(100)
136+
left(90)
137+
forward(100)
138+
left(90)
139+
forward(100)
140+
left(90)
141+
142+
end_fill()
143+
144+
done()
145+
146+
```
147+
148+
### Draw an N-Sided figure
149+
```python
150+
from turtle import *
151+
152+
edge_length = 100
153+
n_sides = 5
154+
155+
i = 0
156+
while i < n_sides:
157+
forward(edge_length/n_sides)
158+
right(360/n_sides)
159+
i = i + 1
160+
161+
done()
162+
163+
```
164+
165+
166+
### 8-Sided Spiral
167+
168+
```python
169+
from turtle import *
170+
171+
fillcolor('blue')
172+
173+
n_sides = 8
174+
edge_length = 0
175+
176+
i = 0
177+
begin_fill()
178+
while i < 150:
179+
forward(edge_length)
180+
right(360/n_sides)
181+
i = i + 1
182+
edge_length = edge_length + 1
183+
end_fill()
184+
done()
185+
186+
```
187+
188+
### Expanding Star
189+
190+
```python
191+
from turtle import *
192+
193+
edge_length = 0
194+
i = 0
195+
while i < 100:
196+
forward(edge_length)
197+
right(144)
198+
199+
edge_length += 4
200+
201+
done()
202+
```

0 commit comments

Comments
 (0)