All pastes #1546855 Raw Edit

awesomeface in python using turt

public text v1 · immutable
#1546855 ·published 2009-08-29 07:43 UTC
rendered paste body
from turtle import *

    ## whoever made the original was nice enough to make the mouth nowhere
    ## near semi-circular, hence this function will make the mouth.
    ## thanks mate.
    ## the three functions below are executed a fair bit further down

def drawmouth_outer():
    counter = 0
    setheading(270)
    while (counter < 182):
        forward(2.2)
        left(1)
        counter = counter + 1
    setheading(180)
    forward(235)   ## go back where we started so the fill() function does its thing

def drawmouth_inner():
    counter = 0
    setheading(270)
    while (counter < 181):
        forward(1.9)  ## this one is slightly narrower than the outer mouth
        left(1)
        counter = counter + 1
    setheading(180)
    forward(219)   ## go back where we started so the fill() function does its thing

def drawtongue():
    counter = 0
    setheading(85)
    while (counter < 120):  ## these counter errors are, of course, trial and error determined
        forward(1)          ## first part is the part of the tongue that seems circular
        right(1)
        counter = counter + 1
    while (counter == 120):
        right(90)           ## have the turtle change direction at an exact point
        counter = counter + 1
    while (counter > 120 and counter < 220):
        forward(1)          ## return to starting point on a silghtly curved path
        right(0.6)
        counter = counter + 1

def masterpiece():
    reset()
    # source code
    title("Happy faces in Turtle!")   ## it says this in the titlebar of the turtle application

    speed('fastest')  ## because otherwise it really does take forever

    up()
    goto(0, -200)   ## start a bit further down so the image is centred
    down()
    fill(True)
    circle(200)  ## large outside circle first
    fill(False)

    up()
    goto(0, -180)  ## we want the next shape inside the first one
    down()
    fill(True)
    color('#fede58')  ## and the next circle in is (a shade of) yellow, creating the thick out layer effect
    circle(180)
    fill(False)

    up()
    goto(-40, 10)  ## move left for the first eye
    down()
    fill(True)
    color('black')
    left(70)         ## black outer layer of eye
    circle(62, 220)
    fill(False)

    up()
    goto(-42, 10)
    down()
    fill(True)
    color('white')  ## this creates another semi-circle inside the first - the white inner part of the eye
    left(150)
    circle(56, 200)  ## this isn't perfect, but neither is turtle's support of ovals...
    fill(False)

    up()
    goto(124, 10)  ## and now for the second eye, on the other side
    down()
    fill(True)
    color('black')
    left(150)    ## like at the first, this angulation is so the turtle faces in the correct direction before starting the eye. it was determined scientifically using trial and error.
    circle(62, 220)
    fill(False)

    up()
    goto(122, 10)  ## second inner eye
    down()
    fill(True)
    color('white')
    left(150)  ## the fact that the angle is the same each time came as a surprise to the programmer (he passed maths C, so the shame is even greater)
    circle(56, 200)
    fill(False)

    up()
    goto(-50, 45)   ## finally, for the pupils looking way up into the stars
    down()
    fill(True)
    color('black')
    right(200)
    circle(20)
    fill(False)
    up()
    goto(114,45)   ## one down...
    down()
    fill(True)
    circle(20)  ## no need to change angle since we're making full circles here
    fill(False) ## (once again discovered scientifically)

    up()
    goto(-158, 20)
    down()
    setheading(270)
    fill(True)      ## add a bottom part to the eye a la original
    forward(10)
    left(90)
    forward(117)
    left(90)
    forward(10)
    left(90)
    forward(117)
    fill(False)
    up()
    goto(10, 20)  ## second eye
    down()
    setheading(270)
    fill(True)
    forward(10)
    left(90)
    forward(117)
    left(90)
    forward(10)
    left(90)
    forward(117)
    fill(False)

    up()
    goto(-154, -35)
    down()
    setheading(0)
    fill(True)     ## top part of the mouth
    forward(276)   ## i contemplated changing the turtle's width()
    right(90)      ## but drawing and filling this rectangle doesn't result
    forward(10)    ## in rounded corners (so is more accurate)
    right(90)
    forward(276)
    right(90)
    forward(10)
    fill(False)

    up()
    goto(-130, -45)    ## outer part of mouth
    down()          
    fill(True)
    drawmouth_outer()  ## i think the output here is pretty good, considering i'm using turtle
    fill(False)

    up()
    goto(-115, -45)    ## inner part of mouth
    down()
    fill(True)
    color('#871945')
    drawmouth_inner()  ## that looks awesome :D
    fill(False)

    up()
    goto(-10, -155)
    down()
    fill(True)         ## draw the pink tongue
    color('#fbc0df')
    drawtongue()
    fill(False)

    up()
    goto(0, 250)
    down()
    color('white')  ## moves the turtle out of the way so we can see the face
    forward(1)      ## without a little arrow in the way
    up()            ## and thus, we're done! :)