|
| 1 | +""" |
| 2 | +Demonstrates using OBJECTS via Turtle Graphics. |
| 3 | +
|
| 4 | +Concepts include: |
| 5 | + -- CONSTRUCT an INSTANCE of a CLASS (we call such instances OBJECTS). |
| 6 | + -- Make an object ** DO ** something by using a METHOD. |
| 7 | + -- Reference an object's ** DATA ** by using an INSTANCE VARIABLE. |
| 8 | +
|
| 9 | +Also: |
| 10 | + -- ASSIGNING a VALUE to a NAME (VARIABLE). |
| 11 | +
|
| 12 | +Authors: David Mutchler, Dave Fisher, Valerie Galluzzi, Amanda Stouder, |
| 13 | + their colleagues and PUT_YOUR_NAME_HERE. March 2016. |
| 14 | +""" |
| 15 | +######################################################################## |
| 16 | +# |
| 17 | +# TODO: 1. |
| 18 | +# (Yes, that means for YOU to DO things per these instructions:) |
| 19 | +# |
| 20 | +# On Line 13 above, replace PUT_YOUR_OWN_NAME_HERE with your OWN name. |
| 21 | +# |
| 22 | +# BTW, the green lines above form what is called a DOC-STRING. |
| 23 | +# It documents what this module does, in a way that exterior programs |
| 24 | +# can make sense of. It has no other effect on this program. |
| 25 | +# |
| 26 | +######################################################################## |
| 27 | + |
| 28 | +import rosegraphics as rg |
| 29 | + |
| 30 | +# ---------------------------------------------------------------------- |
| 31 | +# Next two lines after this comment set up a TurtleWindow object |
| 32 | +# for animation. The definition of a TurtleWindow is in the |
| 33 | +# rg (shorthand for rosegraphics) module. |
| 34 | +# ---------------------------------------------------------------------- |
| 35 | +window = rg.TurtleWindow() |
| 36 | +window.delay(20) # Bigger numbers mean slower animation. |
| 37 | + |
| 38 | +# ---------------------------------------------------------------------- |
| 39 | +# Next two lines make (construct) two SimpleTurtle objects. |
| 40 | +# ---------------------------------------------------------------------- |
| 41 | +nadia = rg.SimpleTurtle() |
| 42 | +akil = rg.SimpleTurtle('turtle') |
| 43 | + |
| 44 | +# ---------------------------------------------------------------------- |
| 45 | +# Next lines ask the SimpleTurtle objects to do things: |
| 46 | +# ---------------------------------------------------------------------- |
| 47 | +nadia.forward(100) |
| 48 | +nadia.left(90) |
| 49 | +nadia.forward(200) |
| 50 | + |
| 51 | +akil.right(45) |
| 52 | +akil.backward(50) |
| 53 | +akil.right(60) |
| 54 | + |
| 55 | +nadia.forward(50) |
| 56 | +nadia.left(135) |
| 57 | + |
| 58 | +# ---------------------------------------------------------------------- |
| 59 | +# Next lines set the pen and speed characteristics of the |
| 60 | +# SimpleTurtle objects. The pen characteristic is itself an object |
| 61 | +# that is constructed, of type Pen. |
| 62 | +# ---------------------------------------------------------------------- |
| 63 | +nadia.pen = rg.Pen('blue', 10) # Second argument is the Pen's thickness |
| 64 | +nadia.speed = 10 # big is faster (maxes out about 100), 1 is slowest) |
| 65 | + |
| 66 | +akil.pen = rg.Pen('red', 30) |
| 67 | +akil.speed = 1 |
| 68 | + |
| 69 | +akil.backward(100) |
| 70 | +nadia.forward(100) |
| 71 | + |
| 72 | +nadia.left(60) |
| 73 | +nadia.forward(500) |
| 74 | +nadia.speed = 1 # was 10, so much slower now |
| 75 | +nadia.right(120) |
| 76 | +nadia.forward(200) |
| 77 | + |
| 78 | +######################################################################## |
| 79 | +# |
| 80 | +# TODO: 2. |
| 81 | +# (Yes, that means for YOU to DO things per these instructions:) |
| 82 | +# |
| 83 | +# Run this module by using the green arrow on the toolbar up top. |
| 84 | +# A window will pop up and Turtles will move around. After the |
| 85 | +# Turtles stop moving, click anywhere in the window to close it. |
| 86 | +# |
| 87 | +# Then look at Lines 47 to 56. Run again and see if you can tell |
| 88 | +# what each Turtle command does. We call each such command a |
| 89 | +# METHOD. |
| 90 | +# |
| 91 | +# Then look at Lines 63 to 67. Run again and see if you can tell |
| 92 | +# what is different when, for a particular Turtle, its Turtle |
| 93 | +# characteristics: |
| 94 | +# pen speed |
| 95 | +# are assigned a value. We call such characteristics |
| 96 | +# INSTANCE VARIABLES, aka DATA ATTRIBUTES and (in Java) FIELDS. |
| 97 | +# |
| 98 | +# Note especially that although both Turtle objects have the |
| 99 | +# same NAMES for their instance variables, each Turtle has |
| 100 | +# its own VALUES for those instance variables. For example, |
| 101 | +# nadia's Pen has 'blue' as its color while akil's Pen is 'red'. |
| 102 | +# |
| 103 | +# No need for you to write anything for this part of the TODO. |
| 104 | +# Just change the TODO to DONE at Line 80 above, as usual |
| 105 | +# (and also the one at Line 17 if you have not already done that). |
| 106 | +# |
| 107 | +######################################################################## |
| 108 | + |
| 109 | +######################################################################## |
| 110 | +# |
| 111 | +# TODO: 3. |
| 112 | +# Add a few more line after line 76 above to make one of the |
| 113 | +# existing Turtles move some more and/or have different |
| 114 | +# characteristics. |
| 115 | +# |
| 116 | +# ** Nothing fancy is required. KISS. ** |
| 117 | +# ** A SUBSEQUENT exercise will let you show your creativity. ** |
| 118 | +# |
| 119 | +# As always, test by running the module. |
| 120 | +# |
| 121 | +######################################################################## |
| 122 | + |
| 123 | +######################################################################## |
| 124 | +# |
| 125 | +# TODO: 4. |
| 126 | +# Lines 41 and 42 CONSTRUCT two SimpleTurtle objects |
| 127 | +# and give those objects NAMES: |
| 128 | +# nadia akil |
| 129 | +# BTW, the definition for what a SimpleTurtle object knows and can do |
| 130 | +# is in the rosegraphics, abbreviated as rg, module, but you do NOT |
| 131 | +# need to (and should not) look at that module for this exercise. |
| 132 | +# |
| 133 | +# After the code that you have already added (from previous TODOs), |
| 134 | +# construct another SimpleTurtle object, naming it whatever you want. |
| 135 | +# Names cannot have spaces or special characters, but they can have |
| 136 | +# digits and underscores like this_1_has (get it?). |
| 137 | +# |
| 138 | +# Then, just below the line that you just wrote |
| 139 | +# to construct a SimpleTurtle, add a few more lines that |
| 140 | +# make YOUR SimpleTurtle move around a bit. |
| 141 | +# |
| 142 | +# ** Nothing fancy is required. KISS. ** |
| 143 | +# ** A SUBSEQUENT exercise will let you show your creativity. ** |
| 144 | +# |
| 145 | +# As always, test by running the module. |
| 146 | +# |
| 147 | +######################################################################## |
| 148 | + |
| 149 | +######################################################################## |
| 150 | +# |
| 151 | +# TODO: 5. |
| 152 | +# After you have completed the above (including testing), |
| 153 | +# change the TODO at the beginning of this pink comment to DONE, |
| 154 | +# along with any other numbered TODOs that you have not yet done so. |
| 155 | +# |
| 156 | +# Run one more time to be sure that all is still OK. |
| 157 | +# Ensure that no blue bars on the scrollbar-thing to the right remain. |
| 158 | +# |
| 159 | +# Then COMMIT your work (which turns it in) by selecting (clicking on) |
| 160 | +# the file with the black dot (or the entire project if you prefer) |
| 161 | +# and then do SVN ~ Commit... from the SVN menu at the top. |
| 162 | +# |
| 163 | +# Check that the black symbol beside the file name in the |
| 164 | +# PyDev Package Explorer on the left has gone away. |
| 165 | +# That's how you can tell that you have turned in your work. |
| 166 | +# |
| 167 | +# You can COMMIT as often as you like. DO FREQUENT COMMITS. |
| 168 | +# |
| 169 | +######################################################################## |
| 170 | + |
| 171 | +# ---------------------------------------------------------------------- |
| 172 | +# Next line keeps the window open until the user clicks in the window: |
| 173 | +# ---------------------------------------------------------------------- |
| 174 | +window.close_on_mouse_click() |
0 commit comments