Run a turtle program in full screen when initiated - python

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.

Related

My turtle object on the screen is not appearing

First, I import the turtle module and then creating a screen. After that, I try to create a turtle object, but it is not showed on the screen. Could anyone tell me what the issue is? I have written the following code in PyCharm:
import turtle as t
screen = t.Screen()
screen.title("My Snake Game")
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.exitonclick()
terry = t.Turtle(shape='square')
terry.color('white')
terry.goto(0, 0)
The screen.exitonclick() method does not only set the property that the screen will close after clicking it, it does also wait for this to happen. As this method waits for the screen to close, every code that is after it won't be executed untill the user closes the screen by clicking on it.
This means that the turtle will be created after the screen is closed and thus does not show up as the screen is closed. The solution for this is moving the screen.exitonclick() line to the end of the code.
screen.exitonclick() seems to wait for a mouse click before continuing. As you define your turtle after that, it doesn't come up on the screen. Try putting screen.exitonclick() at the very end of the code
The problem here is that you have the screen.exitonclick() in the middle. That generates another window that apears and desapears after screen.exitonclick() is executed. What you should do is to put screen.exitonclick() at the end.
import turtle as t
screen = t.Screen()
screen.title("My Snake Game")
screen.setup(width=600, height=600)
screen.bgcolor("black")
terry = t.Turtle(shape='square')
terry.color('white')
terry.goto(0, 0)
screen.exitonclick()
This would be the code.

How do I use the python turtle on click function to clear the canvas?

This is for a school project - I am still a beginner and have lots of trouble with functions. How would I clear the canvas with a click?
The answer depends on how severe a clearing you want to do. If you simply want to remove the drawing done by an individual turtle, but leave drawings by other turtles (background) in tact, as well as preserve aspects of the screen like its background color, you can use the screen's onclick() event to invoke an individual turtle's clear() method:
from turtle import Screen, Turtle
def clear_turtle(x, y):
turtle.clear()
screen = Screen()
screen.bgcolor('lavender')
screen.onclick(clear_turtle)
yertle = Turtle()
yertle.hideturtle()
yertle.dot(100)
turtle = Turtle()
turtle.hideturtle()
turtle.dot(50, 'yellow')
screen.mainloop()
If, on the other hand, you want a more scorched earth result, you can use the screen's onclick() event to invoke the screen's clear() method:
from turtle import Screen, Turtle
def clear_screen(x, y):
screen.clear()
screen = Screen()
screen.bgcolor('lavender')
screen.onclick(clear_screen)
yertle = Turtle()
yertle.hideturtle()
yertle.dot(100)
turtle = Turtle()
turtle.hideturtle()
turtle.dot(50, 'yellow')
screen.mainloop()
This will return the screen to it's initial blank (white) state and destroy all your turtle drawings as well as your turtles all the way down.

turtle.write() function not writing in turtle window

I am writing program using python turtle. To write text in turtle window, I am using this code:
def write_text(center_x,center_y,text): # Write text on the Screen
board.speed(1)
print("I am here")
board.penup()
board.goto(center_x,center_y)
board.write(text)
But this code is not writing anything in turtle window. Can anyone help me?
Your write_text() funtion is basically fine. I'd look for other reasons why you're not seeing the text. E.g. is your pen color the same as the background color? Is your screen DPI too dense to see the tiny font that turtle uses by default?
Try this simplification of your function along with some code to call it:
from turtle import Screen, Turtle
FONT = ('Arial', 16, 'normal')
def write_text(center_x, center_y, text):
''' Write text on the Screen '''
board.penup()
board.goto(center_x, center_y)
board.write(text, font=FONT)
board = Turtle()
write_text(100, 100, "You are here.")
screen = Screen()
screen.exitonclick()
Do you still not get any writing in your turtle window?

Turtle from image won't draw over another turtle's stamp

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.

How to make a turtle object look at where the mouse has clicked

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())

Categories

Resources