Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

Chameleons

# A chameleon is a type of turtle that knows how to change color.
class Chameleon(Turtle):

  # Create/initialize a chameleon. It's green all over by default.
  def __init__(self, world):
    Turtle.__init__(self, 200, 200, world)
    Turtle.setBodyColor(self, green)
    Turtle.setShellColor(self, green)
    self.getPen().setColor(green)

  # How a chameleon changes its color. It has to change the color
  # of its body parts, its shell, and the slime it leaves behind
  # as it moves (i.e. the color of its pen).
  def changeColor(self, color):
    pen = self.getPen()
    self.getPen().setColor(color)
    Turtle.setBodyColor(self, color)
    Turtle.setShellColor(self, color)

# A simulation of a lone kaleidoscopic chameleon! It moves at random for a
# specified number of steps, waiting for a half second before moving again.
import random
import time
def run(numSteps):

  # Create a world containing one chameleon, called ch and a bunch of colors.
  world = makeWorld()
  ch = Chameleon(world)
  colors = [red, yellow, green, cyan, blue, magenta, black, lightGray, darkGray]

  # Have the chameleon keep doing its thing for the specified number of steps
  for step in range(numSteps):

    # Change heading at random, move, change color, and finally rest.
    ch.turn(random.choice(range(360)))
    ch.forward(100) 
    ch.changeColor(random.choice(colors))
    time.sleep(0.5)


Links to this Page