[pythonvis] Thinking Python book file for chapter 4

  • From: "Richard Dinger" <rrdinger@xxxxxxxxxx>
  • To: <pythonvis@xxxxxxxxxxxxx>
  • Date: Fri, 16 May 2014 07:34:51 -0700

For those using the thinking python book, when you get to chapter 4 you are 
instructed to download a graphics module for the chapter.  Since most of us are 
not very interested in looking at graphics, I prepared an alternate file that 
prints messages of what is happening.  The file is attached and instructions 
are in the file.  If attachments are not allowed for this list, I will resend 
in the message text.

Richard
# begin TurtleWorld.py source code file for Chapter 4
""" This file is an alternate TurtleWorld module for the pythonvis group.  The 
original book version
is a turtle graphics module for use in exercises given throughout the chapter.

Instructions:
- put a copy of TurtleWorld.py in the directory you are working in
- change the import statement given in the book to:
  from TurtleWorld import *

Features:
This TurtleWorld.py replacement file will print the result
of each command.  So instead of actually drawing a line from
one coordinate to another this will print that message.
The default working area goes from -500 to +500 for x and y coordinates.
You can alter the working area by argument at initialization.
Moving out-of-bounds will cause a fatal assertion error.
This module is not well tested so you may be able to break it.
"""

import math

# global dimensions of the work area
minX = 0
minY = 0
maxX = 0
maxY = 0

def TurtleWorld(x0=-500, y0=-500, x1=500, y1=500):
  """ set up world."""
  global minX, minY, maxX, maxY

  minX = x0
  minY = y0
  maxX = x1
  maxY = y1
  # end TurtleWorld

def wait_for_user():
  """keep the screen up  """
  raw_input("Press enter to continue")
  # end wait_for_user


# Turtle control functions
def rt(t, mag = 90.0):
  """ turn right """

  t.dir -= mag
  t.dir %= 360.0
  print 'Turn right %.1f degrees to %.1f' % (mag, t.dir)
  # end rt


def lt(t, mag= 90.0):
  """ turn left """

  t.dir += mag
  t.dir %= 360.0
  print 'Turn left %.1f degrees to %.1f' % (mag, t.dir)
  # end lt

def fw(t, mag):
  """ go forward mag steps """

  t.draw(mag)
  # end fw

def bk(t, mag):
  """ go backward mag steps """

  t.draw(-mag)
  # end bk

def pu(t):
  """ pen up """
  t.pen = False
  print 'Pen up'
  # end pu
  
def pd(t):
  """ pen down """
  t.pen = True
  print 'Pen down'
  # end pd




class Turtle(object):
  def __init__(self):
    self.x = 0
    self.y = 0
    self.dir = 0.0
    self.pen = True
    # end __init__

  def draw(self, n):
    """ draw n steps in current direction dir"""
    # calculate components
    angle = self.dir * 2.0 * math.pi/ 360.0
    #print 'angle in rad', angle
    nX = self.x + n * math.cos(angle)
    #print 'nX', nX
    nY = self.y + n * math.sin(angle)
    #print 'nY', nY

    # check if out-of-bounds
    assert minX <= nX <= maxX, 'x=%.1f not in range %.1f to %.1f' % (nX, minX, 
maxX)
    assert minY <= nY <= maxY, 'y=%.1f not in range %.1f to %.1f' % (nY, minY, 
maxY)


    if self.pen:
      print 'Draw',
    else:
      print 'Move',

    print 'from (%.1f, %.1f) to (%.1f, %.1f)' % (self.x, self.y, nX, nY)

    self.x = nX
    self.y = nY

  # end draw



if __name__ == '__main__':
  world = TurtleWorld()
  Bob = Turtle()

  print Bob
  fw(Bob, 10)
  lt(Bob)
  fw(Bob, 10)

  lt(Bob, 45)
  fw(Bob, 10)
  pu(Bob)
  bk(Bob, 20)
  rt(Bob, 90)
  pd(Bob)
  fw(Bob, 10)

  wait_for_user()

# end TurtleWorld.py source code file for Chapter 4

Other related posts:

  • » [pythonvis] Thinking Python book file for chapter 4 - Richard Dinger