Python:Turtle

From PrattWiki
Revision as of 21:12, 1 October 2018 by DukeEgr93 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

References

Primary references are:

Common Script

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()

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:

wn.exitonclick()

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

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