0

I have four turtles I want to be all at the same y pos and pass in an x pos to have them in a line along the bottom of the screen.

I was wondering if this is possible with a for loop?

code:

from turtle import *

canvas = Screen()
bg_img = "assets\\board.gif"

canvas.setup(1.0, 1.0, None, None)
canvas.bgpic(bg_img)
canvas.bgcolor("#222")
canvas.title("Boardgame")

start_ypos = -250
starta = (-20, start_ypos)

a = Turtle()
b = Turtle()
c = Turtle()
d = Turtle()

a.shape("square")
a.speed(0)
a.penup()
a.setpos(starta)
unor
  • 92,415
  • 26
  • 211
  • 360
Thomas Carroll
  • 161
  • 1
  • 13

1 Answers1

2

Yes, you can iterate over your Turtle instances, for example by putting them into a list and looping over it:

for turtle_instance in [a, b, c, d]:
    turtle_instance.setpos(...)

In fact, it is probably easier to start with them in a list:

turtles = [Turtle() for _ in range(4)]  # see "list comprehension" if unfamiliar

and then access turtles[0] rather than a.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Quick question regarding `_` in list comprehension; does it do anything special or could I put something like `i` there and get the same result? – Thomas Carroll Feb 03 '15 at 15:06
  • 2
    @ThomasCarroll it's just the convention for *"I'm not actually going to use this value"*, see e.g. http://stackoverflow.com/q/5893163/3001761 – jonrsharpe Feb 03 '15 at 15:07