1 / 14

Python Turtle World : chapter 4

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 turtle environment. You first need to download the files from http://www.greenteapress.com/thinkpython/swampy /

corine
Download Presentation

Python Turtle World : chapter 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PythonTurtle 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 Required for this lab only! • 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() 90 heading 180 0 270

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

  6. More Commandshttp://www.greenteapress.com/thinkpython/swampy/TurtleWorld.html Attributes: x: position  y: position r: radius of shellheading: what direction the turtle is  facing, in degrees.  0 is east. pen:boolean, whether the pen is downcolor: string turtle color delay: delay between instructions The can be set directly For example self.x = 10 • 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.

  7. Lets draw a square with a loop • from swampy.TurtleWorldimport * • 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! • 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

More Related