Skip to content

Instantly share code, notes, and snippets.

@topherPedersen
Created July 18, 2018 19:12
Show Gist options
  • Save topherPedersen/20dcf708716c42cd3eb0f1a377605393 to your computer and use it in GitHub Desktop.
Save topherPedersen/20dcf708716c42cd3eb0f1a377605393 to your computer and use it in GitHub Desktop.
How to create add a background, create a player sprite, and add game controller functionality in Python using Turtle Graphics
# This code was written by the team at www.trinket.io for use in their trinket.io
# web based integrated python development environment. The game may need some
# modification to work using the standard Python 2.7 distribution. This code
# has been posted here to my personal github profile for reference purposes
# related to my teaching duties at theCoderSchool in Flower Mound, TX.
# This code is from the public domain, is free to use and modify, and does
# not require a software license of any kind.
# Click in the righthand window to make it active then use your arrow
# keys to control the spaceship!
import turtle
screen = turtle.Screen()
# this assures that the size of the screen will always be 400x400 ...
screen.setup(400, 400)
#screen.setup(800, 800)
# ... which is the same size as our image
# now set the background to our space image
screen.bgpic("space.jpg")
screen.bgpic("")
# Or, set the shape of a turtle
screen.addshape("rocketship.png")
turtle.shape("rocketship.png")
move_speed = 10
turn_speed = 10
# these defs control the movement of our "turtle"
def forward():
turtle.forward(move_speed)
def backward():
turtle.backward(move_speed)
def left():
turtle.left(turn_speed)
def right():
turtle.right(turn_speed)
turtle.penup()
turtle.speed(0)
turtle.home()
# now associate the defs from above with certain keyboard events
screen.onkey(forward, "Up")
screen.onkey(backward, "Down")
screen.onkey(left, "Left")
screen.onkey(right, "Right")
screen.listen()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment