-1

I am creating a chinese checkers AI project, and I created the checkers using a for loop. What I want to ask is, is it possible to assign a variable to each checker I created with create_image with the for loop? I use this code:

black = []
black = PhotoImage(file="black.gif")
black_sub = black.subsample(8, 8)
for i in range(4):
    black_id.append(i)

    canvas.create_image(425 + 24 * i,800 - 10 - 45 * i, anchor=S, 
    image=black_sub)

for i in range(4):

    black_id.append(i+4)

    canvas.create_image(425 - 24 * i,800 - 10 - 45 * i, anchor=S, 
    image=black_sub)

Can I possibly assign each list number to it's corresponding checker?

F. Zeng
  • 85
  • 1
  • 2
  • 11
  • You should use lists for this. – DYZ Nov 09 '18 at 03:12
  • Oh, right yeah. I included a list at the beginning. – F. Zeng Nov 09 '18 at 03:17
  • Please provide some result what you expected. – Geeocode Nov 09 '18 at 03:33
  • I expect that i can move the checkers using their id. I have already made a list for the id's and i want to assign the id's using a for loop, then put them in the list. – F. Zeng Nov 09 '18 at 03:36
  • where is where your checkers assigned to a variable? – Geeocode Nov 09 '18 at 03:42
  • I want to create it so that a variable is created in the for loop, then assign it to each checker. I am confused how to assign individual and different variables in a for loop. If i do i = canvas....., then it will just constantly change the variable, and not create one. Is that possible? – F. Zeng Nov 09 '18 at 03:44

1 Answers1

0

Hard to define your question based of the given information, but you don't have to fetch ID-s, as it will be the index of your checker list, so you easily can get any of the checkers indexing with their id as follows:

black = PhotoImage(file="black.gif")
black_sub = black.subsample(8, 8)
checkers = []

for i in range(4):
    checkers.append( 
                    canvas.create_image(425 + 24 * i,800 - 10 - 45 * i, anchor=S, 
                    image=black_sub) 
                    )

for i in range(4):
    checkers.append( 
                    canvas.create_image(425 - 24 * i,800 - 10 - 45 * i, anchor=S, 
                    image=black_sub) 
                    )

checkers[5].move(10,10) # get some of the checker based on its ID
F. Zeng
  • 85
  • 1
  • 2
  • 11
Geeocode
  • 5,705
  • 3
  • 20
  • 34