Home > OS >  how do i name turtles after a loops index and use them afterwards
how do i name turtles after a loops index and use them afterwards

Time:12-16

i am trying to make a game using images as sprites where the 10 rocks papers and scissors float about killing each other tryign to make all of them one character but i want to make the turtles easily using an index they are all made but cant be controlled

import turtle
import random
#
wn = turtle.Screen()
wn.title("Rock Paper Scisors simulator")
wn.setup(width=600, height=600)
#
wn.addshape("scissors_sprite.gif")#if you are trying this 
wn.addshape("rock_sprite.gif")#these three lines aren't
wn.addshape("paper_sprite.gif")#important at the moment 
##########################################
string=""
for i in range(10):
    string=[f"paper{i}"]
    string=turtle.Turtle()
#
for i in range(10):
    string=[f"rock{i}"]
    string=turtle.Turtle()
#
for i in range(10):
    string=[f"scissors{i}"]
    string=turtle.Turtle()
    
string=globals()[f"scissors{3}"]
string.forward(100)


CodePudding user response:

Saved them in a dictionary.

import turtle
import random

wn = turtle.Screen()
wn.title("Rock Paper Scisors simulator")
wn.setup(width=600, height=600)
#
wn.addshape("scissors_sprite.gif")#if you are trying this 
wn.addshape("rock_sprite.gif")#these three lines aren't
wn.addshape("paper_sprite.gif")#important at the moment 
##########################################

allTurtles = {}
for i in range(10):
    allTurtles[f"paper{i}"] = turtle.Turtle()
#
for i in range(10):
    allTurtles[f"rock{i}"] = turtle.Turtle()
#
for i in range(10):
    allTurtles[f"scissors{i}"] = turtle.Turtle()
    
string=allTurtles[f"scissors{3}"]
string.forward(100)

turtle.exitonclick()

  • Related