In: Computer Science
Assignment
Write a program using turtle graphics which writes your initials, or any other three unique letters, to the display. Look to your program for lab 1 for reminders of how to use turtle graphics.
Functions that must be written:
def drawLetter (x, y, letterColor): Write three functions, one for three unique letters. Personally, I would write drawA, drawR, and drawS. Each function must draw that letter to the screen. The x and y values determine the upper left-hand location for the start of the letter. Ensure that each letter takes the same amount of space, both vertically and horizontally. The letterColor parameter will be a string used to determine the color of the letter – use it to set the fillcolor, and maybe pencolor. You can also set pencolor to “black” to create an outline of the letter. In the Turtle Graphics Resources link in blackboard I have a link to valid turttle graphic colors.
Each function can have one, and only one, goto command. If drawing a letter which requires two “shapes,” such as a “O” with a two circles you can use a second goto. The initial goto should position the turtle in the upper left-hand location of the letter to be drawn. It might be smart to ensure that you know what direction the turtle is pointing before starting to draw the letter. All of the actual drawing must be done with basic movements, such as forward, backward, left, or right. Remember to use penup and pendown to ensure you only draw the desired lines.
def drawShadowedLetter (x, y, letterColor, offset): Write three more functions, one for each letter (mine would be drawshadowedT, drawShadowedL, and drawShadowedS). These functions will use your drawLetter functions, once using letterColor, and once adding offset to the x and y values and using “black” as the color. This will create a “shadowed” letter. These three functions will not be doing any actual drawing, all of the graphics work will be done in the drawLetter functions.
In the main program, draw your initials to the screen twice. The first time write your initials to the screen using the drawLetter functions. Somewhere else on the display, write your initials to the screen using the drawShadowedLetter functions.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#code with no comments, for easy copying
import turtle
LETTER_SIZE = 50
def drawS(x, y, letterColor):
turtle.up()
turtle.goto(x, y - LETTER_SIZE * 2)
turtle.setheading(0)
turtle.color(letterColor)
turtle.pensize(10)
turtle.down()
turtle.forward(LETTER_SIZE)
turtle.left(90)
turtle.forward(LETTER_SIZE)
turtle.left(90)
turtle.forward(LETTER_SIZE)
turtle.right(90)
turtle.forward(LETTER_SIZE)
turtle.right(90)
turtle.forward(LETTER_SIZE)
def drawU(x, y, letterColor):
turtle.up()
turtle.goto(x, y)
turtle.setheading(-90)
turtle.color(letterColor)
turtle.pensize(10)
turtle.down()
turtle.forward(LETTER_SIZE * 2)
turtle.left(90)
turtle.forward(LETTER_SIZE)
turtle.left(90)
turtle.forward(LETTER_SIZE * 2)
def drawE(x, y, letterColor):
turtle.up()
turtle.goto(x + LETTER_SIZE, y)
turtle.setheading(180)
turtle.color(letterColor)
turtle.pensize(10)
turtle.down()
turtle.forward(LETTER_SIZE)
turtle.left(90)
turtle.forward(LETTER_SIZE)
turtle.left(90)
turtle.forward(LETTER_SIZE)
turtle.backward(LETTER_SIZE)
turtle.right(90)
turtle.forward(LETTER_SIZE)
turtle.left(90)
turtle.forward(LETTER_SIZE)
def drawShadowedS(x, y, letterColor, offset):
drawS(x + offset, y + offset, 'black')
drawS(x, y, letterColor)
def drawShadowedU(x, y, letterColor, offset):
drawU(x + offset, y + offset, 'black')
drawU(x, y, letterColor)
def drawShadowedE(x, y, letterColor, offset):
drawE(x + offset, y + offset, 'black')
drawE(x, y, letterColor)
if __name__ == '__main__':
x = -300
y = -200
turtle.speed(0)
drawS(x, y, 'red')
drawU(x + LETTER_SIZE + 20, y, 'green')
drawE(x + 2 * (LETTER_SIZE + 20), y, 'blue')
x = 200
y = 200
drawShadowedS(x, y, 'red', 5)
drawShadowedU(x + LETTER_SIZE + 20, y, 'green', 5)
drawShadowedE(x + 2 * (LETTER_SIZE + 20), y, 'blue', 5)
turtle.done()
#same code with comments, for learning
import turtle
# width of each letter
LETTER_SIZE = 50
# method to draw an 'S'
def drawS(x, y, letterColor):
# pen up
turtle.up()
# moving to bottom left position of S
turtle.goto(x, y - LETTER_SIZE * 2)
# facing right
turtle.setheading(0)
# using given color
turtle.color(letterColor)
# thicker pen
turtle.pensize(10)
# pen down
turtle.down()
# moving forward LETTER_SIZE spaces
turtle.forward(LETTER_SIZE)
# turning left
turtle.left(90)
# moving forward LETTER_SIZE spaces, turning left again
turtle.forward(LETTER_SIZE)
turtle.left(90)
# moving forward LETTER_SIZE spaces, turning right
turtle.forward(LETTER_SIZE)
turtle.right(90)
# moving forward LETTER_SIZE spaces, turning right again
turtle.forward(LETTER_SIZE)
turtle.right(90)
# moving forward LETTER_SIZE spaces to complete S
turtle.forward(LETTER_SIZE)
# method to draw a 'U'
def drawU(x, y, letterColor):
turtle.up()
turtle.goto(x, y) # top left position of U
turtle.setheading(-90) # facing down
turtle.color(letterColor)
turtle.pensize(10)
turtle.down()
# moving down LETTER_SIZE*2 spaces, then moving right LETTER_SIZE spaces and then moving
# up LETTER_SIZE*2 spaces to complete U
turtle.forward(LETTER_SIZE * 2)
turtle.left(90)
turtle.forward(LETTER_SIZE)
turtle.left(90)
turtle.forward(LETTER_SIZE * 2)
# method to draw an 'E'
def drawE(x, y, letterColor):
turtle.up()
turtle.goto(x + LETTER_SIZE, y) # top right position
turtle.setheading(180) # facing left
turtle.color(letterColor)
turtle.pensize(10)
turtle.down()
# moving forward LETTER_SIZE spaces, turning left
turtle.forward(LETTER_SIZE)
turtle.left(90)
# moving forward LETTER_SIZE spaces, turning left again
turtle.forward(LETTER_SIZE)
turtle.left(90)
# moving forward LETTER_SIZE spaces, and then backwards LETTER_SIZE spaces
turtle.forward(LETTER_SIZE)
turtle.backward(LETTER_SIZE)
# turning right
turtle.right(90)
# moving forward LETTER_SIZE spaces, turning left
turtle.forward(LETTER_SIZE)
turtle.left(90)
# moving forward LETTER_SIZE spaces to complete 'E'
turtle.forward(LETTER_SIZE)
# method to draw a shadowed version of 'S'
def drawShadowedS(x, y, letterColor, offset):
# drawing shadow first
drawS(x + offset, y + offset, 'black')
# drawing S at x,y
drawS(x, y, letterColor)
# method to draw a shadowed version of 'U'
def drawShadowedU(x, y, letterColor, offset):
drawU(x + offset, y + offset, 'black')
drawU(x, y, letterColor)
# method to draw a shadowed version of 'E'
def drawShadowedE(x, y, letterColor, offset):
drawE(x + offset, y + offset, 'black')
drawE(x, y, letterColor)
if __name__ == '__main__':
# at (-300,-200) drawing S, U and E, spaced evenly
x = -300
y = -200
turtle.speed(0) # max speed
drawS(x, y, 'red')
drawU(x + LETTER_SIZE + 20, y, 'green')
drawE(x + 2 * (LETTER_SIZE + 20), y, 'blue')
# at (200,200), drawing shadowed versions of S,U and E
x = 200
y = 200
drawShadowedS(x, y, 'red', 5)
drawShadowedU(x + LETTER_SIZE + 20, y, 'green', 5)
drawShadowedE(x + 2 * (LETTER_SIZE + 20), y, 'blue', 5)
turtle.done()
#output