Create a coded Christmas card. Learn how to import images to use with turtle graphics; how to bind keys to functions; and how to use those keys to make your Santa turtle move around the screen and deliver presents around the world! Share your card with family and friends.
A coded Christmas card like this one.
Using images in turtle graphics
Binding keys to functions
While loops
Sharing trinket projects
A computer or tablet capable of accessing the internet.
Click on this trinket project link and hit the remix button trinket.io/python/8a940515cd
Start your code as usual with import turtle.
Create a screen object screen = turtle.Screen() set its size equal to the WorldMap.png image screen.setup(600, 467) and then set that image as the background screen.bgpic("WorldMap.png").
Create a turtle called 'santa' santa = turtle.Turtle(), import the Santa.png image screen.addshape("Santa.png") and set the turtle shape to this image santa.shape("Santa.png").
Give santa turtle some starting instructions santa.penup(), santa.speed(0), santa.goto(0,220), santa.setheading(90).
Now create a turtle called 'presents' and give it similar starting code but adding the hideturtle() function so the image does not appear at first:
presents = turtle.Turtle()
presents.hideturtle()
screen.addshape("Presents.png")
presents.shape("Presents.png")
presents.penup()
presents.speed(0)
presents.setheading(90)
Create a turtle called 'merry' and give it the following starting code:
merry = turtle.Turtle()
merry.hideturtle()
screen.addshape("Merry.png")
merry.shape("Merry.png")
merry.setheading(90)
Create variables for the speed that santa turtle will move and turn move_speed = 10, turn_speed = 10.
Now define functions for Santa turtle's movement e.g:
def forward():
santa.forward(move_speed)
def left():
santa.left(turn_speed)
Binding these movement functions to keys on your keyboard will let you control santa turtle on the screen. Use the onkey() function with first parameter set to the function you want run when the key is pressed, and the second parameter the name of the key e.g. screen.onkey(forward, 'Up'). Finish with the screen.listen() command, which tells the computer to keep checking for key presses.
Create a variable to count the number of presents that santa turtle delivers count = 0.
Define a function that lets santa turtle place a present on the map. When santa turtle places a present, the invisible presents turtle moves to santa turtle's location and 'stamps' itself on the map. Every time a present is place, count goes up by 1:
def place():
global count
presents.goto(santa.xcor(), santa.ycor())
presents.stamp()
count += 1
Now bind this place function to the spacebar key screen.onkey(place, 'space').
Finish your code with a while loop that will continually check how many presents have been placed. Once 10 presents have been placed, the merry turtle you created in step 1 will appear with a Christmas message!
while True:
if count == 10:
merry.showturtle()
Finally, share the link to your trinket with anyone you want to send a Christmas card to. In your email include the following instructions:
Click on the map and then use the arrow keys to help Santa travel around the world on Christmas Eve. Press the spacebar to deliver presents, at least 10. Merry Christmas!
Here is a link to the finished trinket trinket.io/python/1a09d6704c happy holidays!