0

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)


ggorlen
  • 44,755
  • 7
  • 76
  • 106
william
  • 28
  • 3

1 Answers1

-1

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

codester_09
  • 5,622
  • 2
  • 5
  • 27
  • `allTurtles[f"paper{i}"]` is a hacky and non-idiomatic way to do this, even if it technically works. Much more appropriate is a dict of lists, for example: `all_turtles = {"paper": [...], "rocks": [...], "scissors": [...]}`. No string concatenation to build keys, just `all_turtles["paper"].append(Turtle())` and retrieval via `for paper in all_turtles["paper"]` or `all_turtles["paper"][i]`. – ggorlen Dec 16 '22 at 18:20