Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I reimplemented the turtle class as suggested by thirtyseven. It is consistent with the api. (i.e. when you turn right in this class, it is the same as turning right in <code>turtle</code>.</p> <p><em>This does not implement all the methods in the api, only common ones. (And the ones you used).</em></p> <p>However, it's short and fairly straightforward to extend. Also, it keeps track of all of the points it has been to. It does this by adding an entry to pointsVisited every time you call forward, backward, or setpos (or any of the aliases for those functions).</p> <pre><code>import math class UndrawnTurtle(): def __init__(self): self.x, self.y, self.angle = 0.0, 0.0, 0.0 self.pointsVisited = [] self._visit() def position(self): return self.x, self.y def xcor(self): return self.x def ycor(self): return self.y def forward(self, distance): angle_radians = math.radians(self.angle) self.x += math.cos(angle_radians) * distance self.y += math.sin(angle_radians) * distance self._visit() def backward(self, distance): self.forward(-distance) def right(self, angle): self.angle -= angle def left(self, angle): self.angle += angle def setpos(self, x, y = None): """Can be passed either a tuple or two numbers.""" if y == None: self.x = x[0] self.y = y[1] else: self.x = x self.y = y self._visit() def _visit(self): """Add point to the list of points gone to by the turtle.""" self.pointsVisited.append(self.position()) # Now for some aliases. Everything that's implemented in this class # should be aliased the same way as the actual api. fd = forward bk = backward back = backward rt = right lt = left setposition = setpos goto = setpos pos = position ut = UndrawnTurtle() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload