forked from CSSE120StartingCode/ObjectsFunctionsAndMethods
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm4_functions_vs_methods.py
More file actions
296 lines (253 loc) · 9.7 KB
/
m4_functions_vs_methods.py
File metadata and controls
296 lines (253 loc) · 9.7 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""
Demonstrates using (calling) FUNCTIONS and using (calling) METHODS:
-- what is similar
-- how they differ.
Authors: David Mutchler, Dave Fisher, Valerie Galluzzi, Amanda Stouder,
their colleagues and Benjamin Feaster.
""" # DONE: 1. PUT YOUR NAME IN THE ABOVE LINE.
########################################################################
#
# DONE: 2.
# READ this comment, ASKING QUESTIONS as needed to understand it.
#
# For objects that are CONSTRUCTED, we use the DOT notation
# to ask them to do things or to change their characteristics.
# For example:
# nadia = rg.SimpleTurtle()
# nadia.pen = rg.Pen('blue', 1)
# nadia.forward(100)
#
# In the above example:
#
# pen is an INSTANCE VARIABLE (aka DATA ATTRIBUTE, FIELD)
# of SimpleTurtles. It is a CHARACTERISTIC of
# SimpleTurtles. Each SimpleTurtle has its own VALUE
# of that characteristic.
#
# forward is a METHOD (aka FUNCTION ATTRIBUTE)
# of SimpleTurtles. It is something that any
# SimpleTurtle can DO. It is just like a FUNCTION
# except for details about the way that it is called.
#
# The statement
# nadia.forward(100)
# CALLS the forward METHOD on nadia, sending that method the number 100 and making the method run (execute).
#
# We ** define ** FUNCTIONS to give NAMES to blocks of code (note FUNCTIONS and METHODS are different).
# For example:
#
# def turtle3():
# maja = rg.SimpleTurtle()
# maja.pen = rg.Pen('green', 10)
# maja.paint_bucket = rg.PaintBucket('black')
# ...
#
# We ** call ** FUNCTIONS (that is, we make them run) in a way that is similar to how we call METHODS,
# but WITHOUT the DOT notation. For example
# def main():
# turtle3()
#
# Run this module and review the existing code. Can you identify the functions calls and methods calls?
#
# When you believe you understand the differences and similarities
# between calling a FUNCTION and calling a METHOD, change the above TO DO to DONE.
#
########################################################################
# ----------------------------------------------------------------------
# There are MORE TODOs further down in this module!
# ----------------------------------------------------------------------
import rosegraphics as rg
def main():
"""
Makes a TurtleWindow,
calls the other functions in this module to test/demo them,
and waits for the user to click anywhere in the window to close it.
"""
window = rg.TurtleWindow()
# Make the animation go much faster.
# First number: bigger means faster.
# Second number: bigger means slower.
window.tracer(1, 1)
jump_and_move_turtle(100, 50, 200, -100)
turtle = rg.SimpleTurtle('square')
draw_many_squares(turtle, 3, 75, 15)
turtle3()
# When the TODOs ask you to test YOUR code, put YOUR tests here:
try_methods()
try_functions()
try_methods_and_functions()
window.close_on_mouse_click()
def jump_and_move_turtle(x1, y1, x2, y2):
"""
Constructs a thick, slow, magenta SimpleTurtle.
Jumps that SimpleTurtle (without drawing) to (x1, y1),
then moves that Turtle (while drawing) to (x2, y2).
"""
# ------------------------------------------------------------------
# Students:
# Do NOT touch this function - it has no TO DO in it.
# Do NOT copy code from this function.
#
# Instead, ** CALL ** this function as needed in the other problems.
# ------------------------------------------------------------------
jumper = rg.SimpleTurtle()
jumper.pen = rg.Pen('magenta', 20)
jumper.speed = 1
jumper.pen_up()
jumper.go_to(rg.Point(x1, y1))
jumper.pen_down()
jumper.go_to(rg.Point(x2, y2))
def draw_many_squares(my_turtle, number_of_squares, size, twist):
"""
Makes the given SimpleTurtle object draw:
-- many squares (how many? answer: NUMBER_OF_SQUARES)
where each square:
-- has the same size (what size? answer: SIZE)
and each square is:
-- "twisted" a bit from the previous one (how much? TWIST degrees)
NOTE: The 3 lines below that begin with :type are called
"type hints". They make the "dot" trick work more effectively.
We will include them in function specifications routinely.
:type my_turtle: rg.SimpleTurtle
:type number_of_squares: int
:type size: int
:type twist: int
"""
# ------------------------------------------------------------------
# Students:
# Do NOT touch this function - it has no TO DO in it.
# Do NOT copy code from this function.
#
# Instead, ** CALL ** this function as needed in the other problems.
# ------------------------------------------------------------------
for _ in range(number_of_squares):
my_turtle.draw_square(size)
my_turtle.left(twist)
def turtle3():
"""
Constructs a classic SimpleTurtle and asks it to draw a
"ball on pole" shape.
"""
# ------------------------------------------------------------------
# Students:
# Do NOT touch this function - it has no TO DO in it.
# ------------------------------------------------------------------
maja = rg.SimpleTurtle()
maja.pen = rg.Pen('green', 10)
maja.paint_bucket = rg.PaintBucket('black')
maja.right(135)
maja.forward(300)
maja.begin_fill()
maja.draw_circle(50)
maja.end_fill()
def try_methods():
"""
Constructs a SimpleTurtle and sets its pen to a new rg.Pen
that is 'brown' with thickness 5.
Then makes the SimpleTurtle move as follows (in the order listed):
-- forward 150 units
-- left 90 degrees
-- forward 50 units
-- backward 100 units
"""
ben = rg.SimpleTurtle()
ben.pen = rg.Pen('brown', 5)
ben.forward(150)
ben.left(90)
ben.forward(50)
ben.backward(100)
####################################################################
# DONE: 3. Implement this function, per its doc-string above.
# Put a statement in main to test this function
# (by calling this function).
####################################################################
def try_functions():
"""
Causes several SimpleTurtles to do the following:
-- One jumps to (200, 100), then moves (while drawing) to (300, 30)
-- One jumps to (100, 200), then moves (while drawing) to (0, 0)
-- One jumps to (-50, 50), then moves (while drawing) to (100, 100)
"""
jump_and_move_turtle(200, 100, 300, 30)
jump_and_move_turtle(100, 200, 0, 0)
jump_and_move_turtle(-50, 50, 100, 100)
####################################################################
# DONE: 4. Implement this function, per its doc-string above.
# Put a statement in main to test this function
# (by calling this function). IMPORTANT, IMPORTANT, IMPORTANT:
# Keep reading the rest of this TO DO before doing the above!
#
# IMPORTANT: This function requires
# ** exactly 3 lines **
# If you think it needs more, ** ASK FOR HELP. **
# HINT: see jump_and_move_turtle above.
#
####################################################################
def try_methods_and_functions():
"""
Constructs a SimpleTurtle and sets its pen to a new rg.Pen
that is 'blue' with thickness 5.
Then makes the SimpleTurtle do the following (in the order listed):
1. Go backward 150 units.
2. Change its speed to 1 (slowest).
Draw 2 squares whose size (width and height) are 100,
each "twisted" from the previous by 30 degrees.
3. Change its speed to 5 (faster).
Change its Pen's color to 'red'.
Draw 10 squares whose size (width and height) are 50,
each "twisted" from the previous by 15 degrees.
4. Change its speed to 100 (about the fastest possible).
Change its Pen's thickness to 35.
Draw 8 squares whose size (width and height) are 300,
each "twisted" from the previous by 60 degrees.
5. Changes its Pen to be a NEW Pen whose color is 'black'
and whose thickness is 3.
6. Goes backward 200 units.
7. Draw a CIRCLE whose radius is 30.
8. Draw a SQUARE whose sides are each of length 50.
"""
#0
neb = rg.SimpleTurtle()
neb.pen = rg.Pen('blue', 5)
#1
neb.backward(100)
#2
neb.speed = 1
for _ in range(2):
neb.draw_square(100)
neb.right(30)
#3
neb.speed = 5
neb.pen = rg.Pen('red', 5)
for _ in range(10):
neb.draw_square(50)
neb.right(15)
#4
neb.speed = 100
neb.pen = rg.Pen('red', 35)
for _ in range(8):
neb.draw_square(300)
neb.right(60)
#5
neb.pen = rg.Pen('black', 3)
#6
neb.backward(200)
#7
neb.draw_circle(30)
#8
neb.draw_square(50)
####################################################################
# DONE: 5. Implement this function, per its doc-string above.
# Put a statement in main to test this function
# (by calling this function). IMPORTANT, IMPORTANT, IMPORTANT:
# Keep reading the rest of this TO DO before doing the above!
#
# IMPORTANT: This function should ** CALL ** the
# draw_many_squares
# function defined above. If you don't see why, ** ASK FOR HELP. **
#
####################################################################
# ----------------------------------------------------------------------
# Calls main to start the ball rolling.
# ----------------------------------------------------------------------
main()