Skip to content

Instantly share code, notes, and snippets.

@topherPedersen
Created August 20, 2019 22:49
Show Gist options
  • Save topherPedersen/350d45465fd4428b189006e844319f63 to your computer and use it in GitHub Desktop.
Save topherPedersen/350d45465fd4428b189006e844319f63 to your computer and use it in GitHub Desktop.
Create Clones with Loops (for use with turtle graphics at trinket.io)
# Click in the righthand window to make it active then use your arrow
# keys to control the spaceship!
import turtle
import random
screen = turtle.Screen()
# this assures that the size of the screen will always be 400x400 ...
screen.setup(400, 400)
# ... which is the same size as our image
# now set the background to our space image
screen.bgpic("space.jpg")
# Or, set the shape of a turtle
screen.addshape("rocketship.png")
turtle.shape("rocketship.png")
# braxon, we are going to be doing our code between this line and...
my_list_of_clones = []
for i in range(1000):
x = random.randint(-200, 200)
y = random.randint(-200, 200)
new_turtle = turtle.Turtle()
new_turtle.penup()
new_turtle.speed(0)
new_turtle.shape("rocketship.png")
new_turtle.goto(x, y)
my_list_of_clones.append(new_turtle)
# this line. do not add code below this line. our code goes up here ^^^
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