Difference between revisions of "Python:Turtle"

From PrattWiki
Jump to navigation Jump to search
 
(Albert Schuller's example)
 
(9 intermediate revisions by the same user not shown)
Line 5: Line 5:
 
* Python.org, [https://docs.python.org/3.7/library/turtle.html Turtle graphics]
 
* Python.org, [https://docs.python.org/3.7/library/turtle.html Turtle graphics]
  
== Common Script ==
+
== Common Commands to Start and End ==
 
For EGR 103, any script using turtles will need to import the module, create the screen, and create the turtle.  A bug at the moment makes it such that creating a turtle only works every other time.  Given that, the following code will be used:
 
For EGR 103, any script using turtles will need to import the module, create the screen, and create the turtle.  A bug at the moment makes it such that creating a turtle only works every other time.  Given that, the following code will be used:
 
<source lang=python>
 
<source lang=python>
Line 16: Line 16:
 
</source>
 
</source>
  
Furthermore, another issue is that the turtle screen does not like to be moved or messed with until some kind of final screen command is issued.  For that reason, if you are typing turtle commands in the console to test them out, do not or even activate the turtle screen.  When you are ready to close the screen, or at the end of your script, you should issue the command:
+
* For Windows, another issue is that the turtle screen does not like to be moved or messed with until some kind of final screen command is issued.  For that reason, if you are typing turtle commands in the console to test them out, do not move or even activate the turtle screen.  When you are ready to close the screen you should issue the command:
 
<source lang=python>
 
<source lang=python>
 
wn.exitonclick()
 
wn.exitonclick()
 
</source>
 
</source>
 
Once that command runs, you can move the turtle window around or close it by clicking in the window.
 
Once that command runs, you can move the turtle window around or close it by clicking in the window.
 +
* Of course, what saves Windows breaks a MAC.  MAC folks can either leave the wn.exitonclick() command off or, if you include it, after you close the Python Turtle Graphics window you will have to right-click the Python icon in your system tray and close Python.  It will start right back up, but that's the only way to end a turtle session if wn.exitonclick() is used.
 +
* Given that, in scripts you will want to include code that checks to see what platform you are on to decide if it should run the wn.exitonclick().  The easiest way to do this is with:
 +
<source lang=python>
 +
from sys import platform
 +
if platform=='win32':
 +
    wn.exitonclick()
 +
</source>
  
 
== Common Commands ==
 
== Common Commands ==
Line 45: Line 52:
  
 
== Sample Programs ==
 
== Sample Programs ==
 +
=== Albert Schuller's example ===
 +
* Here is the EGR 103-version of the program to go with [https://vimeo.com/98180020 Albert Schueller's Flow of Execution Video]:
 +
<html>
 +
<iframe src="https://trinket.io/embed/python/4f063c5137?start=result" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 +
</html>
 +
<!--
 +
<div class="mw-collapsible mw-collapsed">
 +
<source lang=python>
 +
# Revised Version of Albert Schueller's Code
 +
</source>
 +
<div class="mw-collapsible-content">
 +
<source lang=python>
 +
 +
"""
 +
debug_turtle
 +
Based on Ch4Example2.py by Albert Schueller:
 +
https://vimeo.com/98180020
 +
 +
Edits by Michael R. Gustafson II:
 +
try/except structure added
 +
mainloop() changed exitonclick()
 +
"""
 +
 +
import numpy as np
 +
import turtle
 +
__import__("turtle").__traceable__ = False
 +
 +
def draw_multicolor_square(t, sz):
 +
    for i in ["red", "purple", "hotpink", "blue"]:
 +
        t.color(i)
 +
        t.forward(sz)
 +
        t.left(90)
 +
 +
wn = turtle.Screen()
 +
wn.clearscreen()
 +
wn.bgcolor("lightgreen")
 +
 +
try:
 +
    tess = turtle.Turtle()
 +
except:
 +
    tess = turtle.Turtle()
 +
 +
tess.pensize(3)
 +
 +
size = 20
 +
for i in range(15):
 +
    draw_multicolor_square(tess, size)
 +
    size = size + 10
 +
    tess.forward(10)
 +
    tess.right(18)
 +
 +
from sys import platform
 +
if platform=='win32':
 +
    wn.exitonclick()
 +
</source>
 +
</div>
 +
</div>
 +
-->
 +
 +
=== Geometric Snowflake ===
 +
<html>
 +
<iframe src="https://trinket.io/embed/python/d4bac57d58?start=result" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
 +
</html>
 +
 +
== Saving Pictures ==
 +
[[File:Koch reindeer.PNG|thumb|I call this the Level 5 Koch Reindeer Curve.  It isn't what you will get but it is a screenshot of a slightly different fractal.]]
 +
For Lab 5 of Fall 2018, you will be taking a screen picture of the Python Turtle Graphics window to document your work.  Save the picture in some convenient graphics format (PNG, JPG, or GIF) and when you turn in Lab 5, also attach the graphics file.
 +
* For Windows, search for the Snipping Tool.  When it opens, change the Mode to Window Snip.  You will then be able to click on the Python Turtle Graphics window and it will be copied as a graphic in the snipping tool.
 +
* For OSX, type shift-command-4; when the screen capture pointer comes up, hit space.  Hover over the window you want to capture and then click.  The picture will be saved on the desktop as a png file. See [https://support.apple.com/en-us/HT201361 How to take a screenshot] for more options.
 +
 +
 +
== More Information ==
 +
=== Colors ===
 +
For Lab 5, if you want the segments in your curve to be different colors, there
 +
is a \LIpy{color} function that applies to turtles; this function
 +
accepts either common color names as strings or a list of three values
 +
between 0 and 1 to indicate how much red, green, and blue you want in
 +
your color.  Here are some examples:
 +
<source lang=python>
 +
wn = turtle.Screen()
 +
try:
 +
    kasa = turtle.Turtle()
 +
except:
 +
    kasa = turtle.Turtle() 
 +
kasa.color('blue')
 +
kasa.forward(100)
 +
kasa.color([.9, .5, .2]) # lots of red, some green, a little blue = orange
 +
kasa.left(90)
 +
kasa.forward(100)
 +
 +
from sys import platform
 +
if platform=='win32':
 +
    wn.exitonclick()
 +
</source>
 +
 +
A list of color names is at the [https://www.webucator.com/blog/2015/03/python-color-constants-module/ Python Color Constants Module] page on the Webucator Blog.  The RGB lists on that page would need to be divided by 255 for the
 +
<code>color()</code> command.  Alternately, if you issue the command
 +
<source lang=python>
 +
turtle.colormode(255)
 +
</source>
 +
you can use values between 0 and 255 instead of between 0 and 1.

Latest revision as of 02:53, 15 October 2019

This is the (very drafty) page for information about using turtle graphics in Python for EGR 103.

References

Primary references are:

Common Commands to Start and End

For EGR 103, any script using turtles will need to import the module, create the screen, and create the turtle. A bug at the moment makes it such that creating a turtle only works every other time. Given that, the following code will be used:

import turtle
wn = turtle.Screen()
try:
    kasa = turtle.Turtle()
except:
    kasa = turtle.Turtle()
  • For Windows, another issue is that the turtle screen does not like to be moved or messed with until some kind of final screen command is issued. For that reason, if you are typing turtle commands in the console to test them out, do not move or even activate the turtle screen. When you are ready to close the screen you should issue the command:
wn.exitonclick()

Once that command runs, you can move the turtle window around or close it by clicking in the window.

  • Of course, what saves Windows breaks a MAC. MAC folks can either leave the wn.exitonclick() command off or, if you include it, after you close the Python Turtle Graphics window you will have to right-click the Python icon in your system tray and close Python. It will start right back up, but that's the only way to end a turtle session if wn.exitonclick() is used.
  • Given that, in scripts you will want to include code that checks to see what platform you are on to decide if it should run the wn.exitonclick(). The easiest way to do this is with:
from sys import platform
if platform=='win32':
    wn.exitonclick()

Common Commands

This section will highlight the most common commands that you can apply to turtles and screens. The full list is at Overview of available Turtle and Screen Methods

Active Turtle Commands

  • forward() or fd() and backward or bk()
  • left() or lt() and right() or rt()
  • setposition() or setpos()
  • setheading() or seth()
  • circle() (can also be used for polygons using steps= kwarg)
  • speed()
  • penup() and pendown()
  • pensize()
  • pencolor()

Passive Turtle Commands

  • position()
  • heading()

Screen Commands

  • bgcolor()
  • clear()
  • exitonclick()

Sample Programs

Albert Schuller's example

Geometric Snowflake

Saving Pictures

I call this the Level 5 Koch Reindeer Curve. It isn't what you will get but it is a screenshot of a slightly different fractal.

For Lab 5 of Fall 2018, you will be taking a screen picture of the Python Turtle Graphics window to document your work. Save the picture in some convenient graphics format (PNG, JPG, or GIF) and when you turn in Lab 5, also attach the graphics file.

  • For Windows, search for the Snipping Tool. When it opens, change the Mode to Window Snip. You will then be able to click on the Python Turtle Graphics window and it will be copied as a graphic in the snipping tool.
  • For OSX, type shift-command-4; when the screen capture pointer comes up, hit space. Hover over the window you want to capture and then click. The picture will be saved on the desktop as a png file. See How to take a screenshot for more options.


More Information

Colors

For Lab 5, if you want the segments in your curve to be different colors, there is a \LIpy{color} function that applies to turtles; this function accepts either common color names as strings or a list of three values between 0 and 1 to indicate how much red, green, and blue you want in your color. Here are some examples:

wn = turtle.Screen()
try:
    kasa = turtle.Turtle()
except:
    kasa = turtle.Turtle()  
kasa.color('blue')
kasa.forward(100)
kasa.color([.9, .5, .2]) # lots of red, some green, a little blue = orange
kasa.left(90)
kasa.forward(100)

from sys import platform
if platform=='win32':
    wn.exitonclick()

A list of color names is at the Python Color Constants Module page on the Webucator Blog. The RGB lists on that page would need to be divided by 255 for the color() command. Alternately, if you issue the command

 
turtle.colormode(255)

you can use values between 0 and 255 instead of between 0 and 1.