Python Multiple Turtle Shapes - python

I want to have multiple turtles for a game. turtle is the main character and monster is another turtle i've added. How to I add a shape to it. Heres the code:
import turtle
monster = turtle.Turtle()
monster.addshape('enemt.gif')
monster.shape('enemt.gif')
monster.goto(0, 0)
I get the error
Traceback (most recent call last):
File "C:\Users\Alfie\Desktop\Tutorial Game\Tutorial Game.py", line 77, in <module>
monster.addshape('enemt.gif')
AttributeError: 'Turtle' object has no attribute 'addshape'

Rereading the documentation, addshape() is listed as a method of turtle but the examples show it as a method of the screen, which works:
import turtle
screen = turtle.Screen()
screen.addshape('enemt.gif')
monster = turtle.Turtle('enemt.gif')
monster.goto(0, 0)
turtle.done()
Actually, turtle.addshape('enemt.gif') also works in this code.
All methods of the Turtle class are also available as top level functions that operate on the default (unnamed) turtle instance. All methods of the Screen class are also available as top level functions that operate on the default (sole) screen instance.

I found it, but I'll answer for anyone else with the problem :P
monster = turtle.Turtle()
monster.addshape('enemt.gif')
monster.goto(0, 0)
monster.shape('enemt.gif')
monster.addshape('enemt.gif') doesn't work because addshape is only for turtle, not multiple. Now if you use
turtle.addshape()
it works! But you use monster.shape() sorry if I can't answer these questions, if anyone knows the actual reason that this works, edit this please :P

Related

Is there a difference in python turtle speed?

In korean python textbook for highschool
to use turtle lib with the fastest pace user can use two verson of command.
import turtle as t
t.speed(0)
t.speed=0
is there a difference with two command?
I try to ask my teacher because of performance evaluation but.. unfortunately she didnt knowㅜㅠ..
t.speed = 0 overwrites the module function (or Turtle() method if you'd made an instance) with an integer 0:
>>> import turtle
>>> turtle.speed = 0
>>> turtle.speed("fastest")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
In general, all turtle API setters are function/method calls, not assignments. For example, turtle.setheading(90) and turtle.pencolor("red") set the heading to 90 degrees and the pen color to red, respectively.
If you want the turtle speed to be instantaneous, try turtle.tracer(0) and run turtle.update() to trigger a rerender after you've done your drawing.
As an aside, please don't use import turtle as t (although that's better than from turtle import *). It's unclear that t is the module and not a turtle instance, which makes ownership of the module confusing. Prefer making an instance of the turtle rather than calling global turtle functions, for example, t = turtle.Turtle().
t.speed=0 doesn't actually work (as ggorlen pointed out). If you want your trutle drawing to go really fast, you can make them happend off screen and only show the result at the end using screen(), tracer() and update():
Try this without the extra lines, then again after uncommenting the 3 lines:
import turtle as tg
def spiral(N):
tg.clear()
d = 1
for i in range(N):
tg.forward(d)
tg.left(20)
d+=0.3
## wn = tg.Screen() # Remove the 3 comments to make it fast
## wn.tracer(0)
tg.speed(0)
spiral(1000)
## wn.update()

AttributeError : 'dict' object has no attribute 'pencolor'

It's my first time here and I'm new in the Python.
When I wrote this code
import turtle
ws = turtle.Screen()
aTurtle = turtle.Turtle()
colors = [ "pink","yellow","blue","green","white","red"]
sketch = turtle.pen()
turtle.bgcolor("black")
for i in range(200):
sketch.pencolor(colors[i % 6])
sketch.width(i/100+1)
sketch.forward(i)
sketch.left(59)
turtle.done()
Then I get this error with black screen
Traceback (most recent call last):
File "C:/Users/wi n7/AppData/Local/Programs/Python/Python37/Lib/draw1.py", line 10, in <module>
sketch.pencolor(colors[i % 6])
AttributeError: 'dict' object has no attribute 'pencolor'
First of all, thanks for posting a minimal, complete example and the full error message with stack trace. That's half the battle--collecting all of the information you need to perform debugging (or provide that info to others to debug for you). The next step is to use this information to actually debug the program. I'll walk you through how I do it in detail so you can do it on your own next time on any problem you might encounter.
The error tells us that sketch.pencolor fails because the sketch is a dictionary object which has no .pencolor attribute. Since .pencolor is a Turtle method, you probably think sketch is a Turtle object. But printing the type just above the error line with print(type(sketch)) or looking at the error message tells us that, indeed, sketch is a dictionary (dict). Running print(dir(sketch)) further confirms that pencolor is not an attribute or method available on dict.
So trace the code backwards from the point of failure. How did sketch become a dict unexpectedly? Find the last (and only) assignment: sketch = turtle.pen().
Looking at the docs for the Turtle.pen() method reveals what it returns:
Return or set the pen’s attributes in a "pen-dictionary"
The turtle library has a somewhat odd design where methods without parameters are getters that return a value, while methods that have a parameter are setters that set properties on the turtle. You're using .pen() in getter mode since there are no parameters. So sketch is a pen-dictionary because that's the return value of .pen().
It's a bit semantically confusing, because you might think you need to get a pen to draw with turtle. Think of the pen as a thing the turtle holds, but you don't touch directly, so you won't need to get the pen for just about any reason other than to see what its current properties are, or to save it to permanent storage or something like that. You'll likely only set pen properties.
Another red flag: unused variables. You took the time to create aTurtle but never did anything with it. Actually, this is the correct variable to be using instead of sketch (although in Python, the correct naming convention is snake_case, a_turtle).
All you need to do is remove sketch = turtle.pen() and use aTurtle everywhere sketch was. That's the real turtle you want to draw with.
In case there's any issue doing this (try it yourself), here's the code:
import turtle
a_turtle = turtle.Turtle()
colors = ["pink", "yellow", "blue", "green", "white", "red"]
turtle.bgcolor("black")
for i in range(200):
a_turtle.pencolor(colors[i % 6])
a_turtle.width(i / 100 + 1)
a_turtle.forward(i)
a_turtle.left(59)
turtle.done()

turtle.textinput() is not working in one of my codes but it works in the other

I am currently working on a game in python using the turtle library import. Before I add anything to the main game always test it in another python file to make sure it works.
I came across the turtle.textinput()
It worked in my test code but not in my actual game. When I tried to put it in the actual game it says
AttributeError: type object 'Turtle' has no attribute textinput
This makes no sense. I use VS code so I don't understand what is going on please help
P.S. : I couldn't figure out the code highlight thing so I put it in brackets.
textinput() is a method in Screen class
Note that textinput() is a method in the Screen instance, and not in the turtle.
from turtle import Screen
screen = Screen()
screen.textinput("Title Example", "Prompt example")

AttributeError: 'Turtle' object has no attribute 'shapesize' on line 14

I'm trying to make a turtle game on Repl.it and I don't know why this error keeps coming up. Here is my code:
import turtle
wn = turtle.Screen()
wn.bgcolor("white")
wn.setup(width=800, height=800)
wn.tracer(0)
# Set up the ground
ground = turtle.Turtle()
ground.color("white")
ground.shape("square")
ground.speed(0)
ground.shapesize(stretch_wid=200, stretch_len=20)
ground.speed(0)
ground.color("black")
ground.penup()
ground.goto(0, -400)
ground.direction = "stop"
It seems that the implementation of the turtle library on repl.it is somewhat limited and not all commands are available. You can either run it locally to use all commands or remove incompatible commands.
Source: https://repl.it/talk/ask/Turtle-python-resizing/7312
There is actually a way to do this.
Whenever you create a new repl instead of creating a "python with turtle" repl just create a normal python one. Then import the turtle module and it should work. When importing it on a basic python file it imports everything.
Whenever you do import turtle it is also a good idea to add from turtle import * that way you import everything.
I also had this problem
You can use Python with pygame instead of python with turtle because for some reason, it works

What's the correct way to call method?

Could someone please explain why the code below throws an error? I know how to make it work - in the last line of code bob.mainloop() has to be replaced with turtle.mainloop() and this is what confuses me. Why does it throw an error Turtle object has no attribute mainloop but when I replace bob with turtle it's ok?
import turtle
bob = turtle.Turtle()
bob.forward(100)
bob.mainloop()
From your question, it seems that mainloop is a function of the module turtle.bob is an object of typeturtle.Turtle, which is not the same.

Categories

Resources