Lots of errors
Lots of errors

Reputation: 51

How to add an image to a turtle object in python?

# register shape
turt = turtle.Turtle()
turt.register_shape('player.png')
# player
p = turtle.Turtle()
p.speed(0)

p.color('magenta')

p.shape('player.png')
p.shapesize(2)
p.setheading(0)
p.penup()

p.setposition(-700, 0)
hp = 3 

then i get this error: 'Turtle' object has no attribute 'register_shape'

Upvotes: 0

Views: 2187

Answers (1)

abarnert
abarnert

Reputation: 365975

register_shape is not a method on Turtle objects, it's a global function.

So, instead of this:

turt.register_shape('player.png')

… do this:

turtle.register_shape('player.png')

Also, notice that you don't have any use for that turt turtle. Your app only wants to display a single turtle, p, so don't create any others.


Finally, even after you fix this:

At least according to the docs, only GIF images are supported, but you're trying to use a PNG image. The docs may be wrong about that, but there's a good chance they're right, and this is going to fail.

If so, the only way to fix it is to use some other program (it could be one you write yourself in 4 lines of Pillow code, or it could be something like MSPaint or Preview, or a command-line tool like ImageMagick convert) to make a GIF image out of your PNG image.

Upvotes: 2

Related Questions