I want to generate (a)
amount of the same RawTurtle object, while all being named separately. It seems like something that should be doable, but I don't know how.
Here's my code:
def spawnEntity(a):
for x in range(0, a):
global entity
spawnpt = (rand.randrange(-(cWidth/2), cWidth/2), rand.randrange(-(cHeight/2), cHeight/2))
entName = (f'entity{x}')
def entGenerate(a):
print(a)
(f'{a}') = turtle.RawTurtle(screen)
(f'{a}').shape("square")
(f'{a}').speed("fastest")
(f'{a}').penup()
(f'{a}').pencolor("gray")
(f'{a}').fillcolor("gray")
(f'{a}').setpos(spawnpt)
print(a)
entGenerate(entName)
entList = []
entList.append(entName)
proportion = (16, 9)
cWidth = (proportion[0] * 80)
cHeight = (proportion[1] * 80)
root = Tk()
root.title("Shooter Game")
canvas = Canvas(master=root, width=cWidth, height=cHeight)
canvas.grid(row=1, column=1)
screen = turtle.TurtleScreen(canvas)
spawnEntity(4)
root.mainloop()
Update: Problem solved! The new code below runs all 4 turtles (aka: "entities") independently from each other, while still being able to all be called under a for
loop:
def spawnEntity(a):
global ent, entity
ent = (a)
entity = {num: turtle.RawTurtle(screen) for num in range(ent)}
for i in range(ent):
spawnpt = (rand.randrange(-(cWidth/2.5), cWidth/2.5), rand.randrange(-(cHeight/2.5), cHeight/2.5))
entity[i].shape("square")
entity[i].speed("fastest")
entity[i].penup()
entity[i].pencolor("gray")
entity[i].fillcolor("gray")
entity[i].setpos(spawnpt)