Monday, January 28, 2019

Turtle Graphics Code Samples - 2





NoNameCodeOutput
1Traingleimport turtle

painter = turtle.Turtle()
painter.pensize(8)
painter.pencolor("blue")
for i in range(3):
    painter.forward(100)
    painter.left(120)
turtle.done()
2Rectangleimport turtle

painter = turtle.Turtle()
painter.pensize(8)
painter.pencolor("red")
for i in range(4):
    painter.forward(150)
    painter.left(90)
turtle.done()
3Pentagonimport turtle

painter = turtle.Turtle()
painter.pensize(8)
painter.pencolor("blue")
for i in range(5):
    painter.forward(100)
    painter.left(72)
turtle.done()
4Hexagonimport turtle

painter = turtle.Turtle()
painter.pensize(8)
painter.pencolor("blue")
for i in range(6):
    painter.forward(100)
    painter.left(60)
turtle.done()
5Spiral Starimport turtle

spiral = turtle.Turtle()
turtle.ht()
for i in range(20):
    spiral.forward(i * 10)
    spiral.right(144)
 
turtle.done()
6Star1from turtle import *
color('red', 'yellow')
begin_fill()
while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()
done()
7Circleimport turtle

painter = turtle.Turtle(shape="turtle")

painter.penup()
painter.setposition(-120, 0)
painter.pendown()
painter.pensize(5)
painter.circle(80)

turtle.done()
8Filled Circleimport turtle as painter

painter.speed('normal')
painter.up()
painter.goto(0,-150)
painter.down()
# default pen width is 1
painter.width(2)
# draw a solid blue circle
painter.color('blue')
painter.begin_fill()
painter.circle(100)
painter.end_fill()
# adust center of semi circles
painter.up()
painter.done()

No comments:

Post a Comment