Difference between revisions of "Python:Turtle"

From PrattWiki
Jump to navigation Jump to search
Line 133: Line 133:
  
 
from sys import platform
 
from sys import platform
    if platform=='win32':
+
if platform=='win32':
 
     wn.exitonclick()
 
     wn.exitonclick()
<\source>
+
</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
 
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

Revision as of 01:13, 2 October 2018

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

# Revised Version of Albert Schueller's Code
"""
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()


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.