Raveen Python Journey
7,8, 9 g,y
Turtle drawing programs
Learning resources
1. Python turtle official documentation:
https://docs.python.org/3/library/turtle.html
2. Tech with tim video channel: basics and events …
https://www.youtube.com/playlist?list=PLzMcBGfZo4-kfGgYZb9dwW3VhoBR
G0h9c
3.
Youtube: tech with tim - understanding events of mouse and keyboard
Learning resources 3
Set up Global variables
Import any module
required.
import turtle Here we import the turtle
module
win = turtle.Screen()
ball = turtle.Turtle() Create global
variables of ball and
win.
so that they can be
used in all functions
Initialize function -
gives a win a title and bgcolor, gives a ball a color and a shape
def initialize():
# set up screen
win.bgcolor("yellow")
win.title("turtle v1.1 demo - simple changeable square")
# create a turtle
ball.color("red")
ball.shape("circle")
main
def main():
initialize()
mainloop() ensures
that the screen
win.mainloop() freezes
main()
square
def draw_square(s):
for i in range(4):
ball.forward(s)
ball.left(90)
Moves forward a fixed
amount given by the size
parameter, and it turns 90
degrees - all 4 times inside a
loop
Triangles
def draw_triangle(s):
for i in range(3):
ball.forward(s)
● Design a loop that runs 3 times - as
ball.left(180-60)
per the no of sides of a triangle
○ Draw a straight line as per the size
parameter passed to the function as s
○ Turn left by exterior angle calculated by:
180-60 (60 = internal angle of a equilateral
triangle)
Polygon shapes
def draw_shape(size=70, sides=5):
# documenting various shapes possible with a change
in the turn
# 30 - 12 sided pentagram
# 40 - 9 " "
# 50 - rough star shapes begin
# 60 - 6 sided pentagram
for i in range(sides):
ball.forward(size)
ball.left(360/sides)
Spirals v1
def draw_spiral():
for x in range (300):
ball.forward(x)
ball.left(139)
Spirals v2
def draw_spiral():
for x in range (300):
ball.forward(x)
ball.left(59)
Spirals with circles
def draw_spiral_v3():
for x in range (300):
ball.forward(x)
ball.left(59)
ball.circle(x)