3

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()
ggorlen
  • 44,755
  • 7
  • 76
  • 106
chappie
  • 119
  • 4
  • 12
  • Does this answer your question? [how to rotate turtle shape in python](https://stackoverflow.com/questions/11277518/how-to-rotate-turtle-shape-in-python) – ggorlen Mar 03 '23 at 05:44

3 Answers3

1

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.

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • I changed shape to "turtle" to make sure the rotation problem was due to image and forgot to change it back before I posted the code. I will take a look at your polygon solution. Thanks... – chappie Jan 04 '18 at 21:28
1

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.

Set Gacia
  • 11
  • 3
0

turtle.settiltangle(angle) has helped me to solve this issue! However, I used a prebuilt "turtle" symbol, not an image. My "turtle" symbol can have several orientations. It helped me to rotate the symbol only once.