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

Slides:



Advertisements
Similar presentations
First of all – lets look at the windows you are going to use. At the top you have a toolbar, with all your various tools you can use when customising your.
Advertisements

Graphics Shapes. Setup for using graphics You have to import the graphics library You can use either “import graphics” or “from graphics import *” or.
Noadswood Science,  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.
Week 8 - Monday.  What did we talk about last time?  StdAudio.
Week 7: Input and Output 1.  Now we are going to talk a little bit about output  You have a lot of experience with System.out.println() and System.out.print()
Python Programming, 2/e1 CS177: Programming in Multimedia Objects Recitation Topic: Graphics Library.
COMPSCI 105 S Principles of Computer Science
Cosc 1P02 Week 2 Lecture slides
Computer Science 1000 LOGO I. LOGO a computer programming language, typically used for education an old language (1967) the basics are simple: move a.
Lecture 2. Review To play with Turtle, we need to download and install followings: 1.JDK 6 2.Dr. Java 3.Sample program (e.g. BookClass)
Logo Lesson 3 TBE 540 Fall 2004 Farah Fisher. Prerequisites for Lesson 3 Before beginning this lesson, the student must be able to… Use simple Logo commands.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
Introduction to TouchDevelop
B.A. (Mahayana Studies) Introduction to Computer Science November March Logo (Part 1) An introduction to Logo: drawing, moving,
+ Learn IT 2014: Python Basic programming. + Agenda Python, PyCharm CE Basic programming concepts Creating an executable file using Py2Exe Where to go.
Area (geometry) the amount of space within a closed shape; the number of square units needed to cover a figure.
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
LOGO SOFTWARE BY: SAVE 9S. INTRODUCTION Logo is a software that can be found at : Shared area> High School > ICT > take home software > LOGO32. This is.
Microsoft® Small Basic
Computer Science 111 Fundamentals of Programming I Introduction to Graphics.
1 CSC 221: Computer Programming I Fall 2011 Fun with turtle graphics  turtle module  relative motion (setup, reset, left, right, forward, backward) 
Main Points: - Python Turtle - Fractals
Art 321 Lecture 7 Dr. J. Parker. Programming In order to ‘make things happen’ on a computer, you really have to program it. Programming is not hard and.
Class 2 Introduction to turtle graphics
Turtle see, turtle do Lesson 1 – Welcome to LOGO.
Agent P, I have been hearing some rumours about a Python Turtle.
1 Turtle Graphics and Math Functions And how you can use them to draw cool pictures!
Introduction to TouchDevelop
MSW Logo By Awin 9s.
1 Building Your Own Turtle Functions For making really cool pictures!
Logo Programming Fall 2011 – Session 4 Programming Class Teacher: M. Taghizadeh Sobhan Highschool.
How to link the robot and the computer (Bluetooth) How to turn on and off How to connect the adaptor Fluke card connection Sec Getting Started How.
CONTROL SYSTEMS Control Systems A command is a directive that performs a specific task An argument is a variable that can be used by the function reveiving.
Georgia Institute of Technology Simulations Barb Ericson Jan 2005.
Processing Workshop. What is processing? “Processing is an open source programming language and environment for people who want to program images, animation,
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
By Liam Lane How To Use MSW LOGO.
1 Building Your Own Turtle Functions For making really cool pictures!
Experimenting with Grammars to Generate L-Systems October 29, 2007
Let’s Learn 3. Modules Saenthong School, January – February 2016
Pablo Revelo. Birds: to 3birds pu home pd lt 80 pu fd 250 pd setpensize 2 setpc "white st bird pu bk random 100 bird pu lt 90 fd random 40 bird ht.
First of all – lets look at the window’s you are going to use. At the top you have a toolbar, with all your various tools you can use when customising.
Turtle Graphics Let’s see what we can draw on Python!
Turtle Graphics Lesson 2 1. There are 3 homeworks to complete during the six lessons of this unit. Your teacher will let you know when a homework has.
Search for it on your computer
LOGO WHAT IS IT? HOW TO USE IT AND HOW USEFUL CAN IT BE?
What is it? How to use it and how useful can it be?
Experimenting with Grammars to Generate L-Systems – in JFLAP March 31, 2011 Prof. Susan Rodger Computer Science Dept.
Using Logo to explore spiral patterns. Paul Broadbent Spiral patterns This is a (1,2,3) spiral path. It repeats lines of three.
Using the Python Turtle
Computer Programming.
Graphics CIS 40 – Introduction to Programming in Python
LOGO BY Kaotip 9S.
LOGO 32 By: Xenon 9S.
Intro CS – Loops & Creating Shapes
Week 8 - Monday CS 121.
Agent P, I have been hearing some rumours about a Python Turtle.
Learning to program with Logo
Frozen Graphics Lesson 3.
CS 100: Roadmap to Computing
CS 100: Roadmap to Computing
Chap. 3 Functions Start Python IDLE (Shell) and open a new file.
Introduction to Turtle Graphics
Using Logo and Logic This presentation uses a version of Logo called StarLogo available through MIT. It can be downloaded for free and installed on your.
Logo Programming.
Prof. Susan Rodger Computer Science Dept
Background for lab: the ord() function
Using Modules.
Bell Work Title: Turtle Intro Date:
Presentation transcript:

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 Click on Install Swampy and do the appropriate install based on your OS. It also requires the installation of Tkinter I download swampy zip Decompress and move swampy directory to C:\Python27\Lib\site-packages\ if you are using Python 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 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

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() heading Required for this lab only!

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.

MORE COMMANDS 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.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!!!

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

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