I am making a simplified version of Space Invaders and I am trying to make the bullets shot from the player's character. I have made a turtle for the character (main_ship) and a turtle for the bullet (bullet). How can I clone the bullet (for the purpose of shooting them)? This is my code for the bullet:
`bullet = turtle.Turtle()
bullet.speed(0)
bullet.shape("circle")
bullet.color("red")
bullet.shapesize(stretch_wid=0.5, stretch_len=0.5)
bullet.penup()
bullet.goto(main_ship.xcor(), main_ship.ycor())
bullet.hideturtle()`
I have not tried anything yet as I cannot find anything explaining how.
Since your bullets are turtles, have you looked at turtle's own clone()
method:
Help on function clone in module turtle:
clone()
No argument.
Create and return a clone of the turtle with same position, heading
and turtle properties.
Example (for a Turtle instance named mick):
mick = Turtle()
joe = mick.clone()
Since turtles are effectively global entities, and never garbage collected, I recommend you not waste bullets but rather keep a pool (list
) of available ones and that you pull from as needed and add back to when a bullet is no longer active. Only clone additional bullets, from a dedicated prototype, when the pool is empty.