Python programming

Nobody
TurtleCode.pdf

PDF_CODE

HIT137 Software Now

Week 7

import turtle

from turtle import*

shape("turtle")

def draw_square():

for i in range(0,4):

turtle.fd(50)

turtle.lt(90)

turtle.fd(75)

def draw_triangle():

for i in range(3):

turtle.fd(50)

turtle.lt(120)

turtle.fd(75)

turtle.goto(-250, 0)

draw_square()

draw_triangle()

draw_square()

draw_triangle()

delay = input("Press enter to finish.")

#Add color to the draw_shape function

import turtle

from turtle import*

shape("turtle")

def draw_shape(shape, color):

turtle.color(color)

if shape == "square":

for i in range(0,4):

turtle.fd(50)

turtle.lt(90)

turtle.fd(75)

if shape == "triangle":

for i in range(3):

turtle.fd(50)

turtle.lt(120)

turtle.fd(75)

turtle.goto(-250, 0)

draw_shape("square", "blue")

draw_shape("triangle", "red")

draw_shape("square", "green")

draw_shape("triangle", "purple")

delay = input("Press enter to finish.")

#Add color to the draw_shape function

#Let's think algorithmically

#Add constants

import turtle

from turtle import*

shape("turtle")

#Define constants

#Use capital letters

SQUARE = 4

TRIANGLE = 3

PENTAGON = 5

HEXAGON = 6

def draw_shape(sides, color):

turtle.color(color)

for i in range(0,sides):

turtle.fd(50)

turtle.lt(360/sides)

turtle.fd(75)

turtle.goto(-250, 0)

draw_shape(SQUARE, "blue")

draw_shape(TRIANGLE, "red")

draw_shape(PENTAGON, "green")

draw_shape(HEXAGON, "purple")

turtle.goto(-250, 100)

draw_shape(SQUARE, "blue")

draw_shape(TRIANGLE, "red")

draw_shape(PENTAGON, "green")

draw_shape(HEXAGON, "purple")

delay = input("Press enter to finish.")

#Add color to the draw_shape function

#Let's think algorithmically

#Add constants

import turtle

from turtle import*

shape("turtle")

#Define constants

#Use capital letters

SQUARE = 4

TRIANGLE = 3

PENTAGON = 5

HEXAGON = 6

def draw_shape(sides, color):

turtle.color(color)

for i in range(0,sides):

turtle.fd(50)

turtle.lt(360/sides)

turtle.fd(75)

turtle.up()

turtle.goto(-250, 0)

turtle.down()

draw_shape(SQUARE, "blue")

draw_shape(TRIANGLE, "red")

draw_shape(PENTAGON, "green")

draw_shape(HEXAGON, "purple")

turtle.up()

turtle.goto(-250, 100)

turtle.down()

draw_shape(SQUARE, "blue")

draw_shape(TRIANGLE, "red")

draw_shape(PENTAGON, "green")

draw_shape(HEXAGON, "purple")

turtle.hideturtle()

delay = input("Press enter to finish.")

import turtle

from turtle import*

t=Turtle()

import random

def randomWalk(t,turns,distance=20):

t.shape("turtle")

for x in range(turns):

t.left(random.randint(0,360))

t.forward(distance)

randomWalk(t,40)

import turtle

from turtle import*

import random

t=Turtle()

t.shape("turtle")

t.color("red")

t.pensize(5)

wn=turtle.Screen()

wn.bgcolor("blue")

for i in range(10):

t.fd(50)

coin=random.randint(0,2)

if coin==0:

t.left(90)

else:

t.right(90)

turtle.mainloop()

import turtle

from turtle import*

import random

t=Turtle()

t.shape("turtle")

t.color("red")

t.pensize(5)

t.speed(10)

wn=turtle.Screen()

wn.bgcolor("blue")

def isInScreen(wn,t):

xmin=-wn.window_width()/2

xmax=wn.window_width()/2

ymin=-wn.window_height()/2

ymax=wn.window_height()/2

x=t.xcor()

y=t.ycor()

if x < xmin or x > xmax:

return False

if y < ymin or y > ymax:

return False

return True

while isInScreen(wn,t):

t.fd(50)

coin=random.randint(0,2)

if coin==0:

t.left(90)

else:

t.right(90)

turtle.mainloop()

import turtle

loadWindow = turtle.Screen()

loadWindow.bgcolor("#AAAAFF")

turtle1=turtle.Pen()

turtle1.color("red")

turtle2=turtle.Pen()

turtle2.color("green")

turtle3=turtle.Pen()

turtle3.color("blue")

turtle4=turtle.Pen()

turtle4.color("yellow")

for x in range(100):

turtle1.circle(100)

turtle1.left(90)

turtle2.circle(100)

turtle2.right(90)

turtle3.forward(-100)

turtle3.left(90)

turtle4.forward(-100)

turtle4.right(90)

turtle.exitonclick()

import turtle

import random

loadWindow = turtle.Screen()

loadWindow.bgcolor("#AAAAFF")

turtle1=turtle.Pen()

turtle1.shape("turtle")

turtle2=turtle.Pen()

turtle2.shape("circle")

turtle3=turtle.Pen()

turtle3.shape("square")

turtle4=turtle.Pen()

turtle4.shape("triangle")

turtle1.left(90)

turtle2.forward(-50)

turtle2.left(90)

turtle3.forward(50)

turtle3.left(90)

turtle4.forward(100)

turtle4.left(90)

for x in range(100):

t1=random.randint(-5,30)

t2=random.randint(-5,30)

t3=random.randint(-5,30)

t4=random.randint(-5,30)

turtle1.forward(t1)

turtle2.forward(t2)

turtle3.forward(t3)

turtle4.forward(t4)

turtle.exitonclick()