-1

I want to make a small game with my own image for the turtle's shape. I've put both the program and the image in the same directory. Whenever I run the code it says:


File "C:\Users\Nobody PC\Desktop\THE PROJECT.py", line 51, in p.shape('beara') File "C:\Users\Nobody\AppData\Local\Programs\Python\Python36-32\lib\turtle.py", line 2777, in shape self.turtle._setshape(name) File "C:\Users\Nobody\AppData\Local\Programs\Python\Python36-32\lib\turtle.py", line 2493, in _setshape if self._type == "polygon" == screen._shapes[shapeIndex]._type: AttributeError: 'str' object has no attribute '_type'


My code includes nothing about a string (I think):


import turtle as t
bear =  "bear.png"
p = t.Turtle()
t.register_shape('beara',bear)
t.bgcolor('black')
p.shape('beara')

I made it:

t.register_shape('beara',"bear.png")

but still had the same error.

cdlane
  • 40,441
  • 5
  • 32
  • 81
Bearly
  • 5
  • 1
  • 4
  • 1
    According to [the docs](https://docs.python.org/3.6/library/turtle.html#turtle.register_shape), that's not how you use the function. – Mike Scotty Jun 18 '18 at 10:12
  • Do you mean that instead of a png file, I should've used a gif file? – Bearly Jun 18 '18 at 10:18
  • Yes, use a gif and make it the first (and only) parameter. – Mike Scotty Jun 18 '18 at 10:19
  • I guess I got confused between introducing/ making a shape and introducing an image. Thank you. – Bearly Jun 18 '18 at 10:20
  • Does this answer your question? [adding an image to the Turtle Screen](https://stackoverflow.com/questions/30427742/adding-an-image-to-the-turtle-screen) – ggorlen Jan 06 '23 at 04:48

2 Answers2

0

In addition to using a PNG instead of a GIF, you're not invoking the turtle methods correctly. I'll force OOP syntax, instead of the function syntax you used, to make the method ownership clearer:

from turtle import Turtle, Screen

image = "bear.gif"

screen = Screen()
screen.bgcolor('black')
screen.register_shape(image)

turtle = Turtle(shape=image)

# ...

screen.mainloop()

Note that when using images as cursors, the name of the file is the name of the shape, you don't define your own name for it (unlike polygons.)

cdlane
  • 40,441
  • 5
  • 32
  • 81
0

I don't think that turtle module accepts ,png as a shape you must use a gif image. in your code just replace the image with a gif image and hope it will work check this out ....

import turtle

t = turtle.Turtle()
screen = turtle.Screen()

screen.addshape('shap1.gif')


t.shape('shap1.gif')
screen.mainloop()
Mandera
  • 2,647
  • 3
  • 21
  • 26