Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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)


>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading