I am having a huge issue using the turtle library. I have to write my initials AR for an assignment. Can anyone help?
import turtle
turtlescreen
turtle.pos(400,400)
turtle.forward()
Here is the code I am trying to use. I am trying to right my initial "AR" with it.
You've managed to cram three errors into four lines of code. First, you don't need this and it's an error:
turtlescreen
so toss it. Second, the pos() function returns the current turtle postion, not set it. So instead of:
turtle.pos(400,400)
You want:
turtle.setpos(400, 400)
and finally, as #Jamie notes, you need to pass a distance (in pixels) to forward(). So instead of:
turtle.forward()
Something like he suggests:
turtle.forward(15)
Complete code:
import turtle
turtle.setpos(400, 400)
turtle.forward(15)
turtle.done()
Your turtle.forward() requires an input variable in pixels. See the documentation for turtle.forward:
Move the turtle forward by the specified distance, in the direction the turtle is headed.
Try changing:
turtle.forward()
to something like:
turtle.forward(15)
There's a few mistakes I can see. Firstly, there is no command called turtlescreen so you can remove that. Next, instead of tom.pos try using tom.setpos(x, y) or tom.goto (x, y). Lastly, you should put a value in tom.forward (length). I also recommend going through the Python Turtle documentation, https://docs.python.org/3.3/library/turtle.html?highlight=turtle
Related
How do you hide the turtle while drawing in Python 3. It is covering my work and I need to see exact corners and other things. Is it also possible to draw with it hidden? Thank you.
I have tried this command and it did nothing.
t = turtle.Turtle()
s = t.getscreen()
You have hide the turtle with the following code.
# hide the turtle
turtle.hide_turtle()
Try using hideturtle() function. The syntax would be <turtle_object>. hideturtle()
I have the following code:
answer = "ABC"
flag.goto(-999, -999)
while (answer.lower != 'y' or answer.lower != 'n'):
print("You got the flag! Free play(y/n)?")
answer = input("")
if answer.lower == 'y':
pass
if answer.lower == 'n':
return None
I am trying to remove the turtle called flag, through adding it to a list then deleting it with del(testlist[0]), but it didn't work.
The output is:
You got the flag! Free play(y/n)?
y
You got the flag! Free play(y/n)?
n
You got the flag! Free play(y/n)?
Your question is confusing as the title and text ask one thing, while your example code and output show something completely different.
Let's address this question:
Is there a way to remove a turtle from the screen?
Generally turtle.hideturtle() will do what you want. The only way to dispose of turtles once created is via a screen.clear() which will destroy all of them.
(The variable turtle above needs to be set to an instance of Turtle() and the variable screen needs to be set to the singular instance of Screen().
You can get a better view on the Visibility of turtles from this documentation.
Basically, you can use either turtle.hideturtle() or turtle.ht() to make a turtle invisible. But, that does not mean that the turtle is removed, and so it still takes up memory.
You can call turtle.Screen.clear(), but that resets everything, even the things you might want to keep.
If I were in a situation where I want to delete turtles instead of hiding them because doing that over and over again will take up too much memory, I'd simply hide the turtle, and when the program need another turtle, instead of creating another one, simply unhide the hidden turtle to be used again.
May I get an explanation about the turtle module command:
.goto(0, 0)
I know that this command moves the turtle but I'm trying to find out what these arguments inside the parentheses exactly mean, in details?
Thanks.. your help is much appreciated.
I believe that the first parameter is the x-coordinate and the second parameter is the y-coordinate. So, calling this function will move the turtle to whatever (x, y) point that you give it.
https://docs.python.org/3/library/turtle.html?highlight=goto#turtle.goto
# 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'
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.
I am using the turtle module in python. the problem is that whenever I have the turtle move i will draw even if the pen is up. for example if I run this program:
import turtle
turtle.penup
turtle.goto(0,50)
the turtle will still draw a line when it moves to (0,50)
why is this and how can it be prevented?
It looks like you're not actually calling turtle.penup. Try this:
import turtle
turtle.penup()
turtle.goto(0,50)
You have a typo, you aren't calling the penup method:
import turtle
turtle.penup() #This needs to be a method call
turtle.goto(0,50)
import turtle
turtle.up()
turtle.goto(0,50)
turtle.down()
if you don't put the pen down it will keep on drawing in invisible condition.
This question is super old and definitely has already been answered, but I'll leave this explanation here for future people
"penup" is a method in Python, aka function in other languages. This means that when you want to use it you have it include some parenthesis just so that your code knows what is supposed to be happening
import turtle
turtle.penup()
turtle.goto(0,50)
When you don't include the parenthesis, the code thinks you are talking about a variable, and looks for one called "penup", but there is no variable of that name, so Python throws its hands up and crashes
you called penup without (). with
turtle.penup()
this will work.
Others here said that, but implicitly. trying to ensure it is clear where the typo is.
You should probably try,
turtle.penup()
no it should be something like this:
turtle.up() # This a method call
turtle.goto(0,50) # Part of the method call