We are attempting a tank game in my class. I'm able to load the tank image however the image doesnt rotate. I did a search but cannot find a solution (or a simple one if it exists).
Here's what I have so far (very elementary). I'm hoping there's a simple solution to rotating the image to turn left/right.
import turtle
screen = turtle.Screen()
tankImage = "tank.gif"
screen.addshape(tankImage)
tank = turtle.Turtle()
tank.shape("turtle")
def left():
tank.left(20)
def right():
tank.right(20)
screen.listen()
screen.onkey(left, "Left")
screen.onkey(right, "Right")
turtle.mainloop()
I know this is an old question but technically I found a way around it not being possible and is having multiple images for whichever position you want (facing_left, facing_right, etc) and updating the image on the Right / Left (etc) functions.
tank = turtle.Turtle()
tank.shape("tank_up.gif")
def left():
tank.shape("tank_left.gif")
tank.left(20)
def right():
tank.shape("tank_right.gif")
tank.right(20)
This is a principle of animation (changing frames for whichever move you want) and it worked.
What's not clear in your example is that when you use tank.shape("turtle")
you can see the turtle change direction but when you use tank.shape(tankImage)
, you don't see any movement. This is covered in the turtle documentation for addshape()
:
Note: Image shapes do not rotate when turning the turtle, so they do not display the heading of the turtle!
The simple answer is you can't do it that way. However, if you're able to draw your tank using polygons, then you can define a cursor that rotates correctly. See my answer to "Change appearance of turtle" which is a tank polygon example as well as my answer to "Logic error in python turtle" which explains how to orient your tank drawing to turn properly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With