PYTHON TURTLE WORLD CHAPTER 4 FROM THINK PYTHON

  • Slides: 14
Download presentation
PYTHON TURTLE WORLD : CHAPTER 4 FROM THINK PYTHON HOW TO THINK LIKE A

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

DOWNLOAD SWAMPY Swampy is a collection of python files/libs that create a Logo like

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: Python 27Libsite-packages if you are using Python 2. 7. 6 Note: You can do all this in Linux, Macintosh or Windows

TEST SWAMPY Run the python GUI and create a new file and save it

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 python. Programs Type in the following program and run it You should get a popup window that shown on the right. The little turtle has drawn a square

TEST PROGRAM Required for this lab only! import sys. path. append("Y: Bolin College of

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

TURTLE COMMANDS bk(self, dist=1) unbound Turtle method Moves the turtle backward by the given

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

MORE COMMANDS HTTP: //WWW. GREENTEAPRESS. COM/THINKPYTHON/SWAMPY/TURTLEWORLD. HTML draw(self) Draws the turtle. get_heading(self) Returns the

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

LETS DRAW A SQUARE WITH A LOOP from swampy. Turtle. World import * world

LETS DRAW A SQUARE WITH A LOOP from swampy. Turtle. World import * world = Turtle. World() 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!!!

LETS DRAW THINGS Write a program to draw each of the following. Use for

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

LETS USE FUNCTIONS TO HELP US HERE Fill in the following function so that

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.

HOW ABOUT DRAWING A REGULAR POLYGON OF N SIDES? If N = 6 What

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

COMPLETE THE FOLLOWING def polygon(self, n, length) angle = 360. 0/n for i in

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!!!

DRAW A CIRCLE OF RADIUS R!!!! We will approximate this with the polygon() function

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!!!

SIMULATING A RANDOM MOVING TURTLE In this project we will first develop a turtle

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

THE CODE from swampy. Turtle. World import * from random import * world =

THE CODE from swampy. Turtle. World import * from random import * world = Turtle. World() 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