CIS 111 Turtle graphics

Objective

  • Draw graphics on the screen using turtle graphics

Overview

Common turtle commands

FunctionPurpose
degrees(n=360) set turtle to use n degrees for a full circle
radians() set turtle to use radian measurements: degrees(2*Pi)
position(), pos() returns turtle coordinate
forward(n), fd(n) moves turtle n units in direction it is facing
back(n), bk(n), backward(n) moves turtle n units opposite to direction it is facing
heading() returns direction turtle is facing in degrees or radians
right(n), rt(n) turns turtle to right by n degrees or radians
left(n), lt(n) turns turtle to right by n degrees or radians
goto(x,y), setpos(x,y),
setposition(x,y)
moves turtle to position specified
setx(x) changes turtle's x coordinate, but leaves y as it is
sety(y) changes turtle's y coordinate, but leaves x as it is
xcor() returns the turtle's x coordinate
ycor() returns the turtle's y coordinate
setheading(n), seth(n) sets turtle's heading to n degrees or radians
heading() returns the turtle's heading
home() sets turtle's coordinates to (0, 0) and resets heading
pendown(), pd(), down() makes turtle draw when moving
penup(), pu(), up() makes turtle not draw when moving
pensize(n), width(n) sets width of pen to n
pensize(), width() returns width of pen
isdown() returns True if pen is down, otherwise returns False
circle(radius, extent, steps) draws all or part of a circle or regular polygon
speed(n) set speed of turtle (1 = slow, 10 = fast)
speed() returns speed of turtle
distance(x,y) returns distance from turtle to (x,y)
towards(x,y) returns angle to (x,y) depending on mode
color() returns current turtle drawing color
color(color) sets current turtle drawing color
fillcolor() returns current turtle drawing fill color
fillcolor(color) sets current turtle drawing fill color
begin_fill() specifies that shape to be filled will be drawn next
end_fill() specifies that shape to be filled has finished being drawn
hideturtle(), ht() makes the turtle invisible
showturtle(), st() makes the turtle visible
isvisible() returns True if turtle is visible, otherwise returns False
begin_poly() starts recording vertices of polygon with current position
end_poly() stop recording polygon vertices and connect current position to first position
get_poly() returns the last recorded polygon
bgcolor() returns the current background color
bgcolor(color) sets the current background color
bgpic() returns the filename of the current background picture
bgpic(filename) sets the current background picture
clear(), clearscreen() clears the screen
textinput(title, prompt) opens a dialog to get text input
numinput(title, prompt) opens a dialog to get numeric input
mode() return current turtle mode ("standard", "logo", or "world")
mode(s) sets current turtle mode ("standard", "logo", or "world")
colormode() returns the color mode (1.0 or 255)
colormode(n) sets the color mode (1.0 or 255)
window_width() returns width of turtle graphics window
window_height() returns height of turtle graphics window
bye() shuts turtle graphics window

Draw flag of Japan

import turtle turtle.speed(0) turtle.hideturtle() turtle.bgcolor("yellow") # draw rectangle turtle.width(3) turtle.color("black") turtle.fillcolor("white") turtle.penup() turtle.goto(-100, 50) turtle.setheading(0) turtle.pendown() turtle.begin_fill() turtle.forward(200) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(200) turtle.right(90) turtle.forward(100) turtle.end_fill() # draw circle turtle.color("red") turtle.fillcolor("red") turtle.penup() turtle.goto(35, 0) turtle.setheading(90) turtle.pendown() turtle.begin_fill() turtle.circle(35) turtle.end_fill()

Draw a smiley face

import turtle turtle.color("black") turtle.bgcolor("yellow") turtle.width(4) turtle.hideturtle() turtle.speed(0) turtle.penup() turtle.goto(-75,150) turtle.pendown() turtle.begin_fill() turtle.circle(10) turtle.end_fill() turtle.goto(-75,145) turtle.pendown() turtle.begin_fill() turtle.circle(10) turtle.end_fill() turtle.penup() turtle.goto(75,150) turtle.pendown() turtle.begin_fill() turtle.circle(10) turtle.end_fill() turtle.goto(75,145) turtle.pendown() turtle.begin_fill() turtle.circle(10) turtle.end_fill() turtle.width(15) turtle.penup() turtle.goto(0,-25) turtle.pendown() turtle.circle(120,90) turtle.penup() turtle.setheading(180) turtle.goto(0,-25) turtle.pendown() turtle.circle(-120,90)

Draw a wobbly jagged spiral

import turtle import random random.seed() turtle.colormode(255) turtle.speed(0) turtle.ht() x = 10 for i in range(1, 2000): turtle.forward(x) turtle.right(36) if i % 5 == random.randint(0, 4): x = x + random.randint(0,4) if i % 5 == random.randint(0, 4): turtle.pencolor(random.randint(0,255),random.randint(0,255),random.randint(0,255)) if i % 5 == random.randint(0, 4): turtle.width(random.randint(1,3))