I want to move the turtle in random directions while keeping the turtle at the center of the screen. How can I do this?
While True:
turtle.setheading(random.randint(0,360))
turtle.forward(10)
I want the camera screen to follow the turtle whereever its going.
You can do this by manipulating the scroll position using methods of the underlying tkinter window structure. Here's a turtle example that moves a ball but keeps it centered in the window.
What do you mean by "center of the screen"? Your code is turning your turtle by 0 to 360 and then move 10 forward. If you dont want to move the turtle remove the turtle.forward(10).
To move your turtle back to the start you could add turtle.home().
While True:
turtle.setheading(random.randint(0,360))
turtle.forward(10)
turtle.home()
Related
This code sets up a background of stamps placed by one turtle. Another turtle, (whose shape is from an imported image file) moves around over the background. But the second turtle is not visible whenever it is positioned over a stamp placed by the first turtle. If I make the moving turtle one of the standard shapes, eg circle, then it stays visible. So there's something odd about using an imported image for the turtle, which causes it to disappear whenever it's on top of one of the stamps.
#!/usr/bin/python3
from turtle import *
from time import sleep
scr = Screen()
scr.register_shape('player.gif')
mover = Turtle()
bgnd = Turtle()
bgnd.color('blue')
mover.shape('player.gif')
bgnd.shape('square')
for i in range(5):
bgnd.goto(i*20,0)
bgnd.stamp()
for i in range(5):
mover.goto((8-i)*20,0)
sleep(1)
The accompanying image is the one I've referred to as player.gif (I can see it at the bottom of the post, but it's very small).
Can anyone help explain why this is and how to get around it?
I'm not sure if this is a bug or subtlety, but stamps aren't pixels on the screen like dot() -- the stamp() function returns an ID that allows selectively removing them. My guess is it's a layering issue with the underlying tkinter. In turtle, if you have a layering issue, sometimes it helps to do things in a different order:
from turtle import Screen, Turtle
from time import sleep
background = Turtle()
background.hideturtle()
background.color('blue')
background.shape('square')
for i in range(5):
background.goto(i * 20, 0)
background.stamp()
screen = Screen()
screen.register_shape('player.gif')
mover = Turtle()
mover.shape('player.gif')
for i in range(5):
mover.goto((8 - i) * 20, 0)
sleep(1)
screen.exitonclick()
In the long run, time.sleep() isn't a friend of event-based turtle. It's fine for examples like this but anything more and it will cause more problems than it solves. Look into the ontimer() method of the screen.
I wrote a Python 3.7 program with Turtle Graphics. It is a simple program and works fine, but I want it to run full-screen when I initiate the program. How can I do this? There is no full-screen option in the Turtle documentation.
import turtle
from turtle import *
a = turtle.Turtle()
a.speed(10)
a.color('red', 'blue')
# a.begin_fill()
for i in range(90):
a.fd(200)
a.lt(169)
# a.end_fill()
turtle.done()
The width and height arguments to the setup() method take integer (pixel) and floating point (percentage of screen). By supplying 1.0 you'll get the largest window that turtle can make:
from turtle import Screen, Turtle
screen = Screen()
screen.setup(width=1.0, height=1.0)
a = Turtle()
a.speed('fastest')
a.color('red', 'blue')
# a.begin_fill()
for _ in range(36):
a.forward(200)
a.left(170)
# a.end_fill()
screen.mainloop()
But this isn't necessarily the operating systems sense of Full Screen where the window overlays everything. It's just the largest window turtle can create given available screen real estate.
I'm using Turtle Graphics Module in Python 3 and I have drawn a Circle using circle method of Turtle with that i'm using Custom Shape which is a pencil, now problem is that i'ts drawing from the center of the pencil. I want it to draw from the tip of the Pencil, so how can i setup turtle to start drawing from the tip of the pencil rather than from the center point.
Here is my Code:
from turtle import Screen, Turtle
screen = Screen()
screen.setup(500, 500)
screen.screensize(500, 500)
screen.register_shape('dpen.gif')
turtle = Turtle('dpen.gif')
def draw_circle():
turtle.home()
turtle.clear()
turtle.circle(90)
screen.listen()
screen.onkeypress(draw_circle, 'space')
screen.mainloop()
Thank You
When setting a custom pen, turtle will always draw from the centre of your image. There might be a way to change this, but a far simpler solution is to change your image so that the pencil is in the top right corner, so that the tip points to the middle of the image. Took me 10 seconds in powerpoint
Very crude example image (demonstrates point):
I want to write a turtle program where the turtle goes to wherever you click. So far I have this:
from turtle import *
screen = Screen()
turtle = Turtle()
screen.onscreenclick(turtle.goto)
But the problem is the turtle object just stays facing in the same direction. I want to somehow make it look towards where it's going. How can I achieve this?
This will do what you describe:
import turtle
screen = turtle.Screen()
turtle = turtle.Turtle()
def turtle_headto(x, y):
turtle.left(turtle.towards(x, y) - turtle.heading())
turtle.goto(x, y)
screen.onscreenclick(turtle_headto)
screen.mainloop()
But the motion of the arrow/turtle isn't always optimal, i.e. sometimes it spins the long way 'round, but that's something for you to optimize (e.g. when to call left() and when to call right())
I would like to create a program where one turtle object moves to where a user clicks their mouse, and a different turtle object moves at the same time. I have the first part, but I can't seem to get the rest to work.
Any help would be appreciated.
This is my code. (Credit for the first part of this goes to #Cygwinnian)
from turtle import *
turtle = Turtle()
screen = Screen()
screen.onscreenclick(turtle.goto)
turtle.getscreen()._root.mainloop()
turtle2 = Turtle()
while True:
turtle2.back(100)
turtle2.forward(200)
turtle2.back(100)
I am by no means an expert at Python's turtle module, but here's some code that I think does what you want. The second turtle will be moving back and forth any time the first turtle is not:
from turtle import *
screen = Screen() # create the screen
turtle = Turtle() # create the first turtle
screen.onscreenclick(turtle.goto) # set up the callback for moving the first turtle
turtle2 = Turtle() # create the second turtle
def move_second(): # the function to move the second turtle
turtle2.back(100)
turtle2.forward(200)
turtle2.back(100)
screen.ontimer(move_second) # which sets itself up to be called again
screen.ontimer(move_second) # set up the initial call to the callback
screen.mainloop() # start everything running
This code creates a function that moves the second turtle repeatedly back and forth from its starting position. It uses the ontimer method of the screen to schedule itself over and over. A slightly more clever version might check a variable to see if it is supposed to quit, but I didn't bother.
This does make both turtles move, but they don't actually move at the same time. Only one can be moving at any given moment. I'm not sure if there is any way of fixing that, other than perhaps splitting up the moves into smaller pieces (e.g. have the turtles alternate moving one pixel at a time). If you want fancier graphics you probably need to move on from the turtle module!