File tree Expand file tree Collapse file tree
python_crash_tutorial/Ch1 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # bounce.py
2+ # Bounce the turtle.
3+
4+ from turtle import *
5+
6+ def move (distance ):
7+ """Move forward, reversing direction at right side."""
8+ forward (distance )
9+ if xcor () > 320 :
10+ setheading (180 )
11+
12+ def main ():
13+ shape ("circle" )
14+ penup ()
15+ speed (0 )
16+ for _ in range (100 ):
17+ move (10 )
18+ exitonclick ()
19+
20+ main ()
Original file line number Diff line number Diff line change 1+ # bowtie.py
2+ # Draw a bowtie
3+
4+ from turtle import *
5+
6+ pensize (7 )
7+ penup ()
8+ goto (- 200 , - 100 )
9+ pendown ()
10+ fillcolor ("red" )
11+ begin_fill ()
12+ goto (- 200 , 100 )
13+ goto (200 , - 100 )
14+ goto (200 , 100 )
15+ goto (- 200 , - 100 )
16+ end_fill ()
17+
18+ exitonclick ()
Original file line number Diff line number Diff line change 1+ # circle_at.py
2+ # Use a function to draw circles
3+
4+ from turtle import *
5+
6+ def circle_at (x , y , r ):
7+ """Draw circle with center (x, y) radius r."""
8+ penup ()
9+ goto (x , y - r )
10+ pendown ()
11+ setheading (0 )
12+ circle (r )
13+
14+ circle_at (- 200 , 0 , 20 )
15+ begin_fill ()
16+ circle_at (0 , 0 , 100 )
17+ end_fill ()
18+ circle_at (200 , 0 , 20 )
19+ hideturtle ()
20+ exitonclick ()
Original file line number Diff line number Diff line change 1+ # face.py
2+ # Draw a face using functions.
3+
4+ from turtle import *
5+
6+ def circle_at (x , y , r ):
7+ """Draw circle with center (x, y) radius r."""
8+ penup ()
9+ goto (x , y - r )
10+ pendown ()
11+ setheading (0 )
12+ circle (r )
13+
14+ def eye (x , y , radius ):
15+ """Draw an eye centered at (x, y) of given radius."""
16+ circle_at (x , y , radius )
17+
18+ def face (x , y , width ):
19+ """Draw face centered at (x, y) of given width."""
20+ circle_at (x , y , width / 2 )
21+ eye (x - width / 6 , y + width / 5 , width / 12 )
22+ eye (x + width / 6 , y + width / 5 , width / 12 )
23+
24+ def main ():
25+ face (0 , 0 , 100 )
26+ face (- 140 , 160 , 200 )
27+ exitonclick ()
28+
29+ main ()
Original file line number Diff line number Diff line change 1+ # mycircle.py
2+ # Mimic circle() with adaptive radius.
3+
4+ def mycircle (radius ):
5+ """Draw circle as polygon."""
6+ if radius < 20 :
7+ sides = 10
8+ elif radius < 100 :
9+ sides = 30
10+ else :
11+ sides = 50
12+ polygon (sides , 6.28 * radius / sides )
Original file line number Diff line number Diff line change 1+ # polygon.py
2+ # Draw regular polygons
3+
4+ from turtle import *
5+
6+ def polygon (n , length ):
7+ """Draw n-sided polygon with given side length."""
8+ for _ in range (n ):
9+ forward (length )
10+ left (360 / n )
11+
12+ def main ():
13+ """Draw polygons with 3-9 sides."""
14+ for n in range (3 , 10 ):
15+ polygon (n , 80 )
16+ exitonclick ()
17+
18+ main ()
Original file line number Diff line number Diff line change 1+ # randomwalk.py
2+ # Draw path of a random walk.
3+
4+ from turtle import *
5+ from random import randrange
6+
7+ def random_move (distance ):
8+ """Take random step on a grid."""
9+ left (randrange (0 , 360 , 90 ))
10+ forward (distance )
11+
12+ def main ():
13+ speed (0 )
14+ while abs (xcor ()) < 200 and abs (ycor ()) < 200 :
15+ random_move (10 )
16+ exitonclick ()
17+
18+ main ()
Original file line number Diff line number Diff line change 1+ # spiral.py
2+ # Draw spiral shapes
3+
4+ from turtle import *
5+
6+ def spiral (firststep , angle , gap ):
7+ """Move turtle on a spiral path."""
8+ step = firststep
9+ while step > 0 :
10+ forward (step )
11+ left (angle )
12+ step -= gap
13+
14+ def main ():
15+ spiral (100 , 71 , 2 )
16+ exitonclick ()
17+
18+ main ()
Original file line number Diff line number Diff line change 1+ # square.py
2+ # Draw a square
3+
4+ from turtle import *
5+
6+ forward (100 )
7+ left (90 )
8+ forward (100 )
9+ left (90 )
10+ forward (100 )
11+ left (90 )
12+ forward (100 )
13+ left (90 )
14+
15+ exitonclick ()
You can’t perform that action at this time.
0 commit comments