Presentation is loading. Please wait.

Presentation is loading. Please wait.

PYTHON TURTLE WORLD : CHAPTER 4 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.

Similar presentations


Presentation on theme: "PYTHON TURTLE WORLD : CHAPTER 4 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST."— Presentation transcript:

1 PYTHON TURTLE WORLD : CHAPTER 4 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST

2 DOWNLOAD SWAMPY Swampy is a collection of python files/libs that create a Logo like turtle environment. You first need to download the files from http://www.greenteapress.com/thinkpython/swampy/ Click on Install Swampy and do the appropriate install based on your OS. It also requires the installation of Tkinter I download swampy-2.1.7.zip Decompress and move swampy directory to C:\Python27\Lib\site-packages\ if you are using Python 2.7.6 Note: You can do all this in Linux, Macintosh or Windows

3 TEST SWAMPY Run the python GUI and create a new file and save it in a directory of your choice testit.py. Pick a directory name like pythonPrograms Type in the following program and run it You should get a popup window that that shown on the right. The little turtle has drawn a square

4 TEST PROGRAM import sys sys.path.append("Y:\Bolin College of Science and Mathematics\Richard Simpson\site-packages") #Test TurtleWorld from swampy.TurtleWorld import * world = TurtleWorld() bob = Turtle()  Create a turtle named bob. Initial heading is east i.e. 0 bob.delay=.01 fd(bob,100) rt(bob,90) fd(bob,100) rt(bob,90) fd(bob,100) rt(bob,90) fd(bob,100) wait_for_user() 0 180 90 270 heading Required for this lab only!

5 TURTLE COMMANDS bk(self, dist=1) unbound Turtle methodTurtle Moves the turtle backward by the given distance. fd(self, dist=1) unbound Turtle methodTurtle Moves the turtle foward by the given distance. lt(self, angle=90) unbound TurtleTurtle methodTurns left by the given angle. pd(self) unbound Turtle methodTurtle Puts the pen down (active). pu(self) unbound Turtle methodTurtle Puts the pen up (inactive). rt(self, angle=90) unbound Turtle methodTurtle Turns right by the given angle. set_color(self, color) unbound Turtle methodTurtle Changes the color of the turtle.

6 MORE COMMANDS HTTP://WWW.GREENTEAPRESS.COM/THINKPYTHON/SWAMPY/TURTLEWORLD.HTML draw(self) Draws the turtle. get_heading(self) Returns the current heading in degrees. 0 is east. get_x(self) Returns the current x coordinate. get_y(self) Returns the current y coordinate. set_pen_color(self, color) Changes the pen color of the turtle. step(self) Takes a step. Default step behavior is forward one pixel. die(self) Removes the animal from the world and undraws it. undraw(self) Undraws the animal. Attributes: x: position y: position r: radius of shell heading: what direction the turtle is facing, in degrees. 0 is east. pen: boolean, whether the pen is down color: string turtle color delay: delay between instructions The can be set directly For example self.x = 10

7 LETS DRAW A SQUARE WITH A LOOP from swampy.TurtleWorld import * world = TurtleWorld() bob = Turtle()  Create a turtle named bob. Initial heading is east i.e. 0 and initial location is at (0,0) bob.delay=.01 for i in range(4):  This is the for loop fd(bob,100) rt(bob, 90) wait_for_user() These 2 instructions are performed 4 times Four space indention is required!!!

8 LETS DRAW THINGS Write a program to draw each of the following. Use for loop! 1. 2. 3. 4. Remember you can bob.x=0 and bob.y=0

9 LETS USE FUNCTIONS TO HELP US HERE Fill in the following function so that it draws a square when called. It has to parameters self and length. Self is the turtle name and length is the length of the edge of the square def square(self, length): fd(self,length) rt(self, 90) # Run it square(bob,200) #type this in and test it out.

10 HOW ABOUT DRAWING A REGULAR POLYGON OF N SIDES? If N = 6 What is this angle? What is the angle in terms of N? 360.0/N

11 COMPLETE THE FOLLOWING def polygon(self, n, length) angle = 360.0/n for i in range(n): fd(self,length) rt(self,angle) # call it polygon(bob, 6) # Advanced problem: Change this so the polygon is drawn centered in the window!!!

12 DRAW A CIRCLE OF RADIUS R!!!! We will approximate this with the polygon() function we already wrote. def circle(self, radius) How can we use C = 2πr to help us here? Hint: as the value of n ( number of sides of the polygon) gets large n*length_of_edge ≈ C (Do you agree?) So Pick n to be C/length_of_edge where length_of_edge = 3 (some small value) This way we use only enough edges as are needed!!!

13 SIMULATING A RANDOM MOVING TURTLE In this project we will first develop a turtle program that creates a turtle the moves around randomly. We first have to pick a random direction the turn to. Python has a built in function called random. We can use it by importing it from random import * Here is an example that returns a number from 0 to 1 x = random() If we want a random number from 0 to 100 we could x = random()*100

14 THE CODE from swampy.TurtleWorld import * from random import * world = TurtleWorld() bob = Turtle() bob.delay=.01 for i in range(1000): # 1000 random steps fd( bob,3) # the following creates a random # number from -45 to +45 angle = random()*90-45 rt(bob,angle) die(bob) wait_for_user() 300 steps example


Download ppt "PYTHON TURTLE WORLD : CHAPTER 4 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST."

Similar presentations


Ads by Google