import turtle
def Draw_turtle(my_turtle):
for i in range(1,5):
my_turtle.turtle.forward(100)
my_turtle.turtle.right(90)
window = turtle.Screen()
window.bgcolor('green')
Alex = turtle.Turtle()
Alex.color('black')
Alex.speed(2)
Alex.shape('triangle')
Draw_turtle(Alex)
I know nothing of coding yet, when I run the above code nothing happens. Can anyone help me, please.
You have most of the right pieces and roughly in the right order. We just need to fine tune your code a little bit as far as indentation, usage and style:
import turtle
def draw_square(my_turtle):
for i in range(4):
my_turtle.forward(100)
my_turtle.right(90)
window = turtle.Screen()
window.bgcolor('green')
alex = turtle.Turtle()
alex.color('black')
alex.speed('slow')
alex.shape('triangle')
draw_square(alex)
window.mainloop()
Related
I am trying to write a program to draw a flower, but no matter what I do it keeps throwing an "invalid syntax" error for the turtle name. I have taken out all of my other code, tried naming the turtle something different, yet nothing works. Any ideas?
import turtle
def draw_flower():
window = turtle.Screen()
window.bgcolor(#42dff4)
sam = turtle.Turtle()
sam.forward(50)
window.exitonclick()
draw_flower()
Besides quoting the color string, as noted in the comments, your lines of code are in the wrong order. For example, generally nothing should follow window.exitonclick():
window.exitonclick()
draw_flower()
Make it (or window.mainloop()) the last statement of your program as that's when your code ends and the Tk event handler loop begins. I.e. reverse the order of these two statements. The second problem is that the variable window is in the wrong scope:
def draw_flower():
window = turtle.Screen()
...
window.exitonclick()
Since it's defined locally in draw_flower(), it's not available to use globally. Here's a rework of your code addressing both issues:
import turtle
def draw_flower():
sam = turtle.Turtle()
sam.forward(50)
window = turtle.Screen()
window.bgcolor("#42dff4")
draw_flower()
window.exitonclick()
I am writing a Python 3 turtle example program, and am trying to use bgpic() to change the displayed background image.
Here's a simplified version of my program:
import turtle
import time
screen = turtle.Screen()
screen.setup(600,400)
screen.bgpic('image1.gif')
time.sleep(2)
screen.bgpic('image2.gif')
When I run this program, I'd like to see the initial image, and then see the image change after 2 seconds. Instead, the screen stays blank until after the second image is drawn.
Any help appreciated,
Thanks!
Add screen.update() after the first screen.bgpic('image1.gif').
Adding a turtle and moving it made it work for me
import turtle
t = turtle.Turtle()
s = turtle.Screen()
s.setup(300, 300)
s.bgpic('image1.gif')
for i in range(180):
t.color("black")
t.fd(20)
I am trying to change the size of turtle to something larger. I am not sure if I am either doing it incorrectly, or it I just can't because Trinket.io does not support it. The following is the code I currently have:
#python 2
from turtle import *
wn = Screen()
wn.setup(640,400,0,0)
wn.bgcolor("#4885ed")
shape("circle")
resizemode("user")
shapesize(5, 5, 12)
When I run this code I get the error:
NameError: name 'resizemode' is not defined on line 10 in main.py
Thank you for any assistance you can provide.
Unfortunately, if you go to https://trinket.io/docs/python you will find out that resizemode() and shapesize() are not mentioned as functions. I presume they have not been implemented into trinket interface.
i think you can try this code
import turtle
a = turtle.Turtle()
wn = turtle.Screen()
wn.setup(640,400,0,0)
wn.bgcolor("#4885ed")
a.shape("circle")
a.resizemode("user")
a.shapesize(5, 5, 12)
I've been trying to make a paint program in Python Turtle and for some reason it won't work. I'm using the pen() tool and my code looks like this
from turtle import *
import random
pen()
bgcolor('black')
pencolor('white')
pen.ondrag(pen.goto)
listen()
mainloop()
I've look at this http://docs.python.org/2/library/turtle.html and it says to type turtle.ondrag(turtle.goto) but since I'm using the pen it should work as pen.ondrag but it doesn't, so can someone please clear this up.
Thanks Jellominer
I will simplify and clarify the code given by the questionner:
from turtle import *
ts = Screen(); tu = Turtle()
ts.listen()
ondrag(tu.goto)
mainloop()
This works. You have to click on the turtle and drag it.
First, pen() is not the function you want. Second, although Pen is a synonym for Turtle, pen is not a synonym for turtle. Here's how to go about using ondrag() if you'd like to use Pen instead of Turtle:
from turtle import Pen, Screen, mainloop
def ondrag_handler(x, y):
pen.ondrag(None) # disable handler inside handler
pen.setheading(pen.towards(x, y)) # turn toward cursor
pen.goto(x, y) # move toward cursor
pen.ondrag(ondrag_handler)
screen = Screen()
screen.bgcolor('black')
pen = Pen()
pen.color('white')
pen.shapesize(2) # make it larger so it's easier to drag
pen.ondrag(ondrag_handler)
screen.listen()
mainloop() # screen.mainloop() preferred but not in Python 2
The turtle.ondrag(turtle.goto) makes for a nice short example in the documentation but in reality isn't practical. You want to disable the event handler while handling the event otherwise the events stack up against you. And it's nice to turn the mouse towards your cursor as you drag it.
from turtle import *
ts = Screen()
ondrag(goto)
shapesize(10)
pensize(40)
speed(0)
mainloop()
I think this will surely work.
You can change the size and other things
In here you are using the default turtle .
Sorry but you'll need to take care of the indentation
I'm trying to get two turtles on a screen. So far I have this:
import turtle
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t1.color("red")
t2.color("blue")
t1.forward(20)
t2.right(90)
t2.forward(100)
But nothing is happening :(
Im using the Turtle IDE downloaded from:http://pythonturtle.org/
All i want to do is illustrate some object orientation but if that code wont work, i cant
Anyone got any suggestions (ive tried using turtles API but its hard to follow)
Thanks
You didn't take an object from the class Screen
You should do that to make sure that the window will not close immediately, plus you should make the program loop.
import turtle
wn = turtle.Screen() # taking window object from Screen class
t1 = turtle.Turtle()
t2 = turtle.Turtle()
t1.color("red")
t2.color("blue")
t1.forward(20)
t2.right(90)
t2.forward(100)
while True: # Making the program loop to make the program continue running.
wn.update()
The main loop also you can make it this way
wn.mainloop()
I recommend you follow the first solution.