forked from CSSE120StartingCode/IntroductionToPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm4e_loopy_turtles.py
More file actions
76 lines (62 loc) · 2.22 KB
/
m4e_loopy_turtles.py
File metadata and controls
76 lines (62 loc) · 2.22 KB
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
"""
Demonstrates:
-- LOOPS (doing something repeatedly) and
-- using OBJECTS
via Turtle Graphics.
Concepts include:
* Using OBJECTS:
-- CONSTRUCT an INSTANCE of a CLASS (we call such instances OBJECTS).
-- Make an object ** DO ** something by using a METHOD.
-- Reference an object's ** DATA ** by using an INSTANCE VARIABLE.
* LOOPS:
-- Using a FOR expresssion like this:
for k in range(41):
blah
blah
blah
The above repeats the body of the FOR expression 41 times.
The name k is:
0 the first time the body runs,
then 1 the next time the body runs,
then 2 the next time the body runs,
etc
then 40 the last time the body runs.
* ASSIGNMENT and NAMES
-- ASSIGNING a VALUE to a NAME (VARIABLE), as in these examples:
jack = 45
jill = 'ran down the hill'
size = size - 12
-- The computer's STATE during a run of a program is the set
of current values of all the names that the computer is tracking.
* The DOT trick: Type expressions like the following,
pausing after typing the DOT (period, full stop).
The window that pops up give lots of clues for what you can do!
rg.
rg.SimpleTurtle().
rg.Pen().
rg.PaintBucket()
Authors: David Mutchler, Dave Fisher, Valerie Galluzzi, Amanda Stouder,
and their colleagues.
"""
import rosegraphics as rg
window = rg.TurtleWindow()
blue_turtle = rg.SimpleTurtle('turtle')
blue_turtle.pen = rg.Pen('midnight blue', 3)
blue_turtle.speed = 10 # Fast
# The first square will be 300 x 300 pixels:
size = 300
# Do the indented code 13 times. Each time draws a square.
for k in range(13):
# Put the pen down, then draw a square of the given size:
blue_turtle.draw_square(size)
# Move a little below and to the right of where the previous
# square started. Do this with the pen up (so nothing is drawn).
blue_turtle.pen_up()
blue_turtle.right(45)
blue_turtle.forward(10)
blue_turtle.left(45)
# Put the pen down again (so drawing resumes).
# Make the size for the NEXT square be 12 pixels smaller.
blue_turtle.pen_down()
size = size - 12
window.close_on_mouse_click()